ebcb5b0c8c4f108a88301074e4531df5a3b50988
[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 // shaders
25 static const char *vertex_shader_source =
26 "#version 100 \n\
27 attribute vec2 position; \n\
28 attribute vec3 color; \n\
29 varying vec3 frag_color; \n\
30 void main() \n\
31 { \n\
32         frag_color = color; \n\
33         gl_Position = vec4(position * vec2(2) - vec2(1), 0.0, 1.0); \n\
34 }";
35 static const char *fragment_shader_source =
36 "#version 100 \n\
37 varying vec3 frag_color; \n\
38 void main(void) \n\
39 { \n\
40         gl_FragColor = vec4(frag_color, 1.0); \n\
41 }";
42
43
44 // OpenGL IDs (yeah, this is a bad hack... do we care?)
45 static GLuint program;
46 static struct {
47         GLint position;
48         GLint color;
49 } attributes;
50
51 // some local hlper functions
52 static void show_info_log(GLuint object)
53 {
54         GLint log_length;
55         glGetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length);
56         char *log = new char[log_length];
57         glGetShaderInfoLog(object, log_length, NULL, log);
58         fprintf(stderr, "%s", log);
59         delete[] log;
60 }
61
62 static GLuint compile_shader(GLenum shader_type, const char *source)
63 {
64         GLuint shader = glCreateShader(shader_type);
65         glShaderSource(shader, 1, &source, NULL);
66         glCompileShader(shader);
67         GLint shader_ok;
68         glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);
69         if (!shader_ok) {
70                 fprintf(stderr, "Failed to compile shader\n");
71                 show_info_log(shader);
72                 exit(1);
73         }
74         return shader;
75 }
76
77 static GLuint createArrayBuffer(GLsizeiptr size, GLfloat *data)
78 {
79         GLuint buffer;
80         glGenBuffers(1, &buffer);
81         glBindBuffer(GL_ARRAY_BUFFER, buffer);
82         glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
83         return buffer;
84 }
85
86 // initialisation
87 void initialise2dProjection()
88 {
89         GLuint vertex_shader = compile_shader(GL_VERTEX_SHADER, vertex_shader_source);
90         GLuint fragment_shader = compile_shader(GL_FRAGMENT_SHADER, fragment_shader_source);
91         program = glCreateProgram();
92         glAttachShader(program, vertex_shader);
93         glAttachShader(program, fragment_shader);
94         glLinkProgram(program);
95         attributes.position = glGetAttribLocation(program, "position");
96         attributes.color = glGetAttribLocation(program, "color");
97 }
98
99 // draw a quad
100 void drawQuad(GLfloat red, GLfloat green, GLfloat blue, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
101 {
102         glUseProgram(program);
103         
104         // send vertex data to card with given attribute
105         GLfloat vertex_buffer_data[] = {
106                 x1, y1,
107                 x2, y1,
108                 x2, y2,
109                 x1, y2,
110         };
111         GLuint vertex_buffer = createArrayBuffer(sizeof(vertex_buffer_data), vertex_buffer_data);
112         glVertexAttribPointer(
113                 attributes.position,              /* attribute */
114                 2,                                /* size */
115                 GL_FLOAT,                         /* type */
116                 GL_FALSE,                         /* normalized? */
117                 0,                                /* stride */
118                 (void*)0                          /* array buffer offset */
119         );
120         glEnableVertexAttribArray(attributes.position);
121         
122         // same with color data
123         GLfloat color_buffer_data[] = {
124                 red, green, blue,
125                 red, green, blue,
126                 red, green, blue,
127                 red, green, blue,
128         };
129         GLuint color_buffer = createArrayBuffer(sizeof(color_buffer_data), color_buffer_data);
130         glVertexAttribPointer(
131                 attributes.color,              /* attribute */
132                 3,                             /* size */
133                 GL_FLOAT,                      /* type */
134                 GL_FALSE,                      /* normalized? */
135                 0,                             /* stride */
136                 (void*)0                       /* array buffer offset */
137         );
138         glEnableVertexAttribArray(attributes.color);
139         
140         // draw
141         glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
142         
143         // cleanup
144         glDeleteBuffers(1, &vertex_buffer);
145         glDeleteBuffers(1, &color_buffer);
146 }