Merge branch 'mesa_7_7_branch'
[mesa.git] / progs / fp / fp-tri.c
1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6 #ifndef WIN32
7 #include <unistd.h>
8 #include <signal.h>
9 #endif
10
11 #include <GL/glew.h>
12 #include <GL/glut.h>
13
14 #include "readtex.c"
15
16
17 #define TEXTURE_FILE "../images/bw.rgb"
18
19 unsigned show_fps = 0;
20 unsigned int frame_cnt = 0;
21 void alarmhandler(int);
22 static const char *filename = NULL;
23
24 static void usage(char *name)
25 {
26 fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);
27 #ifndef WIN32
28 fprintf(stderr, "\n" );
29 fprintf(stderr, "options:\n");
30 fprintf(stderr, " -fps show frames per second\n");
31 #endif
32 }
33
34 #ifndef WIN32
35 void alarmhandler (int sig)
36 {
37 if (sig == SIGALRM) {
38 printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt,
39 frame_cnt / 5.0);
40
41 frame_cnt = 0;
42 }
43 signal(SIGALRM, alarmhandler);
44 alarm(5);
45 }
46 #endif
47
48 static void args(int argc, char *argv[])
49 {
50 GLint i;
51
52 for (i = 1; i < argc; i++) {
53 if (strcmp(argv[i], "-fps") == 0) {
54 show_fps = 1;
55 }
56 else if (i == argc - 1) {
57 filename = argv[i];
58 }
59 else {
60 usage(argv[0]);
61 exit(1);
62 }
63 }
64
65 if (!filename) {
66 usage(argv[0]);
67 exit(1);
68 }
69 }
70
71 static void Init( void )
72 {
73 GLuint Texture;
74 GLint errno;
75 GLuint prognum;
76 char buf[50000];
77 GLuint sz;
78 FILE *f;
79
80 if ((f = fopen(filename, "r")) == NULL) {
81 fprintf(stderr, "Couldn't open %s\n", filename);
82 exit(1);
83 }
84
85 sz = fread(buf, 1, sizeof(buf), f);
86 if (!feof(f)) {
87 fprintf(stderr, "file too long\n");
88 exit(1);
89 }
90 fprintf(stderr, "%.*s\n", sz, buf);
91
92 if (!GLEW_ARB_fragment_program) {
93 printf("Error: GL_ARB_fragment_program not supported!\n");
94 exit(1);
95 }
96 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
97
98 /* Setup the fragment program */
99 glGenProgramsARB(1, &prognum);
100 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, prognum);
101 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
102 sz, (const GLubyte *)buf);
103
104 errno = glGetError();
105 printf("glGetError = 0x%x\n", errno);
106 if (errno != GL_NO_ERROR) {
107 GLint errorpos;
108
109 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
110 printf("errorpos: %d\n", errorpos);
111 printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n",
112 (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
113 }
114 glEnable(GL_FRAGMENT_PROGRAM_ARB);
115
116
117 /* Load texture */
118 glGenTextures(1, &Texture);
119 glBindTexture(GL_TEXTURE_2D, Texture);
120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
122 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
123 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
124 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE);
125 exit(1);
126 }
127
128
129 glGenTextures(1, &Texture);
130 glActiveTextureARB(GL_TEXTURE0_ARB + 1);
131 glBindTexture(GL_TEXTURE_2D, Texture);
132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
134 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
135
136 {
137 GLubyte data[32][32];
138 int width = 32;
139 int height = 32;
140 int i;
141 int j;
142
143 for (i = 0; i < 32; i++)
144 for (j = 0; j < 32; j++)
145 {
146 /**
147 ** +-----------+
148 ** | W |
149 ** | +-----+ |
150 ** | | | |
151 ** | | B | |
152 ** | | | |
153 ** | +-----+ |
154 ** | |
155 ** +-----------+
156 **/
157 int i2 = i - height / 2;
158 int j2 = j - width / 2;
159 int h8 = height / 8;
160 int w8 = width / 8;
161 if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) {
162 data[i][j] = 0x00;
163 } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) {
164 data[i][j] = 0x55;
165 } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) {
166 data[i][j] = 0xaa;
167 } else {
168 data[i][j] = 0xff;
169 }
170 }
171
172 glTexImage2D( GL_TEXTURE_2D, 0,
173 GL_ALPHA8,
174 32, 32, 0,
175 GL_ALPHA, GL_UNSIGNED_BYTE, data );
176 }
177
178
179 {
180 const float Ambient[4] = { 0.0, 1.0, 0.0, 0.0 };
181 const float Diffuse[4] = { 1.0, 0.0, 0.0, 0.0 };
182 const float Specular[4] = { 0.0, 0.0, 1.0, 0.0 };
183 const float Emission[4] = { 0.0, 0.0, 0.0, 1.0 };
184 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, Ambient);
185 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Diffuse);
186 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Specular);
187 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Emission);
188 }
189
190 glClearColor(.1, .3, .5, 0);
191 }
192
193 static void Reshape(int width, int height)
194 {
195
196 glViewport(0, 0, (GLint)width, (GLint)height);
197
198 glMatrixMode(GL_PROJECTION);
199 glLoadIdentity();
200 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
201 glMatrixMode(GL_MODELVIEW);
202 }
203
204 static void Key(unsigned char key, int x, int y)
205 {
206
207 switch (key) {
208 case 27:
209 exit(1);
210 default:
211 break;
212 }
213
214 glutPostRedisplay();
215 }
216
217 static void Display(void)
218 {
219 glClear(GL_COLOR_BUFFER_BIT);
220
221 glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, 1.0, 1.0, 0.0, 0.0);
222 glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, 0.0, 0.0, 1.0, 1.0);
223 glBegin(GL_TRIANGLES);
224
225 glColor3f(0,0,1);
226 glTexCoord3f(1,1,0);
227 glVertex3f( 0.9, -0.9, -30.0);
228
229 glColor3f(1,0,0);
230 glTexCoord3f(1,-1,0);
231 glVertex3f( 0.9, 0.9, -30.0);
232
233 glColor3f(0,1,0);
234 glTexCoord3f(-1,0,0);
235 glVertex3f(-0.9, 0.0, -30.0);
236 glEnd();
237
238 glFlush();
239 if (show_fps) {
240 ++frame_cnt;
241 glutPostRedisplay();
242 }
243 }
244
245
246 int main(int argc, char **argv)
247 {
248 glutInit(&argc, argv);
249 glutInitWindowPosition(0, 0);
250 glutInitWindowSize(250, 250);
251 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
252 args(argc, argv);
253 glutCreateWindow(filename);
254 glewInit();
255 glutReshapeFunc(Reshape);
256 glutKeyboardFunc(Key);
257 glutDisplayFunc(Display);
258 Init();
259 #ifndef WIN32
260 if (show_fps) {
261 signal(SIGALRM, alarmhandler);
262 alarm(5);
263 }
264 #endif
265 glutMainLoop();
266 return 0;
267 }