erebus

view src/main.cc @ 19:6204e4d3f445

scene loading
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 27 May 2014 07:43:55 +0300
parents 20d6c05529f1
children e49f4d7ad04c
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <vector>
5 #include "opengl.h"
6 #include "erebus.h"
8 static bool init();
9 static void cleanup();
10 static void resize_rtarget(int xsz, int ysz);
11 static void update_rect(int x, int y, int xsz, int ysz, float *pixels);
12 static void idle();
13 static void display();
14 static void reshape(int x, int y);
15 static void keyb(unsigned char key, int x, int y);
16 static void keyb_up(unsigned char key, int x, int y);
17 static void mouse(int bn, int st, int x, int y);
18 static void motion(int x, int y);
19 static void sball_button(int bn, int st);
20 static void sball_motion(int x, int y, int z);
21 static int next_pow2(int x);
23 static int width, height, rtex_width, rtex_height;
24 static unsigned int rtex;
26 static erebus *erb;
27 static bool render_pending;
29 static std::vector<char*> sfiles;
31 int main(int argc, char **argv)
32 {
33 glutInitWindowSize(1024, 600);
34 glutInit(&argc, argv);
36 for(int i=1; i<argc; i++) {
37 sfiles.push_back(argv[i]);
38 }
40 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
41 glutCreateWindow("erebus OpenGL frontend");
43 glutDisplayFunc(display);
44 glutReshapeFunc(reshape);
45 glutKeyboardFunc(keyb);
46 glutKeyboardUpFunc(keyb_up);
47 glutMouseFunc(mouse);
48 glutMotionFunc(motion);
49 glutSpaceballButtonFunc(sball_button);
50 glutSpaceballMotionFunc(sball_motion);
52 if(!init()) {
53 return 1;
54 }
55 atexit(cleanup);
57 glutMainLoop();
58 }
60 static bool init()
61 {
62 width = glutGet(GLUT_WINDOW_WIDTH) / 2;
63 height = glutGet(GLUT_WINDOW_HEIGHT) / 2;
65 if(!(erb = erb_init())) {
66 return false;
67 }
68 erb_setopti(erb, ERB_OPT_WIDTH, width);
69 erb_setopti(erb, ERB_OPT_HEIGHT, height);
71 for(size_t i=0; i<sfiles.size(); i++) {
72 printf("loading scene file: %s\n", sfiles[i]);
73 if(erb_load_scene(erb, sfiles[i]) == -1) {
74 return false;
75 }
76 }
78 printf("begin rendering\n");
79 render_pending = true;
80 glutIdleFunc(idle);
81 erb_begin_frame(erb, 0);
83 glEnable(GL_TEXTURE_2D);
84 return true;
85 }
87 static void cleanup()
88 {
89 erb_destroy(erb);
90 }
92 static void resize_rtarget(int xsz, int ysz)
93 {
94 static unsigned char *defpix;
96 width = xsz / 2;
97 height = ysz / 2;
99 if(width <= rtex_width && height <= rtex_height) {
100 return;
101 }
102 rtex_width = next_pow2(width);
103 rtex_height = next_pow2(height);
105 printf("resizing framebuffer texture: %dx%d\n", rtex_width, rtex_height);
107 if(!rtex) {
108 glGenTextures(1, &rtex);
109 }
111 delete [] defpix;
112 defpix = new unsigned char[rtex_width * rtex_height * 4];
113 unsigned char *ptr = defpix;
114 for(int i=0; i<rtex_height; i++) {
115 for(int j=0; j<rtex_width; j++) {
116 bool chess = ((i >> 4) & 1) == ((j >> 4) & 1);
118 int val = chess ? 64 : 48;
120 *ptr++ = val;
121 *ptr++ = val;
122 *ptr++ = val;
123 *ptr++ = 255;
124 }
125 }
127 glBindTexture(GL_TEXTURE_2D, rtex);
128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
129 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
130 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, rtex_width, rtex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, defpix);
131 }
133 static void update_rect(int x, int y, int xsz, int ysz, float *pixels)
134 {
135 glBindTexture(GL_TEXTURE_2D, rtex);
136 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, xsz, ysz, GL_RGBA, GL_FLOAT, pixels);
137 }
139 static void idle()
140 {
141 glutPostRedisplay();
142 }
144 static void display()
145 {
146 if(render_pending) {
147 if(erb_render(erb, 64) == 0) {
148 render_pending = false;
149 glutIdleFunc(0);
150 }
151 update_rect(0, 0, width, height, erb_get_framebuffer(erb));
152 }
154 float maxu = (float)width / (float)rtex_width;
155 float maxv = (float)height / (float)rtex_height;
157 glBegin(GL_QUADS);
158 glTexCoord2f(0, maxv); glVertex2f(-1, -1);
159 glTexCoord2f(maxu, maxv); glVertex2f(1, -1);
160 glTexCoord2f(maxu, 0); glVertex2f(1, 1);
161 glTexCoord2f(0, 0); glVertex2f(-1, 1);
162 glEnd();
164 glutSwapBuffers();
165 assert(glGetError() == GL_NO_ERROR);
166 }
168 static void reshape(int x, int y)
169 {
170 glViewport(0, 0, x, y);
171 resize_rtarget(x, y);
173 erb_setopti(erb, ERB_OPT_WIDTH, width);
174 erb_setopti(erb, ERB_OPT_HEIGHT, height);
175 }
177 static void keyb(unsigned char key, int x, int y)
178 {
179 switch(key) {
180 case 27:
181 exit(0);
183 case ' ':
184 printf("begin rendering\n");
185 render_pending = true;
186 glutIdleFunc(idle);
187 erb_begin_frame(erb, 0);
188 break;
189 }
191 if(erb_input_keyboard(erb, key, true)) {
192 glutPostRedisplay();
193 }
194 }
196 static void keyb_up(unsigned char key, int x, int y)
197 {
198 if(erb_input_keyboard(erb, key, false)) {
199 glutPostRedisplay();
200 }
201 }
203 static void mouse(int bn, int st, int x, int y)
204 {
205 if(erb_input_mouse_button(erb, bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y)) {
206 glutPostRedisplay();
207 }
208 }
210 static void motion(int x, int y)
211 {
212 if(erb_input_mouse_motion(erb, x, y)) {
213 glutPostRedisplay();
214 }
215 }
217 static void sball_button(int bn, int state)
218 {
219 if(erb_input_6dof_button(erb, bn, state == GLUT_DOWN)) {
220 glutPostRedisplay();
221 }
222 }
224 static void sball_motion(int x, int y, int z)
225 {
226 if(erb_input_6dof_motion(erb, x / 65536.0, y / 65536.0, z / 65536.0)) {
227 glutPostRedisplay();
228 }
229 }
231 static int next_pow2(int x)
232 {
233 int res = 2;
234 while(res < x) {
235 res <<= 1;
236 }
237 return res;
238 }