Skip to content

Commit ce5193c

Browse files
committed
Start improving opengl 3 examples
1 parent 4ff0bf4 commit ce5193c

File tree

11 files changed

+213
-138
lines changed

11 files changed

+213
-138
lines changed

opengl/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
1. [IBO](glfw_ibo.c)
5050
1. [transform](glfw_transform.c)
5151
1. [gl_FragCoord](glfw_gl_frag_coord.c)
52-
1. [heatplot](heatplot.c)
52+
1. [heatmap](glfw_heatmap.c)
53+
1. [Texture](glfw_texture.c)
5354
1. Naughty
5455
1. [Infinite loop shader](glfw_infinite_loop_shader.c.off)
5556
1. [Memory overflow shader](glfw_memory_overflow_shader.c.off)

opengl/TODO.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@
3434
- http://stackoverflow.com/questions/5734794/mouse-movement-opengl
3535
- http://stackoverflow.com/questions/1426415/opengl-moving-camera-with-mouse
3636
- http://gamedev.stackexchange.com/questions/53333/how-to-implement-a-basic-arcball-camera-in-opengl-with-glm
37-
- VAO, VBO http://stackoverflow.com/questions/11821336/what-are-vertex-array-objects
38-
- Immediate mode vs retained mode:
39-
- https://en.wikipedia.org/wiki/Retained_mode
40-
- https://en.wikipedia.org/wiki/Immediate_mode_(computer_graphics)
41-
- immediate mode is deprecated. So like, glBegin, glEnd and glVertex which we use in every test program are deprecated. Lol.
42-
- http://gamedev.stackexchange.com/questions/21733/why-does-opengl-3-only-allow-vbos
43-
- http://stackoverflow.com/questions/6733934/what-does-immediate-mode-mean-in-opengl Hello world requested but not provided, too long, lol. Opportunity!
44-
- http://gamedev.stackexchange.com/questions/21733/why-does-opengl-3-only-allow-vbos
37+
- Immediate mode vs retained mode:
38+
- https://en.wikipedia.org/wiki/Retained_mode
39+
- https://en.wikipedia.org/wiki/Immediate_mode_(computer_graphics)
40+
- immediate mode is deprecated. So like, glBegin, glEnd and glVertex which we use in every test program are deprecated. Lol.
41+
- http://gamedev.stackexchange.com/questions/21733/why-does-opengl-3-only-allow-vbos
42+
- http://stackoverflow.com/questions/6733934/what-does-immediate-mode-mean-in-opengl Hello world requested but not provided, too long, lol. Opportunity!
43+
- http://gamedev.stackexchange.com/questions/21733/why-does-opengl-3-only-allow-vbos
4544
- https://en.wikipedia.org/wiki/Stencil_buffer
4645
- http://www.learnopengl.com/#!Advanced-OpenGL/Stencil-testing
4746
- shading, glsl

opengl/bibliography.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Demos:
6262

6363
- <https://github.com/prideout/recipes>
6464
- <https://www.youtube.com/watch?v=H-3yPXXZUC8>
65+
- <https://github.com/premsasidharan/gpu_img_proc/tree/master/test/edge_camera> <https://www.youtube.com/watch?v=fJtOdJ9MHsA> GLSL Linux camera real time edge detection shown on screen. Getting started: <https://github.com/premsasidharan/gpu_img_proc/issues/1#issuecomment-257504913>
6566

6667
Unevaluated:
6768

opengl/common.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ static void common_fps_print() {
3535
}
3636
}
3737

38-
/* Build and compile shader program, return it's ID. */
38+
/* Build and compile shader program, return its ID. */
3939
GLint common_get_shader_program(
40-
const char *vertex_shader_source,
41-
const char *fragment_shader_source) {
40+
const char *vertex_shader_source,
41+
const char *fragment_shader_source
42+
) {
4243
GLchar *log = NULL;
43-
GLint fragment_shader, log_length, program, success, vertex_shader;
44+
GLint log_length, success;
45+
GLuint fragment_shader, program, vertex_shader;
4446

4547
/* Vertex shader */
4648
vertex_shader = glCreateShader(GL_VERTEX_SHADER);

opengl/glfw_color_array.c

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,26 @@ Color interpolation on the fragment shader is automatic.
66
http://stackoverflow.com/questions/6733934/what-does-immediate-mode-mean-in-opengl
77
*/
88

9-
#include <stdio.h>
10-
#include <stdlib.h>
11-
12-
#define GLEW_STATIC
13-
#include <GL/glew.h>
14-
#include <GLFW/glfw3.h>
15-
169
#include "common.h"
1710

18-
static const GLuint WIDTH = 800;
19-
static const GLuint HEIGHT = 600;
20-
/* ourColor is passed on to the fragment shader. */
11+
static const GLuint WIDTH = 500;
12+
static const GLuint HEIGHT = 500;
13+
/* fragColor is passed on to the fragment shader. */
2114
static const GLchar *vertex_shader_source =
2215
"#version 330 core\n"
23-
"layout (location = 0) in vec3 position;\n"
24-
"layout (location = 1) in vec3 color;\n"
25-
"out vec3 ourColor;\n"
16+
"in vec3 position;\n"
17+
"in vec3 vertColor;\n"
18+
"out vec3 fragColor;\n"
2619
"void main() {\n"
2720
" gl_Position = vec4(position, 1.0f);\n"
28-
" ourColor = color;\n"
21+
" fragColor = vertColor;\n"
2922
"}\n";
3023
static const GLchar *fragment_shader_source =
3124
"#version 330 core\n"
32-
"in vec3 ourColor;\n"
25+
"in vec3 fragColor;\n"
3326
"out vec4 color;\n"
3427
"void main() {\n"
35-
" color = vec4(ourColor, 1.0f);\n"
28+
" color = vec4(fragColor, 1.0f);\n"
3629
"}\n";
3730
static GLfloat vertices[] = {
3831
/* Positions Colors */
@@ -42,43 +35,66 @@ static GLfloat vertices[] = {
4235
};
4336

4437
int main(void) {
45-
GLint shader_program;
38+
GLFWwindow *window;
39+
GLuint attribute_vertColor, attribute_position, program, vbo, vao;
4640

41+
/* Window system. */
4742
glfwInit();
48-
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, __FILE__, NULL, NULL);
43+
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
44+
window = glfwCreateWindow(WIDTH, HEIGHT, __FILE__, NULL, NULL);
4945
glfwMakeContextCurrent(window);
50-
glewExperimental = GL_TRUE;
5146
glewInit();
52-
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
53-
glViewport(0, 0, WIDTH, HEIGHT);
5447

55-
shader_program = common_get_shader_program(vertex_shader_source, fragment_shader_source);
48+
/* Shader setup. */
49+
program = common_get_shader_program(vertex_shader_source, fragment_shader_source);
50+
attribute_position = glGetAttribLocation(program, "position");
51+
attribute_vertColor = glGetAttribLocation(program, "vertColor");
5652

57-
GLuint vbo, vao;
53+
/* Buffer setup. */
5854
glGenVertexArrays(1, &vao);
5955
glGenBuffers(1, &vbo);
6056
glBindVertexArray(vao);
6157
glBindBuffer(GL_ARRAY_BUFFER, vbo);
6258
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
63-
/* Position attribute */
64-
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
65-
glEnableVertexAttribArray(0);
66-
/* Color attribute */
67-
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
68-
glEnableVertexAttribArray(1);
59+
glVertexAttribPointer(
60+
attribute_position,
61+
3,
62+
GL_FLOAT,
63+
GL_FALSE,
64+
6 * sizeof(GLfloat),
65+
(GLvoid*)0
66+
);
67+
glEnableVertexAttribArray(attribute_position);
68+
glVertexAttribPointer(
69+
attribute_vertColor,
70+
3,
71+
GL_FLOAT,
72+
GL_FALSE,
73+
6 * sizeof(GLfloat),
74+
(GLvoid*)(3 * sizeof(GLfloat))
75+
);
76+
glEnableVertexAttribArray(attribute_vertColor);
77+
glBindVertexArray(0);
78+
79+
/* Draw. */
80+
glViewport(0, 0, WIDTH, HEIGHT);
81+
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
82+
glClear(GL_COLOR_BUFFER_BIT);
83+
glUseProgram(program);
84+
glBindVertexArray(vao);
85+
glDrawArrays(GL_TRIANGLES, 0, 3);
6986
glBindVertexArray(0);
87+
glfwSwapBuffers(window);
7088

89+
/* Main loop. */
7190
while (!glfwWindowShouldClose(window)) {
7291
glfwPollEvents();
73-
glClear(GL_COLOR_BUFFER_BIT);
74-
glUseProgram(shader_program);
75-
glBindVertexArray(vao);
76-
glDrawArrays(GL_TRIANGLES, 0, 3);
77-
glBindVertexArray(0);
78-
glfwSwapBuffers(window);
7992
}
93+
94+
/* Cleanup. */
8095
glDeleteVertexArrays(1, &vao);
8196
glDeleteBuffers(1, &vbo);
97+
glDeleteProgram(program);
8298
glfwTerminate();
8399
return EXIT_SUCCESS;
84100
}

opengl/glfw_heatmap.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,43 +57,46 @@ int main(void) {
5757
pi2 = 2.0 * acos(-1.0)
5858
;
5959

60+
/* Window system. */
6061
glfwInit();
6162
window = glfwCreateWindow(WIDTH, HEIGHT, __FILE__, NULL, NULL);
6263
glfwMakeContextCurrent(window);
6364
glewInit();
6465

66+
/* Shader setup. */
6567
program = common_get_shader_program(vertex_shader_source, fragment_shader_source);
6668
attribute_coord2d = glGetAttribLocation(program, attribute_name);
67-
if (attribute_coord2d == -1) {
68-
fprintf(stderr, "error: attribute_coord2d: %s\n", attribute_name);
69-
return EXIT_FAILURE;
70-
}
7169
height_location = glGetUniformLocation(program, "height");
7270
periods_x_location = glGetUniformLocation(program, "periods_x");
7371
periods_y_location = glGetUniformLocation(program, "periods_y");
7472
pi2_location = glGetUniformLocation(program, "pi2");
7573
time_location = glGetUniformLocation(program, "time");
7674
width_location = glGetUniformLocation(program, "width");
7775

76+
/* Global settings. */
7877
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
7978
glUseProgram(program);
8079
glViewport(0, 0, WIDTH, HEIGHT);
8180

81+
/* vbo */
8282
glGenBuffers(1, &vbo);
8383
glBindBuffer(GL_ARRAY_BUFFER, vbo);
8484
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
8585

86+
/* ibo */
8687
glGenBuffers(1, &ibo);
8788
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
8889
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexes), indexes, GL_STATIC_DRAW);
8990
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &ibo_size);
9091

92+
/* Uniforms. */
9193
glUniform1f(pi2_location, pi2);
9294
glUniform1f(width_location, WIDTH);
9395
glUniform1f(height_location, HEIGHT);
9496
glUniform1f(periods_x_location, periods_x);
9597
glUniform1f(periods_y_location, periods_y);
9698

99+
/* Main loop. */
97100
common_fps_init();
98101
while (!glfwWindowShouldClose(window)) {
99102
glfwPollEvents();
@@ -116,6 +119,7 @@ int main(void) {
116119
common_fps_print();
117120
}
118121

122+
/* Cleanup. */
119123
glDeleteBuffers(1, &ibo);
120124
glDeleteBuffers(1, &vbo);
121125
glDeleteProgram(program);

0 commit comments

Comments
 (0)