c8705d5052b5b64ea32e2811069123967ff1408f
[mesa.git] / progs / tests / getteximage.c
1 /**
2 * Test glGetTexImage()
3 * Brian Paul
4 * 9 June 2009
5 */
6
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include <GL/glut.h>
12
13 static int Win;
14
15
16 static void
17 TestGetTexImage(void)
18 {
19 GLuint iter;
20
21 for (iter = 0; iter < 20; iter++) {
22 GLint p = (iter % 6) + 4;
23 GLint w = (1 << p);
24 GLint h = (1 << p);
25 GLubyte data[512*512*4];
26 GLubyte data2[512*512*4];
27 GLuint i;
28 GLint level = 0;
29
30 printf("Testing %d x %d tex image\n", w, h);
31 /* fill data */
32 for (i = 0; i < w * h * 4; i++) {
33 data[i] = i & 0xff;
34 data2[i] = 0;
35 }
36
37 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0,
38 GL_RGBA, GL_UNSIGNED_BYTE, data);
39
40 glBegin(GL_POINTS);
41 glVertex2f(0, 0);
42 glEnd();
43
44 /* get */
45 glGetTexImage(GL_TEXTURE_2D, level, GL_RGBA, GL_UNSIGNED_BYTE, data2);
46
47 /* compare */
48 for (i = 0; i < w * h * 4; i++) {
49 if (data2[i] != data[i]) {
50 printf("Failure!\n");
51 abort();
52 }
53 }
54 }
55 printf("Passed\n");
56 glutDestroyWindow(Win);
57 exit(0);
58 }
59
60
61 static void
62 Draw(void)
63 {
64 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
65
66 TestGetTexImage();
67
68 glutSwapBuffers();
69 }
70
71
72 static void
73 Reshape(int width, int height)
74 {
75 glViewport(0, 0, width, height);
76 glMatrixMode(GL_PROJECTION);
77 glLoadIdentity();
78 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
79 glMatrixMode(GL_MODELVIEW);
80 glLoadIdentity();
81 glTranslatef(0.0, 0.0, -15.0);
82 }
83
84
85 static void
86 Key(unsigned char key, int x, int y)
87 {
88 (void) x;
89 (void) y;
90 switch (key) {
91 case 27:
92 glutDestroyWindow(Win);
93 exit(0);
94 break;
95 }
96 glutPostRedisplay();
97 }
98
99
100 static void
101 Init(void)
102 {
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
105 glEnable(GL_TEXTURE_2D);
106 }
107
108
109 int
110 main(int argc, char *argv[])
111 {
112 glutInit(&argc, argv);
113 glutInitWindowPosition(0, 0);
114 glutInitWindowSize(400, 400);
115 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
116 Win = glutCreateWindow(argv[0]);
117 glutReshapeFunc(Reshape);
118 glutKeyboardFunc(Key);
119 glutDisplayFunc(Draw);
120 Init();
121 glutMainLoop();
122 return 0;
123 }