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
+ */
8
10
9
11
#include "common.h"
10
12
@@ -34,9 +36,11 @@ static GLfloat vertices[] = {
34
36
0.0f , 0.5f , 0.0f , 0.0f , 0.0f , 1.0f
35
37
};
36
38
37
- int main (void ) {
39
+ int main (int argc , char * * argv ) {
40
+ int immediate = (argc > 1 ) && argv [1 ][0 ] == '1' ;
38
41
GLFWwindow * window ;
39
- GLint attribute_vertColor , attribute_position ;
42
+
43
+ /* Only needed for shader version. */
40
44
GLuint program , vbo , vao ;
41
45
42
46
/* Window system. */
@@ -46,49 +50,73 @@ int main(void) {
46
50
glfwMakeContextCurrent (window );
47
51
glewInit ();
48
52
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 ;
53
75
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" );
59
80
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 );
83
86
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
+ }
92
120
glfwSwapBuffers (window );
93
121
94
122
/* Main loop. */
@@ -97,9 +125,11 @@ int main(void) {
97
125
}
98
126
99
127
/* 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
+ }
103
133
glfwTerminate ();
104
134
return EXIT_SUCCESS ;
105
135
}
0 commit comments