after I've set up the (orthogonal) projection matrix for my simple 2d game, nothing renders on the screen. I am using cglm (glm but in c) and compared the results of cglm with the normal glm ortho projection implementation that renders well, and the results of the projection matrix match. Here is my render loop:
void RenderSprite(const struct Sprite *sprite) {
struct Shader *shader = GetSpriteShader(sprite);
UseShader(shader);
/* cglm starts here */
mat4 proj;
glm_ortho(0.0f, 800.0f, 600.0f, 0.0f, -1.0f, 1.0f, proj); /* screen width: 800, height: 600 */
mat4 model;
glm_mat4_identity(model); /* an identity model matrix - does nothing */
/* cglm ends here */
SetShaderUniformMat4(shader, "u_Projection", proj); /* set the relevant uniforms */
SetShaderUniformMat4(shader, "u_Model", model);
/* finally, bind the VAO and call the draw call (note that I am not using batch rendering - I am using just a simple plain rendering system) */
glBindVertexArray(ezGetSpriteVAO(sprite));
glDrawElements(GL_TRIANGLES, ezGetSpriteIndexCount(sprite), GL_UNSIGNED_INT, 0);
}
However, this results in a blank screen - nothing renders. I believe I've did everything in order - but the problem is nothing renders. For anyone interested, here is my vertex shader:
#version 330 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec2 uv;
uniform mat4 u_Model;
uniform mat4 u_Projection;
void main() {
gl_Position = u_Projection * u_Model * vec4(pos, 1.0f);
}
And here is my fragment shader:
#version 330 core
out vec4 color;
void main() {
color = vec4(1.0f);
}
As far as I am aware, cglm matrices are ordered column-major, which OpenGL wants.
Any help would be appreciated. Thanks in advance.
EDIT
The sprite coordinates are (in this case it is the vertex data, I guess):
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f
EDIT 2 After @BDL's comment, I have adjusted the vertex data as following:
float vertices[] = {
/* Position UV */
5.0f, 5.0f, 0.0f, 0.0f, 0.0f,// bottom left
10.0f, 5.0f, 0.0f, 1.0f, 0.0f, // bottom right
10.0f, 10.0f, 0.0f, 1.0f, 1.0f, // top right
5.0f, 10.0f, 0.0f, 0.0f, 1.0f // top left
};
But, I can't see anything on the screen - nothing is being rendered at this point.
As @BDL and others have suggested, here is my final render loop that works like a charm right now: (posting as a reference)
void RenderSprite(const struct Sprite *sprite) {
ASSERT(sprite, "[ERROR]: Can't render a sprite which is NULL\n");
struct Shader *shader = GetSpriteShader(sprite);
UseShader(shader);
mat4 proj;
glm_ortho(0.0f, 800.0f, 600.0f, 0.0f, -1.0f, 1.0f, proj);
mat4 model, view;
glm_mat4_identity(model);
glm_mat4_identity(view);
SetShaderUniformMat4(shader, "u_Projection", proj);
SetShaderUniformMat4(shader, "u_Model", model);
SetShaderUniformMat4(shader, "u_View", view);
glBindVertexArray(GetSpriteVAO(sprite));
glDrawElements(GL_TRIANGLES, GetSpriteIndexCount(sprite), GL_UNSIGNED_INT, 0);
}
And here are my coordinates:
float vertices[] = {
/* Position UV */
350.0f, 350.0f, 0.0f, 0.0f, 0.0f,// bottom left
450.0f, 350.0f, 0.0f, 1.0f, 0.0f, // bottom right
450.0f, 250.0f, 0.0f, 1.0f, 1.0f, // top right
350.0f, 250.0f, 0.0f, 0.0f, 1.0f // top left
};
Also here are my shaders:
const char *default_vs = "#version 330 core\n"
"layout (location = 0) in vec3 pos;\n"
"layout (location = 1) in vec2 uv;\n"
"uniform mat4 u_Model;\n"
"uniform mat4 u_Projection;\n"
"uniform mat4 u_View;\n"
"void main() {\n"
" gl_Position = u_Projection * u_View * u_Model * vec4(pos, 1.0f);\n"
"}";
const char *default_fs = "#version 330 core\n"
"out vec4 color;\n"
"void main() {\n"
" color = vec4(1.0f);\n"
"}";
Hope this helps anyone that is having the same problem!