demos: add missing binaries to .gitignore
[mesa-demos.git] / src / tests / viewmemory.c
blob16044b93a05184412f543ee2e9eaefca070872f8
1 /**
2 * Try to see other parts of the user's video memory by creating textures
3 * with uninitialized contents. Display those textures in the window.
4 * Insprired by stories of WebGL security issues.
6 * The OpenGL driver should probably initialize all memory allocations
7 * to zero (textures, renderbuffers, buffer objects, etc).
9 * Brian Paul
10 * June 2011
13 #define GL_GLEXT_PROTOTYPES
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <math.h>
18 #include <GL/glew.h>
19 #include "glut_wrap.h"
21 static int Win;
22 static int WinWidth = 1024, WinHeight = 512;
23 static GLboolean Anim = GL_FALSE;
25 static GLuint *Textures;
26 static GLuint TexWidth = 1024, TexHeight = 512;
27 static GLuint NumTextures = 50, CurTexture;
30 static void
31 Idle(void)
33 static int prevTime = 0;
34 int curTime = glutGet(GLUT_ELAPSED_TIME);
36 if (!prevTime) {
37 prevTime = curTime;
39 else {
40 if (curTime - prevTime > 250) {
41 prevTime = curTime;
42 CurTexture = (CurTexture + 1) % NumTextures;
43 glutPostRedisplay();
49 static void
50 MakeTextures(void)
52 GLuint i;
54 Textures = (GLuint *) malloc(NumTextures * sizeof(GLuint));
56 glGenTextures(NumTextures, Textures);
57 for (i = 0; i < NumTextures; i++) {
58 glBindTexture(GL_TEXTURE_2D, Textures[i]);
59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
61 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TexWidth, TexHeight, 0,
62 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
67 static void
68 PrintString(const char *s)
70 while (*s) {
71 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
72 s++;
77 static void
78 Draw(void)
80 char s[100];
82 sprintf(s, "Texture %u", CurTexture);
84 glClear(GL_COLOR_BUFFER_BIT);
86 glEnable(GL_TEXTURE_2D);
87 glBindTexture(GL_TEXTURE_2D, CurTexture);
89 glBegin(GL_POLYGON);
90 glTexCoord2f(0, 0); glVertex2f(0, 0);
91 glTexCoord2f(1, 0); glVertex2f(WinWidth, 0);
92 glTexCoord2f(1, 1); glVertex2f(WinWidth, WinHeight);
93 glTexCoord2f(0, 1); glVertex2f(0, WinHeight);
94 glEnd();
96 glDisable(GL_TEXTURE_2D);
97 glColor3f(0, 1, 0);
98 glWindowPos2iARB(10, 10);
99 PrintString(s);
101 glutSwapBuffers();
105 static void
106 Reshape(int width, int height)
108 WinWidth = width;
109 WinHeight = height;
110 glViewport(0, 0, width, height);
111 glMatrixMode(GL_PROJECTION);
112 glLoadIdentity();
113 glOrtho(0, WinWidth, 0, WinHeight, -1, 1);
114 glMatrixMode(GL_MODELVIEW);
115 glLoadIdentity();
119 static void
120 Key(unsigned char key, int x, int y)
122 switch (key) {
123 case 'a':
124 case 'A':
125 Anim = !Anim;
126 if (Anim)
127 glutIdleFunc(Idle);
128 else
129 glutIdleFunc(NULL);
130 break;
131 case 27:
132 glutDestroyWindow(Win);
133 exit(0);
134 break;
136 glutPostRedisplay();
140 static void
141 SpecialKey(int key, int x, int y)
143 (void) x;
144 (void) y;
145 switch (key) {
146 case GLUT_KEY_LEFT:
147 if (CurTexture > 0)
148 CurTexture--;
149 break;
150 case GLUT_KEY_RIGHT:
151 if (CurTexture + 1 < NumTextures)
152 CurTexture++;
153 break;
155 glutPostRedisplay();
159 static void
160 Init(void)
162 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
163 if (glutExtensionSupported("GL_ARB_robustness"))
164 printf("GL_ARB_robustness supported\n");
165 else
166 printf("GL_ARB_robustness not supported\n");
168 MakeTextures();
169 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
171 printf("Keys:\n");
172 printf(" a: toggle animation\n");
173 printf(" Left/Right: back / next frame\n");
174 printf(" Esc: exit\n");
179 main(int argc, char *argv[])
181 glutInit(&argc, argv);
183 if (argc > 1) {
184 NumTextures = atoi(argv[1]);
185 if (NumTextures < 1) {
186 printf("Invalid argument (number of textures)\n");
187 return 1;
190 printf("Creating %u textures\n", NumTextures);
192 glutInitWindowSize(WinWidth, WinHeight);
193 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
194 Win = glutCreateWindow(argv[0]);
195 glewInit();
196 glutReshapeFunc(Reshape);
197 glutKeyboardFunc(Key);
198 glutSpecialFunc(SpecialKey);
199 glutDisplayFunc(Draw);
201 if (Anim)
202 glutIdleFunc(Idle);
204 Init();
206 glutMainLoop();
207 return 0;