]> git.cworth.org Git - apitrace-tests/blob - apps/gl/tri_glsl.c
Add --exact, --no-deps, or --no-prune to trim tests as needed
[apitrace-tests] / apps / gl / tri_glsl.c
1 /* Test fragment shader
2  *
3  * Brian Pau
4  */
5
6 #ifdef _WIN32
7 #include <windows.h>
8 #endif
9
10 #include <assert.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h>
15
16 #include <GL/glew.h>
17 #ifdef __APPLE__
18 #  include <GLUT/glut.h>
19 #else
20 #  include <GL/glut.h>
21 #endif
22
23
24 static GLuint fragShader;
25 static GLuint vertShader;
26 static GLuint program;
27 static GLint win = 0;
28 static GLfloat xpos = 0, ypos = 0;
29
30
31 static void
32 LoadAndCompileShader(GLuint shader, const char *text)
33 {
34    const GLint length = -1;
35    GLint stat;
36
37    glShaderSource(shader, 1, (const GLchar **) &text, &length);
38
39    glCompileShader(shader);
40
41    glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
42    if (!stat) {
43       GLchar log[1000];
44       GLsizei len;
45       glGetShaderInfoLog(shader, 1000, &len, log);
46       fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log);
47       exit(1);
48    }
49 }
50
51
52 static void
53 CheckLink(GLuint prog)
54 {
55    GLint stat;
56    glGetProgramiv(prog, GL_LINK_STATUS, &stat);
57    if (!stat) {
58       GLchar log[1000];
59       GLsizei len;
60       glGetProgramInfoLog(prog, 1000, &len, log);
61       fprintf(stderr, "Linker error:\n%s\n", log);
62    }
63 }
64
65
66 static void
67 Init(void)
68 {
69    static const char *fragShaderText =
70       "void main() {\n"
71       "   gl_FragColor = gl_Color; \n"
72       "}\n";
73    static const char *vertShaderText =
74       "void main() {\n"
75       "   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
76       "   gl_FrontColor = gl_Color;\n"
77       "}\n";
78
79    if (!GLEW_VERSION_2_0) {
80       printf("This program requires OpenGL 2.x\n");
81       exit(1);
82    }
83
84    fragShader = glCreateShader(GL_FRAGMENT_SHADER);
85    LoadAndCompileShader(fragShader, fragShaderText);
86
87    vertShader = glCreateShader(GL_VERTEX_SHADER);
88    LoadAndCompileShader(vertShader, vertShaderText);
89
90    program = glCreateProgram();
91    glAttachShader(program, fragShader);
92    glAttachShader(program, vertShader);
93    glLinkProgram(program);
94    CheckLink(program);
95    glUseProgram(program);
96
97    assert(glGetError() == 0);
98
99    glClearColor(0.3f, 0.1f, 0.3f, 1.0f);
100 }
101
102
103 static void
104 Reshape(int width, int height)
105 {
106    glViewport(0, 0, width, height);
107    glMatrixMode(GL_PROJECTION);
108    glLoadIdentity();
109    glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
110    glMatrixMode(GL_MODELVIEW);
111 }
112
113
114 static void
115 Draw(void)
116 {
117    glClear(GL_COLOR_BUFFER_BIT);
118
119    glBegin(GL_TRIANGLES);
120    glColor3f(.8, 0, 0); 
121    glVertex3f(-0.9, -0.9, -30.0);
122    glColor3f(0, .9, 0); 
123    glVertex3f( 0.9, -0.9, -30.0);
124    glColor3f(0, 0, .7); 
125    glVertex3f( 0.0,  0.9, -30.0);
126    glEnd();
127
128    glFlush();
129
130    glutSwapBuffers();
131
132    glDeleteShader(fragShader);
133    glDeleteShader(vertShader);
134    glDeleteProgram(program);
135    glutDestroyWindow(win);
136
137    exit(0);
138 }
139
140
141 int
142 main(int argc, char **argv)
143 {
144    glutInit(&argc, argv);
145    glutInitWindowSize(250, 250);
146    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
147    win = glutCreateWindow(argv[0]);
148    glewInit();
149    glutReshapeFunc(Reshape);
150    glutDisplayFunc(Draw);
151    Init();
152    glutMainLoop();
153    return 0;
154 }