I’m attempting a basic textured quad using GL3.2 only.
The quad is drawn - but it only shows up black - no texture is visible. Help please !!
The texture is a TGA file loaded using the GLFW function. The textureID is created and bound prior to loading.
This is run after the shaders are compiled and linked:
glUniform1i(glGetUniformLocation(shaderProgram[SHADER_NORMAL].programID, "ColorMapSampler"), 0);
Shaders -
#version 150
precision highp float;
uniform sampler2D ColorMapSampler;
in vec2 outTexCoord;
in vec4 outColor;
out vec4 FragColor;
void main(void)
{
FragColor = texture(ColorMapSampler, outTexCoord.st);
}
#version 150
uniform mat4 projMat;
uniform mat4 modelMat;
in vec2 inTexture0;
in vec2 inPosition;
out vec2 outTexCoord;
in vec3 inColor;
out vec4 outColor;
void main(void)
{
vec4 pos = modelMat * vec4(inPosition, 0.0, 1.0);
gl_Position = projMat * pos;
outTexCoord = inTexture0;
}
Init code for the quad
bool gl_setupVBOFirstUse(int whichTexture, float posX, float posY, float texWidth, float texHeight)
//-----------------------------------------------------------------------------
{
myQuad[0].x = posX;
myQuad[0].y = posY;
myQuad[0].s = 0.0f;
myQuad[0].t = 1.0f;
myQuad[1].x = posX + texWidth;
myQuad[1].y = posY;
myQuad[1].s = 0.0f;
myQuad[1].t = 0.0f;
myQuad[2].x = posX + texWidth;
myQuad[2].y = posY + texHeight;
myQuad[2].s = 1.0f;
myQuad[2].t = 1.0f;
myQuad[3].x = posX;
myQuad[3].y = posY + texHeight;
myQuad[3].s = 1.0f;
myQuad[3].t = 0.0f;
GLuint indexBufferObject;
glGenVertexArrays(1, &myvao[0]);
glGenBuffers(2, &myvbo[0]);
glBindVertexArray(myvao[0]); // first vao is created
glBindBuffer(GL_ARRAY_BUFFER, myvbo[0]); // position buffer is created
glBufferData(GL_ARRAY_BUFFER, sizeof(myVertex) * 4, myQuad, GL_STATIC_DRAW);
const GLuint positionIdx = glGetAttribLocation(shaderProgram[SHADER_NORMAL].programID, "inPosition");
//
// this binds position buffer to VAOs
glVertexAttribPointer(positionIdx, 2, GL_FLOAT, GL_FALSE, sizeof(myVertex), BUFFER_OFFSET(0));
glEnableVertexAttribArray(positionIdx);
const GLuint textureIdx = glGetAttribLocation(shaderProgram[SHADER_NORMAL].programID, "inTexture0");
//
// this binds texture positions to VAO
glVertexAttribPointer(textureIdx, 2, GL_FLOAT, GL_FALSE, sizeof(myVertex), BUFFER_OFFSET(sizeof(GLfloat) * 2));
glEnableVertexAttribArray(textureIdx);
// const GLuint colorIdx = glGetAttribLocation(shaderProgram[SHADER_NORMAL].programID, "inColor");
// glBindBuffer(GL_ARRAY_BUFFER, myvbo[1]);
float colorData0[] = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
// glBufferData(GL_ARRAY_BUFFER, sizeof(colorData0), colorData0, GL_STATIC_DRAW);
// this binds color buffer to VAO
//glVertexAttribPointer(colorIdx, 3, GL_FLOAT, GL_FALSE, 0, 0);
// glEnableVertexAttribArray(colorIdx);
projMatrixLocation = glGetUniformLocation(shaderProgram[SHADER_NORMAL].programID, "projMat");
modelMatrixLocation = glGetUniformLocation(shaderProgram[SHADER_NORMAL].programID, "modelMat");
return GL_TRUE;
}
And the drawing function
void updateScreen(float interpolation)
//-----------------------------------------------------------------------------
{
kmGLMatrixMode(KM_GL_PROJECTION);
kmGLLoadIdentity();
kmMat4OrthographicProjection(&projectionMatrix, 0.0f, (float)winWidth, (float)winHeight, 0.0f, 1.0f, 100.0f);
kmGLMatrixMode(KM_GL_MODELVIEW);
kmGLLoadIdentity();
kmGLTranslatef(0.0, 0.0f, -1.0f);
kmGLGetMatrix(KM_GL_MODELVIEW, &modelview);
//
// End matrix generation
//
glViewport(0, 0, winWidth, winHeight);
glClearColor(0.0, 0.0, 0.3, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram[SHADER_NORMAL].programID);
//
// Load the matrixes into the vertex shader
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, modelview.mat);
glUniformMatrix4fv(projMatrixLocation, 1, GL_FALSE, projectionMatrix.mat);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0].texID);
// Set to texture unit bound - not texture ID
glUniform1i(glGetUniformLocation(shaderProgram[SHADER_NORMAL].programID, "ColorMapSampler"), 0);
glBindVertexArray(myvao[0]);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
// glDrawElements(GL_TRIANGLE_STRIP, sizeof(myIndexes) / sizeof(myIndexes[0]), GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));
glfwSwapBuffers(); // Swap buffers
gl_getAllGLErrors("updateScreen");
glUseProgram(0);
glBindTexture(GL_TEXTURE_2D, 0);
// Check if the ESC key was pressed or the window was closed
quitProgram = glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED );
}
I do get this error from rendering the screen - but only once - it seems to be related to the way the GLFW window is setup.
10:16:00 > OpenGL Error: Called from [ updateScreen ]
10:16:00 > glError [ GL_INVALID_OPERATION: The specified operation is not allowed in the current state. ]