erebus

annotate src/main.cc @ 0:4abdce1361b9

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 27 Apr 2014 16:02:47 +0300
parents
children 474a0244f57d
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <assert.h>
nuclear@0 4 #include "opengl.h"
nuclear@0 5
nuclear@0 6 static bool init();
nuclear@0 7 static void cleanup();
nuclear@0 8 static void resize_rtarget(int xsz, int ysz);
nuclear@0 9 static void update_rect(int x, int y, int xsz, int ysz, float *pixels);
nuclear@0 10 static void display();
nuclear@0 11 static void reshape(int x, int y);
nuclear@0 12 static void keyb(unsigned char key, int x, int y);
nuclear@0 13 static void mouse(int bn, int st, int x, int y);
nuclear@0 14 static int next_pow2(int x);
nuclear@0 15
nuclear@0 16 static int width, height, rtex_width, rtex_height;
nuclear@0 17 static unsigned int rtex;
nuclear@0 18
nuclear@0 19 int main(int argc, char **argv)
nuclear@0 20 {
nuclear@0 21 glutInitWindowSize(1024, 600);
nuclear@0 22 glutInit(&argc, argv);
nuclear@0 23 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
nuclear@0 24 glutCreateWindow("erebus OpenGL frontend");
nuclear@0 25
nuclear@0 26 glutDisplayFunc(display);
nuclear@0 27 glutReshapeFunc(reshape);
nuclear@0 28 glutKeyboardFunc(keyb);
nuclear@0 29 glutMouseFunc(mouse);
nuclear@0 30
nuclear@0 31 if(!init()) {
nuclear@0 32 return 1;
nuclear@0 33 }
nuclear@0 34 atexit(cleanup);
nuclear@0 35
nuclear@0 36 glutMainLoop();
nuclear@0 37 }
nuclear@0 38
nuclear@0 39 static bool init()
nuclear@0 40 {
nuclear@0 41 return true;
nuclear@0 42 }
nuclear@0 43
nuclear@0 44 static void cleanup()
nuclear@0 45 {
nuclear@0 46 }
nuclear@0 47
nuclear@0 48 static void resize_rtarget(int xsz, int ysz)
nuclear@0 49 {
nuclear@0 50 static unsigned char *defpix;
nuclear@0 51
nuclear@0 52 width = xsz;
nuclear@0 53 height = ysz;
nuclear@0 54
nuclear@0 55 if(xsz <= rtex_width && ysz <= rtex_height) {
nuclear@0 56 return;
nuclear@0 57 }
nuclear@0 58 rtex_width = next_pow2(xsz);
nuclear@0 59 rtex_height = next_pow2(ysz);
nuclear@0 60
nuclear@0 61 printf("resizing framebuffer texture: %dx%d\n", rtex_width, rtex_height);
nuclear@0 62
nuclear@0 63 if(!rtex) {
nuclear@0 64 glGenTextures(1, &rtex);
nuclear@0 65 }
nuclear@0 66
nuclear@0 67 delete [] defpix;
nuclear@0 68 defpix = new unsigned char[rtex_width * rtex_height * 4];
nuclear@0 69 unsigned char *ptr = defpix;
nuclear@0 70 for(int i=0; i<rtex_height; i++) {
nuclear@0 71 for(int j=0; j<rtex_width; j++) {
nuclear@0 72 bool chess = ((i >> 4) & 1) == ((j >> 4) & 1);
nuclear@0 73
nuclear@0 74 int val = chess ? 64 : 48;
nuclear@0 75
nuclear@0 76 *ptr++ = val;
nuclear@0 77 *ptr++ = val;
nuclear@0 78 *ptr++ = val;
nuclear@0 79 *ptr++ = 255;
nuclear@0 80 }
nuclear@0 81 }
nuclear@0 82
nuclear@0 83 glBindTexture(GL_TEXTURE_2D, rtex);
nuclear@0 84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nuclear@0 85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nuclear@0 86 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, rtex_width, rtex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, defpix);
nuclear@0 87 }
nuclear@0 88
nuclear@0 89 static void update_rect(int x, int y, int xsz, int ysz, float *pixels)
nuclear@0 90 {
nuclear@0 91 glBindTexture(GL_TEXTURE_2D, rtex);
nuclear@0 92 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, xsz, ysz, GL_RGBA, GL_FLOAT, pixels);
nuclear@0 93 }
nuclear@0 94
nuclear@0 95 static void display()
nuclear@0 96 {
nuclear@0 97 glBindTexture(GL_TEXTURE_2D, rtex);
nuclear@0 98 glEnable(GL_TEXTURE_2D);
nuclear@0 99
nuclear@0 100 float maxu = (float)width / (float)rtex_width;
nuclear@0 101 float maxv = (float)height / (float)rtex_height;
nuclear@0 102
nuclear@0 103 glBegin(GL_QUADS);
nuclear@0 104 glTexCoord2f(0, maxv); glVertex2f(-1, -1);
nuclear@0 105 glTexCoord2f(maxu, maxv); glVertex2f(1, -1);
nuclear@0 106 glTexCoord2f(maxu, 0); glVertex2f(1, 1);
nuclear@0 107 glTexCoord2f(0, 0); glVertex2f(-1, 1);
nuclear@0 108 glEnd();
nuclear@0 109
nuclear@0 110 glDisable(GL_TEXTURE_2D);
nuclear@0 111
nuclear@0 112 glutSwapBuffers();
nuclear@0 113 assert(glGetError() == GL_NO_ERROR);
nuclear@0 114 }
nuclear@0 115
nuclear@0 116 static void reshape(int x, int y)
nuclear@0 117 {
nuclear@0 118 glViewport(0, 0, x, y);
nuclear@0 119 resize_rtarget(x, y);
nuclear@0 120 }
nuclear@0 121
nuclear@0 122 static void keyb(unsigned char key, int x, int y)
nuclear@0 123 {
nuclear@0 124 switch(key) {
nuclear@0 125 case 27:
nuclear@0 126 exit(0);
nuclear@0 127 }
nuclear@0 128 }
nuclear@0 129
nuclear@0 130 static void mouse(int bn, int st, int x, int y)
nuclear@0 131 {
nuclear@0 132 }
nuclear@0 133
nuclear@0 134 static int next_pow2(int x)
nuclear@0 135 {
nuclear@0 136 int res = 2;
nuclear@0 137 while(res < x) {
nuclear@0 138 res <<= 1;
nuclear@0 139 }
nuclear@0 140 return res;
nuclear@0 141 }