progs/fp: close file (bug 26559)
[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 fclose(f);
192 }
193
194 static void Reshape(int width, int height)
195 {
196
197 glViewport(0, 0, (GLint)width, (GLint)height);
198
199 glMatrixMode(GL_PROJECTION);
200 glLoadIdentity();
201 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
202 glMatrixMode(GL_MODELVIEW);
203 }
204
205 static void Key(unsigned char key, int x, int y)
206 {
207
208 switch (key) {
209 case 27:
210 exit(1);
211 default:
212 break;
213 }
214
215 glutPostRedisplay();
216 }
217
218 static void Display(void)
219 {
220 glClear(GL_COLOR_BUFFER_BIT);
221
222 glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, 1.0, 1.0, 0.0, 0.0);
223 glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, 0.0, 0.0, 1.0, 1.0);
224 glBegin(GL_TRIANGLES);
225
226 glColor3f(0,0,1);
227 glTexCoord3f(1,1,0);
228 glVertex3f( 0.9, -0.9, -30.0);
229
230 glColor3f(1,0,0);
231 glTexCoord3f(1,-1,0);
232 glVertex3f( 0.9, 0.9, -30.0);
233
234 glColor3f(0,1,0);
235 glTexCoord3f(-1,0,0);
236 glVertex3f(-0.9, 0.0, -30.0);
237 glEnd();
238
239 glFlush();
240 if (show_fps) {
241 ++frame_cnt;
242 glutPostRedisplay();
243 }
244 }
245
246
247 int main(int argc, char **argv)
248 {
249 glutInit(&argc, argv);
250 glutInitWindowPosition(0, 0);
251 glutInitWindowSize(250, 250);
252 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
253 args(argc, argv);
254 glutCreateWindow(filename);
255 glewInit();
256 glutReshapeFunc(Reshape);
257 glutKeyboardFunc(Key);
258 glutDisplayFunc(Display);
259 Init();
260 #ifndef WIN32
261 if (show_fps) {
262 signal(SIGALRM, alarmhandler);
263 alarm(5);
264 }
265 #endif
266 glutMainLoop();
267 return 0;
268 }