commit ddfb5806153cd123889e2900006d17eab3e4f870
parent dccf2b0738e933aa67110634812c9803c20b8010
Author: David DiPaola <DavidDiPaola@users.noreply.github.com>
Date: Sat, 9 Jun 2018 20:55:32 -0400
00,01,02: made code more consistent so it can be diffed for easier learnings
Diffstat:
M | 00-triangle.c | | | 41 | ++++++++++++++++++----------------------- |
M | 01-perspective.c | | | 61 | ++++++++++++++++++++++++++----------------------------------- |
M | 02-cube.c | | | 62 | ++++++++++++++++++++++++++++---------------------------------- |
3 files changed, 72 insertions(+), 92 deletions(-)
diff --git a/00-triangle.c b/00-triangle.c
@@ -26,40 +26,42 @@ main() {
return status;
}
- const char * vertexshader =
+ const char * program_vertex_source =
"#version 100" "\n"
- "attribute vec4 position;" "\n"
+ "precision highp float;" "\n"
+ "attribute vec3 attribute_position;" "\n" /* attributes are inputs to vertex shaders */
"void main(){" "\n"
- " gl_Position = position;" "\n"
+ " gl_Position = vec4(attribute_position,1);" "\n"
"}" "\n"
;
- const char * fragmentshader =
+ const char * program_fragment_source =
"#version 100" "\n"
+ "precision lowp float;" "\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);
+ status = ogl_program_build(program_vertex_source, program_fragment_source, &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);
+ status = ogl_program_attribute_get_ID(program_ID, "attribute_position", &program_attribute_position_ID);
if (status < 0) {
fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__);
return status;
}
- static const GLfloat triangle_vertex_data[] = {
- -1.0f, -1.0f, 0.0f,
- 1.0f, -1.0f, 0.0f,
- 0.0f, 1.0f, 0.0f,
+ const GLfloat triangle_vertexbuffer_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);
+ const size_t triangle_vertexbuffer_data_size = sizeof(triangle_vertexbuffer_data);
+ const size_t triangle_vertexbuffer_data_vertexes = (sizeof(triangle_vertexbuffer_data) / sizeof(*triangle_vertexbuffer_data)) / 3;
+ GLuint triangle_vertexbuffer_ID = 0;
+ status = ogl_vertex_buffer_load(triangle_vertexbuffer_data, triangle_vertexbuffer_data_size, &triangle_vertexbuffer_ID);
if (status < 0) {
return status;
}
@@ -70,16 +72,9 @@ main() {
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, triangle_vertexbuffer_ID);
+ glVertexAttribPointer(program_attribute_position_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0);
+ glDrawArrays(GL_TRIANGLES, 0, triangle_vertexbuffer_data_vertexes);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(program_attribute_position_ID);
@@ -92,7 +87,7 @@ main() {
(glfwWindowShouldClose(window) == 0)
);
- glDeleteBuffers(1, &triangle_vertex_buffer_ID);
+ glDeleteBuffers(1, &triangle_vertexbuffer_ID);
glDeleteProgram(program_ID);
glfwTerminate();
diff --git a/01-perspective.c b/01-perspective.c
@@ -3,6 +3,8 @@
licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/)
*/
+/* followed this tutorial: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ */
+
#include <stdio.h>
#include <stdlib.h>
@@ -24,32 +26,31 @@ main() {
return status;
}
- const char * vertexshader =
+ const char * program_vertex_source =
"#version 100" "\n"
- "uniform mat4 MVP;" "\n"
- "attribute vec4 position;" "\n"
+ "precision highp float;" "\n"
+ "uniform mat4 uniform_MVP;" "\n" /* uniforms are inputs to vertex shaders that don't change per-vertex */
+ "attribute vec3 attribute_position;" "\n" /* attributes are inputs to vertex shaders */
"void main(){" "\n"
- " gl_Position = MVP * position;" "\n"
+ " gl_Position = uniform_MVP * vec4(attribute_position,1);" "\n"
"}" "\n"
;
- const char * fragmentshader =
+ const char * program_fragment_source =
"#version 100" "\n"
+ "precision lowp float;" "\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
- );
+ status = ogl_program_build(program_vertex_source, program_fragment_source, &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);
+ status = ogl_program_attribute_get_ID(program_ID, "attribute_position", &program_attribute_position_ID);
if (status < 0) {
fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__);
return status;
@@ -63,13 +64,10 @@ main() {
}
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_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);
@@ -80,13 +78,13 @@ main() {
ogl_mat4f_multiply(MVP, MVP_view, &MVP);
ogl_mat4f_multiply(MVP, MVP_model, &MVP);
- static const GLfloat triangle_vertex_data[] = {
- -1.0f, -1.0f, 0.0f,
- 1.0f, -1.0f, 0.0f,
- 0.0f, 1.0f, 0.0f,
+ const GLfloat triangle_vertexbuffer_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);
+ const size_t triangle_vertexbuffer_data_size = sizeof(triangle_vertexbuffer_data);
+ const size_t triangle_vertexbuffer_data_vertexes = (sizeof(triangle_vertexbuffer_data) / sizeof(*triangle_vertexbuffer_data)) / 3;
+ GLuint triangle_vertexbuffer_ID = 0;
+ status = ogl_vertex_buffer_load(triangle_vertexbuffer_data, triangle_vertexbuffer_data_size, &triangle_vertexbuffer_ID);
if (status < 0) {
return status;
}
@@ -95,19 +93,12 @@ main() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program_ID);
- ogl_program_uniform_set_mat4f(program_ID, "MVP", MVP);
+ ogl_program_uniform_set_mat4f(program_ID, "uniform_MVP", MVP);
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, triangle_vertexbuffer_ID);
+ glVertexAttribPointer(program_attribute_position_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0);
+ glDrawArrays(GL_TRIANGLES, 0, triangle_vertexbuffer_data_vertexes);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(program_attribute_position_ID);
@@ -120,7 +111,7 @@ main() {
(glfwWindowShouldClose(window) == 0)
);
- glDeleteBuffers(1, &triangle_vertex_buffer_ID);
+ glDeleteBuffers(1, &triangle_vertexbuffer_ID);
glDeleteProgram(program_ID);
glfwTerminate();
diff --git a/02-cube.c b/02-cube.c
@@ -3,6 +3,8 @@
licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/)
*/
+/* followed this tutorial: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-4-a-colored-cube/ */
+
#include <stdio.h>
#include <stdlib.h>
@@ -24,45 +26,42 @@ main() {
return status;
}
- const char * vertexshader =
+ const char * program_vertex_source =
"#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"
+ "uniform mat4 uniform_MVP;" "\n" /* uniforms are inputs to vertex shaders that don't change per-vertex */
+ "attribute vec3 attribute_position;" "\n" /* attributes are inputs to vertex shaders */
+ "attribute vec3 attribute_color;" "\n"
+ "varying vec4 varying_color;" "\n" /* varyings are outputs of vertex shaders and inputs to fragment shaders */
"void main(){" "\n"
- " gl_Position = MVP * vec4(vertex_in_position, 1);" "\n"
- " vertex_out_fragment_in_color = vec4(vertex_in_color, 1);" "\n"
+ " gl_Position = uniform_MVP * vec4(attribute_position,1);" "\n"
+ " varying_color = vec4(attribute_color,1);" "\n"
"}" "\n"
;
- const char * fragmentshader =
+ const char * program_fragment_source =
"#version 100" "\n"
"precision lowp float;" "\n"
- "varying vec4 vertex_out_fragment_in_color;" "\n"
+ "varying vec4 varying_color;" "\n"
"void main() {" "\n"
- " gl_FragColor = vertex_out_fragment_in_color;" "\n"
+ " gl_FragColor = varying_color;" "\n"
"}" "\n"
;
GLuint program_ID = 0;
- status = ogl_program_build(
- vertexshader, fragmentshader,
- &program_ID
- );
+ status = ogl_program_build(program_vertex_source, program_fragment_source, &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);
+ GLint program_attribute_position_ID = 0;
+ status = ogl_program_attribute_get_ID(program_ID, "attribute_position", &program_attribute_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);
+ GLint program_attribute_color_ID = 0;
+ status = ogl_program_attribute_get_ID(program_ID, "attribute_color", &program_attribute_color_ID);
if (status < 0) {
fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__);
return status;
@@ -79,10 +78,7 @@ main() {
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
- );
+ ogl_lookat(MVP_view_eye, MVP_view_center, MVP_view_up, &MVP_view);
struct ogl_mat4f MVP_model;
ogl_mat4f_identity(&MVP_model);
@@ -107,9 +103,8 @@ main() {
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;
-
+ static const size_t cube_vertexbuffer_data_size = sizeof(cube_vertexbuffer_data);
+ static const size_t cube_vertexbuffer_data_vertexes = (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) {
@@ -131,7 +126,6 @@ main() {
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) {
@@ -142,21 +136,21 @@ main() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program_ID);
- ogl_program_uniform_set_mat4f(program_ID, "MVP", MVP);
+ ogl_program_uniform_set_mat4f(program_ID, "uniform_MVP", MVP);
- glEnableVertexAttribArray(program_vertex_in_position_ID);
+ glEnableVertexAttribArray(program_attribute_position_ID);
glBindBuffer(GL_ARRAY_BUFFER, cube_vertexbuffer_ID);
- glVertexAttribPointer(program_vertex_in_position_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0);
+ glVertexAttribPointer(program_attribute_position_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0);
- glEnableVertexAttribArray(program_vertex_in_color_ID);
+ glEnableVertexAttribArray(program_attribute_color_ID);
glBindBuffer(GL_ARRAY_BUFFER, cube_colorbuffer_ID);
- glVertexAttribPointer(program_vertex_in_color_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0);
+ glVertexAttribPointer(program_attribute_color_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0);
- glDrawArrays(GL_TRIANGLES, 0, cube_vertexbuffer_data_triangles);
+ glDrawArrays(GL_TRIANGLES, 0, cube_vertexbuffer_data_vertexes);
glBindBuffer(GL_ARRAY_BUFFER, 0);
- glDisableVertexAttribArray(program_vertex_in_position_ID);
- glDisableVertexAttribArray(program_vertex_in_color_ID);
+ glDisableVertexAttribArray(program_attribute_position_ID);
+ glDisableVertexAttribArray(program_attribute_color_ID);
glfwSwapBuffers(window);
glfwPollEvents();