ad89138d14f3748e90a52fd8919c3a4372b9794f
[gltest.git] / glutil_gl2.cpp
1 /* gltest - small OpenGL tearing test program
2  * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include "glutil.h"
23
24 #if defined(CON_GL1)
25 #error "GL1 contexts do not support GL2 functionality"
26 #endif
27
28 // shaders
29 static const char *vertex_shader_source =
30 "#version 100 \n\
31 attribute vec2 position; \n\
32 attribute vec3 color; \n\
33 varying vec3 frag_color; \n\
34 void main() \n\
35 { \n\
36         frag_color = color; \n\
37         gl_Position = vec4(position * vec2(2) - vec2(1), 0.0, 1.0); \n\
38 }";
39 static const char *fragment_shader_source =
40 "#version 100 \n\
41 varying vec3 frag_color; \n\
42 void main(void) \n\
43 { \n\
44         gl_FragColor = vec4(frag_color, 1.0); \n\
45 }";
46
47
48 // OpenGL IDs (yeah, this is a bad hack... do we care?)
49 static GLuint program;
50 static struct {
51         GLint position;
52         GLint color;
53 } attributes;
54
55 // some local hlper functions
56 static void show_info_log(GLuint object)
57 {
58         GLint log_length;
59         glGetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length);
60         char *log = new char[log_length];
61         glGetShaderInfoLog(object, log_length, NULL, log);
62         fprintf(stderr, "%s", log);
63         delete[] log;
64 }
65
66 static GLuint compile_shader(GLenum shader_type, const char *source)
67 {
68         GLuint shader = glCreateShader(shader_type);
69         glShaderSource(shader, 1, &source, NULL);
70         glCompileShader(shader);
71         GLint shader_ok;
72         glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);
73         if (!shader_ok) {
74                 fprintf(stderr, "Failed to compile shader\n");
75                 show_info_log(shader);
76                 exit(1);
77         }
78         return shader;
79 }
80
81 static GLuint createArrayBuffer(GLsizeiptr size, GLfloat *data)
82 {
83         GLuint buffer;
84         glGenBuffers(1, &buffer);
85         glBindBuffer(GL_ARRAY_BUFFER, buffer);
86         glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
87         return buffer;
88 }
89
90 // initialisation
91 void initialise2dProjection()
92 {
93         GLuint vertex_shader = compile_shader(GL_VERTEX_SHADER, vertex_shader_source);
94         GLuint fragment_shader = compile_shader(GL_FRAGMENT_SHADER, fragment_shader_source);
95         program = glCreateProgram();
96         glAttachShader(program, vertex_shader);
97         glAttachShader(program, fragment_shader);
98         glLinkProgram(program);
99         attributes.position = glGetAttribLocation(program, "position");
100         attributes.color = glGetAttribLocation(program, "color");
101 }
102
103 // draw a quad
104 void drawQuad(GLfloat red, GLfloat green, GLfloat blue, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
105 {
106         glUseProgram(program);
107         
108         // send vertex data to card with given attribute
109         GLfloat vertex_buffer_data[] = {
110                 x1, y1,
111                 x2, y1,
112                 x2, y2,
113                 x1, y2,
114         };
115         GLuint vertex_buffer = createArrayBuffer(sizeof(vertex_buffer_data), vertex_buffer_data);
116         glVertexAttribPointer(
117                 attributes.position,              /* attribute */
118                 2,                                /* size */
119                 GL_FLOAT,                         /* type */
120                 GL_FALSE,                         /* normalized? */
121                 0,                                /* stride */
122                 (void*)0                          /* array buffer offset */
123         );
124         glEnableVertexAttribArray(attributes.position);
125         
126         // same with color data
127         GLfloat color_buffer_data[] = {
128                 red, green, blue,
129                 red, green, blue,
130                 red, green, blue,
131                 red, green, blue,
132         };
133         GLuint color_buffer = createArrayBuffer(sizeof(color_buffer_data), color_buffer_data);
134         glVertexAttribPointer(
135                 attributes.color,              /* attribute */
136                 3,                             /* size */
137                 GL_FLOAT,                      /* type */
138                 GL_FALSE,                      /* normalized? */
139                 0,                             /* stride */
140                 (void*)0                       /* array buffer offset */
141         );
142         glEnableVertexAttribArray(attributes.color);
143         
144         // draw
145         glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
146         
147         // cleanup
148         glDeleteBuffers(1, &vertex_buffer);
149         glDeleteBuffers(1, &color_buffer);
150 }