1

我正在尝试将元素索引与存储在矢量类型中的顶点和纹理坐标一起使用。

我的问题是纹理没有在我的矩形上正确渲染。

这是相关的着色器初始化:

prog = f.compile_shaders(vshader, fshader);
const char* uniform_name = "mvp";
this->uni_mvp = glGetUniformLocation(prog, uniform_name);
if (this->uni_mvp == -1) {
    fprintf(stderr, "Could not bind uniform %s\n", uniform_name);
    return;
}

uniform_name = "texData";
this->uni_texdata = glGetUniformLocation(prog, uniform_name);
if (this->uni_texdata == -1) {
    fprintf(stderr, "Could not bind uniform %s\n", uniform_name);
    return;
}

和顶点着色器:

layout(location = 0) in vec4 coord3d;
layout(location = 1) in vec2 texcoord;
uniform mat4 mvp;
out vec2 uv;

void main(void) {
    gl_Position = mvp * coord3d;
    uv = texcoord;
}

和片段着色器:

in vec2 uv;
out vec3 color;

uniform sampler2D texData;

void main(){
    color = texture2D(texData, uv).rgb;
}

这是我相关的 BUFFER 初始化代码:

    glGenVertexArrays(1, &this->vao);
    glBindVertexArray(this->vao);

    glm::vec2 bar;
    bar.x = 0.0; bar.y = 0.0;
    texcoords.push_back(bar);
    bar.x = 1.0; bar.y = 0.0;
    texcoords.push_back(bar);
    bar.x = 1.0; bar.y = 1.0;
    texcoords.push_back(bar);
    bar.x = 0.0; bar.y = 1.0;
    texcoords.push_back(bar);


    glm::vec4 foo;
    foo.x = 0.0; foo.y = 0.0; foo.z = 0; foo.w = 1.0;
    vertices.push_back(foo);
    foo.x = 10.0; foo.y = 0.0; foo.z = 0; foo.w = 1.0;
    vertices.push_back(foo);
    foo.x = 10.0; foo.y = 10.0; foo.z = 0; foo.w = 1.0;
    vertices.push_back(foo);
    foo.x = 0.0; foo.y = 10.0; foo.z = 0; foo.w = 1.0;
    vertices.push_back(foo);

    elements.push_back(0);
    elements.push_back(1);
    elements.push_back(2);
    elements.push_back(3);

    glGenBuffers(1, &this->tbo);
    glBindBuffer(GL_ARRAY_BUFFER, this->tbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords) * sizeof(texcoords[0]), texcoords.data(), GL_STATIC_DRAW);

    glGenBuffers(1, &this->vbo);
    glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices[0]), vertices.data(), GL_STATIC_DRAW);

    glGenBuffers(1, &this->ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(elements[0]), elements.data(), GL_STATIC_DRAW);

这是我相关的 TEXTURE 初始化代码:

    glGenTextures(1, &this->texture_id);
glBindTexture(GL_TEXTURE_2D, this->texture_id);

int k = 12;
GLubyte image[12][12][3];
for ( int i = 0; i < k; i++ ) {
    for ( int j = 0; j < k; j++ ) {
        GLubyte c = (((i & 0x8) == 0) ^ ((j & 0x8)  == 0)) * 255;
        image[i][j][0]  = c;
        image[i][j][1]  = c;
        image[i][j][2]  = c;
    }
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);    
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, k, k, 0, GL_RGB, GL_UNSIGNED_BYTE, image);

这是相关的渲染/显示代码:

    glUseProgram(this->prog);
glBindVertexArray(this->vao);
glUniformMatrix4fv(this->uni_mvp, 1, GL_FALSE, glm::value_ptr(f.mvp));

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,  this->texture_id);
glUniform1i(this->uni_texdata, 0);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);

glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, this->tbo);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->ibo);
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_QUADS, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

glBindVertexArray(0);

这段代码用纹理渲染我的矩形,但是纹理是错误的(只是没有在矩形上正确显示)。据我所知,这个简单示例的纹理坐标应该是正确的。索引和纹理是否存在问题 - 我如何执行此操作的顶点坐标?

这是我的绿色背景上的渲染图片:

在此处输入图像描述

我期待一个矩形黑白棋盘格(12 个部分)。

好的,添加最后一个纹理坐标后,渲染如下所示:

在此处输入图像描述

UDPATE:例如,当创建更大的棋盘纹理时,GLubyte image[16][16][3];我得到以下渲染:

在此处输入图像描述

我还注意到我的 .raw 文件可以正确呈现。我仍然希望在我的棋盘纹理中看到 16 x 16 的正方形吗?

4

1 回答 1

1

我不认为你的 OpenGL 有什么问题了,尽管我认为你的纹理生成函数没有达到你的预期。

for ( int i = 0; i < k; i++ ) {
    for ( int j = 0; j < k; j++ ) {
        GLubyte c = (((i & 0x8) == 0) ^ ((j & 0x8)  == 0)) * 255;
        ...
    }
}

您从左到右有 16 个纹素,并且在 X 或 Y 方向上每 8 个颜色交替一次。

为什么你认为这会产生 12 个(或 16 个)跳棋?& 0x8如果您想在每个增量上交替颜色,您应该删除。

于 2012-09-09T14:50:45.110 回答