7
« on: March 05, 2012, 05:51:48 pm »
Hi,
I'm having an odd issue with texture mapping using OpenGL version 3.3.1 and SFML 2.0. I'm loading an image using SFML and applying the texture to my object in OpenGL. This words great most of the time, but sometimes after I restart my computer or putting it to sleep, the texture doesn't show up. I'm running Windows 7 64-bit with an ATI 5700 card.
Here's where I am loading the texture:
void LoadTextures(){
sf::Image img;
img.LoadFromFile("panda-model.png");
img.FlipVertically();
glGenTextures(1, &TexId);
glBindTexture(GL_TEXTURE_2D, TexId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.GetWidth(), img.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.GetPixelsPtr());
OnGLError("img load: use program");
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glUseProgram(ProgramId);
samplerId = glGetUniformLocation(ProgramId, "s_tex");
glActiveTexture(GL_TEXTURE0);
glUniform1i(samplerId, 0);
HGLRC test = wglGetCurrentContext();
OnGLError("pre LoadTextures: use program");
glUseProgram(ProgramId);
OnGLError("post LoadTexutres: use program");
}
And here is my render function:
void RenderFunction(sf::Window* & app){
app->SetActive();
++FrameCount;
WCtoLC[0][0] = 1;
WCtoLC[1][1] = 1;
WCtoLC[2][2] = 1;
//WCtoLC[3][1] = 4;
//WCtoLC[3][2] = 4;
NormalMatrix[0][0] = ModelMatrix[0][0];
NormalMatrix[0][1] = ModelMatrix[0][1];
NormalMatrix[0][2] = ModelMatrix[0][2];
NormalMatrix[1][0] = ModelMatrix[1][0];
NormalMatrix[1][1] = ModelMatrix[1][1];
NormalMatrix[1][2] = ModelMatrix[1][2];
NormalMatrix[2][0] = ModelMatrix[2][0];
NormalMatrix[2][1] = ModelMatrix[2][1];
NormalMatrix[2][2] = ModelMatrix[2][2];
NormalMatrix = glm::transpose(glm::inverse(NormalMatrix));
glUseProgram(ProgramId);
OnGLError("DRAW_ERROR: Could not use the shader program");
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUniformMatrix4fv(ModelMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(ModelMatrix));
glUniformMatrix4fv(ViewMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(ViewMatrix));
glUniformMatrix4fv(ProjectionMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(ProjectionMatrix));
glUniformMatrix3fv(NormalMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(NormalMatrix));
glUniformMatrix4fv(MCtoWCLoc, 1, GL_FALSE, glm::value_ptr(MCtoWC));
glUniformMatrix4fv(MCtoWCitLoc, 1, GL_FALSE, glm::value_ptr(MCtoWCit));
glUniformMatrix4fv(WCtoLCLoc, 1, GL_FALSE, glm::value_ptr(WCtoLC));
glUniformMatrix4fv(WCtoLCitLoc, 1, GL_FALSE, glm::value_ptr(WCtoLCit));
/*MCtoWCLoc = glGetUniformLocation(ProgramId, "MCtoWC");
MCtoWCitLoc = glGetUniformLocation(ProgramId, "MCtoWCit");
WCtoLCLoc = glGetUniformLocation(ProgramId, "WCtoLC");
WCtoLCitLoc = glGetUniformLocation(ProgramId, "WCtoLCit");*/
OnGLError("ERROR: Could not set the shader uniforms");
glBindTexture(GL_TEXTURE_2D, TexId);
glBindVertexArray(VaoId);
OnGLError("ERROR: Could not bind the VAO for drawing purposes");
glDrawElements(GL_TRIANGLES, 5412, GL_UNSIGNED_INT, (GLvoid*)0);
OnGLError("ERROR: Could not draw the panda");
app->Display();
}
I'm fairly new to OpenGL, so I'm not absolutely certain that this issue is being caused by SFML or if it's something else, but please let me know your thoughts. Thank you in advance!