Merge commit 'origin/openvg-1.0'
[mesa.git] / progs / glsl / vert-or-frag-only.c
1 /**
2 * Draw two quads, one using only a vertex shader, the other only with a
3 * fragment shader. They should appear the same.
4 * 17 Dec 2008
5 * Brian Paul
6 */
7
8
9 #include <assert.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <math.h>
14 #include <GL/glew.h>
15 #include <GL/gl.h>
16 #include <GL/glut.h>
17 #include <GL/glext.h>
18 #include "extfuncs.h"
19 #include "shaderutil.h"
20
21
22 static char *FragProgFile = NULL;
23 static char *VertProgFile = NULL;
24 static GLuint FragShader;
25 static GLuint VertShader;
26 static GLuint VertProgram; /* w/out vertex shader */
27 static GLuint FragProgram; /* w/out fragment shader */
28 static GLint Win = 0;
29
30
31 static void
32 DrawQuadColor(void)
33 {
34 glBegin(GL_QUADS);
35 glColor3f(1, 0, 0); glVertex2f(-1, -1);
36 glColor3f(0, 1, 0); glVertex2f( 1, -1);
37 glColor3f(0, 0, 1); glVertex2f( 1, 1);
38 glColor3f(1, 0, 1); glVertex2f(-1, 1);
39 glEnd();
40 }
41
42
43 /** as above, but specify color via texcoords */
44 static void
45 DrawQuadTex(void)
46 {
47 glBegin(GL_QUADS);
48 glTexCoord3f(1, 0, 0); glVertex2f(-1, -1);
49 glTexCoord3f(0, 1, 0); glVertex2f( 1, -1);
50 glTexCoord3f(0, 0, 1); glVertex2f( 1, 1);
51 glTexCoord3f(1, 0, 1); glVertex2f(-1, 1);
52 glEnd();
53 }
54
55
56 static void
57 Redisplay(void)
58 {
59 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
60
61 /* render with vertex shader only */
62 glUseProgram_func(VertProgram);
63 glPushMatrix();
64 glTranslatef(-1.5, 0, 0);
65 DrawQuadTex();
66 glPopMatrix();
67
68 /* render with fragment shader only */
69 glUseProgram_func(FragProgram);
70 glPushMatrix();
71 glTranslatef(+1.5, 0, 0);
72 DrawQuadColor();
73 glPopMatrix();
74
75 glutSwapBuffers();
76 }
77
78
79 static void
80 Reshape(int width, int height)
81 {
82 glViewport(0, 0, width, height);
83 glMatrixMode(GL_PROJECTION);
84 glLoadIdentity();
85 glOrtho(-4, 4, -2, 2, -1, 1);
86 glMatrixMode(GL_MODELVIEW);
87 glLoadIdentity();
88 }
89
90
91 static void
92 CleanUp(void)
93 {
94 glDeleteShader_func(FragShader);
95 glDeleteShader_func(VertShader);
96 glDeleteProgram_func(VertProgram);
97 glDeleteProgram_func(FragProgram);
98 glutDestroyWindow(Win);
99 }
100
101
102 static void
103 Key(unsigned char key, int x, int y)
104 {
105 (void) x;
106 (void) y;
107 switch(key) {
108 case 27:
109 CleanUp();
110 exit(0);
111 break;
112 }
113 glutPostRedisplay();
114 }
115
116
117 static void
118 Init(void)
119 {
120 static const char *fragShaderText =
121 "void main() {\n"
122 " gl_FragColor = gl_Color;\n"
123 "}\n";
124 static const char *vertShaderText =
125 "void main() {\n"
126 " gl_Position = ftransform();\n"
127 " gl_FrontColor = gl_MultiTexCoord0;\n" /* see DrawQuadTex() */
128 "}\n";
129
130 if (!ShadersSupported())
131 exit(1);
132
133 GetExtensionFuncs();
134
135 if (FragProgFile)
136 FragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
137 else
138 FragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
139
140 if (VertProgFile)
141 VertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
142 else
143 VertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
144
145 VertProgram = LinkShaders(VertShader, 0);
146 FragProgram = LinkShaders(0, FragShader);
147
148 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
149 glEnable(GL_DEPTH_TEST);
150
151 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
152
153 assert(glIsProgram_func(VertProgram));
154 assert(glIsProgram_func(FragProgram));
155 assert(glIsShader_func(FragShader));
156 assert(glIsShader_func(VertShader));
157
158 glColor3f(1, 0, 0);
159 }
160
161
162 static void
163 ParseOptions(int argc, char *argv[])
164 {
165 int i;
166 for (i = 1; i < argc; i++) {
167 if (strcmp(argv[i], "-fs") == 0) {
168 FragProgFile = argv[i+1];
169 }
170 else if (strcmp(argv[i], "-vs") == 0) {
171 VertProgFile = argv[i+1];
172 }
173 }
174 }
175
176
177 int
178 main(int argc, char *argv[])
179 {
180 glutInit(&argc, argv);
181 glutInitWindowPosition( 0, 0);
182 glutInitWindowSize(400, 200);
183 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
184 Win = glutCreateWindow(argv[0]);
185 glewInit();
186 glutReshapeFunc(Reshape);
187 glutKeyboardFunc(Key);
188 glutDisplayFunc(Redisplay);
189 ParseOptions(argc, argv);
190 Init();
191 glutMainLoop();
192 return 0;
193 }