132001f49Smrg
232001f49Smrg#include <assert.h>
332001f49Smrg#include <stdio.h>
432001f49Smrg#include <stdlib.h>
532001f49Smrg#include <math.h>
632001f49Smrg#include <string.h>
732001f49Smrg
832001f49Smrg#include <GL/glew.h>
932001f49Smrg#include "glut_wrap.h"
1032001f49Smrg
1132001f49Smrg
1232001f49Smrg
1332001f49Smrgstatic GLenum TexTarget = GL_TEXTURE_2D;
1432001f49Smrgstatic int TexWidth = 512, TexHeight = 512;
1532001f49Smrgstatic GLenum TexIntFormat = GL_RGBA; /* either GL_RGB or GL_RGBA */
1632001f49Smrg
1732001f49Smrgstatic int Width = 512, Height = 512;
1832001f49Smrgstatic GLuint MyFB, TexObj;
1932001f49Smrg
2032001f49Smrg
2132001f49Smrg#define CheckError() assert(glGetError() == 0)
2232001f49Smrg
2332001f49SmrgGLenum doubleBuffer;
2432001f49Smrg
2532001f49Smrgstatic void Init(void)
2632001f49Smrg{
2732001f49Smrg   fprintf(stderr, "GL_RENDERER   = %s\n", (char *) glGetString(GL_RENDERER));
2832001f49Smrg   fprintf(stderr, "GL_VERSION    = %s\n", (char *) glGetString(GL_VERSION));
2932001f49Smrg   fprintf(stderr, "GL_VENDOR     = %s\n", (char *) glGetString(GL_VENDOR));
3032001f49Smrg   fflush(stderr);
3132001f49Smrg
3232001f49Smrg
3332001f49Smrg   if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
3432001f49Smrg      printf("GL_EXT_framebuffer_object not found!\n");
3532001f49Smrg      exit(0);
3632001f49Smrg   }
3732001f49Smrg
3832001f49Smrg
3932001f49Smrg   glGenFramebuffersEXT(1, &MyFB);
4032001f49Smrg   glGenTextures(1, &TexObj);
4132001f49Smrg
4232001f49Smrg   /* Make texture object/image */
4332001f49Smrg   glBindTexture(TexTarget, TexObj);
4432001f49Smrg   glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4532001f49Smrg   glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4632001f49Smrg   glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, 0);
4732001f49Smrg   glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, 0);
4832001f49Smrg
4932001f49Smrg   glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0,
5032001f49Smrg                GL_RGBA, GL_UNSIGNED_BYTE, NULL);
5132001f49Smrg
5232001f49Smrg
5332001f49Smrg   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
5432001f49Smrg
5532001f49Smrg
5632001f49Smrg
5732001f49Smrg   {
5832001f49Smrg      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
5932001f49Smrg
6032001f49Smrg      glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
6132001f49Smrg				TexTarget, TexObj, 0);
6232001f49Smrg
6332001f49Smrg
6432001f49Smrg      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
6532001f49Smrg   }
6632001f49Smrg
6732001f49Smrg}
6832001f49Smrg
6932001f49Smrg
7032001f49Smrg
7132001f49Smrgstatic void
7232001f49SmrgReshape( int width, int height )
7332001f49Smrg{
7432001f49Smrg   glViewport( 0, 0, width, height );
7532001f49Smrg   glMatrixMode( GL_PROJECTION );
7632001f49Smrg   glLoadIdentity();
7732001f49Smrg   glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
7832001f49Smrg   glMatrixMode( GL_MODELVIEW );
7932001f49Smrg
8032001f49Smrg   Width = width;
8132001f49Smrg   Height = height;
8232001f49Smrg}
8332001f49Smrg
8432001f49Smrgstatic void Key(unsigned char key, int x, int y)
8532001f49Smrg{
8632001f49Smrg
8732001f49Smrg    switch (key) {
8832001f49Smrg      case 27:
8932001f49Smrg	exit(1);
9032001f49Smrg      default:
9132001f49Smrg	break;
9232001f49Smrg    }
9332001f49Smrg
9432001f49Smrg    glutPostRedisplay();
9532001f49Smrg}
9632001f49Smrg
9732001f49Smrg
9832001f49Smrg
9932001f49Smrgstatic void Draw( void )
10032001f49Smrg{
10132001f49Smrg
10232001f49Smrg
10332001f49Smrg   /* draw to texture image */
10432001f49Smrg   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
10532001f49Smrg   /* glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT); */
10632001f49Smrg   /* glReadBuffer(GL_COLOR_ATTACHMENT1_EXT); */
10732001f49Smrg
10832001f49Smrg
10932001f49Smrg   glViewport(0, 0, TexWidth, TexHeight);
11032001f49Smrg   CheckError();
11132001f49Smrg
11232001f49Smrg   glClearColor(0.5, 0.5, 1.0, 0.0);
11332001f49Smrg   glClear(GL_COLOR_BUFFER_BIT);
11432001f49Smrg   CheckError();
11532001f49Smrg
11632001f49Smrg   if (0) {
11732001f49Smrg      glBegin(GL_TRIANGLES);
11832001f49Smrg      glColor3f(0,0,.7);
11932001f49Smrg      glVertex3f( 0.9, -0.9, -30.0);
12032001f49Smrg      glColor3f(.8,0,0);
12132001f49Smrg      glVertex3f( 0.9,  0.9, -30.0);
12232001f49Smrg      glColor3f(0,.9,0);
12332001f49Smrg      glVertex3f(-0.9,  0.0, -30.0);
12432001f49Smrg      glEnd();
12532001f49Smrg   }
12632001f49Smrg
12732001f49Smrg   {
12832001f49Smrg      GLubyte *buffer = malloc(Width * Height * 4);
12932001f49Smrg
13032001f49Smrg      /* read from user framebuffer */
13132001f49Smrg      glReadPixels(0, 0, Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
13232001f49Smrg      CheckError();
13332001f49Smrg
13432001f49Smrg      /* draw to window */
13532001f49Smrg      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
13632001f49Smrg      glViewport(0, 0, Width, Height);
13732001f49Smrg
13832001f49Smrg      /* Try to clear the window, but will overwrite:
13932001f49Smrg       */
14032001f49Smrg      glClearColor(0.8, 0.8, 0, 0.0);
14132001f49Smrg      glClear(GL_COLOR_BUFFER_BIT);
14232001f49Smrg
14332001f49Smrg      glWindowPos2iARB(30, 30);
14432001f49Smrg      glDrawPixels(Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
14532001f49Smrg
14632001f49Smrg      free(buffer);
14732001f49Smrg   }
14832001f49Smrg
14932001f49Smrg   /* Bind normal framebuffer */
15032001f49Smrg   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
15132001f49Smrg   glViewport(0, 0, Width, Height);
15232001f49Smrg
15332001f49Smrg   if (0) {
15432001f49Smrg      glBegin(GL_TRIANGLES);
15532001f49Smrg      glColor3f(0,.7,0);
15632001f49Smrg      glVertex3f( 0.5, -0.5, -30.0);
15732001f49Smrg      glColor3f(0,0,.8);
15832001f49Smrg      glVertex3f( 0.5,  0.5, -30.0);
15932001f49Smrg      glColor3f(.9,0,0);
16032001f49Smrg      glVertex3f(-0.5,  0.0, -30.0);
16132001f49Smrg      glEnd();
16232001f49Smrg   }
16332001f49Smrg
16432001f49Smrg   if (doubleBuffer) {
16532001f49Smrg      glutSwapBuffers();
16632001f49Smrg   } else {
16732001f49Smrg      glFlush();
16832001f49Smrg   }
16932001f49Smrg
17032001f49Smrg   CheckError();
17132001f49Smrg}
17232001f49Smrg
17332001f49Smrg
17432001f49Smrgstatic GLenum Args(int argc, char **argv)
17532001f49Smrg{
17632001f49Smrg    GLint i;
17732001f49Smrg
17832001f49Smrg    doubleBuffer = GL_FALSE;
17932001f49Smrg
18032001f49Smrg    for (i = 1; i < argc; i++) {
18132001f49Smrg        if (strcmp(argv[i], "-sb") == 0) {
18232001f49Smrg	    doubleBuffer = GL_FALSE;
18332001f49Smrg	} else if (strcmp(argv[i], "-db") == 0) {
18432001f49Smrg	    doubleBuffer = GL_TRUE;
18532001f49Smrg	} else {
18632001f49Smrg	    fprintf(stderr, "%s (Bad option).\n", argv[i]);
18732001f49Smrg	    return GL_FALSE;
18832001f49Smrg	}
18932001f49Smrg    }
19032001f49Smrg    return GL_TRUE;
19132001f49Smrg}
19232001f49Smrg
19332001f49Smrg
19432001f49Smrg
19532001f49Smrgint
19632001f49Smrgmain( int argc, char *argv[] )
19732001f49Smrg{
19832001f49Smrg    GLenum type;
19932001f49Smrg
20032001f49Smrg    glutInit(&argc, argv);
20132001f49Smrg
20232001f49Smrg    if (Args(argc, argv) == GL_FALSE) {
20332001f49Smrg	exit(1);
20432001f49Smrg    }
20532001f49Smrg
20632001f49Smrg    glutInitWindowPosition(100, 0); glutInitWindowSize( Width, Height );
20732001f49Smrg
20832001f49Smrg    type = GLUT_RGB;
20932001f49Smrg    type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
21032001f49Smrg    glutInitDisplayMode(type);
21132001f49Smrg
21232001f49Smrg    if (glutCreateWindow(argv[0]) == GL_FALSE) {
21332001f49Smrg	exit(1);
21432001f49Smrg    }
21532001f49Smrg
21632001f49Smrg    glewInit();
21732001f49Smrg
21832001f49Smrg    Init();
21932001f49Smrg
22032001f49Smrg    glutReshapeFunc(Reshape);
22132001f49Smrg    glutKeyboardFunc(Key);
22232001f49Smrg    glutDisplayFunc(Draw);
22332001f49Smrg    glutMainLoop();
22432001f49Smrg    return 0;
22532001f49Smrg}
226