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.
24 static const char *vertex_shader_source =
26 attribute vec2 position; \n\
27 attribute vec3 color; \n\
28 varying vec3 frag_color; \n\
31 frag_color = color; \n\
32 gl_Position = vec4(position * vec2(2) - vec2(1), 0.0, 1.0); \n\
34 static const char *fragment_shader_source =
36 varying vec3 frag_color; \n\
39 gl_FragColor = vec4(frag_color, 1.0); \n\
43 // OpenGL IDs (yeah, this is a bad hack... do we care?)
44 static GLuint program;
50 // some local hlper functions
51 static void show_info_log(GLuint object)
54 glGetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length);
55 char *log = new char[log_length];
56 glGetShaderInfoLog(object, log_length, NULL, log);
57 fprintf(stderr, "%s", log);
61 static GLuint compile_shader(GLenum shader_type, const char *source)
63 GLuint shader = glCreateShader(shader_type);
64 glShaderSource(shader, 1, &source, NULL);
65 glCompileShader(shader);
67 glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);
69 fprintf(stderr, "Failed to compile shader\n");
70 show_info_log(shader);
76 static GLuint createArrayBuffer(GLsizeiptr size, GLfloat *data)
79 glGenBuffers(1, &buffer);
80 glBindBuffer(GL_ARRAY_BUFFER, buffer);
81 glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
86 void initialise2dProjection()
88 GLuint vertex_shader = compile_shader(GL_VERTEX_SHADER, vertex_shader_source);
89 GLuint fragment_shader = compile_shader(GL_FRAGMENT_SHADER, fragment_shader_source);
90 program = glCreateProgram();
91 glAttachShader(program, vertex_shader);
92 glAttachShader(program, fragment_shader);
93 glLinkProgram(program);
94 attributes.position = glGetAttribLocation(program, "position");
95 attributes.color = glGetAttribLocation(program, "color");
99 void drawQuad(GLfloat red, GLfloat green, GLfloat blue, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
101 glUseProgram(program);
103 // send vertex data to card with given attribute
104 GLfloat vertex_buffer_data[] = {
110 GLuint vertex_buffer = createArrayBuffer(sizeof(vertex_buffer_data), vertex_buffer_data);
111 glVertexAttribPointer(
112 attributes.position, /* attribute */
115 GL_FALSE, /* normalized? */
117 (void*)0 /* array buffer offset */
119 glEnableVertexAttribArray(attributes.position);
121 // same with color data
122 GLfloat color_buffer_data[] = {
128 GLuint color_buffer = createArrayBuffer(sizeof(color_buffer_data), color_buffer_data);
129 glVertexAttribPointer(
130 attributes.color, /* attribute */
133 GL_FALSE, /* normalized? */
135 (void*)0 /* array buffer offset */
137 glEnableVertexAttribArray(attributes.color);
140 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
143 glDeleteBuffers(1, &vertex_buffer);
144 glDeleteBuffers(1, &color_buffer);