Keith Whitwell | b1399a5 | 2007-11-25 15:06:54 +0000 | [diff] [blame] | 1 | |
| 2 | #include <assert.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <math.h> |
| 6 | #include <string.h> |
| 7 | |
José Fonseca | 479ea7d | 2009-01-23 14:35:36 +0000 | [diff] [blame] | 8 | #include <GL/glew.h> |
Keith Whitwell | b1399a5 | 2007-11-25 15:06:54 +0000 | [diff] [blame] | 9 | #include <GL/glut.h> |
| 10 | |
| 11 | |
| 12 | |
| 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 */ |
| 16 | |
| 17 | static int Width = 512, Height = 512; |
| 18 | static GLuint MyFB, TexObj; |
| 19 | |
| 20 | |
| 21 | #define CheckError() assert(glGetError() == 0) |
| 22 | |
| 23 | GLenum doubleBuffer; |
| 24 | |
| 25 | static void Init(void) |
| 26 | { |
| 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)); |
Keith Whitwell | eb9801c | 2009-03-24 16:35:29 +0000 | [diff] [blame] | 30 | fflush(stderr); |
Keith Whitwell | b1399a5 | 2007-11-25 15:06:54 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | if (!glutExtensionSupported("GL_EXT_framebuffer_object")) { |
| 34 | printf("GL_EXT_framebuffer_object not found!\n"); |
| 35 | exit(0); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | glGenFramebuffersEXT(1, &MyFB); |
| 40 | glGenTextures(1, &TexObj); |
| 41 | |
| 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); |
| 48 | |
| 49 | glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0, |
| 50 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 51 | |
| 52 | |
| 53 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 54 | |
| 55 | |
| 56 | |
| 57 | { |
| 58 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
| 59 | |
| 60 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, |
| 61 | TexTarget, TexObj, 0); |
| 62 | |
| 63 | |
| 64 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | static void |
| 72 | Reshape( int width, int height ) |
| 73 | { |
| 74 | glViewport( 0, 0, width, height ); |
| 75 | glMatrixMode( GL_PROJECTION ); |
| 76 | glLoadIdentity(); |
| 77 | glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); |
| 78 | glMatrixMode( GL_MODELVIEW ); |
| 79 | |
| 80 | Width = width; |
| 81 | Height = height; |
| 82 | } |
| 83 | |
| 84 | static void Key(unsigned char key, int x, int y) |
| 85 | { |
| 86 | |
| 87 | switch (key) { |
| 88 | case 27: |
| 89 | exit(1); |
| 90 | default: |
Vinson Lee | 34a0b22 | 2009-11-19 13:03:12 -0800 | [diff] [blame] | 91 | break; |
Keith Whitwell | b1399a5 | 2007-11-25 15:06:54 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | glutPostRedisplay(); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | static void Draw( void ) |
| 100 | { |
| 101 | |
| 102 | |
| 103 | /* draw to texture image */ |
| 104 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
Vinson Lee | d87d710 | 2009-12-19 00:28:01 -0800 | [diff] [blame] | 105 | /* glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT); */ |
| 106 | /* glReadBuffer(GL_COLOR_ATTACHMENT1_EXT); */ |
Keith Whitwell | b1399a5 | 2007-11-25 15:06:54 +0000 | [diff] [blame] | 107 | |
| 108 | |
| 109 | glViewport(0, 0, TexWidth, TexHeight); |
| 110 | CheckError(); |
| 111 | |
| 112 | glClearColor(0.5, 0.5, 1.0, 0.0); |
| 113 | glClear(GL_COLOR_BUFFER_BIT); |
| 114 | CheckError(); |
| 115 | |
| 116 | if (0) { |
| 117 | glBegin(GL_TRIANGLES); |
| 118 | glColor3f(0,0,.7); |
| 119 | glVertex3f( 0.9, -0.9, -30.0); |
| 120 | glColor3f(.8,0,0); |
| 121 | glVertex3f( 0.9, 0.9, -30.0); |
| 122 | glColor3f(0,.9,0); |
| 123 | glVertex3f(-0.9, 0.0, -30.0); |
| 124 | glEnd(); |
| 125 | } |
| 126 | |
| 127 | { |
| 128 | GLubyte *buffer = malloc(Width * Height * 4); |
| 129 | |
| 130 | /* read from user framebuffer */ |
| 131 | glReadPixels(0, 0, Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
| 132 | CheckError(); |
| 133 | |
| 134 | /* draw to window */ |
| 135 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 136 | glViewport(0, 0, Width, Height); |
| 137 | |
| 138 | /* Try to clear the window, but will overwrite: |
| 139 | */ |
| 140 | glClearColor(0.8, 0.8, 0, 0.0); |
| 141 | glClear(GL_COLOR_BUFFER_BIT); |
| 142 | |
| 143 | glWindowPos2iARB(30, 30); |
| 144 | glDrawPixels(Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
| 145 | |
| 146 | free(buffer); |
| 147 | } |
| 148 | |
| 149 | /* Bind normal framebuffer */ |
| 150 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 151 | glViewport(0, 0, Width, Height); |
| 152 | |
| 153 | if (0) { |
| 154 | glBegin(GL_TRIANGLES); |
| 155 | glColor3f(0,.7,0); |
| 156 | glVertex3f( 0.5, -0.5, -30.0); |
| 157 | glColor3f(0,0,.8); |
| 158 | glVertex3f( 0.5, 0.5, -30.0); |
| 159 | glColor3f(.9,0,0); |
| 160 | glVertex3f(-0.5, 0.0, -30.0); |
| 161 | glEnd(); |
| 162 | } |
| 163 | |
| 164 | if (doubleBuffer) { |
| 165 | glutSwapBuffers(); |
| 166 | } |
| 167 | |
| 168 | CheckError(); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | static GLenum Args(int argc, char **argv) |
| 173 | { |
| 174 | GLint i; |
| 175 | |
| 176 | doubleBuffer = GL_FALSE; |
| 177 | |
| 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; |
| 183 | } else { |
| 184 | fprintf(stderr, "%s (Bad option).\n", argv[i]); |
| 185 | return GL_FALSE; |
| 186 | } |
| 187 | } |
| 188 | return GL_TRUE; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | int |
| 194 | main( int argc, char *argv[] ) |
| 195 | { |
| 196 | GLenum type; |
| 197 | |
| 198 | glutInit(&argc, argv); |
| 199 | |
| 200 | if (Args(argc, argv) == GL_FALSE) { |
| 201 | exit(1); |
| 202 | } |
| 203 | |
| 204 | glutInitWindowPosition(100, 0); glutInitWindowSize( Width, Height ); |
| 205 | |
| 206 | type = GLUT_RGB; |
| 207 | type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; |
| 208 | glutInitDisplayMode(type); |
| 209 | |
| 210 | if (glutCreateWindow(argv[0]) == GL_FALSE) { |
| 211 | exit(1); |
| 212 | } |
| 213 | |
José Fonseca | 479ea7d | 2009-01-23 14:35:36 +0000 | [diff] [blame] | 214 | glewInit(); |
| 215 | |
Keith Whitwell | b1399a5 | 2007-11-25 15:06:54 +0000 | [diff] [blame] | 216 | Init(); |
| 217 | |
| 218 | glutReshapeFunc(Reshape); |
| 219 | glutKeyboardFunc(Key); |
| 220 | glutDisplayFunc(Draw); |
| 221 | glutMainLoop(); |
| 222 | return 0; |
| 223 | } |