我试图渲染一个立方体的正面,但它并没有按我想要的那样做。它显示了它的正面和底面,我不认为顶点有问题,我只是想不出来,请帮帮我
这是我的所有代码:
#include <iostream>
#include <Windows.h>
#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>
#include <GL\glew.h>
#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
using namespace std;
using namespace sf;
using namespace glm;
const GLchar* vertexSource =
"#version 430\r\n"
"in vec3 position;"
"in vec3 color;"
"uniform mat4 fullMatrix;"
"out vec3 theColor;"
"void main()"
"{"
"vec4 v = vec4(position, 1.0);"
"gl_Position = fullMatrix * v;"
"theColor = color;"
"}";
const GLchar* fragmentSource =
"#version 430\r\n"
"in vec3 theColor;"
"out vec4 daColor;"
"void main()"
"{"
" daColor = vec4(theColor, 1.0);"
"}";
int main() {
Window window(VideoMode(800, 600), "OpenGL", Style::Close);
window.setFramerateLimit(60);
glewExperimental = GL_TRUE;
glewInit();
glEnable(GL_DEPTH_TEST);
struct vertex {
vec3 position;
vec3 color;
};
vertex vert[] = {
vec3(-1.0f, +1.0f, +1.0f), // 0
vec3(+1.0f, +0.0f, +0.0f),
vec3(+1.0f, +1.0f, +1.0f), // 1
vec3(+0.0f, +1.0f, +0.0f),
vec3(+1.0f, +1.0f, -1.0f), // 2
vec3(+0.0f, +0.0f, +1.0f),
vec3(-1.0f, +1.0f, -1.0f), // 3
vec3(+1.0f, +1.0f, +1.0f),
vec3(-1.0f, +1.0f, -1.0f), // 4
vec3(+1.0f, +0.0f, +1.0f),
vec3(+1.0f, +1.0f, -1.0f), // 5
vec3(+0.0f, +0.5f, +0.2f),
vec3(+1.0f, -1.0f, -1.0f), // 6
vec3(+0.8f, +0.6f, +0.4f),
vec3(-1.0f, -1.0f, -1.0f), // 7
vec3(+0.3f, +1.0f, +0.5f),
vec3(+1.0f, +1.0f, -1.0f), // 8
vec3(+0.2f, +0.5f, +0.2f),
vec3(+1.0f, +1.0f, +1.0f), // 9
vec3(+0.9f, +0.3f, +0.7f),
vec3(+1.0f, -1.0f, +1.0f), // 10
vec3(+0.3f, +0.7f, +1.0f),
vec3(+1.0f, -1.0f, -1.0f), // 11
vec3(+0.5f, +0.7f, +0.5f),
vec3(-1.0f, +1.0f, +1.0f), // 12
vec3(+0.7f, +0.8f, +0.2f),
vec3(-1.0f, +1.0f, -1.0f), // 13
vec3(+0.5f, +0.7f, +0.3f),
vec3(-1.0f, -1.0f, -1.0f), // 14
vec3(+0.4f, +0.7f, +0.7f),
vec3(-1.0f, -1.0f, +1.0f), // 15
vec3(+0.2f, +0.5f, +1.0f),
vec3(+1.0f, +1.0f, +1.0f), // 16
vec3(+0.6f, +1.0f, +0.7f),
vec3(-1.0f, +1.0f, +1.0f), // 17
vec3(+0.6f, +0.4f, +0.8f),
vec3(-1.0f, -1.0f, +1.0f), // 18
vec3(+0.2f, +0.8f, +0.7f),
vec3(+1.0f, -1.0f, +1.0f), // 19
vec3(+0.2f, +0.7f, +1.0f),
vec3(+1.0f, -1.0f, -1.0f), // 20
vec3(+0.8f, +0.3f, +0.7f),
vec3(-1.0f, -1.0f, -1.0f), // 21
vec3(+0.8f, +0.9f, +0.5f),
vec3(-1.0f, -1.0f, +1.0f), // 22
vec3(+0.5f, +0.8f, +0.5f),
vec3(+1.0f, -1.0f, +1.0f), // 23
vec3(+0.9f, +1.0f, +0.2f),
};
GLuint numVertices = sizeof(vert) / sizeof(*vert);
GLuint vboBuffer;
glGenBuffers(1, &vboBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vboBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vert), vert, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (void*)(sizeof(float) * 3));
GLushort indi[] = {
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 numIndices = sizeof(indi) / sizeof(*indi);
GLuint indiBuffer;
glGenBuffers(1, &indiBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indiBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indi), indi, GL_STATIC_DRAW);
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
GLuint programID = glCreateProgram();
glShaderSource(vertexShaderID, 1, &vertexSource, 0);
glShaderSource(fragmentShaderID, 1, &fragmentSource, 0);
glCompileShader(vertexShaderID);
glCompileShader(fragmentShaderID);
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID);
glLinkProgram(programID);
glUseProgram(programID);
bool running = true;
while (running)
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed) {
running = false;
}
else if (event.type == Event::KeyPressed && event.key.code == Keyboard::Escape) {
running = false;
}
}
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
mat4 transformMatrix = translate(mat4(), vec3(0.0f, 0.0f, -3.0f));
mat4 projectionMatrix = perspective(45.0f, (float)800 / 600, 0.1f, 10.0f);
mat4 fullMatrix = projectionMatrix * transformMatrix;
GLint fullLocation = glGetUniformLocation(programID, "fullMatrix");
glUniformMatrix4fv(fullLocation, 1, GL_FALSE, &fullMatrix[0][0]);
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
window.display();
}
}
显示此图片
答案 0 :(得分:2)
由于深度测试已经启用(glEnable(GL_DEPTH_TEST
),但显然不起作用,似乎窗口没有深度缓冲区。从SFML文档中看起来好像必须手动请求:
sf::ContextSettings settings;
settings.depthBits = 24; //<-- This is the important line
settings.stencilBits = 8;
settings.antialiasingLevel = 4;
settings.majorVersion = 3;
settings.minorVersion = 0;
sf::Window window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, settings);
您可能需要根据需要调整OpenGL版本等,但请确保不要将depthBits
设置为零。