erebus

annotate src/main.cc @ 25:6ef4b10fa468

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