opengl_learn

Step-by-step introduction to OpenGL
git clone https://0xdd.org/code/opengl_learn.git
Log | Files | Refs | README | LICENSE

commit 1c01d7506ba73a9ec35bc46da53b7ce1b76fb464
parent 39dd3cf30febe75c207214f96aa7751f5a226ce7
Author: David DiPaola <DavidDiPaola@users.noreply.github.com>
Date:   Sat,  9 Jun 2018 17:36:43 -0400

02-cube: initial commit

Diffstat:
M.gitignore | 1+
A02-cube.c | 178+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MMakefile | 12++++++++++--
Mogl/ogl_init.c | 5++++-
Mogl/ogl_program_uniform_set_mat4f.c | 9++++++++-
5 files changed, 201 insertions(+), 4 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,6 +1,7 @@ *.o 00-triangle 01-perspective +02-cube .*.swp diff --git a/02-cube.c b/02-cube.c @@ -0,0 +1,178 @@ +/* +2018 David DiPaola +licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/) +*/ + +#include <stdio.h> + +#include <stdlib.h> + +#include <GL/glew.h> + +#include <GLFW/glfw3.h> + +#include "ogl/ogl.h" + +int +main() { + int status; + + GLFWwindow * window = NULL; + status = ogl_init(400, 240, "02 - cube", &window); + if (status < 0) { + fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); + return status; + } + + const char * vertexshader = + "#version 100" "\n" + "precision highp float;" "\n" + "attribute vec3 vertex_in_position;" "\n" + "attribute vec3 vertex_in_color;" "\n" + "varying vec4 vertex_out_fragment_in_color;" "\n" + "uniform mat4 MVP;" "\n" + "void main(){" "\n" + " gl_Position = MVP * vec4(vertex_in_position, 1);" "\n" + " vertex_out_fragment_in_color = vec4(vertex_in_color, 1);" "\n" + "}" "\n" + ; + const char * fragmentshader = + "#version 100" "\n" + "precision lowp float;" "\n" + "varying vec4 vertex_out_fragment_in_color;" "\n" + "void main() {" "\n" + " gl_FragColor = vertex_out_fragment_in_color;" "\n" + "}" "\n" + ; + GLuint program_ID = 0; + status = ogl_program_build( + vertexshader, fragmentshader, + &program_ID + ); + if (status < 0) { + fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); + return status; + } + + GLint program_vertex_in_position_ID = 0; + status = ogl_program_attribute_get_ID(program_ID, "vertex_in_position", &program_vertex_in_position_ID); + if (status < 0) { + fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); + return status; + } + + GLint program_vertex_in_color_ID = 0; + status = ogl_program_attribute_get_ID(program_ID, "vertex_in_color", &program_vertex_in_color_ID); + if (status < 0) { + fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); + return status; + } + + struct ogl_mat4f MVP_projection; + status = ogl_perspective(45.0f, 0.1f, 100.0f, &MVP_projection); + if (status < 0) { + fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); + return status; + } + + struct ogl_mat4f MVP_view; + struct ogl_vec3f MVP_view_eye = { .x= 4.0f, .y= 3.0f, .z=-3.0f }; + struct ogl_vec3f MVP_view_center = { .x= 0.0f, .y= 0.0f, .z= 0.0f }; + struct ogl_vec3f MVP_view_up = { .x= 0.0f, .y= 1.0f, .z= 0.0f }; + ogl_lookat( + MVP_view_eye, MVP_view_center, MVP_view_up, + &MVP_view + ); + + struct ogl_mat4f MVP_model; + ogl_mat4f_identity(&MVP_model); + + struct ogl_mat4f MVP; + ogl_mat4f_identity(&MVP); + ogl_mat4f_multiply(MVP, MVP_projection, &MVP); + ogl_mat4f_multiply(MVP, MVP_view, &MVP); + ogl_mat4f_multiply(MVP, MVP_model, &MVP); + + static const GLfloat cube_vertexbuffer_data[] = { + -1.0f,-1.0f,-1.0f, -1.0f,-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, + 1.0f, 1.0f,-1.0f, -1.0f,-1.0f,-1.0f, -1.0f, 1.0f,-1.0f, + 1.0f,-1.0f, 1.0f, -1.0f,-1.0f,-1.0f, 1.0f,-1.0f,-1.0f, + 1.0f, 1.0f,-1.0f, 1.0f,-1.0f,-1.0f, -1.0f,-1.0f,-1.0f, + -1.0f,-1.0f,-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f,-1.0f, + 1.0f,-1.0f, 1.0f, -1.0f,-1.0f, 1.0f, -1.0f,-1.0f,-1.0f, + -1.0f, 1.0f, 1.0f, -1.0f,-1.0f, 1.0f, 1.0f,-1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f,-1.0f,-1.0f, 1.0f, 1.0f,-1.0f, + 1.0f,-1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, -1.0f, 1.0f,-1.0f, + 1.0f, 1.0f, 1.0f, -1.0f, 1.0f,-1.0f, -1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,-1.0f, 1.0f, + }; + static const size_t cube_vertexbuffer_data_size = sizeof(cube_vertexbuffer_data); + static const size_t cube_vertexbuffer_data_triangles = (sizeof(cube_vertexbuffer_data) / sizeof(*cube_vertexbuffer_data)) / 3; + + GLuint cube_vertexbuffer_ID = 0; + status = ogl_vertex_buffer_load(cube_vertexbuffer_data, cube_vertexbuffer_data_size, &cube_vertexbuffer_ID); + if (status < 0) { + return status; + } + + static const GLfloat cube_colorbuffer_data[] = { + 0.583f,0.771f,0.014f, 0.609f,0.115f,0.436f, 0.327f,0.483f,0.844f, + 0.822f,0.569f,0.201f, 0.435f,0.602f,0.223f, 0.310f,0.747f,0.185f, + 0.597f,0.770f,0.761f, 0.559f,0.436f,0.730f, 0.359f,0.583f,0.152f, + 0.483f,0.596f,0.789f, 0.559f,0.861f,0.639f, 0.195f,0.548f,0.859f, + 0.014f,0.184f,0.576f, 0.771f,0.328f,0.970f, 0.406f,0.615f,0.116f, + 0.676f,0.977f,0.133f, 0.971f,0.572f,0.833f, 0.140f,0.616f,0.489f, + 0.997f,0.513f,0.064f, 0.945f,0.719f,0.592f, 0.543f,0.021f,0.978f, + 0.279f,0.317f,0.505f, 0.167f,0.620f,0.077f, 0.347f,0.857f,0.137f, + 0.055f,0.953f,0.042f, 0.714f,0.505f,0.345f, 0.783f,0.290f,0.734f, + 0.722f,0.645f,0.174f, 0.302f,0.455f,0.848f, 0.225f,0.587f,0.040f, + 0.517f,0.713f,0.338f, 0.053f,0.959f,0.120f, 0.393f,0.621f,0.362f, + 0.673f,0.211f,0.457f, 0.820f,0.883f,0.371f, 0.982f,0.099f,0.879f, + }; + static const size_t cube_colorbuffer_data_size = sizeof(cube_colorbuffer_data); + + GLuint cube_colorbuffer_ID = 0; + status = ogl_vertex_buffer_load(cube_colorbuffer_data, cube_colorbuffer_data_size, &cube_colorbuffer_ID); + if (status < 0) { + return status; + } + + do { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glUseProgram(program_ID); + ogl_program_uniform_set_mat4f(program_ID, "MVP", MVP); + + glEnableVertexAttribArray(program_vertex_in_position_ID); + glBindBuffer(GL_ARRAY_BUFFER, cube_vertexbuffer_ID); + glVertexAttribPointer(program_vertex_in_position_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0); + + glEnableVertexAttribArray(program_vertex_in_color_ID); + glBindBuffer(GL_ARRAY_BUFFER, cube_colorbuffer_ID); + glVertexAttribPointer(program_vertex_in_color_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0); + + glDrawArrays(GL_TRIANGLES, 0, cube_vertexbuffer_data_triangles); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glDisableVertexAttribArray(program_vertex_in_position_ID); + glDisableVertexAttribArray(program_vertex_in_color_ID); + + glfwSwapBuffers(window); + glfwPollEvents(); + } + while ( + (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS) + && + (glfwWindowShouldClose(window) == 0) + ); + + glDeleteBuffers(1, &cube_vertexbuffer_ID); + glDeleteBuffers(1, &cube_colorbuffer_ID); + glDeleteProgram(program_ID); + + glfwTerminate(); + + return 0; +} + diff --git a/Makefile b/Makefile @@ -8,25 +8,33 @@ OGL_BIN = ogl/ogl.a 01_OBJ = $(01_SRC:.c=.o) $(OGL_BIN) 01_BIN = 01-perspective +02_SRC = 02-cube.c +02_OBJ = $(02_SRC:.c=.o) $(OGL_BIN) +02_BIN = 02-cube + + CFLAGS ?= -std=c99 -Wall -fwrapv -g LIB_CFLAGS += $(shell pkg-config --cflags glew gl glfw3) LIB_LDFLAGS += $(shell pkg-config --libs glew gl glfw3) -lm .PHONY: all -all: $(00_BIN) $(01_BIN) +all: $(00_BIN) $(01_BIN) $(02_BIN) .PHONY: clean clean: rm -rf \ $(00_OBJ) $(00_BIN) \ - $(01_OBJ) $(01_BIN) + $(01_OBJ) $(01_BIN) \ + $(02_OBJ) $(02_BIN) $(MAKE) --directory=./ogl/ clean $(00_BIN): $(00_OBJ) $(01_BIN): $(01_OBJ) +$(02_BIN): $(02_OBJ) + $(OGL_BIN): $(MAKE) --directory=./ogl/ all diff --git a/ogl/ogl_init.c b/ogl/ogl_init.c @@ -54,7 +54,10 @@ ogl_init( glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); - glClearColor(0.0f, 0.0f, 0.4f, 0.0f); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); (*out_window) = window; _ogl_window_width = window_width; diff --git a/ogl/ogl_program_uniform_set_mat4f.c b/ogl/ogl_program_uniform_set_mat4f.c @@ -20,7 +20,14 @@ ogl_program_uniform_set_mat4f( return status; } - glUniformMatrix4fv(uniform_ID, 1, GL_FALSE, matrix.values); + const GLfloat * v = matrix.values; + const GLfloat v_columnmajor[16] = { + v[ 0], v[ 4], v[ 8], v[12], + v[ 1], v[ 5], v[ 9], v[13], + v[ 2], v[ 6], v[10], v[14], + v[ 3], v[ 7], v[11], v[15], + }; + glUniformMatrix4fv(uniform_ID, 1, GL_FALSE, v_columnmajor); return 0; }