erebus

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