C++ OPENGL如何有一个背景精灵表,并且在上面还有另一个相同类的对象
我在同一个问题上被困了一段时间,非常感谢您的帮助。我正在尝试为我的游戏背景加载一个纹理,我只是在一个图像前面有一辆移动的汽车作为背景,但是纹理根本没有为背景渲染,我不知道为什么。相关代码如下:C++ OPENGL如何有一个背景精灵表,并且在上面还有另一个相同类的对象,c++,opengl,textures,texture-mapping,C++,Opengl,Textures,Texture Mapping,我在同一个问题上被困了一段时间,非常感谢您的帮助。我正在尝试为我的游戏背景加载一个纹理,我只是在一个图像前面有一辆移动的汽车作为背景,但是纹理根本没有为背景渲染,我不知道为什么。相关代码如下: void display() { //clear the colour and depth buffers glClear(GL_COLOR_BUFFER_BIT); glm::mat4 ModelViewMatr
void display()
{
//clear the colour and depth buffers
glClear(GL_COLOR_BUFFER_BIT);
glm::mat4 ModelViewMatrix;
glm::mat4 ModelViewMatrixBackground;
ViewMatrix = glm::translate(glm::mat4(1.0), glm::vec3(0.0, 0.0, 0.0));
glEnable(GL_BLEND);
ModelViewMatrixBackground = glm::translate(ViewMatrix, glm::vec3(0,0,0.0));
ModelViewMatrix = glm::translate(ViewMatrix, glm::vec3(mySpriteSquare.GetXPos(), mySpriteSquare.GetYPos(), 0.0));
//ModelViewMatrix = glm::rotate(ModelViewMatrix, glm::radians(AngleInDegrees),glm::vec3(0.0,0.0,1.0));
//ModelViewMatrix = glm::rotate(ModelViewMatrix, glm::radians(degrees), glm::vec3(0, 1, 0));
myBackground.Render(shader, ModelViewMatrixBackground, ProjectionMatrix);
mySpriteSquare.Render(shader, ModelViewMatrix, ProjectionMatrix);
glDisable(GL_BLEND);
glm::mat4 TriangleTransform = glm::translate(ViewMatrix, glm::vec3(Xtri, Ytri, 0.0));
myCircle.Render(shader, TriangleTransform, ProjectionMatrix);
glutSwapBuffers();
}
void init()
{
FreeImage_初始化();
glClearColor(0.0,0.0,1.0,0.0);//将透明颜色设置为黑色
//加载GLSL程序
如果(!shader.load(“Basic”、“/glslfiles/basicTexture.vert”、“/glslfiles/basicTexture.frag”))
{
标准::cout
void init()
{
FreeImage_Initialise();
glClearColor(0.0,0.0,1.0,0.0); //sets the clear colour to black
//Load the GLSL program
if (!shader.load("Basic", "./glslfiles/basicTexture.vert", "./glslfiles/basicTexture.frag"))
{
std::cout << "failed to load shader" << std::endl;
}
///This part commented is to scale the width of the sprite to match the dimensions of the car.png image.
mySpriteSquare.SetWidth(5.0f *(500 / 264.0f));
mySpriteSquare.SetHeight(5.0f);
float red[3] = { 1,0,0 };
mySpriteSquare.Init(shader, red, "textures/car.png");
myBackground.SetWidth(900.0f);
myBackground.SetHeight(900.0f);
float redBackground[3] = { 1,0,0 };
myBackground.Init(shader, redBackground, "textures/bricks.png");
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
#include "Background.h"
#include "shaders\Shader.h"
#include <string>
#include "ImageLoading.h"
#include <string>
#include <iostream>
Background::Background()
{
m_vaoID = 0;
m_vboID[0] = 0;
m_vboID[1] = 0;
m_Width = 0.0f;
m_Height = 0.0f;
m_NumberOfVerts = 0;
m_xpos = 0;
m_ypos = 0;
}
void Background::SetWidth(float size)
{
m_Width = size;
}
void Background::SetHeight(float size)
{
m_Height = size;
}
void Background::SetXpos(float x)
{
m_xpos = x;
}
void Background::SetYpos(float y)
{
m_ypos = y;
}
float Background::GetXPos()
{
return m_xpos;
}
float Background::GetYPos()
{
return m_ypos;
}
void Background::IncPos(float x, float y)
{
m_xpos += x;
m_ypos += y;
}
void Background::Init(Shader& shader, float colour[3], std::string filename)
{
//load png image
int imageHeight = 0;
int imageWidth = 0;
//create the texture on the GPU
glGenTextures(1, &m_TexName);
glBindTexture(GL_TEXTURE_2D, m_TexName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); //or use GL_CLAMP
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
bool success = ImageLoading::loadImage(filename);
if (!success) {
std::cout << "Unable to load image file" << std::endl;
glDeleteTextures(1, &m_TexName);
return;
}
else
{
std::cout << "Image loaded " << std::endl;
}
//Create the geometry
m_NumberOfVerts = 6;
float vert[18]; // create a vertex array
float halfWidth = m_Width / 2.0f;
float halfHeight = m_Height / 2.0f;
vert[0] = -halfWidth; vert[1] = halfHeight; vert[2] = 0.0; //x,y,z values for each vertex
vert[3] = -halfWidth; vert[4] = -halfHeight; vert[5] = 0.0;
vert[6] = halfWidth; vert[7] = -halfHeight; vert[8] = 0.0;
vert[9] = -halfWidth; vert[10] = halfHeight; vert[11] = 0.0;
vert[12] = halfWidth; vert[13] = halfHeight; vert[14] = 0.0;
vert[15] = halfWidth; vert[16] = -halfHeight; vert[17] = 0.0;
/********INIT CORNERS FOR OBB***********/
obb.vertOriginal[0].x = -halfWidth;
obb.vertOriginal[0].y = -halfHeight;
obb.vertOriginal[1].x = halfWidth;
obb.vertOriginal[1].y = -halfHeight;
obb.vertOriginal[2].x = halfWidth;
obb.vertOriginal[2].y = halfHeight;
obb.vertOriginal[3].x = -halfWidth;
obb.vertOriginal[3].y = halfHeight;
/*******************/
float tex[12];
tex[0] = 0.0f; tex[1] = 1.0f;
tex[2] = 0.0f; tex[3] = 0.0;
tex[4] = 1.0f; tex[5] = 0.0;
tex[6] = 0.0f; tex[7] = 1.0f;
tex[8] = 1.0f; tex[9] = 1.0f;
tex[10] = 1.0f; tex[11] = 0.0;
float col[18]; // colour array
col[0] = colour[0]; col[1] = colour[1]; col[2] = colour[2]; //r,g,b values for each vertex
col[3] = colour[0]; col[4] = colour[1]; col[5] = colour[2]; //r,g,b values for each vertex
col[6] = colour[0]; col[7] = colour[1]; col[8] = colour[2]; //r,g,b values for each vertex
col[9] = colour[0]; col[10] = colour[1]; col[11] = colour[2]; //r,g,b values for each vertex
col[12] = colour[0]; col[13] = colour[1]; col[14] = colour[2]; //r,g,b values for each vertex
col[15] = colour[0]; col[16] = colour[1]; col[17] = colour[2]; //r,g,b values for each vertex
//VAO allocation
glGenVertexArrays(1, &m_vaoID);
// First VAO setup
glBindVertexArray(m_vaoID);
glGenBuffers(3, m_vboID); // we need three VBOs - one for the vertices and one for the colours
//and an extra one for the texture coordinates
//Lets set up the vertices.
glBindBuffer(GL_ARRAY_BUFFER, m_vboID[0]);
//initialises data storage of vertex buffer object
glBufferData(GL_ARRAY_BUFFER, m_NumberOfVerts * 3 * sizeof(GLfloat), vert, GL_STATIC_DRAW);
//set the position - linked to the position shader input
GLint vertexLocation = glGetAttribLocation(shader.handle(), "in_Position");
glEnableVertexAttribArray(vertexLocation);
glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
//Now set up the colours
glBindBuffer(GL_ARRAY_BUFFER, m_vboID[1]);
glBufferData(GL_ARRAY_BUFFER, m_NumberOfVerts * 3 * sizeof(GLfloat), col, GL_STATIC_DRAW);
//set the colour - linked to the colour shader input.
GLint colorLocation = glGetAttribLocation(shader.handle(), "in_Color");
glEnableVertexAttribArray(colorLocation);
//location in shader, number of componentns, type, normalised, stride, pointer to first attribute
glVertexAttribPointer(colorLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
//Now set up the texture coordinates
glBindBuffer(GL_ARRAY_BUFFER, m_vboID[2]);
glBufferData(GL_ARRAY_BUFFER, m_NumberOfVerts * 3 * sizeof(GLfloat), tex, GL_STATIC_DRAW);
//set the texture coords - linked to the texcoord shader input.
GLint texLocation = glGetAttribLocation(shader.handle(), "in_TexCoord");
glEnableVertexAttribArray(texLocation);
//location in shader, number of componentns, type, normalised, stride, pointer to first attribute
glVertexAttribPointer(texLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
//good practice to bind to 0.
glEnableVertexAttribArray(0);
glBindVertexArray(0);
}
void Background::Render(Shader& shader, glm::mat4& ModelViewMatrix, glm::mat4& ProjectionMatrix)
{
/****UPDATE THE CORNER VALUES BASED ON TRANSFORMATION***/
obb.transformPoints(ModelViewMatrix);
/*******************************************************/
glUseProgram(shader.handle()); // use the shader
//set the DiffuseMap in GLSL to the texture unit 0.
glUniform1i(glGetUniformLocation(shader.handle(), "DiffuseMap"), 0);
glBindTexture(GL_TEXTURE_2D, m_TexName);
//set the uniform for the projectionmatrix
glUniformMatrix4fv(glGetUniformLocation(shader.handle(), "ProjectionMatrix"), 1, GL_FALSE, &ProjectionMatrix[0][0]);
//pass the uniform for the ModelView matrix to the shader
glUniformMatrix4fv(glGetUniformLocation(shader.handle(), "ModelViewMatrix"), 1, GL_FALSE, &ModelViewMatrix[0][0]);
//Draw the object
glBindVertexArray(m_vaoID); // select first VAO
glDrawArrays(GL_TRIANGLES, 0, m_NumberOfVerts); // draw first object
glBindVertexArray(0); //unbind the vertex array object
glUseProgram(0); //turn off the current shader
}
OBB& Background::GetOBB()
{
return obb;
}
bool Background::IsInCollision(OBB& anotherOBB)
{
if (obb.SAT2D(anotherOBB))
{
return true;
}
return false;
}