我倾向于使用OpenGL,目前我学习如何添加第三维。我想制作一个立方体,但其中只有一面不透明:
我的绘图功能如下所示:
void draw()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH);
glUseProgram(program);
// The attributes are like they have to be, nothing wrong here
glBindBuffer(GL_ARRAY_BUFFER, vboCube);
glEnableVertexAttribArray(attributePosition);
glVertexAttribPointer(
attributePosition,
3,
GL_FLOAT,
GL_FALSE,
sizeof(attributes),
(GLvoid *)offsetof(attributes, position)
);
glEnableVertexAttribArray(attributeColor);
glVertexAttribPointer(
attributeColor,
3,
GL_FLOAT,
GL_FALSE,
sizeof(attributes),
(GLvoid *)offsetof(attributes, color)
);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboCube);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(attributePosition);
glDisableVertexAttribArray(attributeColor);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
我的tick函数(每秒调用40次)如下所示:
void tick()
{
glm::mat4 model = glm::translate(glm::mat4(1), glm::vec3(0, 0, -4));
glm::mat4 view = glm::lookAt(glm::vec3(0, -2, 0), glm::vec3(0, 0, -4), glm::vec3(0, 1, 0));
glm::mat4 projection = glm::perspective(45.0f, WIDTH / (float) HEIGHT, 0.1f, 10.0f);
glm::mat4 daRealMvp = projection * view * model; // 9gaggers will get this
// adding some rotation so i can see why the cube looks strange
float angle = glfwGetTime();
daRealMvp = daRealMvp * glm::rotate(glm::mat4(1), angle, glm::vec3(0, 1, 0));
glUniformMatrix4fv(uniformMvp, 1, GL_FALSE, glm::value_ptr(daRealMvp));
}
如果我删除glEnable(GL_DEPTH);
(或将其移至另一行)或将glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
更改为glClear(GL_COLOR_BUFFER_BIT);
没有任何更改,那么我认为清除深度缓冲区存在问题,但是我不知道它是什么。
那么,我该怎么做才能得到一个漂亮的立方体? :)
EDIT1:
有趣的是,蓝色面(不透明)不是前面的那个(所有顶点都有正Z),右边是那个(所有顶点都有正X)。答案 0 :(得分:4)
你应该使用
glEnable(GL_DEPTH);
而不是
gettextCatalog.getString
在第三行。