erebus

view src/main.cc @ 8:e2d9bf168a41

semi-works ...
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 24 May 2014 06:12:57 +0300
parents 9621beb22694
children d38e13d6063c
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) / 2;
48 height = glutGet(GLUT_WINDOW_HEIGHT) / 2;
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 glEnable(GL_TEXTURE_2D);
66 return true;
67 }
69 static void cleanup()
70 {
71 erb_destroy(erb);
72 }
74 static void resize_rtarget(int xsz, int ysz)
75 {
76 static unsigned char *defpix;
78 width = xsz / 2;
79 height = ysz / 2;
81 if(width <= rtex_width && height <= rtex_height) {
82 return;
83 }
84 rtex_width = next_pow2(width);
85 rtex_height = next_pow2(height);
87 printf("resizing framebuffer texture: %dx%d\n", rtex_width, rtex_height);
89 if(!rtex) {
90 glGenTextures(1, &rtex);
91 }
93 delete [] defpix;
94 defpix = new unsigned char[rtex_width * rtex_height * 4];
95 unsigned char *ptr = defpix;
96 for(int i=0; i<rtex_height; i++) {
97 for(int j=0; j<rtex_width; j++) {
98 bool chess = ((i >> 4) & 1) == ((j >> 4) & 1);
100 int val = chess ? 64 : 48;
102 *ptr++ = val;
103 *ptr++ = val;
104 *ptr++ = val;
105 *ptr++ = 255;
106 }
107 }
109 glBindTexture(GL_TEXTURE_2D, rtex);
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
112 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, rtex_width, rtex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, defpix);
113 }
115 static void update_rect(int x, int y, int xsz, int ysz, float *pixels)
116 {
117 glBindTexture(GL_TEXTURE_2D, rtex);
118 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, xsz, ysz, GL_RGBA, GL_FLOAT, pixels);
119 }
121 static void idle()
122 {
123 glutPostRedisplay();
124 }
126 static void display()
127 {
128 if(render_pending) {
129 if(erb_render(erb, 64) == 0) {
130 render_pending = false;
131 glutIdleFunc(0);
132 }
133 update_rect(0, 0, width, height, erb_get_framebuffer(erb));
134 }
136 float maxu = (float)width / (float)rtex_width;
137 float maxv = (float)height / (float)rtex_height;
139 glBegin(GL_QUADS);
140 glTexCoord2f(0, maxv); glVertex2f(-1, -1);
141 glTexCoord2f(maxu, maxv); glVertex2f(1, -1);
142 glTexCoord2f(maxu, 0); glVertex2f(1, 1);
143 glTexCoord2f(0, 0); glVertex2f(-1, 1);
144 glEnd();
146 glutSwapBuffers();
147 assert(glGetError() == GL_NO_ERROR);
148 }
150 static void reshape(int x, int y)
151 {
152 glViewport(0, 0, x, y);
153 resize_rtarget(x, y);
155 erb_setopti(erb, ERB_OPT_WIDTH, width);
156 erb_setopti(erb, ERB_OPT_HEIGHT, height);
157 }
159 static void keyb(unsigned char key, int x, int y)
160 {
161 switch(key) {
162 case 27:
163 exit(0);
165 case ' ':
166 printf("begin rendering\n");
167 render_pending = true;
168 glutIdleFunc(idle);
169 erb_begin_frame(erb, 0);
170 break;
171 }
172 }
174 static void mouse(int bn, int st, int x, int y)
175 {
176 }
178 static int next_pow2(int x)
179 {
180 int res = 2;
181 while(res < x) {
182 res <<= 1;
183 }
184 return res;
185 }