opengl_learn

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

01-perspective.c (3249B)


      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-3-matrices/ */
      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, "01 - triangle with perspective");
     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 		"void main(){" "\n"
     29 		"	gl_Position = vu_vertex_MVP * vec4(va_vertex_position,1);" "\n"
     30 		"}" "\n"
     31 	;
     32 	const char * program_fragment_source =
     33 		"#version 100" "\n"
     34 		"precision lowp float;" "\n"
     35 		"void main() {" "\n"
     36 		"	gl_FragColor = vec4(1,0,0,1);" "\n"
     37 		"}" "\n"
     38 	;
     39 	GLuint program_ID = ogl_program_build(program_vertex_source, program_fragment_source);
     40 	GLint program_va_vertex_position_ID = glGetAttribLocation(program_ID, "va_vertex_position");
     41 
     42 	struct ogl_mat4f MVP_projection;
     43 	ogl_perspective(45.0f, 0.1f, 100.0f, &MVP_projection);
     44 
     45 	struct ogl_mat4f MVP_view;
     46 	struct ogl_vec3f MVP_view_eye    = { .x= 4.0f,  .y= 3.0f,  .z= 3.0f };
     47 	struct ogl_vec3f MVP_view_center = { .x= 0.0f,  .y= 0.0f,  .z= 0.0f };
     48 	struct ogl_vec3f MVP_view_up     = { .x= 0.0f,  .y= 1.0f,  .z= 0.0f };
     49 	ogl_lookat(MVP_view_eye, MVP_view_center, MVP_view_up, &MVP_view);
     50 
     51 	struct ogl_mat4f MVP_model;
     52 	ogl_mat4f_identity(&MVP_model);
     53 
     54 	struct ogl_mat4f MVP;
     55 	ogl_mat4f_identity(&MVP);
     56 	ogl_mat4f_multiply(MVP, MVP_projection, &MVP);
     57 	ogl_mat4f_multiply(MVP, MVP_view,       &MVP);
     58 	ogl_mat4f_multiply(MVP, MVP_model,      &MVP);
     59 	
     60 	const GLfloat triangle_vertexbuffer_data[] = {
     61 		-1.0f, -1.0f, 0.0f,   1.0f, -1.0f, 0.0f,   0.0f,  1.0f, 0.0f,
     62 	};
     63 	const size_t triangle_vertexbuffer_data_size     = sizeof(triangle_vertexbuffer_data);
     64 	const size_t triangle_vertexbuffer_data_vertexes = (sizeof(triangle_vertexbuffer_data) / sizeof(*triangle_vertexbuffer_data)) / 3;
     65 	GLuint triangle_vertexbuffer_ID = ogl_arraybuffer_load(triangle_vertexbuffer_data, triangle_vertexbuffer_data_size);
     66 
     67 	do {
     68 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     69 
     70 		glUseProgram(program_ID);
     71 		ogl_program_uniform_set_mat4f(program_ID, "vu_vertex_MVP", MVP);
     72 
     73 		glEnableVertexAttribArray(program_va_vertex_position_ID);
     74 		glBindBuffer(GL_ARRAY_BUFFER, triangle_vertexbuffer_ID);
     75 		glVertexAttribPointer(program_va_vertex_position_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0);
     76 		glDrawArrays(GL_TRIANGLES, 0, triangle_vertexbuffer_data_vertexes);
     77 		glBindBuffer(GL_ARRAY_BUFFER, 0);
     78 		glDisableVertexAttribArray(program_va_vertex_position_ID);
     79 
     80 		glfwSwapBuffers(window);
     81 		glfwPollEvents();
     82 	}
     83 	while (
     84 		(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS)
     85 		&&
     86 		(glfwWindowShouldClose(window) == 0)
     87 	);
     88 
     89 	glDeleteBuffers(1, &triangle_vertexbuffer_ID);
     90 	glDeleteProgram(program_ID);
     91 
     92 	glfwTerminate();
     93 
     94 	return 0;
     95 }
     96