简体   繁体   English

OpenGL-纹理映射到三角形

[英]Opengl - texture mapping to triangles

To apply an image, as a texture, to a rectangle formed by two triangles, I am following a tutorial in open.gl/textures. 要将图像作为纹理应用于由两个三角形形成的矩形,我正在关注open.gl/textures中的教程。

I am using freeglut for context creation and the only alterations I made to the original code were in respect to that. 我正在使用freeglut进行上下文创建,而我对原始代码所做的唯一更改就是相对于此。

I was expecting to obtain my whole image displayed on a rectangle, but instead I got an image with a rectangle formed by to triangles, with the one in the right having the left side of the image and the one in the left having the right side of the image. 我原本希望将整个图像显示在一个矩形上,但是相反,我得到了一个具有由三角形组成的矩形的图像,其中右侧的一个位于图像的左侧,而左侧的一个位于右侧。图片。

I've checked the texture coordinates (code below) and according to other tutorials I've come to the conclusion that other disposition would make more sense (commented in the code below) but the results are even worse (a black triangle appears). 我检查了纹理坐标(下面的代码),并根据其他教程得出的结论是,其他处理方式更有意义(在下面的代码中进行了评论),但结果甚至更糟(出现黑色三角形)。

// Create a Vertex Buffer Object and copy the vertex data to it
    GLuint vbo;
    glGenBuffers(1, &vbo);

    GLfloat vertices[] = {
    // Position Color Texcoords
      /* 
        -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Top-left
        0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // Top-right
    -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,   // Bottom-left
    0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f  // Bottom-righ
      */
        -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Top-left
         0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // Top-right
         0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
        -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f // Bottom-left
    };

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    // Create an element array
    GLuint ebo;
    glGenBuffers(1, &ebo);

    GLuint elements[] = {
      0, 1, 2,
      2, 3, 0
     };

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

These are the calls to glVertexAttribPointer(): 这些是glVertexAttribPointer()的调用:

GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), 0);

GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));

GLint texAttrib = glGetAttribLocation(shaderProgram, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(5 * sizeof(GLfloat)));

I would appreciate some help in order to understand what I am doing wrong. 我将不胜感激,以便了解我在做什么错。 I am on a Linux platform, using g++ to compile, SOIL to upload images, freeglut for context creation and using OpenGL 3 functions. 我在Linux平台上,使用g ++进行编译,使用SOIL上传图像,使用freeglut创建上下文并使用OpenGL 3函数。

Your UV's appears to be in incorrect order. 您的紫外线似乎顺序不正确。 Try to swap again your Texcoords, but keeping the vertex coordinates in the same order and it should work. 尝试再次交换您的Texcoords,但保持顶点坐标相同的顺序,并且应该可以工作。

It should give something like this example (not tested): 它应该给出类似此示例的内容(未经测试):

-0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Top-left
0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // Top-right
0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // Bottom-right (swapped)
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f // Bottom-left (swapped)

After looking at the image you posted in one of your comments: 查看您在评论之一中发布的图像后:

在此处输入图片说明

I think this is not a problem with texture coordinates. 我认为这与纹理坐标无关。 You can tell that the "diagonal" that appears in the image does not match the diagonal of the quad, like you would expect if some texture coordinates were mixed up. 您可以说出图像中出现的“对角线”与四边形的对角线不匹配,就像您希望将某些纹理坐标混合在一起一样。

The most likely cause is that the image data does not match the size you specified in your glTexImage2D() call, or there is a problem with row alignment. 最可能的原因是图像数据与您在glTexImage2D()调用中指定的大小不匹配,或者行对齐出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM