I’ve been trying to map a texture onto two triangles. After running into issues, I compressed the code into these few segments. The “Texture” at this point is a single red pixel for testing. You can see it in the variable “pixel”. I’ve checked several tutorials, The OpenGL Programming Guide, and even some older code of mine that worked. I feel like there’s a dumb mistake here that I’m just missing.
I expect it to just draw a giant red square on the screen. Instead the actual drawn image is at the bottom of the post. I set glClearColor to a dark green so that the (accidently) black geometry is visible. Does anyone know what I might have done wrong?
I can provide more code as requested, thanks.
GLint texUniform = glGetUniformLocation(program, "colorMap");
SpriteInvariant.texUniform = texUniform;
Sprite sprite;
{
u8 pixel[4] = { 255, 0, 0, 255};
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
sprite = (Sprite) {
.position = (v3) {0,0,-0.5},
.scale = (v3) {1,1,1},
.rotation = QuatFromEuler(0,0,0),
.texture = texture
};
}
glClearColor(0,0.1,0,1);
glClear(GL_COLOR_BUFFER_BIT);
{
FrameRenderInfo renderState = frameRenderInfo;
v2 windowSize = renderState.windowSize;
r32 aspectRatio = windowSize.x / windowSize.y;
mat4 cam = MulMat4(
Mat4FromQuat(renderState.camera.rotation),
Mat4FromPosition(renderState.camera.position));
mat4 proj = MulMat4(
Mat4FromEuler(0,0,0),
Mat4FromPerspective(aspectRatio, renderState.camera.fov, renderState.camera.nearPlane,
renderState.camera.farPlane));
mat4 view = InvertMat4(cam);
glUseProgram(SpriteInvariant.shader);
glBindVertexArray(SpriteInvariant.vao);
glUniformMatrix4fv(SpriteInvariant.projectionUniform, 1, GL_FALSE, proj.e);
glUniformMatrix4fv(SpriteInvariant.viewSpaceUniform, 1, GL_FALSE, view.e);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, sprite.texture);
glUniform1i(SpriteInvariant.texUniform, sprite.texture);
mat4 modelSpace = MulMat4s( 3,
Mat4FromScaleV3(sprite.scale),
Mat4FromQuat(sprite.rotation),
Mat4FromPosition(sprite.position)
);
glUniformMatrix4fv(SpriteInvariant.modelSpaceUniform, 1, GL_FALSE, modelSpace.e);
glDrawArrays(GL_TRIANGLES, 0, 6);
glerror_to_debug(__FILE__,__LINE__);
}
SwapBuffers(deviceContext);
Shaders:
Vertex
#version 330 core
in vec3 pos;
in vec2 uv;
uniform mat4 modelSpace;
uniform mat4 viewSpace;
uniform mat4 projection;
out vec4 passPos;
out vec2 passUv;
void main()
{
vec4 renderPosition = projection * viewSpace * modelSpace * vec4(pos,1f);
// render position
//vec4 renderPosition = projection * vec4(pos,1);
gl_Position = renderPosition;
// vert uv
passUv = uv;
// world position
vec4 vertWorldPosition = (modelSpace*vec4(pos,1.0f));
passPos = (vertWorldPosition)/vertWorldPosition.w;
}
Fragment
#version 330 core
in vec4 passPos;
in vec2 passUv;
uniform sampler2D colorMap;
out vec4 color;
void main()
{
float dist = 1.0f - length(passPos)/256.0f;
color = texture(colorMap, passUv);//+ vec4(0,passUv.x,passUv.y,1);
}
Right now, this code produces this output: