Skip to content

Commit b0be4ce

Browse files
committed
opengl glfw_triangle_shader.c
1 parent 70cc171 commit b0be4ce

File tree

3 files changed

+84
-53
lines changed

3 files changed

+84
-53
lines changed

cpp/class.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ int main() {
14541454
14551455
Great explanation: <http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom>
14561456
1457-
Why swap should be afriend: <http://stackoverflow.com/questions/5695548/public-friend-swap-member-function>
1457+
Why swap should be a friend: <http://stackoverflow.com/questions/5695548/public-friend-swap-member-function>
14581458
*/
14591459
{
14601460
CopyAndSwap c0(2, 2);

opengl/glfw_color_array.c

Lines changed: 82 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
/*
2-
One color per vertex, taken from the same array as the vertices.
3-
4-
Color interpolation on the fragment shader is automatic.
5-
6-
http://stackoverflow.com/questions/6733934/what-does-immediate-mode-mean-in-opengl
7-
*/
1+
/* One color per vertex, taken from the same array as the vertices.
2+
*
3+
* Color interpolation on the fragment shader is automatic.
4+
*
5+
* Adapted from: https://github.com/JoeyDeVries/LearnOpenGL/blob/d5c3be70ab2b884cf2b2c94cbf73a31f632fbf47/src/1.getting_started/3.shaders/shaders-using-object.cpp
6+
*
7+
* - https://stackoverflow.com/questions/17789575/what-are-shaders-in-opengl-and-what-do-we-need-them-for/36211337#36211337
8+
* - https://stackoverflow.com/questions/6733934/what-does-immediate-mode-mean-in-opengl/36166310#36166310
9+
*/
810

911
#include "common.h"
1012

@@ -34,9 +36,11 @@ static GLfloat vertices[] = {
3436
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
3537
};
3638

37-
int main(void) {
39+
int main(int argc, char **argv) {
40+
int immediate = (argc > 1) && argv[1][0] == '1';
3841
GLFWwindow *window;
39-
GLint attribute_vertColor, attribute_position;
42+
43+
/* Only needed for shader version. */
4044
GLuint program, vbo, vao;
4145

4246
/* Window system. */
@@ -46,49 +50,73 @@ int main(void) {
4650
glfwMakeContextCurrent(window);
4751
glewInit();
4852

49-
/* Shader setup. */
50-
program = common_get_shader_program(vertex_shader_source, fragment_shader_source);
51-
attribute_position = glGetAttribLocation(program, "position");
52-
attribute_vertColor = glGetAttribLocation(program, "vertColor");
53+
if (immediate) {
54+
puts("immediate");
55+
float ratio;
56+
int width, height;
57+
glfwGetFramebufferSize(window, &width, &height);
58+
ratio = width / (float) height;
59+
glClear(GL_COLOR_BUFFER_BIT);
60+
glMatrixMode(GL_PROJECTION);
61+
glLoadIdentity();
62+
glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
63+
glMatrixMode(GL_MODELVIEW);
64+
glLoadIdentity();
65+
glBegin(GL_TRIANGLES);
66+
glColor3f( 1.0f, 0.0f, 0.0f);
67+
glVertex3f(-0.5f, -0.5f, 0.0f);
68+
glColor3f( 0.0f, 1.0f, 0.0f);
69+
glVertex3f( 0.5f, -0.5f, 0.0f);
70+
glColor3f( 0.0f, 0.0f, 1.0f);
71+
glVertex3f( 0.0f, 0.5f, 0.0f);
72+
glEnd();
73+
} else {
74+
GLint attribute_vertColor, attribute_position;
5375

54-
/* vbo */
55-
glGenBuffers(1, &vbo);
56-
glBindBuffer(GL_ARRAY_BUFFER, vbo);
57-
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
58-
glBindBuffer(GL_ARRAY_BUFFER, 0);
76+
/* Shader setup. */
77+
program = common_get_shader_program(vertex_shader_source, fragment_shader_source);
78+
attribute_position = glGetAttribLocation(program, "position");
79+
attribute_vertColor = glGetAttribLocation(program, "vertColor");
5980

60-
/* Buffer setup. */
61-
glGenVertexArrays(1, &vao);
62-
glBindVertexArray(vao);
63-
glBindBuffer(GL_ARRAY_BUFFER, vbo);
64-
glVertexAttribPointer(
65-
attribute_position,
66-
3,
67-
GL_FLOAT,
68-
GL_FALSE,
69-
6 * sizeof(GLfloat),
70-
(GLvoid*)0
71-
);
72-
glEnableVertexAttribArray(attribute_position);
73-
glVertexAttribPointer(
74-
attribute_vertColor,
75-
3,
76-
GL_FLOAT,
77-
GL_FALSE,
78-
6 * sizeof(GLfloat),
79-
(GLvoid*)(3 * sizeof(GLfloat))
80-
);
81-
glEnableVertexAttribArray(attribute_vertColor);
82-
glBindVertexArray(0);
81+
/* vbo */
82+
glGenBuffers(1, &vbo);
83+
glBindBuffer(GL_ARRAY_BUFFER, vbo);
84+
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
85+
glBindBuffer(GL_ARRAY_BUFFER, 0);
8386

84-
/* Draw. */
85-
glViewport(0, 0, WIDTH, HEIGHT);
86-
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
87-
glClear(GL_COLOR_BUFFER_BIT);
88-
glUseProgram(program);
89-
glBindVertexArray(vao);
90-
glDrawArrays(GL_TRIANGLES, 0, 3);
91-
glBindVertexArray(0);
87+
/* Buffer setup. */
88+
glGenVertexArrays(1, &vao);
89+
glBindVertexArray(vao);
90+
glBindBuffer(GL_ARRAY_BUFFER, vbo);
91+
glVertexAttribPointer(
92+
attribute_position,
93+
3,
94+
GL_FLOAT,
95+
GL_FALSE,
96+
6 * sizeof(GLfloat),
97+
(GLvoid*)0
98+
);
99+
glEnableVertexAttribArray(attribute_position);
100+
glVertexAttribPointer(
101+
attribute_vertColor,
102+
3,
103+
GL_FLOAT,
104+
GL_FALSE,
105+
6 * sizeof(GLfloat),
106+
(GLvoid*)(3 * sizeof(GLfloat))
107+
);
108+
glEnableVertexAttribArray(attribute_vertColor);
109+
glBindVertexArray(0);
110+
111+
/* Draw. */
112+
glViewport(0, 0, WIDTH, HEIGHT);
113+
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
114+
glClear(GL_COLOR_BUFFER_BIT);
115+
glUseProgram(program);
116+
glBindVertexArray(vao);
117+
glDrawArrays(GL_TRIANGLES, 0, 3);
118+
glBindVertexArray(0);
119+
}
92120
glfwSwapBuffers(window);
93121

94122
/* Main loop. */
@@ -97,9 +125,11 @@ int main(void) {
97125
}
98126

99127
/* Cleanup. */
100-
glDeleteVertexArrays(1, &vao);
101-
glDeleteBuffers(1, &vbo);
102-
glDeleteProgram(program);
128+
if (!immediate) {
129+
glDeleteVertexArrays(1, &vao);
130+
glDeleteBuffers(1, &vbo);
131+
glDeleteProgram(program);
132+
}
103133
glfwTerminate();
104134
return EXIT_SUCCESS;
105135
}

shared_library/basic/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ Basic example. Creates a shared and a static library from `a.c` and `b.c`.
44

55
Used in the following answers:
66

7+
- https://stackoverflow.com/questions/3961446/why-does-gcc-not-implicitly-supply-the-fpic-flag-when-compiling-static-librarie/65005457#65005457
78
- https://unix.stackexchange.com/questions/89211/how-to-test-whether-a-linux-binary-was-compiled-as-position-independent-code/435038#435038
89
- https://unix.stackexchange.com/questions/226524/what-system-call-is-used-to-load-libraries-in-linux/462710#462710

0 commit comments

Comments
 (0)