1 /* gltest - small OpenGL tearing test program
2 * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
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.
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.
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.
25 #error "GL1 contexts do not support GL2 functionality"
29 static const char *vertex_shader_source =
31 attribute vec2 position; \n\
32 attribute vec3 color; \n\
33 varying vec3 frag_color; \n\
36 frag_color = color; \n\
37 gl_Position = vec4(position * vec2(2) - vec2(1), 0.0, 1.0); \n\
39 static const char *fragment_shader_source =
41 varying vec3 frag_color; \n\
44 gl_FragColor = vec4(frag_color, 1.0); \n\
48 // OpenGL IDs (yeah, this is a bad hack... do we care?)
49 static GLuint program;
55 // some local hlper functions
56 static void show_info_log(GLuint object)
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);
66 static GLuint compile_shader(GLenum shader_type, const char *source)
68 GLuint shader = glCreateShader(shader_type);
69 glShaderSource(shader, 1, &source, NULL);
70 glCompileShader(shader);
72 glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);
74 fprintf(stderr, "Failed to compile shader\n");
75 show_info_log(shader);
81 static GLuint createArrayBuffer(GLsizeiptr size, GLfloat *data)
84 glGenBuffers(1, &buffer);
85 glBindBuffer(GL_ARRAY_BUFFER, buffer);
86 glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
91 void initialise2dProjection()
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");
104 void drawQuad(GLfloat red, GLfloat green, GLfloat blue, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
106 glUseProgram(program);
108 // send vertex data to card with given attribute
109 GLfloat vertex_buffer_data[] = {
115 GLuint vertex_buffer = createArrayBuffer(sizeof(vertex_buffer_data), vertex_buffer_data);
116 glVertexAttribPointer(
117 attributes.position, /* attribute */
120 GL_FALSE, /* normalized? */
122 (void*)0 /* array buffer offset */
124 glEnableVertexAttribArray(attributes.position);
126 // same with color data
127 GLfloat color_buffer_data[] = {
133 GLuint color_buffer = createArrayBuffer(sizeof(color_buffer_data), color_buffer_data);
134 glVertexAttribPointer(
135 attributes.color, /* attribute */
138 GL_FALSE, /* normalized? */
140 (void*)0 /* array buffer offset */
142 glEnableVertexAttribArray(attributes.color);
145 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
148 glDeleteBuffers(1, &vertex_buffer);
149 glDeleteBuffers(1, &color_buffer);