gpt4 book ai didi

c++ - 在 opengl 中绘制具有六个不同颜色面的立方体

转载 作者:行者123 更新时间:2023-11-30 05:23:29 25 4
gpt4 key购买 nike

我在 OpenGL 中绘制了一个立方体,但我希望立方体的每个面都填充一种颜色。

这就是我所做的,我不知道为什么只有2张脸被填充了颜色。

void DrawCube(GLfloat centerPosX, GLfloat centerPosY, GLfloat centerPosZ, GLfloat edgeLength)
{
GLfloat halfSideLength = edgeLength * 0.5f;

GLfloat vertices[] =
{
// front face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // top left
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // top right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom left

// back face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, // top left
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, // top right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom left

// left face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // top left
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, // top right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom left

// right face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // top left
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, // top right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom left

// top face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // top left
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, // top right
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, // bottom right
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // bottom left

// bottom face // right face
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // top left
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // top right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom left centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom right centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength // bottom left
};

GLfloat colour[] = {
255, 0, 0,
255, 0, 0,
255, 0, 0,
255, 0, 0,
0, 255, 0,
0, 255, 0,
0, 255, 0,
0, 255, 0,
0, 0, 255,
0, 0, 255,
0, 0, 255,
0, 0, 255,
100,100,100,
100,100,100,
100,100,100,
100,100,100,
200,200,200,
200,200,200,
200,200,200,
200,200,200,
40,220,75,
40,220,75,
40,220,75,
40,220,75,
};

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colour);
glDrawArrays(GL_QUADS, 0, 24);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}

我的程序的输出是这样的: enter image description here

如你所见,我有 1 张红脸、1 张绿脸和 4 张非彩色脸

最佳答案

虽然 Armen 是正确的,但还有其他问题。

您忘记开启深度测试。这就是为什么你可以看到红色的一面,即使从你的角度来看你不应该看到。这也是为什么您可以“透过”前墙看到后绿墙的原因。

关于c++ - 在 opengl 中绘制具有六个不同颜色面的立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39138419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com