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