vp-tri-cb-tex.c revision 32001f49
1/* Test glGenProgramsNV(), glIsProgramNV(), glLoadProgramNV() */
2
3#include <stdio.h>
4#include <assert.h>
5#include <string.h>
6#include <stdlib.h>
7#include <math.h>
8#include <GL/glew.h>
9#include "glut_wrap.h"
10
11
12
13GLenum doubleBuffer;
14
15static void Init(void)
16{
17   GLint errno;
18   GLuint prognum;
19
20   static const char *prog1 =
21      "!!ARBvp1.0\n"
22      "PARAM Emission = state.material.emission; \n"
23      "PARAM Ambient = state.material.ambient; \n"
24      "PARAM Diffuse = state.material.diffuse; \n"
25      "PARAM Specular = state.material.specular; \n"
26      "DP4  result.position.x, Ambient, vertex.position;\n"
27      "DP4  result.position.y, Diffuse, vertex.position;\n"
28      "DP4  result.position.z, Specular, vertex.position;\n"
29      "DP4  result.position.w, Emission, vertex.position;\n"
30      "MOV  result.texcoord[0], vertex.texcoord[0];\n"
31      "END\n";
32
33   const float Ambient[4] = { 0.0, 1.0, 0.0, 0.0 };
34   const float Diffuse[4] = { 1.0, 0.0, 0.0, 0.0 };
35   const float Specular[4] = { 0.0, 0.0, 1.0, 0.0 };
36   const float Emission[4] = { 0.0, 0.0, 0.0, 1.0 };
37   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, Ambient);
38   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Diffuse);
39   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Specular);
40   glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Emission);
41
42
43   glGenProgramsARB(1, &prognum);
44
45   glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum);
46   glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
47		      strlen(prog1), (const GLubyte *) prog1);
48
49   assert(glIsProgramARB(prognum));
50   errno = glGetError();
51   printf("glGetError = %d\n", errno);
52   if (errno != GL_NO_ERROR)
53   {
54      GLint errorpos;
55
56      glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
57      printf("errorpos: %d\n", errorpos);
58      printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB));
59   }
60
61   glEnable(GL_VERTEX_PROGRAM_NV);
62
63#define SIZE 32
64   {
65      GLubyte tex2d[SIZE][SIZE][3];
66      GLint s, t;
67
68      for (s = 0; s < SIZE; s++) {
69	 for (t = 0; t < SIZE; t++) {
70#if 0
71	    tex2d[t][s][0] = (s < SIZE/2) ? 0 : 255;
72	    tex2d[t][s][1] = (t < SIZE/2) ? 0 : 255;
73	    tex2d[t][s][2] = 0;
74#else
75	    tex2d[t][s][0] = s*255/(SIZE-1);
76	    tex2d[t][s][1] = t*255/(SIZE-1);
77	    tex2d[t][s][2] = 0;
78#endif
79	 }
80      }
81
82      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
83
84      glTexImage2D(GL_TEXTURE_2D, 0, 3, SIZE, SIZE, 0,
85                   GL_RGB, GL_UNSIGNED_BYTE, tex2d);
86
87      glEnable(GL_TEXTURE_2D);
88      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
89      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
90      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
91      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
92
93      glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
94   }
95
96}
97
98
99static void Reshape(int width, int height)
100{
101
102    glViewport(0, 0, (GLint)width, (GLint)height);
103
104    glMatrixMode(GL_PROJECTION);
105    glLoadIdentity();
106/*     glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); */
107    glMatrixMode(GL_MODELVIEW);
108}
109
110static void Key(unsigned char key, int x, int y)
111{
112
113    switch (key) {
114      case 27:
115	exit(1);
116      default:
117	break;
118    }
119
120    glutPostRedisplay();
121}
122
123static void Draw(void)
124{
125   glClear(GL_COLOR_BUFFER_BIT);
126
127   glBegin(GL_TRIANGLES);
128   glTexCoord2f(1,-1);
129   glVertex3f( 0.9, -0.9, -0.0);
130   glTexCoord2f(1,1);
131   glVertex3f( 0.9,  0.9, -0.0);
132   glTexCoord2f(-1,0);
133   glVertex3f(-0.9,  0.0, -0.0);
134   glEnd();
135
136   glFlush();
137
138   if (doubleBuffer) {
139      glutSwapBuffers();
140   }
141}
142
143static GLenum Args(int argc, char **argv)
144{
145    GLint i;
146
147    doubleBuffer = GL_FALSE;
148
149    for (i = 1; i < argc; i++) {
150        if (strcmp(argv[i], "-sb") == 0) {
151	    doubleBuffer = GL_FALSE;
152	} else if (strcmp(argv[i], "-db") == 0) {
153	    doubleBuffer = GL_TRUE;
154	} else {
155	    fprintf(stderr, "%s (Bad option).\n", argv[i]);
156	    return GL_FALSE;
157	}
158    }
159    return GL_TRUE;
160}
161
162int main(int argc, char **argv)
163{
164    GLenum type;
165
166    glutInit(&argc, argv);
167
168    if (Args(argc, argv) == GL_FALSE) {
169	exit(1);
170    }
171
172    glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
173
174    type = GLUT_RGB | GLUT_ALPHA;
175    type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
176    glutInitDisplayMode(type);
177
178    if (glutCreateWindow(*argv) == GL_FALSE) {
179	exit(1);
180    }
181
182    glewInit();
183
184    Init();
185
186    glutReshapeFunc(Reshape);
187    glutKeyboardFunc(Key);
188    glutDisplayFunc(Draw);
189    glutMainLoop();
190	return 0;
191}
192