rev |
line source |
nuclear@2
|
1 #include <stdio.h>
|
nuclear@2
|
2 #include <stdlib.h>
|
nuclear@2
|
3 #include <assert.h>
|
nuclear@2
|
4 #include "opengl.h"
|
nuclear@2
|
5
|
nuclear@2
|
6 static bool init();
|
nuclear@2
|
7 static void cleanup();
|
nuclear@2
|
8 static void resize_rtarget(int xsz, int ysz);
|
nuclear@2
|
9 static void update_rect(int x, int y, int xsz, int ysz, float *pixels);
|
nuclear@2
|
10 static void display();
|
nuclear@2
|
11 static void reshape(int x, int y);
|
nuclear@2
|
12 static void keyb(unsigned char key, int x, int y);
|
nuclear@2
|
13 static void mouse(int bn, int st, int x, int y);
|
nuclear@2
|
14 static int next_pow2(int x);
|
nuclear@2
|
15
|
nuclear@2
|
16 static int width, height, rtex_width, rtex_height;
|
nuclear@2
|
17 static unsigned int rtex;
|
nuclear@2
|
18
|
nuclear@2
|
19 int main(int argc, char **argv)
|
nuclear@2
|
20 {
|
nuclear@2
|
21 glutInitWindowSize(1024, 600);
|
nuclear@2
|
22 glutInit(&argc, argv);
|
nuclear@2
|
23 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
|
nuclear@2
|
24 glutCreateWindow("erebus OpenGL frontend");
|
nuclear@2
|
25
|
nuclear@2
|
26 glutDisplayFunc(display);
|
nuclear@2
|
27 glutReshapeFunc(reshape);
|
nuclear@2
|
28 glutKeyboardFunc(keyb);
|
nuclear@2
|
29 glutMouseFunc(mouse);
|
nuclear@2
|
30
|
nuclear@2
|
31 if(!init()) {
|
nuclear@2
|
32 return 1;
|
nuclear@2
|
33 }
|
nuclear@2
|
34 atexit(cleanup);
|
nuclear@2
|
35
|
nuclear@2
|
36 glutMainLoop();
|
nuclear@2
|
37 }
|
nuclear@2
|
38
|
nuclear@2
|
39 static bool init()
|
nuclear@2
|
40 {
|
nuclear@2
|
41 return true;
|
nuclear@2
|
42 }
|
nuclear@2
|
43
|
nuclear@2
|
44 static void cleanup()
|
nuclear@2
|
45 {
|
nuclear@2
|
46 }
|
nuclear@2
|
47
|
nuclear@2
|
48 static void resize_rtarget(int xsz, int ysz)
|
nuclear@2
|
49 {
|
nuclear@2
|
50 static unsigned char *defpix;
|
nuclear@2
|
51
|
nuclear@2
|
52 width = xsz;
|
nuclear@2
|
53 height = ysz;
|
nuclear@2
|
54
|
nuclear@2
|
55 if(xsz <= rtex_width && ysz <= rtex_height) {
|
nuclear@2
|
56 return;
|
nuclear@2
|
57 }
|
nuclear@2
|
58 rtex_width = next_pow2(xsz);
|
nuclear@2
|
59 rtex_height = next_pow2(ysz);
|
nuclear@2
|
60
|
nuclear@2
|
61 printf("resizing framebuffer texture: %dx%d\n", rtex_width, rtex_height);
|
nuclear@2
|
62
|
nuclear@2
|
63 if(!rtex) {
|
nuclear@2
|
64 glGenTextures(1, &rtex);
|
nuclear@2
|
65 }
|
nuclear@2
|
66
|
nuclear@2
|
67 delete [] defpix;
|
nuclear@2
|
68 defpix = new unsigned char[rtex_width * rtex_height * 4];
|
nuclear@2
|
69 unsigned char *ptr = defpix;
|
nuclear@2
|
70 for(int i=0; i<rtex_height; i++) {
|
nuclear@2
|
71 for(int j=0; j<rtex_width; j++) {
|
nuclear@2
|
72 bool chess = ((i >> 4) & 1) == ((j >> 4) & 1);
|
nuclear@2
|
73
|
nuclear@2
|
74 int val = chess ? 64 : 48;
|
nuclear@2
|
75
|
nuclear@2
|
76 *ptr++ = val;
|
nuclear@2
|
77 *ptr++ = val;
|
nuclear@2
|
78 *ptr++ = val;
|
nuclear@2
|
79 *ptr++ = 255;
|
nuclear@2
|
80 }
|
nuclear@2
|
81 }
|
nuclear@2
|
82
|
nuclear@2
|
83 glBindTexture(GL_TEXTURE_2D, rtex);
|
nuclear@2
|
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
nuclear@2
|
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
nuclear@2
|
86 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, rtex_width, rtex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, defpix);
|
nuclear@2
|
87 }
|
nuclear@2
|
88
|
nuclear@2
|
89 static void update_rect(int x, int y, int xsz, int ysz, float *pixels)
|
nuclear@2
|
90 {
|
nuclear@2
|
91 glBindTexture(GL_TEXTURE_2D, rtex);
|
nuclear@2
|
92 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, xsz, ysz, GL_RGBA, GL_FLOAT, pixels);
|
nuclear@2
|
93 }
|
nuclear@2
|
94
|
nuclear@2
|
95 static void display()
|
nuclear@2
|
96 {
|
nuclear@2
|
97 glBindTexture(GL_TEXTURE_2D, rtex);
|
nuclear@2
|
98 glEnable(GL_TEXTURE_2D);
|
nuclear@2
|
99
|
nuclear@2
|
100 float maxu = (float)width / (float)rtex_width;
|
nuclear@2
|
101 float maxv = (float)height / (float)rtex_height;
|
nuclear@2
|
102
|
nuclear@2
|
103 glBegin(GL_QUADS);
|
nuclear@2
|
104 glTexCoord2f(0, maxv); glVertex2f(-1, -1);
|
nuclear@2
|
105 glTexCoord2f(maxu, maxv); glVertex2f(1, -1);
|
nuclear@2
|
106 glTexCoord2f(maxu, 0); glVertex2f(1, 1);
|
nuclear@2
|
107 glTexCoord2f(0, 0); glVertex2f(-1, 1);
|
nuclear@2
|
108 glEnd();
|
nuclear@2
|
109
|
nuclear@2
|
110 glDisable(GL_TEXTURE_2D);
|
nuclear@2
|
111
|
nuclear@2
|
112 glutSwapBuffers();
|
nuclear@2
|
113 assert(glGetError() == GL_NO_ERROR);
|
nuclear@2
|
114 }
|
nuclear@2
|
115
|
nuclear@2
|
116 static void reshape(int x, int y)
|
nuclear@2
|
117 {
|
nuclear@2
|
118 glViewport(0, 0, x, y);
|
nuclear@2
|
119 resize_rtarget(x, y);
|
nuclear@2
|
120 }
|
nuclear@2
|
121
|
nuclear@2
|
122 static void keyb(unsigned char key, int x, int y)
|
nuclear@2
|
123 {
|
nuclear@2
|
124 switch(key) {
|
nuclear@2
|
125 case 27:
|
nuclear@2
|
126 exit(0);
|
nuclear@2
|
127 }
|
nuclear@2
|
128 }
|
nuclear@2
|
129
|
nuclear@2
|
130 static void mouse(int bn, int st, int x, int y)
|
nuclear@2
|
131 {
|
nuclear@2
|
132 }
|
nuclear@2
|
133
|
nuclear@2
|
134 static int next_pow2(int x)
|
nuclear@2
|
135 {
|
nuclear@2
|
136 int res = 2;
|
nuclear@2
|
137 while(res < x) {
|
nuclear@2
|
138 res <<= 1;
|
nuclear@2
|
139 }
|
nuclear@2
|
140 return res;
|
nuclear@2
|
141 }
|