Hi all, I’m currently trying to render a textured quad but for some reason the quad always appears as black.
I’ve made sure that the file is present, and the library that I’m useing (SOIL) is loading it correctly.
Here are some rellevent parts
Image loading:
int iwidth, iheight;
unsigned char* image = SOIL_load_image ("img.jpg", &iwidth, &iheight, NULL, SOIL_LOAD_RGB);
GLuint texture;
glGenTextures (1, &texture);
glBindTexture (GL_TEXTURE_2D, texture);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, iwidth, iheight, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glGenerateMipmap (GL_TEXTURE_2D);
SOIL_free_image_data (image);
glBindTexture (GL_TEXTURE_2D, 0);
Set up for Renderer:
void SpriteRenderer::init () {
GLuint vbo;
GLfloat verts[] = { // pos(2) texcoord(2)
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f};
glGenVertexArrays (1, &Vao);
glGenBuffers (1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glBindVertexArray (Vao);
glEnableVertexAttribArray (0);
glVertexAttribPointer (0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);
glVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)(sizeof(GLfloat) * 2));
glBindBuffer (GL_ARRAY_BUFFER, 0);
glBindVertexArray (0);
}
Rendering (texture
from above is passed to this function as tex
and color is(1.0f, 1.0f, 1.0f)
):
void SpriteRenderer::drawSprite (unsigned int tex, V2 pos, V2 size, float rotation, V3 color) {
Shader->use ();
glm::mat4 model;
model = glm::translate (model, V3(pos, 0.0f));
model = glm::translate (model, V3(0.5f * size.x, 0.5f * size.y, 0.0f));
model = glm::rotate (model, rotation, V3(0.0f, 0.0f, 1.0f));
model = glm::translate (model, V3(-0.5f * size.x, -0.5f * size.y, 0.0f));
model = glm::scale (model, V3(size, 1.0f));
Shader->setMat4 ("model", model);
Shader->setV3 ("spriteColor", color);
glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, tex);
glBindVertexArray (Vao);
glDrawArrays (GL_TRIANGLES, 0, 6);
glBindVertexArray (0);
glBindTexture (GL_TEXTURE_2D, 0);
}
The vertex shader:
#version 330 core
layout (location = 0) in vec2 pos;
layout (location = 1) in vec2 texcoord;
uniform mat4 model;
uniform mat4 projection;
out vec2 TexCoords;
void main () {
TexCoords = texcoord;
gl_Position = projection * model * vec4(pos, 0.0, 1.0);
}
And fragment shader:
#version 330 core
in vec2 TexCoords;
uniform sampler2D image;
uniform vec3 spriteColor;
out vec4 color;
void main () {
color = vec4 (spriteColor, 1.0) * texture(image, TexCoords);
}
What’s even stranger is that there I’m able to render text just fine, which is just lots of textured quads, so I’m really at a loss for what’s going wrong here. Thank you for any help.