erebus

view src/main.cc @ 2:474a0244f57d

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