2 * Test glFramebufferBlit()
17 static int WinWidth
= 1100, WinHeight
= 600;
19 static int SrcWidth
= 512, SrcHeight
= 512;
20 static int DstWidth
= 512, DstHeight
= 512;
22 static GLuint SrcFB
, DstFB
;
23 static GLuint SrcTex
, DstTex
;
26 static GLenum SrcTexTarget
= GL_TEXTURE_2D
, SrcTexFace
= GL_TEXTURE_2D
;
28 static GLenum SrcTexTarget
= GL_TEXTURE_CUBE_MAP
, SrcTexFace
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
31 static GLenum DstTexTarget
= GL_TEXTURE_2D
, DstTexFace
= GL_TEXTURE_2D
;
33 static GLuint SrcTexLevel
= 01, DstTexLevel
= 0;
39 GLboolean rp
= GL_FALSE
;
41 GLint srcWidth
= SrcWidth
>> SrcTexLevel
;
42 GLint srcHeight
= SrcHeight
>> SrcTexLevel
;
43 GLint dstWidth
= DstWidth
>> DstTexLevel
;
44 GLint dstHeight
= DstHeight
>> DstTexLevel
;
48 glBindFramebufferEXT(GL_FRAMEBUFFER
, 0);
49 glClearColor(0.5, 0.5, 0.5, 1.0);
50 glClear(GL_COLOR_BUFFER_BIT
);
54 glBindFramebufferEXT(GL_FRAMEBUFFER
, SrcFB
);
55 status
= glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
56 assert(status
== GL_FRAMEBUFFER_COMPLETE_EXT
);
57 glClearColor(0, 1, 0, 0);
58 glClear(GL_COLOR_BUFFER_BIT
);
61 glBindFramebufferEXT(GL_FRAMEBUFFER
, DstFB
);
62 status
= glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
63 assert(status
== GL_FRAMEBUFFER_COMPLETE_EXT
);
64 glClearColor(1, 0, 0, 0);
65 glClear(GL_COLOR_BUFFER_BIT
);
68 glBindFramebufferEXT(GL_READ_FRAMEBUFFER
, SrcFB
);
69 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER
, DstFB
);
70 glBlitFramebufferEXT(0, 0, srcWidth
, srcHeight
,
71 0, 0, dstWidth
, dstHeight
,
72 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
75 /* read src results */
76 buf
= malloc(4 * srcWidth
* srcHeight
);
77 memset(buf
, 0x88, 4 * srcWidth
* srcHeight
);
78 glBindFramebufferEXT(GL_FRAMEBUFFER
, SrcFB
);
80 glReadPixels(0, 0, srcWidth
, srcHeight
, GL_RGBA
, GL_UNSIGNED_BYTE
, buf
);
82 glBindTexture(SrcTexTarget
, SrcTex
);
83 glGetTexImage(SrcTexFace
, SrcTexLevel
, GL_RGBA
, GL_UNSIGNED_BYTE
, buf
);
86 /* draw dst in window */
87 glBindFramebufferEXT(GL_FRAMEBUFFER
, 0);
89 glDrawPixels(srcWidth
, srcHeight
, GL_RGBA
, GL_UNSIGNED_BYTE
, buf
);
91 printf("Src Pix[0] = %d %d %d %d\n", buf
[0], buf
[1], buf
[2], buf
[3]);
97 /* read dst results */
98 buf
= malloc(4 * dstWidth
* dstHeight
);
99 memset(buf
, 0x88, 4 * dstWidth
* dstHeight
);
100 glBindFramebufferEXT(GL_FRAMEBUFFER
, DstFB
);
102 glReadPixels(0, 0, dstWidth
, dstHeight
, GL_RGBA
, GL_UNSIGNED_BYTE
, buf
);
104 glBindTexture(DstTexTarget
, DstTex
);
105 glGetTexImage(DstTexFace
, DstTexLevel
, GL_RGBA
, GL_UNSIGNED_BYTE
, buf
);
108 /* draw dst in window */
109 glBindFramebufferEXT(GL_FRAMEBUFFER
, 0);
110 glWindowPos2i(srcWidth
+ 2, 0);
111 glDrawPixels(dstWidth
, dstHeight
, GL_RGBA
, GL_UNSIGNED_BYTE
, buf
);
113 printf("Dst Pix[0] = %d %d %d %d\n", buf
[0], buf
[1], buf
[2], buf
[3]);
123 Reshape(int width
, int height
)
127 glViewport(0, 0, width
, height
);
128 glMatrixMode(GL_PROJECTION
);
130 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
131 glMatrixMode(GL_MODELVIEW
);
133 glTranslatef(0.0, 0.0, -15.0);
138 Key(unsigned char key
, int x
, int y
)
144 glutDestroyWindow(Win
);
153 SpecialKey(int key
, int x
, int y
)
169 glGenTextures(1, &SrcTex
);
170 glBindTexture(SrcTexTarget
, SrcTex
);
174 for (lvl
= 0; ; lvl
++) {
175 if (SrcTexTarget
== GL_TEXTURE_CUBE_MAP
) {
177 for (f
= 0; f
< 6; f
++) {
178 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ f
, lvl
, GL_RGBA8
,
179 w
, h
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
184 glTexImage2D(SrcTexFace
, lvl
, GL_RGBA8
, w
, h
, 0,
185 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
187 if (w
== 1 && h
== 1)
195 glGenFramebuffersEXT(1, &SrcFB
);
196 glBindFramebufferEXT(GL_FRAMEBUFFER
, SrcFB
);
197 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
198 SrcTexFace
, SrcTex
, SrcTexLevel
);
201 glGenTextures(1, &DstTex
);
202 glBindTexture(DstTexTarget
, DstTex
);
206 for (lvl
= 0; ; lvl
++) {
207 glTexImage2D(DstTexFace
, lvl
, GL_RGBA8
, w
, h
, 0,
208 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
209 if (w
== 1 && h
== 1)
217 glGenFramebuffersEXT(1, &DstFB
);
218 glBindFramebufferEXT(GL_FRAMEBUFFER
, DstFB
);
219 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
220 DstTexFace
, DstTex
, DstTexLevel
);
227 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
228 fprintf(stderr
, "This test requires GL_EXT_framebuffer_object\n");
232 if (!glutExtensionSupported("GL_EXT_framebuffer_blit")) {
233 fprintf(stderr
, "This test requires GL_EXT_framebuffer_blit,\n");
239 printf("Left rect = src FBO, Right rect = dst FBO.\n");
240 printf("Both should be green.\n");
245 main(int argc
, char *argv
[])
247 glutInit(&argc
, argv
);
248 glutInitWindowSize(WinWidth
, WinHeight
);
249 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
250 Win
= glutCreateWindow(argv
[0]);
252 glutReshapeFunc(Reshape
);
253 glutKeyboardFunc(Key
);
254 glutSpecialFunc(SpecialKey
);
255 glutDisplayFunc(Draw
);