mesa-demos: NOTE! Default branch is now main
[mesa-demos.git] / src / tests / vpeval.c
blob5845025317ffd6e39eef29a5fa78c695a34149cb
1 /*
2 * Vertex program evaluators test.
3 * Based on book/bezmesh.c
5 * Brian Paul
6 * 22 June 2002
7 */
9 #include <assert.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <glad/glad.h>
14 #include "glut_wrap.h"
18 * Transform position by modelview/projection.
19 * Square incoming color.
21 static const char prog[] =
22 "!!VP1.0\n"
24 "# Typical modelview/projection\n"
25 "DP4 o[HPOS].x, c[0], v[OPOS] ; # object x MVP -> clip\n"
26 "DP4 o[HPOS].y, c[1], v[OPOS] ;\n"
27 "DP4 o[HPOS].z, c[2], v[OPOS] ;\n"
28 "DP4 o[HPOS].w, c[3], v[OPOS] ;\n"
30 "MOV R0, v[COL0];\n # square the color\n"
31 "MUL R0, R0, R0;\n"
32 "MOV o[COL0], R0;\n # store output color\n"
34 "END";
37 static int program = 1;
40 GLfloat ctrlpoints[4][4][4] =
43 {-1.5, -1.5, 4.0, 1.0},
44 {-0.5, -1.5, 2.0, 1.0},
45 {0.5, -1.5, -1.0, 1.0},
46 {1.5, -1.5, 2.0, 1.0}},
48 {-1.5, -0.5, 1.0, 1.0},
49 {-0.5, -0.5, 3.0, 1.0},
50 {0.5, -0.5, 0.0, 1.0},
51 {1.5, -0.5, -1.0, 1.0}},
53 {-1.5, 0.5, 4.0, 1.0},
54 {-0.5, 0.5, 0.0, 1.0},
55 {0.5, 0.5, 3.0, 1.0},
56 {1.5, 0.5, 4.0, 1.0}},
58 {-1.5, 1.5, -2.0, 1.0},
59 {-0.5, 1.5, -2.0, 1.0},
60 {0.5, 1.5, 0.0, 1.0},
61 {1.5, 1.5, -1.0, 1.0}}
65 * +-------------+
66 * |green |yellow
67 * | |
68 * | |
69 * |black |red
70 * +-------------+
72 GLfloat colorPoints[4][4][4] =
75 {0.0, 0.0, 0.0, 1.0},
76 {0.3, 0.0, 0.0, 1.0},
77 {0.6, 0.0, 0.0, 1.0},
78 {1.0, 0.0, 0.0, 1.0}},
80 {0.0, 0.3, 0.0, 1.0},
81 {0.3, 0.3, 0.0, 1.0},
82 {0.6, 0.3, 0.0, 1.0},
83 {1.0, 0.3, 0.0, 1.0}},
85 {0.0, 0.6, 0.0, 1.0},
86 {0.3, 0.6, 0.0, 1.0},
87 {0.6, 0.6, 0.0, 1.0},
88 {1.0, 0.6, 0.0, 1.0}},
90 {0.0, 1.0, 0.0, 1.0},
91 {0.3, 1.0, 0.0, 1.0},
92 {0.6, 1.0, 0.0, 1.0},
93 {1.0, 1.0, 0.0, 1.0}}
97 static void
98 initlights(void)
100 #if 0 /* no lighting for now */
101 GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
102 GLfloat position[] = {0.0, 0.0, 2.0, 1.0};
103 GLfloat mat_diffuse[] = {0.6, 0.6, 0.6, 1.0};
104 GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
105 GLfloat mat_shininess[] = {50.0};
107 glEnable(GL_LIGHTING);
108 glEnable(GL_LIGHT0);
110 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
111 glLightfv(GL_LIGHT0, GL_POSITION, position);
113 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
114 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
115 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
116 #endif
119 static void
120 display(void)
122 glClearColor(.3, .3, .3, 0);
123 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
124 glPushMatrix();
125 #if 1
126 glRotatef(85.0, 1.0, 1.0, 1.0);
127 #endif
128 glEvalMesh2(GL_FILL, 0, 8, 0, 8);
129 glPopMatrix();
130 glFlush();
133 static void
134 myinit(int argc, char *argv[])
136 glClearColor(0.0, 0.0, 0.0, 1.0);
137 glEnable(GL_DEPTH_TEST);
139 initlights(); /* for lighted version only */
141 glMapGrid2f(8, 0.0, 1.0, 8, 0.0, 1.0);
143 if (argc > 1)
144 program = 0;
146 printf("Using vertex program attribs? %s\n", program ? "yes" : "no");
148 if (program && !glutExtensionSupported("GL_NV_vertex_program")) {
149 printf("Sorry, this requires GL_NV_vertex_program\n");
150 exit(1);
153 if (!program) {
154 glMap2f(GL_MAP2_VERTEX_4,
155 0.0, 1.0, 4, 4,
156 0.0, 1.0, 16, 4, &ctrlpoints[0][0][0]);
157 glMap2f(GL_MAP2_COLOR_4,
158 0.0, 1.0, 4, 4,
159 0.0, 1.0, 16, 4, &colorPoints[0][0][0]);
160 glEnable(GL_MAP2_VERTEX_4);
161 glEnable(GL_MAP2_COLOR_4);
163 glEnable(GL_AUTO_NORMAL);
164 glEnable(GL_NORMALIZE);
167 else {
168 glMap2f(GL_MAP2_VERTEX_ATTRIB0_4_NV,
169 0.0, 1.0, 4, 4,
170 0.0, 1.0, 16, 4, &ctrlpoints[0][0][0]);
171 glMap2f(GL_MAP2_VERTEX_ATTRIB3_4_NV,
172 0.0, 1.0, 4, 4,
173 0.0, 1.0, 16, 4, &colorPoints[0][0][0]);
174 glEnable(GL_MAP2_VERTEX_ATTRIB0_4_NV);
175 glEnable(GL_MAP2_VERTEX_ATTRIB3_4_NV);
178 glEnable(GL_AUTO_NORMAL);
179 glEnable(GL_NORMALIZE);
182 /* vertex program init */
183 glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 1,
184 strlen(prog), (const GLubyte *) prog);
185 assert(glIsProgramNV(1));
186 glBindProgramNV(GL_VERTEX_PROGRAM_NV, 1);
188 /* track matrices */
189 glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 0, GL_MODELVIEW_PROJECTION_NV, GL_IDENTITY_NV);
190 glEnable(GL_VERTEX_PROGRAM_NV);
194 static void
195 myReshape(int w, int h)
197 glViewport(0, 0, w, h);
198 glMatrixMode(GL_PROJECTION);
199 glLoadIdentity();
200 if (w <= h)
201 glOrtho(-4.0, 4.0, -4.0 * (GLfloat) h / (GLfloat) w,
202 4.0 * (GLfloat) h / (GLfloat) w, -4.0, 4.0);
203 else
204 glOrtho(-4.0 * (GLfloat) w / (GLfloat) h,
205 4.0 * (GLfloat) w / (GLfloat) h, -4.0, 4.0, -4.0, 4.0);
206 glMatrixMode(GL_MODELVIEW);
207 glLoadIdentity();
210 static void
211 key(unsigned char k, int x, int y)
213 switch (k) {
214 case 27: /* Escape */
215 exit(0);
216 break;
217 default:
218 return;
220 glutPostRedisplay();
224 main(int argc, char **argv)
226 glutInit(&argc, argv);
227 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
228 glutInitWindowPosition(0, 0);
229 glutCreateWindow(argv[0]);
230 gladLoadGL();
231 myinit(argc, argv);
232 glutReshapeFunc(myReshape);
233 glutDisplayFunc(display);
234 glutKeyboardFunc(key);
235 glutMainLoop();
236 return 0; /* ANSI C requires main to return int. */