opengl_learn

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

00-triangle.c (2366B)


      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-2-the-first-triangle/ */
      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, "00 - triangle");
     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 		"attribute vec3 va_vertex_position;" "\n"  /* attributes are inputs to vertex shaders */
     27 		"void main(){" "\n"
     28 		"	gl_Position = vec4(va_vertex_position,1);" "\n"
     29 		"}" "\n"
     30 	;
     31 	const char * program_fragment_source =
     32 		"#version 100" "\n"
     33 		"precision lowp float;" "\n"
     34 		"void main() {" "\n"
     35 		"	gl_FragColor = vec4(1,0,0,1);" "\n"
     36 		"}" "\n"
     37 	;
     38 	GLuint program_ID = ogl_program_build(program_vertex_source, program_fragment_source);
     39 	GLint program_va_vertex_position_ID = glGetAttribLocation(program_ID, "va_vertex_position");
     40 
     41 	const GLfloat triangle_vertexbuffer_data[] = { 
     42 		-1.0f, -1.0f, 0.0f,   1.0f, -1.0f, 0.0f,   0.0f,  1.0f, 0.0f,
     43 	};
     44 	const size_t triangle_vertexbuffer_data_size     = sizeof(triangle_vertexbuffer_data);
     45 	const size_t triangle_vertexbuffer_data_vertexes = (sizeof(triangle_vertexbuffer_data) / sizeof(*triangle_vertexbuffer_data)) / 3;
     46 	GLuint triangle_vertexbuffer_ID = ogl_arraybuffer_load(triangle_vertexbuffer_data, triangle_vertexbuffer_data_size);
     47 
     48 	do {
     49 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     50 
     51 		glUseProgram(program_ID);
     52 
     53 		glEnableVertexAttribArray(program_va_vertex_position_ID);
     54 		glBindBuffer(GL_ARRAY_BUFFER, triangle_vertexbuffer_ID);
     55 		glVertexAttribPointer(program_va_vertex_position_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0);
     56 		glDrawArrays(GL_TRIANGLES, 0, triangle_vertexbuffer_data_vertexes);
     57 		glBindBuffer(GL_ARRAY_BUFFER, 0);
     58 		glDisableVertexAttribArray(program_va_vertex_position_ID);
     59 
     60 		glfwSwapBuffers(window);
     61 		glfwPollEvents();
     62 	}
     63 	while (
     64 		(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS)
     65 		&&
     66 		(glfwWindowShouldClose(window) == 0)
     67 	);
     68 
     69 	glDeleteBuffers(1, &triangle_vertexbuffer_ID);
     70 	glDeleteProgram(program_ID);
     71 
     72 	glfwTerminate();
     73 
     74 	return 0;
     75 }
     76