erebus

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