8 #define GL_GLEXT_PROTOTYPES
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
));
32 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
33 printf("GL_EXT_framebuffer_object not found!\n");
38 glGenFramebuffersEXT(1, &MyFB
);
39 glGenTextures(1, &TexObj
);
41 /* Make texture object/image */
42 glBindTexture(TexTarget
, TexObj
);
43 glTexParameteri(TexTarget
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
44 glTexParameteri(TexTarget
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
45 glTexParameteri(TexTarget
, GL_TEXTURE_BASE_LEVEL
, 0);
46 glTexParameteri(TexTarget
, GL_TEXTURE_MAX_LEVEL
, 0);
48 glTexImage2D(TexTarget
, 0, TexIntFormat
, TexWidth
, TexHeight
, 0,
49 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
52 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
57 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
59 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
60 TexTarget
, TexObj
, 0);
63 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
71 Reshape( int width
, int height
)
73 glViewport( 0, 0, width
, height
);
74 glMatrixMode( GL_PROJECTION
);
76 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
77 glMatrixMode( GL_MODELVIEW
);
83 static void Key(unsigned char key
, int x
, int y
)
98 static void Draw( void )
102 /* draw to texture image */
103 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
104 // glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
105 // glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
108 glViewport(0, 0, TexWidth
, TexHeight
);
111 glClearColor(0.5, 0.5, 1.0, 0.0);
112 glClear(GL_COLOR_BUFFER_BIT
);
116 glBegin(GL_TRIANGLES
);
118 glVertex3f( 0.9, -0.9, -30.0);
120 glVertex3f( 0.9, 0.9, -30.0);
122 glVertex3f(-0.9, 0.0, -30.0);
127 GLubyte
*buffer
= malloc(Width
* Height
* 4);
129 /* read from user framebuffer */
130 glReadPixels(0, 0, Width
-60, Height
-60, GL_RGBA
, GL_UNSIGNED_BYTE
, buffer
);
134 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
135 glViewport(0, 0, Width
, Height
);
137 /* Try to clear the window, but will overwrite:
139 glClearColor(0.8, 0.8, 0, 0.0);
140 glClear(GL_COLOR_BUFFER_BIT
);
142 glWindowPos2iARB(30, 30);
143 glDrawPixels(Width
-60, Height
-60, GL_RGBA
, GL_UNSIGNED_BYTE
, buffer
);
148 /* Bind normal framebuffer */
149 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
150 glViewport(0, 0, Width
, Height
);
153 glBegin(GL_TRIANGLES
);
155 glVertex3f( 0.5, -0.5, -30.0);
157 glVertex3f( 0.5, 0.5, -30.0);
159 glVertex3f(-0.5, 0.0, -30.0);
171 static GLenum
Args(int argc
, char **argv
)
175 doubleBuffer
= GL_FALSE
;
177 for (i
= 1; i
< argc
; i
++) {
178 if (strcmp(argv
[i
], "-sb") == 0) {
179 doubleBuffer
= GL_FALSE
;
180 } else if (strcmp(argv
[i
], "-db") == 0) {
181 doubleBuffer
= GL_TRUE
;
183 fprintf(stderr
, "%s (Bad option).\n", argv
[i
]);
193 main( int argc
, char *argv
[] )
197 glutInit(&argc
, argv
);
199 if (Args(argc
, argv
) == GL_FALSE
) {
203 glutInitWindowPosition(100, 0); glutInitWindowSize( Width
, Height
);
206 type
|= (doubleBuffer
) ? GLUT_DOUBLE
: GLUT_SINGLE
;
207 glutInitDisplayMode(type
);
209 if (glutCreateWindow(argv
[0]) == GL_FALSE
) {
215 glutReshapeFunc(Reshape
);
216 glutKeyboardFunc(Key
);
217 glutDisplayFunc(Draw
);