Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: GLSL Texture mapping problems  (Read 2442 times)

0 Members and 1 Guest are viewing this topic.

KraHen

  • Newbie
  • *
  • Posts: 10
    • View Profile
GLSL Texture mapping problems
« on: August 02, 2013, 11:49:20 am »
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? :)
« Last Edit: August 02, 2013, 07:09:49 pm by KraHen »

zweifel

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: GLSL Texture mapping problems
« Reply #1 on: August 05, 2013, 04:41:14 pm »
I understand you solved the problem.
But do you mean that you did not use the SFML framework for it?
In other words, you did not use the shader.loadFromMemory?

I only have experience with compiling in usual OpenGL. Still, I am interested in using GLSL with SFML and that is why I am asking.

KraHen

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: GLSL Texture mapping problems
« Reply #2 on: August 06, 2013, 05:38:06 pm »
Nope, I wrote myself a class to handle shaders manually, I really feel like SFML`s shader class was intended for pure SFML applications (quite obvious and rational choice). I still use SFML for other purposes though. :)

zweifel

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: GLSL Texture mapping problems
« Reply #3 on: August 07, 2013, 08:30:02 am »
For me it is going pretty nice with shaders until now.
Did you use these functions (I assume you are using OpenGL):

sf::Shader shader;
...

// bind the shader
sf::Shader::bind(&shader);

// draw your OpenGL entity here...

// bind no shader
sf::Shader::bind(NULL);

« Last Edit: August 07, 2013, 08:37:19 am by zweifel »