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 static const char *vertex_shader_source =
27 attribute vec2 position; \n\
28 attribute vec3 color; \n\
29 varying vec3 frag_color; \n\
32 frag_color = color; \n\
33 gl_Position = vec4(position * vec2(2) - vec2(1), 0.0, 1.0); \n\
35 static const char *fragment_shader_source =
37 varying vec3 frag_color; \n\
40 gl_FragColor = vec4(frag_color, 1.0); \n\
44 // OpenGL IDs (yeah, this is a bad hack... do we care?)
45 static GLuint program;
51 // some local hlper functions
52 static void show_info_log(GLuint object)
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);
62 static GLuint compile_shader(GLenum shader_type, const char *source)
64 GLuint shader = glCreateShader(shader_type);
65 glShaderSource(shader, 1, &source, NULL);
66 glCompileShader(shader);
68 glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);
70 fprintf(stderr, "Failed to compile shader\n");
71 show_info_log(shader);
77 static GLuint createArrayBuffer(GLsizeiptr size, GLfloat *data)
80 glGenBuffers(1, &buffer);
81 glBindBuffer(GL_ARRAY_BUFFER, buffer);
82 glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
87 void initialise2dProjection()
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");
100 void drawQuad(GLfloat red, GLfloat green, GLfloat blue, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
102 glUseProgram(program);
104 // send vertex data to card with given attribute
105 GLfloat vertex_buffer_data[] = {
111 GLuint vertex_buffer = createArrayBuffer(sizeof(vertex_buffer_data), vertex_buffer_data);
112 glVertexAttribPointer(
113 attributes.position, /* attribute */
116 GL_FALSE, /* normalized? */
118 (void*)0 /* array buffer offset */
120 glEnableVertexAttribArray(attributes.position);
122 // same with color data
123 GLfloat color_buffer_data[] = {
129 GLuint color_buffer = createArrayBuffer(sizeof(color_buffer_data), color_buffer_data);
130 glVertexAttribPointer(
131 attributes.color, /* attribute */
134 GL_FALSE, /* normalized? */
136 (void*)0 /* array buffer offset */
138 glEnableVertexAttribArray(attributes.color);
141 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
144 glDeleteBuffers(1, &vertex_buffer);
145 glDeleteBuffers(1, &color_buffer);