opengl_learn

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

02-cube.c (5608B)


      1 /*
      2 2018 David DiPaola
      3 licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/)
      4 */
      5 
      6 /* followed this tutorial: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-4-a-colored-cube/ */
      7 
      8 #include <stdio.h>
      9 
     10 #include <stdlib.h>
     11 
     12 #include <GL/glew.h>
     13 
     14 #include <GLFW/glfw3.h>
     15 
     16 #include "ogl/ogl.h"
     17 
     18 int
     19 main() {
     20 	GLFWwindow * window = ogl_init(400, 240, 1, "02 - cube");
     21 
     22 	/* vu: vertex shader uniform,  va: vertex shader attribute,  fu: fragment shader uniform,  fv: fragment shader varying */
     23 	const char * program_vertex_source =
     24 		"#version 100" "\n"
     25 		"precision highp float;" "\n"
     26 		"uniform   mat4 vu_vertex_MVP;" "\n"       /* uniforms are inputs to vertex and fragment shaders that don't change per-vertex */
     27 		"attribute vec3 va_vertex_position;" "\n"  /* attributes are inputs to vertex shaders */
     28 		"attribute vec3 va_vertex_color;" "\n"
     29 		"varying   vec4 fv_vertex_color;" "\n"     /* varyings are outputs of vertex shaders and inputs to fragment shaders */
     30 		"void main(){" "\n"
     31 		"	gl_Position = vu_vertex_MVP * vec4(va_vertex_position,1);" "\n"
     32 		"	fv_vertex_color = vec4(va_vertex_color,1);" "\n"
     33 		"}" "\n"
     34 	;
     35 	const char * program_fragment_source =
     36 		"#version 100" "\n"
     37 		"precision lowp float;" "\n"
     38 		"varying vec4 fv_vertex_color;" "\n"
     39 		"void main() {" "\n"
     40 		"	gl_FragColor = fv_vertex_color;" "\n"
     41 		"}" "\n"
     42 	;
     43 	GLuint program_ID = ogl_program_build(program_vertex_source, program_fragment_source);
     44 	GLint program_va_vertex_position_ID = glGetAttribLocation(program_ID, "va_vertex_position");
     45 	GLint program_va_vertex_color_ID    = glGetAttribLocation(program_ID, "va_vertex_color");
     46 
     47 	struct ogl_mat4f MVP_projection;
     48 	ogl_perspective(45.0f, 0.1f, 100.0f, &MVP_projection);
     49 
     50 	struct ogl_mat4f MVP_view;
     51 	struct ogl_vec3f MVP_view_eye    = { .x= 4.0f,  .y= 3.0f,  .z= 3.0f };
     52 	struct ogl_vec3f MVP_view_center = { .x= 0.0f,  .y= 0.0f,  .z= 0.0f };
     53 	struct ogl_vec3f MVP_view_up     = { .x= 0.0f,  .y= 1.0f,  .z= 0.0f };
     54 	ogl_lookat(MVP_view_eye, MVP_view_center, MVP_view_up, &MVP_view);
     55 
     56 	struct ogl_mat4f MVP_model;
     57 	ogl_mat4f_identity(&MVP_model);
     58 
     59 	struct ogl_mat4f MVP;
     60 	ogl_mat4f_identity(&MVP);
     61 	ogl_mat4f_multiply(MVP, MVP_projection, &MVP);
     62 	ogl_mat4f_multiply(MVP, MVP_view,       &MVP);
     63 	ogl_mat4f_multiply(MVP, MVP_model,      &MVP);
     64 
     65 	static const GLfloat cube_vertexbuffer_data[] = { 
     66 		-1.0f,-1.0f,-1.0f,  -1.0f,-1.0f, 1.0f,  -1.0f, 1.0f, 1.0f,
     67 		 1.0f, 1.0f,-1.0f,  -1.0f,-1.0f,-1.0f,  -1.0f, 1.0f,-1.0f,
     68 		 1.0f,-1.0f, 1.0f,  -1.0f,-1.0f,-1.0f,   1.0f,-1.0f,-1.0f,
     69 		 1.0f, 1.0f,-1.0f,   1.0f,-1.0f,-1.0f,  -1.0f,-1.0f,-1.0f,
     70 		-1.0f,-1.0f,-1.0f,  -1.0f, 1.0f, 1.0f,  -1.0f, 1.0f,-1.0f,
     71 		 1.0f,-1.0f, 1.0f,  -1.0f,-1.0f, 1.0f,  -1.0f,-1.0f,-1.0f,
     72 		-1.0f, 1.0f, 1.0f,  -1.0f,-1.0f, 1.0f,   1.0f,-1.0f, 1.0f,
     73 		 1.0f, 1.0f, 1.0f,   1.0f,-1.0f,-1.0f,   1.0f, 1.0f,-1.0f,
     74 		 1.0f,-1.0f,-1.0f,   1.0f, 1.0f, 1.0f,   1.0f,-1.0f, 1.0f,
     75 		 1.0f, 1.0f, 1.0f,   1.0f, 1.0f,-1.0f,  -1.0f, 1.0f,-1.0f,
     76 		 1.0f, 1.0f, 1.0f,  -1.0f, 1.0f,-1.0f,  -1.0f, 1.0f, 1.0f,
     77 		 1.0f, 1.0f, 1.0f,  -1.0f, 1.0f, 1.0f,   1.0f,-1.0f, 1.0f,
     78 	};
     79 	static const size_t cube_vertexbuffer_data_size     = sizeof(cube_vertexbuffer_data);
     80 	static const size_t cube_vertexbuffer_data_vertexes = (sizeof(cube_vertexbuffer_data) / sizeof(*cube_vertexbuffer_data)) / 3;
     81 	GLuint cube_vertexbuffer_ID = ogl_arraybuffer_load(cube_vertexbuffer_data, cube_vertexbuffer_data_size);
     82 
     83 	static const GLfloat cube_colorbuffer_data[] = {
     84 		0.583f,0.771f,0.014f,  0.609f,0.115f,0.436f,  0.327f,0.483f,0.844f,
     85 		0.822f,0.569f,0.201f,  0.435f,0.602f,0.223f,  0.310f,0.747f,0.185f,
     86 		0.597f,0.770f,0.761f,  0.559f,0.436f,0.730f,  0.359f,0.583f,0.152f,
     87 		0.483f,0.596f,0.789f,  0.559f,0.861f,0.639f,  0.195f,0.548f,0.859f,
     88 		0.014f,0.184f,0.576f,  0.771f,0.328f,0.970f,  0.406f,0.615f,0.116f,
     89 		0.676f,0.977f,0.133f,  0.971f,0.572f,0.833f,  0.140f,0.616f,0.489f,
     90 		0.997f,0.513f,0.064f,  0.945f,0.719f,0.592f,  0.543f,0.021f,0.978f,
     91 		0.279f,0.317f,0.505f,  0.167f,0.620f,0.077f,  0.347f,0.857f,0.137f,
     92 		0.055f,0.953f,0.042f,  0.714f,0.505f,0.345f,  0.783f,0.290f,0.734f,
     93 		0.722f,0.645f,0.174f,  0.302f,0.455f,0.848f,  0.225f,0.587f,0.040f,
     94 		0.517f,0.713f,0.338f,  0.053f,0.959f,0.120f,  0.393f,0.621f,0.362f,
     95 		0.673f,0.211f,0.457f,  0.820f,0.883f,0.371f,  0.982f,0.099f,0.879f,
     96 	};
     97 	static const size_t cube_colorbuffer_data_size = sizeof(cube_colorbuffer_data);
     98 	GLuint cube_colorbuffer_ID = ogl_arraybuffer_load(cube_colorbuffer_data, cube_colorbuffer_data_size);
     99 
    100 	do {
    101 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    102 
    103 		glUseProgram(program_ID);
    104 		ogl_program_uniform_set_mat4f(program_ID, "vu_vertex_MVP", MVP);
    105 
    106 		glEnableVertexAttribArray(program_va_vertex_position_ID);
    107 		glBindBuffer(GL_ARRAY_BUFFER, cube_vertexbuffer_ID);
    108 		glVertexAttribPointer(program_va_vertex_position_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0);
    109 
    110 		glEnableVertexAttribArray(program_va_vertex_color_ID);
    111 		glBindBuffer(GL_ARRAY_BUFFER, cube_colorbuffer_ID);
    112 		glVertexAttribPointer(program_va_vertex_color_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0);
    113 
    114 		glDrawArrays(GL_TRIANGLES, 0, cube_vertexbuffer_data_vertexes);
    115 
    116 		glBindBuffer(GL_ARRAY_BUFFER, 0);
    117 		glDisableVertexAttribArray(program_va_vertex_position_ID);
    118 		glDisableVertexAttribArray(program_va_vertex_color_ID);
    119 
    120 		glfwSwapBuffers(window);
    121 		glfwPollEvents();
    122 	}
    123 	while (
    124 		(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS)
    125 		&&
    126 		(glfwWindowShouldClose(window) == 0)
    127 	);
    128 
    129 	glDeleteBuffers(1, &cube_vertexbuffer_ID);
    130 	glDeleteBuffers(1, &cube_colorbuffer_ID);
    131 	glDeleteProgram(program_ID);
    132 
    133 	glfwTerminate();
    134 
    135 	return 0;
    136 }
    137