~ubuntu-branches/ubuntu/oneiric/mesa-demos/oneiric

« back to all changes in this revision

Viewing changes to src/trivial/tri-fbo-tex.c

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2010-09-27 16:18:27 UTC
  • Revision ID: james.westby@ubuntu.com-20100927161827-1yfgolc1oy9sjhi8
Tags: upstream-8.0.1
ImportĀ upstreamĀ versionĀ 8.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Framebuffer object test */
 
2
 
 
3
 
 
4
#include <GL/glew.h>
 
5
#include <GL/glut.h>
 
6
#include <assert.h>
 
7
#include <stdio.h>
 
8
#include <stdlib.h>
 
9
 
 
10
/* For debug */
 
11
 
 
12
 
 
13
static int Win = 0;
 
14
static int Width = 512, Height = 512;
 
15
 
 
16
static GLenum TexTarget = GL_TEXTURE_2D;
 
17
static int TexWidth = 512, TexHeight = 512;
 
18
 
 
19
static GLuint MyFB;
 
20
static GLuint TexObj;
 
21
static GLboolean Anim = GL_FALSE;
 
22
static GLfloat Rot = 0.0;
 
23
static GLuint TextureLevel = 0;  /* which texture level to render to */
 
24
static GLenum TexIntFormat = GL_RGB; /* either GL_RGB or GL_RGBA */
 
25
 
 
26
 
 
27
static void
 
28
CheckError(int line)
 
29
{
 
30
   GLenum err = glGetError();
 
31
   if (err) {
 
32
      printf("GL Error 0x%x at line %d\n", (int) err, line);
 
33
   }
 
34
}
 
35
 
 
36
 
 
37
static void
 
38
Idle(void)
 
39
{
 
40
   Rot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
 
41
   glutPostRedisplay();
 
42
}
 
43
 
 
44
 
 
45
static void
 
46
RenderTexture(void)
 
47
{
 
48
   GLenum status;
 
49
 
 
50
   glMatrixMode(GL_PROJECTION);
 
51
   glLoadIdentity();
 
52
   glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
 
53
   glMatrixMode(GL_MODELVIEW);
 
54
   glLoadIdentity();
 
55
   glTranslatef(0.0, 0.0, -15.0);
 
56
 
 
57
   if (1) {
 
58
      /* draw to texture image */
 
59
      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
 
60
 
 
61
      status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
 
62
      if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
 
63
         printf("Framebuffer incomplete!!!\n");
 
64
      }
 
65
 
 
66
      glViewport(0, 0, TexWidth, TexHeight);
 
67
 
 
68
      glClearColor(0.5, 0.5, 1.0, 0.0);
 
69
      glClear(GL_COLOR_BUFFER_BIT);
 
70
      
 
71
      CheckError(__LINE__);
 
72
 
 
73
      glBegin(GL_POLYGON);
 
74
      glColor3f(1, 0, 0);
 
75
      glVertex2f(-1, -1);
 
76
      glColor3f(0, 1, 0);
 
77
      glVertex2f(1, -1);
 
78
      glColor3f(0, 0, 1);
 
79
      glVertex2f(0, 1);
 
80
      glEnd();
 
81
 
 
82
      /* Bind normal framebuffer */
 
83
      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
 
84
   }
 
85
   else {
 
86
   }
 
87
 
 
88
   CheckError(__LINE__);
 
89
}
 
90
 
 
91
 
 
92
 
 
93
static void
 
94
Display(void)
 
95
{
 
96
   float ar = (float) Width / (float) Height;
 
97
 
 
98
   RenderTexture();
 
99
   
 
100
   /* draw textured quad in the window */
 
101
   glMatrixMode(GL_PROJECTION);
 
102
   glLoadIdentity();
 
103
   glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0);
 
104
   glMatrixMode(GL_MODELVIEW);
 
105
   glLoadIdentity();
 
106
   glTranslatef(0.0, 0.0, -7.0);
 
107
 
 
108
   glViewport(0, 0, Width, Height);
 
109
 
 
110
   glClearColor(0.25, 0.25, 0.25, 0);
 
111
   glClear(GL_COLOR_BUFFER_BIT);
 
112
 
 
113
   glPushMatrix();
 
114
   glRotatef(Rot, 0, 1, 0);
 
115
   glEnable(TexTarget);
 
116
   glBindTexture(TexTarget, TexObj);
 
117
 
 
118
   {
 
119
      glBegin(GL_POLYGON);
 
120
      glColor3f(0.25, 0.25, 0.25);
 
121
      glTexCoord2f(0, 0);
 
122
      glVertex2f(-1, -1);
 
123
      glTexCoord2f(1, 0);
 
124
      glVertex2f(1, -1);
 
125
      glColor3f(1.0, 1.0, 1.0);
 
126
      glTexCoord2f(1, 1);
 
127
      glVertex2f(1, 1);
 
128
      glTexCoord2f(0, 1);
 
129
      glVertex2f(-1, 1);
 
130
      glEnd();
 
131
   }
 
132
 
 
133
   glPopMatrix();
 
134
   glDisable(TexTarget);
 
135
 
 
136
   glutSwapBuffers();
 
137
   CheckError(__LINE__);
 
138
}
 
139
 
 
140
 
 
141
static void
 
142
Reshape(int width, int height)
 
143
{
 
144
   glViewport(0, 0, width, height);
 
145
   Width = width;
 
146
   Height = height;
 
147
}
 
148
 
 
149
 
 
150
static void
 
151
CleanUp(void)
 
152
{
 
153
   glDeleteFramebuffersEXT(1, &MyFB);
 
154
 
 
155
   glDeleteTextures(1, &TexObj);
 
156
 
 
157
   glutDestroyWindow(Win);
 
158
 
 
159
   exit(0);
 
160
}
 
161
 
 
162
 
 
163
static void
 
164
Key(unsigned char key, int x, int y)
 
165
{
 
166
   (void) x;
 
167
   (void) y;
 
168
   switch (key) {
 
169
      case 'a':
 
170
         Anim = !Anim;
 
171
         if (Anim)
 
172
            glutIdleFunc(Idle);
 
173
         else
 
174
            glutIdleFunc(NULL);
 
175
         break;
 
176
      case 's':
 
177
         Rot += 2.0;
 
178
         break;
 
179
      case 27:
 
180
         CleanUp();
 
181
         break;
 
182
   }
 
183
   glutPostRedisplay();
 
184
}
 
185
 
 
186
 
 
187
static void
 
188
Init(int argc, char *argv[])
 
189
{
 
190
   if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
 
191
      printf("GL_EXT_framebuffer_object not found!\n");
 
192
      exit(0);
 
193
   }
 
194
 
 
195
   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
 
196
 
 
197
 
 
198
   /* Make texture object/image */
 
199
   glGenTextures(1, &TexObj);
 
200
   glBindTexture(TexTarget, TexObj);
 
201
   glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
202
   glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
203
   glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, TextureLevel);
 
204
   glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, TextureLevel);
 
205
 
 
206
   glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0,
 
207
                GL_RGBA, GL_UNSIGNED_BYTE, NULL);
 
208
 
 
209
 
 
210
   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
211
 
 
212
 
 
213
 
 
214
 
 
215
   /* gen framebuffer id, delete it, do some assertions, just for testing */
 
216
   glGenFramebuffersEXT(1, &MyFB);
 
217
   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
 
218
   assert(glIsFramebufferEXT(MyFB));
 
219
 
 
220
 
 
221
   CheckError(__LINE__);
 
222
 
 
223
   /* Render color to texture */
 
224
   glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
 
225
                             TexTarget, TexObj, TextureLevel);
 
226
 
 
227
 
 
228
 
 
229
   CheckError(__LINE__);
 
230
 
 
231
   /* bind regular framebuffer */
 
232
   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
 
233
 
 
234
 
 
235
}
 
236
 
 
237
 
 
238
int
 
239
main(int argc, char *argv[])
 
240
{
 
241
   glutInit(&argc, argv);
 
242
   glutInitWindowPosition(0, 0);
 
243
   glutInitWindowSize(Width, Height);
 
244
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
 
245
   Win = glutCreateWindow(argv[0]);
 
246
   glewInit();
 
247
   glutReshapeFunc(Reshape);
 
248
   glutKeyboardFunc(Key);
 
249
   glutDisplayFunc(Display);
 
250
   if (Anim)
 
251
      glutIdleFunc(Idle);
 
252
   Init(argc, argv);
 
253
   glutMainLoop();
 
254
   return 0;
 
255
}