如何在OpenGL / C ++中绘制立方体?

时间:2015-07-18 05:33:35

标签: c++ opengl

我看到很多人使用Glbegin(GL_POLYGON)来绘制多维数据集,我在课堂上了解到这一点,我的老师认为我的方式更差。

GLfloat verts[] =
{
    -1.0f, +1.0f, +1.0f, // 0
    +1.0f, +0.0f, +0.0f, // Color
    +1.0f, +1.0f, +1.0f, // 1
    +0.0f, +1.0f, +0.0f, // Color
    +1.0f, +1.0f, -1.0f, // 2
    +0.0f, +0.0f, +1.0f, // Color
    -1.0f, +1.0f, -1.0f, // 3
    +1.0f, +1.0f, +1.0f, // Color

    -1.0f, +1.0f, -1.0f, // 4
    +1.0f, +0.0f, +1.0f, // Color
    +1.0f, +1.0f, -1.0f, // 5
    +0.0f, +0.5f, +0.2f, // Color
    +1.0f, -1.0f, -1.0f, // 6
    +0.8f, +0.6f, +0.4f, // Color
    -1.0f, -1.0f, -1.0f, // 7
    +0.3f, +1.0f, +0.5f, // Color

    +1.0f, +1.0f, -1.0f, // 8
    +0.2f, +0.5f, +0.2f, // Color
    +1.0f, +1.0f, +1.0f, // 9
    +0.9f, +0.3f, +0.7f, // Color
    +1.0f, -1.0f, +1.0f, // 10
    +0.3f, +0.7f, +0.5f, // Color
    +1.0f, -1.0f, -1.0f, // 11
    +0.5f, +0.7f, +0.5f, // Color

    -1.0f, +1.0f, +1.0f, // 12
    +0.7f, +0.8f, +0.2f, // Color
    -1.0f, +1.0f, -1.0f, // 13
    +0.5f, +0.7f, +0.3f, // Color
    -1.0f, -1.0f, -1.0f, // 14
    +0.4f, +0.7f, +0.7f, // Color
    -1.0f, -1.0f, +1.0f, // 15
    +0.2f, +0.5f, +1.0f, // Color

    +1.0f, +1.0f, +1.0f, // 16
    +0.6f, +1.0f, +0.7f, // Color
    -1.0f, +1.0f, +1.0f, // 17
    +0.6f, +0.4f, +0.8f, // Color
    -1.0f, -1.0f, +1.0f, // 18
    +0.2f, +0.8f, +0.7f, // Color
    +1.0f, -1.0f, +1.0f, // 19
    +0.2f, +0.7f, +1.0f, // Color

    +1.0f, -1.0f, -1.0f, // 20
    +0.8f, +0.3f, +0.7f, // Color
    -1.0f, -1.0f, -1.0f, // 21
    +0.8f, +0.9f, +0.5f, // Color
    -1.0f, -1.0f, +1.0f, // 22
    +0.5f, +0.8f, +0.5f, // Color
    +1.0f, -1.0f, +1.0f, // 23
    +0.9f, +1.0f, +0.2f, // Color
};

GLushort indices[] = {
    0, 1, 2, 0, 2, 3, // Top
    4, 5, 6, 4, 6, 7, // Front
    8, 9, 10, 8, 10, 11, // Right 
    12, 13, 14, 12, 14, 15, // Left
    16, 17, 18, 16, 18, 19, // Back
    20, 22, 21, 20, 23, 22, // Bottom
};


GLuint VertexBufferID;
glGenBuffers(1, &VertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 6, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (char*)(sizeof(float) * 3));

//GLushort indices[] = { 0, 1, 2, 3, 1, 2, 0, 1, 2, 3, 1, 2 };
GLuint indexBufferID;
glGenBuffers(1, &indexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

所以即时尝试使用它绘制一个立方体,但它看起来像一个正方形,当我旋转它不是一个立方体。

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, width(), height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); 

GLint TriPositionUniformLocation = glGetUniformLocation(programID, "TriPosition");
GLint domColorUniformLocation = glGetUniformLocation(programID, "domColor");

vec3 domColor(1.0f, 0.0f, 0.0f);

glUniformMatrix4fv(TriPositionUniformLocation, 1, GL_FALSE, &TriPosition[0][0]);
glUniform3fv(domColorUniformLocation, 1, &domColor[0]);

glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);

为什么没有立方体被绘制到屏幕上?我错过了什么方法或者我做错了什么?

mat4 moveObject(mat4 mat, int left, int up, int right, int down, float                 speed, float angle,float dt)
{
short Left = GetAsyncKeyState(left);
short Up = GetAsyncKeyState(up);
short Right = GetAsyncKeyState(right);
short Down = GetAsyncKeyState(down);

mat4 matTemp = mat;

if (Left != 0)
{
    matTemp = rotate(matTemp, angle, vec3(1.0f, 1.0f, 1.0f));
}

if (Right != 0)
{
    matTemp = rotate(matTemp, -angle, vec3(0.0f, 0.0f, 1.0f));
}
if (Up != 0)
{
    matTemp = translate(matTemp, vec3(0.0f, 0.0f, speed)* dt);
}
if (Down != 0)
{
    matTemp = translate(matTemp, vec3(0.0f, -speed, 0.0f)* dt);
}

return matTemp;

}

这就是我设置triPosition的方法,我获取返回的值并将triPosition设置为等于。

#version 430

in layout(location=0) vec3 position;
in layout(location=1) vec3 vertexColor;

uniform mat4 TriPosition;
uniform vec3 domColor;

out vec3 theColor;

void main()
{
    vec4 v = vec4(position, 1.0);
    gl_Position = TriPosition * v;
    theColor = vertexColor;
};

我现在的问题是我看到了立方体,但我无法在3d空间移动它可能非常接近我的相机,但即使按下它也无法移动它。

1 个答案:

答案 0 :(得分:2)

您错误地指出您的顶点坐标在您glVertexAttribPointer的调用中只有2个组件(即2维)。所以你得到的是方形而不是立方体。

第二个参数应为3:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, 0);