blob: c35906de5cf61ed01001c47ccadabfd63d03d30c [file] [log] [blame]
Brian Paul4db3c482002-06-23 02:57:33 +00001/*
2 * Vertex program evaluators test.
3 * Based on book/bezmesh.c
4 *
5 * Brian Paul
6 * 22 June 2002
7 */
8
9#include <assert.h>
10#include <stdlib.h>
11#include <GL/glut.h>
12
13
14/*
15 * Transform position by modelview/projection.
16 * Square incoming color.
17 */
18static const char prog[] =
19"!!VP1.0\n"
20
21"# Typical modelview/projection\n"
22"DP4 o[HPOS].x, c[0], v[OPOS] ; # object x MVP -> clip\n"
23"DP4 o[HPOS].y, c[1], v[OPOS] ;\n"
24"DP4 o[HPOS].z, c[2], v[OPOS] ;\n"
25"DP4 o[HPOS].w, c[3], v[OPOS] ;\n"
26
27"MOV R0, v[COL0];\n # square the color\n"
28"MUL R0, R0, R0;\n"
29"MOV o[COL0], R0;\n # store output color\n"
30
31"END";
32
33
34static int program = 1;
35
36
37GLfloat ctrlpoints[4][4][4] =
38{
39 {
40 {-1.5, -1.5, 4.0, 1.0},
41 {-0.5, -1.5, 2.0, 1.0},
42 {0.5, -1.5, -1.0, 1.0},
43 {1.5, -1.5, 2.0, 1.0}},
44 {
45 {-1.5, -0.5, 1.0, 1.0},
46 {-0.5, -0.5, 3.0, 1.0},
47 {0.5, -0.5, 0.0, 1.0},
48 {1.5, -0.5, -1.0, 1.0}},
49 {
50 {-1.5, 0.5, 4.0, 1.0},
51 {-0.5, 0.5, 0.0, 1.0},
52 {0.5, 0.5, 3.0, 1.0},
53 {1.5, 0.5, 4.0, 1.0}},
54 {
55 {-1.5, 1.5, -2.0, 1.0},
56 {-0.5, 1.5, -2.0, 1.0},
57 {0.5, 1.5, 0.0, 1.0},
58 {1.5, 1.5, -1.0, 1.0}}
59};
60
61/*
62 * +-------------+
63 * |green |yellow
64 * | |
65 * | |
66 * |black |red
67 * +-------------+
68 */
69GLfloat colorPoints[4][4][4] =
70{
71 {
72 {0.0, 0.0, 0.0, 1.0},
73 {0.3, 0.0, 0.0, 1.0},
74 {0.6, 0.0, 0.0, 1.0},
75 {1.0, 0.0, 0.0, 1.0}},
76 {
77 {0.0, 0.3, 0.0, 1.0},
78 {0.3, 0.3, 0.0, 1.0},
79 {0.6, 0.3, 0.0, 1.0},
80 {1.0, 0.3, 0.0, 1.0}},
81 {
82 {0.0, 0.6, 0.0, 1.0},
83 {0.3, 0.6, 0.0, 1.0},
84 {0.6, 0.6, 0.0, 1.0},
85 {1.0, 0.6, 0.0, 1.0}},
86 {
87 {0.0, 1.0, 0.0, 1.0},
88 {0.3, 1.0, 0.0, 1.0},
89 {0.6, 1.0, 0.0, 1.0},
90 {1.0, 1.0, 0.0, 1.0}}
91};
92
93
94void
95initlights(void)
96{
97 GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
98 GLfloat position[] = {0.0, 0.0, 2.0, 1.0};
99 GLfloat mat_diffuse[] = {0.6, 0.6, 0.6, 1.0};
100 GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
101 GLfloat mat_shininess[] = {50.0};
102
103#if 0 /* no lighting for now */
104 glEnable(GL_LIGHTING);
105 glEnable(GL_LIGHT0);
106
107 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
108 glLightfv(GL_LIGHT0, GL_POSITION, position);
109
110 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
111 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
112 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
113#endif
114}
115
116void
117display(void)
118{
119 glClearColor(.3, .3, .3, 0);
120 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
121 glPushMatrix();
122#if 1
123 glRotatef(85.0, 1.0, 1.0, 1.0);
124#endif
125 glEvalMesh2(GL_FILL, 0, 8, 0, 8);
126 glPopMatrix();
127 glFlush();
128}
129
130void
131myinit(int argc, char *argv[])
132{
133 glClearColor(0.0, 0.0, 0.0, 1.0);
134 glEnable(GL_DEPTH_TEST);
135
136 initlights(); /* for lighted version only */
137
138 glMapGrid2f(8, 0.0, 1.0, 8, 0.0, 1.0);
139
140 if (argc > 1)
141 program = 0;
142
143 printf("Using vertex program attribs? %s\n", program ? "yes" : "no");
144
145 if (!program) {
146 glMap2f(GL_MAP2_VERTEX_4,
147 0.0, 1.0, 4, 4,
148 0.0, 1.0, 16, 4, &ctrlpoints[0][0][0]);
149 glMap2f(GL_MAP2_COLOR_4,
150 0.0, 1.0, 4, 4,
151 0.0, 1.0, 16, 4, &colorPoints[0][0][0]);
152 glEnable(GL_MAP2_VERTEX_4);
153 glEnable(GL_MAP2_COLOR_4);
154 /*
155 glEnable(GL_AUTO_NORMAL);
156 glEnable(GL_NORMALIZE);
157 */
158 }
159 else {
160 glMap2f(GL_MAP2_VERTEX_ATTRIB0_4_NV,
161 0.0, 1.0, 4, 4,
162 0.0, 1.0, 16, 4, &ctrlpoints[0][0][0]);
163 glMap2f(GL_MAP2_VERTEX_ATTRIB3_4_NV,
164 0.0, 1.0, 4, 4,
165 0.0, 1.0, 16, 4, &colorPoints[0][0][0]);
166 glEnable(GL_MAP2_VERTEX_ATTRIB0_4_NV);
167 glEnable(GL_MAP2_VERTEX_ATTRIB3_4_NV);
168
169 /*
170 glEnable(GL_AUTO_NORMAL);
171 glEnable(GL_NORMALIZE);
172 */
173
174 /* vertex program init */
175 glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 1,
176 strlen(prog), (const GLubyte *) prog);
177 assert(glIsProgramNV(1));
178 glBindProgramNV(GL_VERTEX_PROGRAM_NV, 1);
179
180 /* track matrices */
181 glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 0, GL_MODELVIEW_PROJECTION_NV, GL_IDENTITY_NV);
182 glEnable(GL_VERTEX_PROGRAM_NV);
183 }
184}
185
186void
187myReshape(int w, int h)
188{
189 glViewport(0, 0, w, h);
190 glMatrixMode(GL_PROJECTION);
191 glLoadIdentity();
192 if (w <= h)
193 glOrtho(-4.0, 4.0, -4.0 * (GLfloat) h / (GLfloat) w,
194 4.0 * (GLfloat) h / (GLfloat) w, -4.0, 4.0);
195 else
196 glOrtho(-4.0 * (GLfloat) w / (GLfloat) h,
197 4.0 * (GLfloat) w / (GLfloat) h, -4.0, 4.0, -4.0, 4.0);
198 glMatrixMode(GL_MODELVIEW);
199 glLoadIdentity();
200}
201
202static void
203key(unsigned char k, int x, int y)
204{
205 switch (k) {
206 case 27: /* Escape */
207 exit(0);
208 break;
209 default:
210 return;
211 }
212 glutPostRedisplay();
213}
214
215int
216main(int argc, char **argv)
217{
218 glutInit(&argc, argv);
219 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
220 glutInitWindowPosition(0, 0);
221 glutCreateWindow(argv[0]);
222 myinit(argc, argv);
223 glutReshapeFunc(myReshape);
224 glutDisplayFunc(display);
225 glutKeyboardFunc(key);
226 glutMainLoop();
227 return 0; /* ANSI C requires main to return int. */
228}