当不应用任何类型的正投影时,我可以按预期显示两个图像。但是,当我尝试使用glm :: ortho()向我的顶点添加正交投影矩阵时,我的精灵会消失。我的代码如下(尝试删除不相关的代码):
Sprite.cpp
Sprite::Sprite()
{
vboID = 0;
}
Sprite::~Sprite()
{
//Clean up sprite buffers on GPU when done with sprite
if (vboID != 0)
glDeleteBuffers(1, &vboID);
}
void Sprite::Init(float x, float y, float width, float height, Blz::string imageFilePath)
{
this->x = x;
this->y = y;
this->width = width;
this->height = height;
if (vboID == 0)
glGenBuffers(1, &vboID);
texture = Blz::OpenGL::LoadImage(imageFilePath);
Vector3D vertexData[6]{
Vector3D {x + width, y + height, 0.0f},
Vector3D {x, y + height, 0.0f },
Vector3D {x, y, 0.0f},
Vector3D {x, y, 0.0f},
Vector3D {x + width, y, 0.0f},
Vector3D {x + width, y + height, 0.0f},
};
for (int i = 0; i < 6; ++i)
{
vertexData[i].color.r = 0;
vertexData[i].color.g = 155;
vertexData[i].color.b = 200;
};
vertexData[0].setUV(1.0f, 1.0f);
vertexData[1].setUV(0.0f, 1.0f);
vertexData[2].setUV(0.0f, 0.0f);
vertexData[3].setUV(0.0f, 0.0f);
vertexData[4].setUV(1.0f, 0.0f);
vertexData[5].setUV(1.0f, 1.0f);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Sprite::Draw()
{
glBindTexture(GL_TEXTURE_2D, texture.id);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3D), (void*)offsetof(Vector3D, position));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vector3D), (void*)offsetof(Vector3D, textCoordinate));
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
的main.cpp
int main(int agrc, char** argv)
{
window.Initialize();
glm::mat4 projectionMatrix = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f, -1.0f, 1.0f);
p_sprites.push_back(new Sprite());
p_sprites.back()->Init(0.0f, 0.0f, .5f, .5f, "CharImage.png");
p_sprites.push_back(new Sprite());
p_sprites.back()->Init(-0.4f, 0.0f, .5f, .5f, "CharImage.png");
GameState gamestate{ GameState::PLAY };
SDL_Event evnt;
Blz::OpenGL::ShaderProgram colorShaderProgram("Source/GameEngine/Shaders/VertexShader.glsl", "Source/GameEngine/Shaders/FragmentShader.glsl");
colorShaderProgram.Compile();
colorShaderProgram.AddAttribute("vertexPosition");
colorShaderProgram.AddAttribute("textCoord");
colorShaderProgram.Link();
colorShaderProgram.Bind();
//Get uniform from Shader and send texture info to shader
GLuint uniformLocation = colorShaderProgram.GetUniformLocation("basicTexture");
glUniform1i(uniformLocation, 0);
//Get uniform location from shader
GLuint ProjectMatrixUniformLocation = colorShaderProgram.GetUniformLocation("projectionMatrix");
while (gamestate != GameState::EXIT)
{
ProcessInput();
//Send down projectionMatrix resulting from glm::ortho() call
glUniformMatrix4fv(ProjectMatrixUniformLocation, 1, GL_FALSE, &projectionMatrix[0][0]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (uint32 i = 0; i < p_sprites.size(); ++i)
p_sprites[i]->Draw();
SDL_GL_SwapWindow(window)
}
return 0;
}
顶点着色器
#version 430
in vec3 vertexPosition;
in vec2 textCoord;
out vec2 TextureCoord;
uniform mat4 projectionMatrix;
void main()
{
vec4 position = vec4(vertexPosition, 1.0f);
gl_Position = projectionMatrix * position;
TextureCoord = textCoord;
};
片段着色器
#version 430
out vec4 daColor;
in vec2 TextureCoord;
uniform sampler2D basicTexture;
void main()
{
vec4 texel = texture(basicTexture, TextureCoord);
daColor = texel;
};