/* * Copyright (C) 1999-2001 Brian Paul All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* * This is a port of the infamous "glxgears" demo to straight EGL * Port by Dane Rushton 10 July 2005 * * No command line options. * Program runs for 5 seconds then exits, outputing framerate to console */ #define MAXFRAME 5 #define PFINISH 1 #include #include #include #define GL_GLEXT_PROTOTYPES #include #include static GLuint win_width = 1024; static GLuint win_height = 512; static GLuint frames = 0; static GLuint shader_prog; static GLuint vs_shader; static GLuint ps_shader; static GLuint uni_index; static GLuint uni_col; static GLuint uni_row; static GLuint uni_expect; const char *vs_shader_src = "#version 120 \n" "void main() \n" "{ \n" " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n" "} \n"; const char *ps_shader_src = "#version 120 \n" "uniform int index; \n" "uniform int col; \n" "uniform int row; \n" "uniform float expect; \n" " \n" "void main() \n" "{ \n" " mat4x4[3] m = mat4x4[3]( \n" " mat4x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0),\n" " mat4x4(17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0),\n" " mat4x4(33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0));\n" " \n" " gl_FragColor = (m[index][col][row] == expect) \n" " ? vec4(0.0, 1.0, 0.0, 1.0) : vec4(1.0, 0.0, 0.0, 1.0); \n" "} \n"; static void draw_rect(float x, float y, float w, float h) { float verts[4][4]; verts[0][0] = x; verts[0][1] = y; verts[0][2] = 0.0; verts[0][3] = 1.0; verts[1][0] = x + w; verts[1][1] = y; verts[1][2] = 0.0; verts[1][3] = 1.0; verts[2][0] = x + w; verts[2][1] = y + h; verts[2][2] = 0.0; verts[2][3] = 1.0; verts[3][0] = x; verts[3][1] = y + h; verts[3][2] = 0.0; verts[3][3] = 1.0; glVertexPointer(4, GL_FLOAT, 0, verts); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_QUADS, 0, 4); glDisableClientState(GL_VERTEX_ARRAY); } static void draw(void) { #if PFINISH fprintf(stderr, "EE FRAME -----------------------------------------------------\n"); #endif glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(0); glColor3f(0.0f, 0.0f, 1.0f); draw_rect(15, 15, 20, 20); #if 0 glUseProgram(shader_prog); glUniform1i(uni_index, 0); glUniform1i(uni_col, 0); glUniform1i(uni_row, 0); glUniform1f(uni_expect, 1.0); draw_rect(5, 5, 10, 10); #endif #if PFINISH glFinish(); fprintf(stderr, "EE FRAME _____________________________________________________\n"); #endif glutSwapBuffers(); frames++; if (frames >= MAXFRAME) { glFinish(); exit(0); } } static void idle(void) { glutPostRedisplay(); } /* new window size or exposure */ static void reshape(int width, int height) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, width, 0, height, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } static void init(void) { const GLchar *p[1]; GLint len[1]; GLint status; shader_prog = glCreateProgram(); vs_shader = glCreateShader(GL_VERTEX_SHADER); p[0] = vs_shader_src; len[0] = strlen(vs_shader_src); glShaderSource(vs_shader, 1, p, len); glCompileShader(vs_shader); glGetShaderiv(vs_shader, GL_COMPILE_STATUS, &status); if (!status) { GLchar log[1024]; glGetShaderInfoLog(vs_shader, 1024, NULL, log); fprintf(stderr, "Error compiling vs shader %s: \n", log); exit(1); } glAttachShader(shader_prog, vs_shader); ps_shader = glCreateShader(GL_FRAGMENT_SHADER); p[0] = ps_shader_src; len[0] = strlen(ps_shader_src); glShaderSource(ps_shader, 1, p, len); glCompileShader(ps_shader); glGetShaderiv(ps_shader, GL_COMPILE_STATUS, &status); if (!status) { GLchar log[1024]; glGetShaderInfoLog(ps_shader, 1024, NULL, log); fprintf(stderr, "Error compiling ps shader %s: \n", log); exit(1); } glAttachShader(shader_prog, ps_shader); glLinkProgram(shader_prog); glGetProgramiv(shader_prog, GL_LINK_STATUS, &status); if (!status) { GLchar log[1024]; glGetProgramInfoLog(shader_prog, 1024, NULL, log); fprintf(stderr, "Error linking shader program failed %s: \n", log); exit(1); } glValidateProgram(shader_prog); glGetProgramiv(shader_prog, GL_VALIDATE_STATUS, &status); if (!status) { GLchar log[1024]; glGetProgramInfoLog(shader_prog, 1024, NULL, log); fprintf(stderr, "Invalid shader program %s: \n", log); exit(1); } uni_index = glGetUniformLocation(shader_prog, "index"); uni_col = glGetUniformLocation(shader_prog, "col"); uni_row = glGetUniformLocation(shader_prog, "row"); uni_expect = glGetUniformLocation(shader_prog, "expect"); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowSize(win_width, win_height); glutCreateWindow("glgears"); GLenum err = glewInit(); if (GLEW_OK != err) { fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); return -1; } glutIdleFunc(idle); glutReshapeFunc(reshape); glutDisplayFunc(draw); init(); reshape(win_width, win_height); glutMainLoop(); return 0; }