Bad texture mapping

Started by
6 comments, last by Deliverance 9 years, 4 months ago

Hello, i'm having an issue. I'm trying to render this texture, but when I did my texture was shown incorrectly.

FPwmk.png

Result:
20r3hxh.png

As you see my other sprites are shown correctly but not this one, where the problem?

Vert shader:


std::string vertSource =
"#version 330 core\n"
"in vec2 position;"
"uniform vec2 trans = vec2(1.0,1.0);"
"uniform vec2 trans2 = vec2(0.0,0.0);"
"uniform vec2 objsize;"
"uniform vec2 winsize;"
"uniform float timer = 0;"
"attribute vec4 texCoord;"
"varying vec2 texCoordV;"
"void main()"
"{"
"   texCoordV = vec2(texCoord);"
//"	texCoordV.x /= 3;"
//"	texCoordV.x += (1.0f/3);"
//"	texCoordV.x *= 2;"
"   vec2 trans3 = ( ( trans2 * vec2( 2.0, 2.0 ) ) / winsize ) + vec2( -1.0, -1.0 );"
"   trans3 += ( objsize / winsize );"
"   vec4 test = vec4( ( position * trans ) + trans3, 0.0, 1.0 );"
"   test.xy *= "
"   mat2"
"   ("
"       vec2( cos( timer ),  -sin( timer )),"
"       vec2( sin( timer ),  cos( timer ))"
"   );"
"	gl_Position = test;"
"}";

Frag shader:


std::string fragSource =
"#version 330 core\n"
"out vec4 out_color;"
"uniform sampler2D textureMap;"
"varying vec2 texCoordV;"
"uniform float text = 0;"
"void main()"
"{"
"   if( text == 1 )"
"       out_color = texture2D(textureMap, texCoordV);"
"   else"
"       out_color = vec4(0.0,1.0,0.0,1.0);"
"}";

Advertisement

Looks like the texture coordinates are assigned incorrectly to the quad's vertices, can you post the client code (opengl code that passes the vertices to the shader ) ?


float triangle[18] =
{
    // Left bottom triangle
    -1.0, 1.0, 0.0,
    -1.0, -1.0, 0.0,
    1.0, -1.0, 0.0,
    // Right top triangle
    1.0, -1.0, 0.0,
    1.0, 1.0, 0.0,
    -1.0, 1.0, 0.0
};

float texture[16] =
{
    // Right top triangle
    0.0, 0.0,
    0.0, 1.0,
    1.0, 1.0,
    0.0, 1.0,
    // Left bottom triangle
    0.0, 0.0,
    1.0, 0.0,
    1.0, 1.0,
    0.0, 1.0
};


    glGenVertexArrays(1, &uiVAO);
    glBindVertexArray(uiVAO);

    glGenBuffers(1, &uiVBO);
    glBindBuffer(GL_ARRAY_BUFFER, uiVBO);
    glBufferData(GL_ARRAY_BUFFER, 18*sizeof(float), triangle, GL_STATIC_DRAW);
    glVertexAttribPointer(
        0,                  // The attribute we want to configure
        3,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // normalized?
        0,                  // stride
        0                   // array buffer offset
    );
    glGenBuffers(1, &uiVBO2);
    glBindBuffer(GL_ARRAY_BUFFER, uiVBO2);
    glBufferData(GL_ARRAY_BUFFER, 16*sizeof(float), texture, GL_STATIC_DRAW);
    glVertexAttribPointer(
        1,                  // The attribute we want to configure
        2,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // normalized?
        0,                  // stride
        0                   // array buffer offset
    );
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

You have 6 vertices but 8 texture-coords.

So the first triangle will get texcoords (0, 0), (0, 1), (1, 1), and then the next one will get (0, 1), (0, 0), (1, 0), which will make it horizontally flipped compared to the first.

I think that none of your textures are displayed correctly, it's just not very easily visible for the others. Try copying the source texture for the stone-block with the grass on top onto the screenshot, and you will probably find that the top right triangle is flipped there too.

I changed texcoords to those which u wrote and the same result happens.

Actually , looking at how your data is arranged texture coordinates should be the following. (also changed from 16 to 12 at the texture coordinates). Erik's suggestion also employs a different arrangement of the vertices. So for a value of -1 throw in a 0 at the texcoords and for a value of 1 just put in an 1.0

A more common arrangement of the vertices would be the following:


float triangle[18] =
{
// Left bottom triangle
0.0, 0.0, 0.0,
1.0, 1.0, 0.0,
1.0, 1.0, 0.0,
// Right top triangle
1.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 0.0
};

Which goes in a countercloickwise order through the vertices of the unit square alligned with the botom left corner in the origin.


The solution would look like this:

float triangle[18] =
{
// Left bottom triangle
-1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
// Right top triangle
1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
-1.0, 1.0, 0.0
};

float texture[12] =
{
// Right top triangle
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
// Left bottom triangle,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0
};

and the code:


  glGenVertexArrays(1, &uiVAO);
    glBindVertexArray(uiVAO);

    glGenBuffers(1, &uiVBO);
    glBindBuffer(GL_ARRAY_BUFFER, uiVBO);
    glBufferData(GL_ARRAY_BUFFER, 18*sizeof(float), triangle, GL_STATIC_DRAW);
    glVertexAttribPointer(
        0,                  // The attribute we want to configure
        3,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // normalized?
        0,                  // stride
        0                   // array buffer offset
    );
    glGenBuffers(1, &uiVBO2);
    glBindBuffer(GL_ARRAY_BUFFER, uiVBO2);
    glBufferData(GL_ARRAY_BUFFER, 12*sizeof(float), texture, GL_STATIC_DRAW);
    glVertexAttribPointer(
        1,                  // The attribute we want to configure
        2,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // normalized?
        0,                  // stride
        0                   // array buffer offset
    );
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

Thank you very much

dry.png

This topic is closed to new replies.

Advertisement