0

只是做我的计算机图形作业——将纹理(具有不同数字的 600x400 位图)放在一个立方体上以形成一个合适的骰子。我设法使用“经典”纹理映射来做到这一点:创建 verices 并为其添加相应的纹理坐标:

int arrayindex = 0;
float xpos = 0.0f;
float xposEnd = 0.32f;
float ypos = 0.0f;
float yposEnd = 0.49f;
int count = 0;
void quad( int a, int b, int c, int d ) {
    colors[arrayindex] = vertex_colors[a];
    points[arrayindex] = vertices[a];
    tex_coord[arrayindex] = new Point2(xpos, ypos);
    arrayindex++;
    colors[arrayindex] = vertex_colors[b];
    points[arrayindex] = vertices[b];
    tex_coord[arrayindex] = new Point2(xpos, yposEnd);
    arrayindex++;
    colors[arrayindex] = vertex_colors[c];
    points[arrayindex] = vertices[c];
    tex_coord[arrayindex] = new Point2(xposEnd, yposEnd);
    arrayindex++;
    colors[arrayindex] = vertex_colors[a];
    points[arrayindex] = vertices[a];
    tex_coord[arrayindex] = new Point2(xpos, ypos);
    arrayindex++;
    colors[arrayindex] = vertex_colors[c];
    points[arrayindex] = vertices[c];
    tex_coord[arrayindex] = new Point2(xposEnd, yposEnd);
    arrayindex++;
    colors[arrayindex] = vertex_colors[d];
    points[arrayindex] = vertices[d]; 
    tex_coord[arrayindex] = new Point2(xposEnd, ypos);
    arrayindex++;
    xpos = xpos + 0.34f;
    xposEnd = xpos + 0.32f;
    count++;
    if (count == 3) {
        xpos = 0.0f;
        xposEnd = 0.33f;
        ypos = 0.51f;
        yposEnd = 1.0f;
    }
}

void colorcube() {
    quad( 1, 0, 3, 2 );
    quad( 2, 3, 7, 6 );
    quad( 3, 0, 4, 7 );
    quad( 6, 5, 1, 2 );
    quad( 5, 4, 0, 1 );
    quad( 4, 5, 6, 7 );

    pointsBuf = VectorMath.toBuffer(points);
    colorsBuf = VectorMath.toBuffer(colors);
    texcoord = VectorMath.toBuffer(tex_coord);
}

将所有这些东西传递给着色器并将它们放在一起。

但是查看幻灯片我注意到这种方法应该是“pre opengl3”。有没有其他方法可以做这些事情?

在讲座示例中,我注意到将它放在顶点着色器中,但它只是用于简单的 2d 平面,而不是 3d 立方体

tex_coords = vec2(vPosition.x+0.5,vPosition.z+0.5);

然后传递给片段着色器以创建纹理。

4

1 回答 1

1

但是查看幻灯片我注意到这种方法应该是“pre opengl3”。

我认为您的幻灯片指的是旧的即时模式。在即时模式下,每个顶点及其属性通过调用立即绘制它们的函数发送到 OpenGL。

但是,在您的代码中,您正在使用顶点数据初始化缓冲区。然后,这个缓冲区可以作为一个整体传递给 OpenGL,并通过一个 OpenGL 调用作为一个批次绘制。我写了“可能”,因为您的问题中没有一个 OpenGL 调用。

于 2012-12-16T22:11:17.997 回答