summaryrefslogtreecommitdiff
path: root/progs/tests/vpeval.c
blob: a4024b5336b2e86db20ed3c376645aafab113148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Vertex program evaluators test.
 * Based on book/bezmesh.c
 *
 * Brian Paul
 * 22 June 2002
 */

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/glut.h>


/*
 * Transform position by modelview/projection.
 * Square incoming color.
 */
static const char prog[] = 
"!!VP1.0\n"

"# Typical modelview/projection\n"
"DP4   o[HPOS].x, c[0], v[OPOS] ;	# object x MVP -> clip\n"
"DP4   o[HPOS].y, c[1], v[OPOS] ;\n"
"DP4   o[HPOS].z, c[2], v[OPOS] ;\n"
"DP4   o[HPOS].w, c[3], v[OPOS] ;\n"

"MOV   R0, v[COL0];\n       # square the color\n"
"MUL   R0, R0, R0;\n"
"MOV   o[COL0], R0;\n       # store output color\n"

"END";


static int program = 1;


GLfloat ctrlpoints[4][4][4] =
{
    {
        {-1.5, -1.5, 4.0, 1.0},
        {-0.5, -1.5, 2.0, 1.0},
        {0.5, -1.5, -1.0, 1.0},
        {1.5, -1.5, 2.0, 1.0}},
    {
        {-1.5, -0.5, 1.0, 1.0},
        {-0.5, -0.5, 3.0, 1.0},
        {0.5, -0.5, 0.0, 1.0},
        {1.5, -0.5, -1.0, 1.0}},
    {
        {-1.5, 0.5, 4.0, 1.0},
        {-0.5, 0.5, 0.0, 1.0},
        {0.5, 0.5, 3.0, 1.0},
        {1.5, 0.5, 4.0, 1.0}},
    {
        {-1.5, 1.5, -2.0, 1.0},
        {-0.5, 1.5, -2.0, 1.0},
        {0.5, 1.5, 0.0, 1.0},
        {1.5, 1.5, -1.0, 1.0}}
};

/*
 * +-------------+
 * |green        |yellow
 * |             |
 * |             |
 * |black        |red
 * +-------------+
 */
GLfloat colorPoints[4][4][4] =
{
    {
        {0.0, 0.0, 0.0, 1.0},
        {0.3, 0.0, 0.0, 1.0},
        {0.6, 0.0, 0.0, 1.0},
        {1.0, 0.0, 0.0, 1.0}},
    {
        {0.0, 0.3, 0.0, 1.0},
        {0.3, 0.3, 0.0, 1.0},
        {0.6, 0.3, 0.0, 1.0},
        {1.0, 0.3, 0.0, 1.0}},
    {
        {0.0, 0.6, 0.0, 1.0},
        {0.3, 0.6, 0.0, 1.0},
        {0.6, 0.6, 0.0, 1.0},
        {1.0, 0.6, 0.0, 1.0}},
    {
        {0.0, 1.0, 0.0, 1.0},
        {0.3, 1.0, 0.0, 1.0},
        {0.6, 1.0, 0.0, 1.0},
        {1.0, 1.0, 0.0, 1.0}}
};


static void
initlights(void)
{
#if 0 /* no lighting for now */
    GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
    GLfloat position[] = {0.0, 0.0, 2.0, 1.0};
    GLfloat mat_diffuse[] = {0.6, 0.6, 0.6, 1.0};
    GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
    GLfloat mat_shininess[] = {50.0};

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
    glLightfv(GL_LIGHT0, GL_POSITION, position);

    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
#endif
}

static void
display(void)
{
   glClearColor(.3, .3, .3, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
#if 1
    glRotatef(85.0, 1.0, 1.0, 1.0);
#endif
    glEvalMesh2(GL_FILL, 0, 8, 0, 8);
    glPopMatrix();
    glFlush();
}

static void
myinit(int argc, char *argv[])
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glEnable(GL_DEPTH_TEST);

    initlights();       /* for lighted version only */

    glMapGrid2f(8, 0.0, 1.0, 8, 0.0, 1.0);

    if (argc > 1)
       program = 0;

    printf("Using vertex program attribs? %s\n", program ? "yes" : "no");

    if (program && !glutExtensionSupported("GL_NV_vertex_program")) {
       printf("Sorry, this requires GL_NV_vertex_program\n");
       exit(1);
    }

    if (!program) {
        glMap2f(GL_MAP2_VERTEX_4,
                0.0, 1.0, 4, 4,
                0.0, 1.0, 16, 4, &ctrlpoints[0][0][0]);
        glMap2f(GL_MAP2_COLOR_4,
                0.0, 1.0, 4, 4,
                0.0, 1.0, 16, 4, &colorPoints[0][0][0]);
        glEnable(GL_MAP2_VERTEX_4);
        glEnable(GL_MAP2_COLOR_4);
        /*
        glEnable(GL_AUTO_NORMAL);
        glEnable(GL_NORMALIZE);
        */
    }
    else {
        glMap2f(GL_MAP2_VERTEX_ATTRIB0_4_NV,
                0.0, 1.0, 4, 4,
                0.0, 1.0, 16, 4, &ctrlpoints[0][0][0]);
        glMap2f(GL_MAP2_VERTEX_ATTRIB3_4_NV,
                0.0, 1.0, 4, 4,
                0.0, 1.0, 16, 4, &colorPoints[0][0][0]);
        glEnable(GL_MAP2_VERTEX_ATTRIB0_4_NV);
        glEnable(GL_MAP2_VERTEX_ATTRIB3_4_NV);

        /*
        glEnable(GL_AUTO_NORMAL);
        glEnable(GL_NORMALIZE);
        */

        /* vertex program init */
        glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 1,
                        strlen(prog), (const GLubyte *) prog);
        assert(glIsProgramNV(1));
        glBindProgramNV(GL_VERTEX_PROGRAM_NV, 1);

        /* track matrices */
        glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 0, GL_MODELVIEW_PROJECTION_NV, GL_IDENTITY_NV);
        glEnable(GL_VERTEX_PROGRAM_NV);
    }
}

static void
myReshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)
        glOrtho(-4.0, 4.0, -4.0 * (GLfloat) h / (GLfloat) w,
            4.0 * (GLfloat) h / (GLfloat) w, -4.0, 4.0);
    else
        glOrtho(-4.0 * (GLfloat) w / (GLfloat) h,
            4.0 * (GLfloat) w / (GLfloat) h, -4.0, 4.0, -4.0, 4.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

static void
key(unsigned char k, int x, int y)
{
  switch (k) {
  case 27:  /* Escape */
    exit(0);
    break;
  default:
    return;
  }
  glutPostRedisplay();
}

int
main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowPosition(0, 0);
    glutCreateWindow(argv[0]);
    glewInit();
    myinit(argc, argv);
    glutReshapeFunc(myReshape);
    glutDisplayFunc(display);
    glutKeyboardFunc(key);
    glutMainLoop();
    return 0;             /* ANSI C requires main to return int. */
}