add backface culling

This commit is contained in:
Luca Conte 2024-04-04 14:05:28 +02:00
parent a2c33e3c76
commit 56cf731b7f
3 changed files with 34 additions and 9 deletions

View File

@ -40,6 +40,8 @@ typedef struct {
GLuint program;
GLuint vao;
GLfloat rotation;
void init(void) {
// create and compile vertex shader
@ -87,6 +89,10 @@ void init(void) {
printf("%s",infoLog);
}
free(fragmentText);
fragmentText = NULL;
fragmentTextConst = NULL;
// create and link shader program
program = glCreateProgram();
glAttachShader(program, vertexShader);
@ -214,6 +220,7 @@ void init(void) {
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
}
@ -222,11 +229,22 @@ void draw(void) {
glUseProgram(program);
glBindVertexArray(vao);
rotation = rotation + 0.01f;
if (rotation > 2 * 3.1415926f) {
rotation = 0;
}
glUniform1f(glGetUniformLocation(program, "rotation"), rotation);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(RESTART);
GLuint indices[] = {0, 1, 2, 3, RESTART, 4, 5, 6, 7, RESTART, 8, 9, 10, 11, RESTART, 12, 13, 14, 15};
GLuint indices[] = {0, 1, 2, 3, RESTART, 4, 6, 5, 7, RESTART, 8, 9, 10, 11, RESTART, 12, 14, 13, 15};
glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices) / sizeof(GLuint), GL_UNSIGNED_INT, indices);
}
@ -236,14 +254,12 @@ void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
}
int main(void) {
printf("Hello World!\n");
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(800, 800, "Computergrafik 1", NULL, NULL);
GLFWwindow *window = glfwCreateWindow(600, 600, "Computergrafik 1", NULL, NULL);
if (!window) {
printf("Failed to create window\n");
@ -257,6 +273,9 @@ int main(void) {
glewInit();
printf("OpenGL version supported by this platform (%s):\n", glGetString(GL_VERSION));
init();
while (!glfwWindowShouldClose(window)) {

View File

@ -7,7 +7,9 @@ unsigned char vertexShader_glsl[] = {
0x61, 0x79, 0x6f, 0x75, 0x74, 0x20, 0x28, 0x6c, 0x6f, 0x63, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x31, 0x29, 0x20, 0x69, 0x6e, 0x20,
0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78,
0x56, 0x61, 0x6c, 0x75, 0x65, 0x3b, 0x0a, 0x6f, 0x75, 0x74, 0x20, 0x66,
0x56, 0x61, 0x6c, 0x75, 0x65, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f,
0x72, 0x6d, 0x20, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x72, 0x6f, 0x74,
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x6f, 0x75, 0x74, 0x20, 0x66,
0x6c, 0x6f, 0x61, 0x74, 0x20, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e,
0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x3b, 0x0a, 0x76, 0x6f, 0x69, 0x64,
0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x66,
@ -15,7 +17,10 @@ unsigned char vertexShader_glsl[] = {
0x20, 0x3d, 0x20, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x56, 0x61, 0x6c,
0x75, 0x65, 0x3b, 0x0a, 0x09, 0x67, 0x6c, 0x5f, 0x50, 0x6f, 0x73, 0x69,
0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28,
0x61, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x30,
0x2e, 0x30, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x29, 0x3b, 0x0a, 0x7d
0x61, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x78, 0x20,
0x2a, 0x20, 0x73, 0x69, 0x6e, 0x28, 0x72, 0x6f, 0x74, 0x61, 0x74, 0x69,
0x6f, 0x6e, 0x29, 0x2c, 0x20, 0x61, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69,
0x6f, 0x6e, 0x2e, 0x79, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x31,
0x2e, 0x30, 0x29, 0x3b, 0x0a, 0x7d
};
unsigned int vertexShader_glsl_len = 215;
unsigned int vertexShader_glsl_len = 270;

View File

@ -1,8 +1,9 @@
#version 330 core
layout (location = 0) in vec2 aPosition;
layout (location = 1) in float vertexValue;
uniform float rotation;
out float fragmentValue;
void main() {
fragmentValue = vertexValue;
gl_Position = vec4(aPosition, 0.0, 1.0);
gl_Position = vec4(aPosition.x * sin(rotation), aPosition.y, 0.0, 1.0);
}