Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: Texture Mapping Issue

  1. #1
    Intern Newbie
    Join Date
    Jun 2015
    Posts
    38

    Texture Mapping Issue

    Hello,

    Using OpenGL tutorials found on opengl-tutorial, I constructed an .obj file loader as well as a texture file loader using SOIL. I have an issue with drawing a textured object. (Shown here) Assuming I created the loaders correctly, my issue is within my rendering method (actually drawing the triangles on the screen). As you can see my texture seems to bend and warp in a very strange way. I've asked a couple of different sources and they can't seem to explain why it happens. My (admittedly messy) code is like this:

    Shaders:

    Code :
        #version 120
        //VShader
        attribute vec3 verPos;
        attribute vec2 UV;
     
        varying vec2 f_UV;
        varying vec3 normal;
        uniform mat4 comb;
     
        void main()
        {     
            normal = gl_Normal;
            vec4 v = vec4(verPos, 1.0);
    	    gl_Position = comb * v;   
            f_UV = UV;
        }
     
        #version 120
        //FShader
        uniform sampler2D TextSamp2D;
        varying vec3 normal;
     
        varying vec2 f_UV;
     
     
        void main()
        {
            gl_FragColor = texture2D(TextSamp2D, f_UV);
        }
    And just in case the view/projection matrices matter, I will include this little tidbit of code as well.

    Code :
    camera::camera(){
            projectionMatrix = glm::perspective(45.0f, float(640) / float(480), 0.1f, 1000.0f);
            viewMatrix = glm::mat4(1.0);
        }
        void camera::setPos(glm::vec3 nPos){
            myPos = nPos;
        }
        void camera::setProjectionMatrix(float fov, float scHeight, float scWidth, float disC, float disF){
            projectionMatrix = glm::perspective(fov, scHeight/scWidth, disC, disF);
        }

    The view matrix is updated by throwing this into the viewMatrix property of a camera instantiated as mainCamera:

    Code :
    mainCamera.viewMatrix = glm::lookAt(myPos, myPos + forwardVec, glm::vec3(0,1,0));

    And this is my renderer, this is run every frame (no I don't frame limit, but my computer is super junky, hence the GLSL 120) :

    Code :
    void obj3D::Draw(){
            if(!loaded) return;
            glDepthMask(GL_TRUE);
            glUseProgram(myProgID);
     
            glm::mat4 modelTrans = glm::translate(myPosition);
            glm::quat myQuat;
            myQuat = glm::quat(myAngle);
            glm::mat4 modelRot = glm::toMat4(myQuat);
            glm::mat4 modelScale = glm::scale(myScale);
            glm::mat4 model = modelTrans * modelRot * modelScale;
     
            glm::mat4 comb = mainCamera.projectionMatrix * mainCamera.viewMatrix * model;
            GLuint matrixID = glGetUniformLocation(myProgID, "comb");
            glUniformMatrix4fv(matrixID, 1, GL_FALSE, &comb[0][0]);
     
            glActiveTexture(GL_TEXTURE0 + 0);
            //myTexture is the texture ID after it is 
            //loaded into OpenGL, it's a member of  the object class
            glBindTexture(GL_TEXTURE_2D, myTexture);
            glUniform1i(shaderTextureID, 0);
     
            glEnableVertexAttribArray(0);
            glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
            glVertexAttribPointer(
                0, 
                3, 
                GL_FLOAT,
                GL_FALSE,
                0, 
                (GLvoid*)0
            );
     
            glEnableVertexAttribArray(1);
            glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
            glVertexAttribPointer(
                1, 
                2, 
                GL_FLOAT,
                GL_FALSE,
                0, 
                (GLvoid*)0
            );
     
            glEnableVertexAttribArray(2);
            glBindBuffer(GL_ARRAY_BUFFER, nBuffer);
            glVertexAttribPointer(
                2,                                // attribute
                3,                                // size
                GL_FLOAT,                         // type
                GL_FALSE,                         // normalized?
                0,                                // stride
                (GLvoid*)0                          // array buffer offset
            );
     
            glDrawArrays(GL_TRIANGLES, 0, numTriangles);
            glDisableVertexAttribArray(0);
            glDisableVertexAttribArray(1);
            glDisableVertexAttribArray(2);
        }

    My question is, why does the texture bend and warp when I rotate the view matrix on the y axis? That is, at what horrifically easy juncture did I go so wrong?

    Thanks,

    Devmane144

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,511
    Quote Originally Posted by devmane144 View Post
    ...my texture seems to bend and warp in a very strange way. ...
    My question is, why does the texture bend and warp when I rotate the view matrix on the y axis? That is, at what horrifically easy juncture did I go so wrong?
    Have you tried removing modelScale from your modeling transform?

  3. #3
    Intern Newbie
    Join Date
    Jun 2015
    Posts
    38
    Thanks for the quick response,

    Yes I tried it just now. And although my player model got smaller, the sky-cube I have was already it's regular size. The texture warping still occurred.

    Devmane144

  4. #4
    Intern Newbie
    Join Date
    Jun 2015
    Posts
    38
    Quote Originally Posted by devmane144 View Post
    Thanks for the quick response,

    Yes I tried it just now. And although my player model got smaller, the sky-cube I have was already it's regular size. The texture warping still occurred.

    Devmane144
    Do you have any other ideas?

  5. #5
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,511
    You might try just rendering the texture coordinate UVs into your color channels (e.g. into red and green). That might help diagnose whether it is the texture that's the problem or the texture coordinate mapping to the vertices.

  6. #6
    Intern Newbie
    Join Date
    Jun 2015
    Posts
    38

    Lightbulb

    Quote Originally Posted by Dark Photon View Post
    You might try just rendering the texture coordinate UVs into your color channels (e.g. into red and green). That might help diagnose whether it is the texture that's the problem or the texture coordinate mapping to the vertices.
    Brilliant idea. So I channelled the UVs into the gl_FragColor and found out that as I turn the UV coordinates seem to bend as well. So why is it that the vertex coordinates do not bend but the UVs seem to?
    Last edited by devmane144; 06-22-2015 at 05:33 PM.

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,955
    Quote Originally Posted by devmane144 View Post
    My question is, why does the texture bend and warp when I rotate the view matrix on the y axis? That is, at what horrifically easy juncture did I go so wrong?
    The effect is a lot like I'd expect if you use quads with a non-affine mapping (e.g. map a rectangular polygon to a non-rectangular region of the texture).

    If you're using quads, try changing them to triangles. The texture mapping will still have kinks, but they won't change with the camera angle.

  8. #8
    Intern Newbie
    Join Date
    Jun 2015
    Posts
    38
    Quote Originally Posted by GClements View Post
    The effect is a lot like I'd expect if you use quads with a non-affine mapping (e.g. map a rectangular polygon to a non-rectangular region of the texture).

    If you're using quads, try changing them to triangles. The texture mapping will still have kinks, but they won't change with the camera angle.
    I took some time to look up this issue, and it seems to fit with my problem, but how would I go about fixing this?

  9. #9
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,955
    Quote Originally Posted by devmane144 View Post
    how would I go about fixing this?
    Use triangles rather than quads. If you need something other than an affine or projective mapping (e.g. bilinear mapping), you need to implement it yourself in the fragment shader.

    One of the reasons that quads were removed from modern OpenGL is that the mapping was never specified or standardised.

  10. #10
    Intern Newbie
    Join Date
    Jun 2015
    Posts
    38
    Quote Originally Posted by GClements View Post
    Use triangles rather than quads. If you need something other than an affine or projective mapping (e.g. bilinear mapping), you need to implement it yourself in the fragment shader.

    One of the reasons that quads were removed from modern OpenGL is that the mapping was never specified or standardised.
    I actually am not using quads. I use triangles because I figured I wouldn't ever be able to use quads in some model with sharp corners. How would I turn on/simulate perspective correction manually?

    Devmane144

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •