Hello there! I`m trying to implement texture mapping on a 3D object in OpenGL based on the tutorials at
http://ogldev.atspace.co.uk/. I got stuck at the texture mapping part, I`m sure I missed core concepts since I`m a beginner with OpenGL/GLSL, but I can`t seem to find where, I was hoping you could help me out a bit.
I`m getting a GL_INVALID_OPERATION error when I try to run my code, I can`t seem to manage to put the texture in the right place. Here is my revelant code :
[EDIT] I still don`t know what the problem was, but after writing my own shader reader/compiler everything works just fine. int _tmain(int argc, char** argv)
{
/// ...
window.setActive();
glewInit();
glViewport(0, 0, window.getSize().x, window.getSize().y);
glClearColor(0.4f, 0.6f, 0.9f, 0.0f); // cornflower blue :)
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);
CreateVertexBuffer();
CreateIndexBuffer();
sf::Shader shader;
shader.loadFromMemory(pVS, pFS);
sf::Texture test2;
test2.loadFromFile("test.png");
shader.setParameter("gSampler", test2);
// ... in the game loop
Render(shader, test2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
window.pushGLStates();
FPSText.setString("FPS : " + int2Str((int)FPS));
window.draw(FPSText);
window.popGLStates();
window.display();
}
Here is the render function
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
sf::Shader::bind(&shader);
static float Scale = 0.0f;
Scale += 0.1f;
glm::mat4 ProjectionMatrix = getProjectionMatrix();
glm::mat4 ViewMatrix = getViewMatrix();
glm::mat4 ModelMatrix = glm::mat4(1.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
glUniformMatrix4fv(gWVPLocation, 1, GL_TRUE, glm::value_ptr(MVP));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)12);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
sf::Texture::bind(&test);
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
sf::Texture::bind(NULL);
sf::Shader::bind(NULL);
And finally the shaders (Which are identical with the ones the tutorial provides but meh)
static const char* pVS = " \n\
#version 330 \n\
\n\
layout (location = 0) in vec3 Position; \n\
layout (location = 1) in vec2 TexCoord; \n\
\n\
uniform mat4 gWVP; \n\
\n\
out vec2 TexCoord0; \n\
\n\
void main() \n\
{ \n\
gl_Position = gWVP * vec4(Position, 1.0); \n\
TexCoord0 = TexCoord; \n\
}";
static const char* pFS = " \n\
#version 330 \n\
\n\
in vec2 TexCoord0; \n\
out vec4 FragColor; \n\
uniform sampler2D gSampler; \n\
\n\
void main() \n\
{ \n\
FragColor = texture2D(gSampler, TexCoord0.st); \n\
}";
I can`t seem to see my error, could you help me out?