erebus
view src/main.cc @ 5:9621beb22694
huh?
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 24 May 2014 02:20:44 +0300 |
parents | 93894c232d65 |
children | e2d9bf168a41 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "opengl.h"
5 #include "erebus.h"
7 static bool init();
8 static void cleanup();
9 static void resize_rtarget(int xsz, int ysz);
10 static void update_rect(int x, int y, int xsz, int ysz, float *pixels);
11 static void idle();
12 static void display();
13 static void reshape(int x, int y);
14 static void keyb(unsigned char key, int x, int y);
15 static void mouse(int bn, int st, int x, int y);
16 static int next_pow2(int x);
18 static int width, height, rtex_width, rtex_height;
19 static unsigned int rtex;
21 static erebus *erb;
22 static bool render_pending;
25 int main(int argc, char **argv)
26 {
27 glutInitWindowSize(1024, 600);
28 glutInit(&argc, argv);
29 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
30 glutCreateWindow("erebus OpenGL frontend");
32 glutDisplayFunc(display);
33 glutReshapeFunc(reshape);
34 glutKeyboardFunc(keyb);
35 glutMouseFunc(mouse);
37 if(!init()) {
38 return 1;
39 }
40 atexit(cleanup);
42 glutMainLoop();
43 }
45 static bool init()
46 {
47 width = glutGet(GLUT_WINDOW_WIDTH);
48 height = glutGet(GLUT_WINDOW_HEIGHT);
50 if(!(erb = erb_init())) {
51 return false;
52 }
53 erb_setopti(erb, ERB_OPT_WIDTH, width);
54 erb_setopti(erb, ERB_OPT_HEIGHT, height);
56 if(erb_load_scene(erb, "scene") == -1) {
57 return false;
58 }
60 printf("begin rendering\n");
61 render_pending = true;
62 glutIdleFunc(idle);
63 erb_begin_frame(erb, 0);
65 return true;
66 }
68 static void cleanup()
69 {
70 erb_destroy(erb);
71 }
73 static void resize_rtarget(int xsz, int ysz)
74 {
75 static unsigned char *defpix;
77 width = xsz;
78 height = ysz;
80 if(xsz <= rtex_width && ysz <= rtex_height) {
81 return;
82 }
83 rtex_width = next_pow2(xsz);
84 rtex_height = next_pow2(ysz);
86 printf("resizing framebuffer texture: %dx%d\n", rtex_width, rtex_height);
88 if(!rtex) {
89 glGenTextures(1, &rtex);
90 }
92 delete [] defpix;
93 defpix = new unsigned char[rtex_width * rtex_height * 4];
94 unsigned char *ptr = defpix;
95 for(int i=0; i<rtex_height; i++) {
96 for(int j=0; j<rtex_width; j++) {
97 bool chess = ((i >> 4) & 1) == ((j >> 4) & 1);
99 int val = chess ? 64 : 48;
101 *ptr++ = val;
102 *ptr++ = val;
103 *ptr++ = val;
104 *ptr++ = 255;
105 }
106 }
108 glBindTexture(GL_TEXTURE_2D, rtex);
109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
111 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, rtex_width, rtex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, defpix);
112 }
114 static void update_rect(int x, int y, int xsz, int ysz, float *pixels)
115 {
116 glBindTexture(GL_TEXTURE_2D, rtex);
117 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, xsz, ysz, GL_RGBA, GL_FLOAT, pixels);
118 }
120 static void idle()
121 {
122 glutPostRedisplay();
123 }
125 static void display()
126 {
127 if(render_pending) {
128 if(erb_render(erb, 128) == 0) {
129 render_pending = false;
130 glutIdleFunc(0);
131 }
132 update_rect(0, 0, width, height, erb_get_framebuffer(erb));
133 }
135 glBindTexture(GL_TEXTURE_2D, rtex);
136 glEnable(GL_TEXTURE_2D);
138 float maxu = (float)width / (float)rtex_width;
139 float maxv = (float)height / (float)rtex_height;
141 glBegin(GL_QUADS);
142 glTexCoord2f(0, maxv); glVertex2f(-1, -1);
143 glTexCoord2f(maxu, maxv); glVertex2f(1, -1);
144 glTexCoord2f(maxu, 0); glVertex2f(1, 1);
145 glTexCoord2f(0, 0); glVertex2f(-1, 1);
146 glEnd();
148 glDisable(GL_TEXTURE_2D);
150 glutSwapBuffers();
151 assert(glGetError() == GL_NO_ERROR);
152 }
154 static void reshape(int x, int y)
155 {
156 glViewport(0, 0, x, y);
157 resize_rtarget(x, y);
159 erb_setopti(erb, ERB_OPT_WIDTH, width);
160 erb_setopti(erb, ERB_OPT_HEIGHT, height);
161 }
163 static void keyb(unsigned char key, int x, int y)
164 {
165 switch(key) {
166 case 27:
167 exit(0);
169 case ' ':
170 printf("begin rendering\n");
171 render_pending = true;
172 glutIdleFunc(idle);
173 erb_begin_frame(erb, 0);
174 break;
175 }
176 }
178 static void mouse(int bn, int st, int x, int y)
179 {
180 }
182 static int next_pow2(int x)
183 {
184 int res = 2;
185 while(res < x) {
186 res <<= 1;
187 }
188 return res;
189 }