I was trying to render the front face of a cube, but it didn't do what I wanted it to. It showed its front face and the bottom face, I don't think there is a problem with the vertices, and I just couldn't figure it out, please help me
this is all the code I have:
#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();
}
}
it shows this image
Since the depth-test already gets enabled ( glEnable(GL_DEPTH_TEST
), but is obviously not working, it seems that the window does not have a depth buffer. From the SFML documentation it looks as if one has to request that manually:
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);
You might have to adapt the OpenGL version etc. to your needs, but make sure not to set depthBits
to zero.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.