13 static GLenum TexTarget
= GL_TEXTURE_2D
;
14 static int TexWidth
= 512, TexHeight
= 512;
15 static GLenum TexIntFormat
= GL_RGBA
; /* either GL_RGB or GL_RGBA */
17 static int Width
= 512, Height
= 512;
18 static GLuint MyFB
, TexObj
;
21 #define CheckError() assert(glGetError() == 0)
25 static void Init(void)
27 fprintf(stderr
, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
28 fprintf(stderr
, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
29 fprintf(stderr
, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
33 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
34 printf("GL_EXT_framebuffer_object not found!\n");
39 glGenFramebuffersEXT(1, &MyFB
);
40 glGenTextures(1, &TexObj
);
42 /* Make texture object/image */
43 glBindTexture(TexTarget
, TexObj
);
44 glTexParameteri(TexTarget
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
45 glTexParameteri(TexTarget
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
46 glTexParameteri(TexTarget
, GL_TEXTURE_BASE_LEVEL
, 0);
47 glTexParameteri(TexTarget
, GL_TEXTURE_MAX_LEVEL
, 0);
49 glTexImage2D(TexTarget
, 0, TexIntFormat
, TexWidth
, TexHeight
, 0,
50 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
53 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
58 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
60 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
61 TexTarget
, TexObj
, 0);
64 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
72 Reshape( int width
, int height
)
74 glViewport( 0, 0, width
, height
);
75 glMatrixMode( GL_PROJECTION
);
77 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
78 glMatrixMode( GL_MODELVIEW
);
84 static void Key(unsigned char key
, int x
, int y
)
99 static void Draw( void )
103 /* draw to texture image */
104 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
105 /* glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT); */
106 /* glReadBuffer(GL_COLOR_ATTACHMENT1_EXT); */
109 glViewport(0, 0, TexWidth
, TexHeight
);
112 glClearColor(0.5, 0.5, 1.0, 0.0);
113 glClear(GL_COLOR_BUFFER_BIT
);
117 glBegin(GL_TRIANGLES
);
119 glVertex3f( 0.9, -0.9, -30.0);
121 glVertex3f( 0.9, 0.9, -30.0);
123 glVertex3f(-0.9, 0.0, -30.0);
128 GLubyte
*buffer
= malloc(Width
* Height
* 4);
130 /* read from user framebuffer */
131 glReadPixels(0, 0, Width
-60, Height
-60, GL_RGBA
, GL_UNSIGNED_BYTE
, buffer
);
135 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
136 glViewport(0, 0, Width
, Height
);
138 /* Try to clear the window, but will overwrite:
140 glClearColor(0.8, 0.8, 0, 0.0);
141 glClear(GL_COLOR_BUFFER_BIT
);
143 glWindowPos2iARB(30, 30);
144 glDrawPixels(Width
-60, Height
-60, GL_RGBA
, GL_UNSIGNED_BYTE
, buffer
);
149 /* Bind normal framebuffer */
150 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
151 glViewport(0, 0, Width
, Height
);
154 glBegin(GL_TRIANGLES
);
156 glVertex3f( 0.5, -0.5, -30.0);
158 glVertex3f( 0.5, 0.5, -30.0);
160 glVertex3f(-0.5, 0.0, -30.0);
172 static GLenum
Args(int argc
, char **argv
)
176 doubleBuffer
= GL_FALSE
;
178 for (i
= 1; i
< argc
; i
++) {
179 if (strcmp(argv
[i
], "-sb") == 0) {
180 doubleBuffer
= GL_FALSE
;
181 } else if (strcmp(argv
[i
], "-db") == 0) {
182 doubleBuffer
= GL_TRUE
;
184 fprintf(stderr
, "%s (Bad option).\n", argv
[i
]);
194 main( int argc
, char *argv
[] )
198 glutInit(&argc
, argv
);
200 if (Args(argc
, argv
) == GL_FALSE
) {
204 glutInitWindowPosition(100, 0); glutInitWindowSize( Width
, Height
);
207 type
|= (doubleBuffer
) ? GLUT_DOUBLE
: GLUT_SINGLE
;
208 glutInitDisplayMode(type
);
210 if (glutCreateWindow(argv
[0]) == GL_FALSE
) {
218 glutReshapeFunc(Reshape
);
219 glutKeyboardFunc(Key
);
220 glutDisplayFunc(Draw
);