erebus

view src/main.cc @ 4:93894c232d65

more changes across the board
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 29 Apr 2014 07:38:40 +0300
parents 474a0244f57d
children 9621beb22694
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 if(!(erb = erb_init())) {
48 return false;
49 }
50 erb_setopti(erb, ERB_OPT_WIDTH, width);
51 erb_setopti(erb, ERB_OPT_HEIGHT, height);
53 if(erb_load_scene(erb, "scene") == -1) {
54 return false;
55 }
57 printf("begin rendering\n");
58 render_pending = true;
59 glutIdleFunc(idle);
60 erb_begin_frame(erb, 0);
62 return true;
63 }
65 static void cleanup()
66 {
67 erb_destroy(erb);
68 }
70 static void resize_rtarget(int xsz, int ysz)
71 {
72 static unsigned char *defpix;
74 width = xsz;
75 height = ysz;
77 if(xsz <= rtex_width && ysz <= rtex_height) {
78 return;
79 }
80 rtex_width = next_pow2(xsz);
81 rtex_height = next_pow2(ysz);
83 printf("resizing framebuffer texture: %dx%d\n", rtex_width, rtex_height);
85 if(!rtex) {
86 glGenTextures(1, &rtex);
87 }
89 delete [] defpix;
90 defpix = new unsigned char[rtex_width * rtex_height * 4];
91 unsigned char *ptr = defpix;
92 for(int i=0; i<rtex_height; i++) {
93 for(int j=0; j<rtex_width; j++) {
94 bool chess = ((i >> 4) & 1) == ((j >> 4) & 1);
96 int val = chess ? 64 : 48;
98 *ptr++ = val;
99 *ptr++ = val;
100 *ptr++ = val;
101 *ptr++ = 255;
102 }
103 }
105 glBindTexture(GL_TEXTURE_2D, rtex);
106 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
107 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
108 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, rtex_width, rtex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, defpix);
109 }
111 static void update_rect(int x, int y, int xsz, int ysz, float *pixels)
112 {
113 glBindTexture(GL_TEXTURE_2D, rtex);
114 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, xsz, ysz, GL_RGBA, GL_FLOAT, pixels);
115 }
117 static void idle()
118 {
119 glutPostRedisplay();
120 }
122 static void display()
123 {
124 if(render_pending) {
125 if(erb_render(erb, 128) == 0) {
126 render_pending = false;
127 glutIdleFunc(0);
128 }
129 update_rect(0, 0, width, height, erb_get_framebuffer(erb));
130 }
132 glBindTexture(GL_TEXTURE_2D, rtex);
133 glEnable(GL_TEXTURE_2D);
135 float maxu = (float)width / (float)rtex_width;
136 float maxv = (float)height / (float)rtex_height;
138 glBegin(GL_QUADS);
139 glTexCoord2f(0, maxv); glVertex2f(-1, -1);
140 glTexCoord2f(maxu, maxv); glVertex2f(1, -1);
141 glTexCoord2f(maxu, 0); glVertex2f(1, 1);
142 glTexCoord2f(0, 0); glVertex2f(-1, 1);
143 glEnd();
145 glDisable(GL_TEXTURE_2D);
147 glutSwapBuffers();
148 assert(glGetError() == GL_NO_ERROR);
149 }
151 static void reshape(int x, int y)
152 {
153 glViewport(0, 0, x, y);
154 resize_rtarget(x, y);
156 erb_setopti(erb, ERB_OPT_WIDTH, width);
157 erb_setopti(erb, ERB_OPT_HEIGHT, height);
158 }
160 static void keyb(unsigned char key, int x, int y)
161 {
162 switch(key) {
163 case 27:
164 exit(0);
166 case ' ':
167 printf("begin rendering\n");
168 render_pending = true;
169 glutIdleFunc(idle);
170 erb_begin_frame(erb, 0);
171 break;
172 }
173 }
175 static void mouse(int bn, int st, int x, int y)
176 {
177 }
179 static int next_pow2(int x)
180 {
181 int res = 2;
182 while(res < x) {
183 res <<= 1;
184 }
185 return res;
186 }