opengl_learn

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

commit 646a0fdcbe6bd6838f0d4a0639f32ba888ae1ac6
parent 41be94b6da3ad67a73189d9e63b8766d3bd1516c
Author: David DiPaola <DavidDiPaola@users.noreply.github.com>
Date:   Sun,  3 Jun 2018 12:33:40 -0400

remove junk file

Diffstat:
D01-perspective-orig.c | 236-------------------------------------------------------------------------------
1 file changed, 0 insertions(+), 236 deletions(-)

diff --git a/01-perspective-orig.c b/01-perspective-orig.c @@ -1,236 +0,0 @@ -/* -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 -ogl_program_uniform_set_m4f( - GLint program_ID, const char * uniform_name, const float * matrix4by4 -) { - GLint uniform_ID = 0; - int status = ogl_program_uniform_get_ID(program_ID, uniform_name, &uniform_ID); - if (status < 0) { - fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); - return status; - } - - glUniformMatrix4fv(uniform_ID, 1, GL_FALSE, matrix4by4); - - return 0; -} - -struct ogl_vec3f { - GLfloat x; - GLfloat y; - GLfloat z; -}; - -int -ogl_model_set( - GLint program_ID, const char * uniform_name -) { - GLfloat model[4*4] = { - 1.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f, - }; - int status = ogl_program_uniform_set_m4f(program_ID, uniform_name, model); - if (status < 0) { - fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); - return status; - } - - return 0; -} - -int -ogl_view_set( - GLint program_ID, const char * uniform_name, struct ogl_vec3f location, struct ogl_vec3f direction -) { - GLfloat view[4*4] = /*{ - location.x, location.y, location.z, 0.0f, - direction.x, direction.y, direction.z, 0.0f, - 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f, - */ - { - 0.600000, 0.000000, -0.800000, -0.000000, - -0.411597, 0.857493, -0.308697, -0.000000, - 0.685994, 0.514496, 0.514496, -5.830953, - 0.000000, 0.000000, 0.000000, 1.000000, - }; - int status = ogl_program_uniform_set_m4f(program_ID, uniform_name, view); - if (status < 0) { - fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); - return status; - } - - return 0; -} - -int -ogl_projection_set( - GLint program_ID, const char * uniform_name, GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat nearVal, GLfloat farVal -) { - GLfloat projection[4*4] = /*{ - (2*nearVal)/(right-left), 0.0f, (right+left)/(right-left), 0.0f, - 0.0f, (2*nearVal)/(top-bottom), (top+bottom)/(top-bottom), 0.0f, - 0.0f, 0.0f, (farVal+nearVal)/(farVal-nearVal), (2*farVal*nearVal)/(farVal-nearVal), - 0.0f, 0.0f, -1.0f, 0.0f, - */ - { - 1.344443, 0.000000, 0.000000, 0.000000, - 0.000000, 1.792591, 0.000000, 0.000000, - 0.000000, 0.000000, -1.002002, -0.200200, - 0.000000, 0.000000, -1.000000, 0.000000, - }; - int status = ogl_program_uniform_set_m4f(program_ID, uniform_name, projection); - if (status < 0) { - fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); - return status; - } - - return 0; -} - - -int -main() { - int status; - - GLFWwindow * window = NULL; - status = ogl_init(400, 240, "01 - triangle with perspective", &window); - if (status < 0) { - fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); - return status; - } - - const char * vertexshader = - "#version 100" "\n" -#if 0 - "uniform mat4 model;" "\n" - "uniform mat4 view;" "\n" - "uniform mat4 projection;" "\n" -#endif - "uniform mat4 MVP;" "\n" - "attribute vec4 position;" "\n" - "void main(){" "\n" - //" gl_Position = projection * view * model * position;" "\n" - " gl_Position = MVP * position;" "\n" - //" gl_Position = position;" "\n" - "}" "\n" - ; - const char * fragmentshader = - "#version 100" "\n" - "void main() {" "\n" - " gl_FragColor = vec4(1,0,0,1);" "\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_attribute_position_ID = 0; - status = ogl_program_attribute_get_ID(program_ID, "position", &program_attribute_position_ID); - if (status < 0) { - fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); - return status; - } - -#if 0 - status = ogl_model_set(program_ID, "model"); - if (status < 0) { - fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); - return status; - } - - struct ogl_vec3f view_location = { .x=4.0f, .y=3.0f, .z=3.0f }; - struct ogl_vec3f view_direction = { .x=0.0f, .y=0.0f, .z=0.0f }; - status = ogl_view_set(program_ID, "view", view_location, view_direction); - if (status < 0) { - fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); - return status; - } - - status = ogl_projection_set(program_ID, "projection", - -100.0f, 100.0f, /* left, right */ - -100.0f, 100.0f, /* bottom, top */ - 0.5f, 5.0f /* nearVal, farVal */ - ); - if (status < 0) { - fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); - return status; - } -#endif -#if 1 - GLfloat MVP[4*4] = { - 0.806666, 0.000000, -1.075554, 0.000000, - -0.737824, 1.537134, -0.553368, 0.000000, - -0.687368, -0.515526, -0.515526, 5.642426, - -0.685994, -0.514496, -0.514496, 5.830953, - }; - ogl_program_uniform_set_m4f(program_ID, "MVP", MVP); -#endif - - static const GLfloat triangle_vertex_data[] = { - -1.0f, -1.0f, 0.0f, - 1.0f, -1.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - }; - GLuint triangle_vertex_buffer_ID = 0; - status = ogl_vertex_buffer_load(triangle_vertex_data, sizeof(triangle_vertex_data), &triangle_vertex_buffer_ID); - if (status < 0) { - return status; - } - - do { - glClear(GL_COLOR_BUFFER_BIT); - - glUseProgram(program_ID); - - glEnableVertexAttribArray(program_attribute_position_ID); - glBindBuffer(GL_ARRAY_BUFFER, triangle_vertex_buffer_ID); - glVertexAttribPointer( - program_attribute_position_ID, // The attribute we want to configure - 3, // size - GL_FLOAT, // type - GL_FALSE, // is normalized? - 0, // stride - (GLvoid *)0 // array buffer offset - ); - glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle - glBindBuffer(GL_ARRAY_BUFFER, 0); - glDisableVertexAttribArray(triangle_vertex_buffer_ID); - - glfwSwapBuffers(window); - glfwPollEvents(); - } - while ( - (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS) - && - (glfwWindowShouldClose(window) == 0) - ); - - glDeleteBuffers(1, &triangle_vertex_buffer_ID); - glDeleteProgram(program_ID); - - glfwTerminate(); - - return 0; -} -