erebus

diff 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 diff
     1.1 --- a/src/main.cc	Mon Apr 28 05:58:22 2014 +0300
     1.2 +++ b/src/main.cc	Mon Apr 28 06:31:10 2014 +0300
     1.3 @@ -1,141 +1,141 @@
     1.4 -#include <stdio.h>
     1.5 -#include <stdlib.h>
     1.6 -#include <assert.h>
     1.7 -#include "opengl.h"
     1.8 -
     1.9 -static bool init();
    1.10 -static void cleanup();
    1.11 -static void resize_rtarget(int xsz, int ysz);
    1.12 -static void update_rect(int x, int y, int xsz, int ysz, float *pixels);
    1.13 -static void display();
    1.14 -static void reshape(int x, int y);
    1.15 -static void keyb(unsigned char key, int x, int y);
    1.16 -static void mouse(int bn, int st, int x, int y);
    1.17 -static int next_pow2(int x);
    1.18 -
    1.19 -static int width, height, rtex_width, rtex_height;
    1.20 -static unsigned int rtex;
    1.21 -
    1.22 -int main(int argc, char **argv)
    1.23 -{
    1.24 -	glutInitWindowSize(1024, 600);
    1.25 -	glutInit(&argc, argv);
    1.26 -	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    1.27 -	glutCreateWindow("erebus OpenGL frontend");
    1.28 -
    1.29 -	glutDisplayFunc(display);
    1.30 -	glutReshapeFunc(reshape);
    1.31 -	glutKeyboardFunc(keyb);
    1.32 -	glutMouseFunc(mouse);
    1.33 -
    1.34 -	if(!init()) {
    1.35 -		return 1;
    1.36 -	}
    1.37 -	atexit(cleanup);
    1.38 -
    1.39 -	glutMainLoop();
    1.40 -}
    1.41 -
    1.42 -static bool init()
    1.43 -{
    1.44 -	return true;
    1.45 -}
    1.46 -
    1.47 -static void cleanup()
    1.48 -{
    1.49 -}
    1.50 -
    1.51 -static void resize_rtarget(int xsz, int ysz)
    1.52 -{
    1.53 -	static unsigned char *defpix;
    1.54 -
    1.55 -	width = xsz;
    1.56 -	height = ysz;
    1.57 -
    1.58 -	if(xsz <= rtex_width && ysz <= rtex_height) {
    1.59 -		return;
    1.60 -	}
    1.61 -	rtex_width = next_pow2(xsz);
    1.62 -	rtex_height = next_pow2(ysz);
    1.63 -
    1.64 -	printf("resizing framebuffer texture: %dx%d\n", rtex_width, rtex_height);
    1.65 -
    1.66 -	if(!rtex) {
    1.67 -		glGenTextures(1, &rtex);
    1.68 -	}
    1.69 -
    1.70 -	delete [] defpix;
    1.71 -	defpix = new unsigned char[rtex_width * rtex_height * 4];
    1.72 -	unsigned char *ptr = defpix;
    1.73 -	for(int i=0; i<rtex_height; i++) {
    1.74 -		for(int j=0; j<rtex_width; j++) {
    1.75 -			bool chess = ((i >> 4) & 1) == ((j >> 4) & 1);
    1.76 -
    1.77 -			int val = chess ? 64 : 48;
    1.78 -
    1.79 -			*ptr++ = val;
    1.80 -			*ptr++ = val;
    1.81 -			*ptr++ = val;
    1.82 -			*ptr++ = 255;
    1.83 -		}
    1.84 -	}
    1.85 -
    1.86 -	glBindTexture(GL_TEXTURE_2D, rtex);
    1.87 -	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    1.88 -	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    1.89 -	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, rtex_width, rtex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, defpix);
    1.90 -}
    1.91 -
    1.92 -static void update_rect(int x, int y, int xsz, int ysz, float *pixels)
    1.93 -{
    1.94 -	glBindTexture(GL_TEXTURE_2D, rtex);
    1.95 -	glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, xsz, ysz, GL_RGBA, GL_FLOAT, pixels);
    1.96 -}
    1.97 -
    1.98 -static void display()
    1.99 -{
   1.100 -	glBindTexture(GL_TEXTURE_2D, rtex);
   1.101 -	glEnable(GL_TEXTURE_2D);
   1.102 -
   1.103 -	float maxu = (float)width / (float)rtex_width;
   1.104 -	float maxv = (float)height / (float)rtex_height;
   1.105 -
   1.106 -	glBegin(GL_QUADS);
   1.107 -	glTexCoord2f(0, maxv); glVertex2f(-1, -1);
   1.108 -	glTexCoord2f(maxu, maxv); glVertex2f(1, -1);
   1.109 -	glTexCoord2f(maxu, 0); glVertex2f(1, 1);
   1.110 -	glTexCoord2f(0, 0); glVertex2f(-1, 1);
   1.111 -	glEnd();
   1.112 -
   1.113 -	glDisable(GL_TEXTURE_2D);
   1.114 -
   1.115 -	glutSwapBuffers();
   1.116 -	assert(glGetError() == GL_NO_ERROR);
   1.117 -}
   1.118 -
   1.119 -static void reshape(int x, int y)
   1.120 -{
   1.121 -	glViewport(0, 0, x, y);
   1.122 -	resize_rtarget(x, y);
   1.123 -}
   1.124 -
   1.125 -static void keyb(unsigned char key, int x, int y)
   1.126 -{
   1.127 -	switch(key) {
   1.128 -	case 27:
   1.129 -		exit(0);
   1.130 -	}
   1.131 -}
   1.132 -
   1.133 -static void mouse(int bn, int st, int x, int y)
   1.134 -{
   1.135 -}
   1.136 -
   1.137 -static int next_pow2(int x)
   1.138 -{
   1.139 -	int res = 2;
   1.140 -	while(res < x) {
   1.141 -		res <<= 1;
   1.142 -	}
   1.143 -	return res;
   1.144 -}
   1.145 \ No newline at end of file
   1.146 +#include <stdio.h>
   1.147 +#include <stdlib.h>
   1.148 +#include <assert.h>
   1.149 +#include "opengl.h"
   1.150 +
   1.151 +static bool init();
   1.152 +static void cleanup();
   1.153 +static void resize_rtarget(int xsz, int ysz);
   1.154 +static void update_rect(int x, int y, int xsz, int ysz, float *pixels);
   1.155 +static void display();
   1.156 +static void reshape(int x, int y);
   1.157 +static void keyb(unsigned char key, int x, int y);
   1.158 +static void mouse(int bn, int st, int x, int y);
   1.159 +static int next_pow2(int x);
   1.160 +
   1.161 +static int width, height, rtex_width, rtex_height;
   1.162 +static unsigned int rtex;
   1.163 +
   1.164 +int main(int argc, char **argv)
   1.165 +{
   1.166 +	glutInitWindowSize(1024, 600);
   1.167 +	glutInit(&argc, argv);
   1.168 +	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
   1.169 +	glutCreateWindow("erebus OpenGL frontend");
   1.170 +
   1.171 +	glutDisplayFunc(display);
   1.172 +	glutReshapeFunc(reshape);
   1.173 +	glutKeyboardFunc(keyb);
   1.174 +	glutMouseFunc(mouse);
   1.175 +
   1.176 +	if(!init()) {
   1.177 +		return 1;
   1.178 +	}
   1.179 +	atexit(cleanup);
   1.180 +
   1.181 +	glutMainLoop();
   1.182 +}
   1.183 +
   1.184 +static bool init()
   1.185 +{
   1.186 +	return true;
   1.187 +}
   1.188 +
   1.189 +static void cleanup()
   1.190 +{
   1.191 +}
   1.192 +
   1.193 +static void resize_rtarget(int xsz, int ysz)
   1.194 +{
   1.195 +	static unsigned char *defpix;
   1.196 +
   1.197 +	width = xsz;
   1.198 +	height = ysz;
   1.199 +
   1.200 +	if(xsz <= rtex_width && ysz <= rtex_height) {
   1.201 +		return;
   1.202 +	}
   1.203 +	rtex_width = next_pow2(xsz);
   1.204 +	rtex_height = next_pow2(ysz);
   1.205 +
   1.206 +	printf("resizing framebuffer texture: %dx%d\n", rtex_width, rtex_height);
   1.207 +
   1.208 +	if(!rtex) {
   1.209 +		glGenTextures(1, &rtex);
   1.210 +	}
   1.211 +
   1.212 +	delete [] defpix;
   1.213 +	defpix = new unsigned char[rtex_width * rtex_height * 4];
   1.214 +	unsigned char *ptr = defpix;
   1.215 +	for(int i=0; i<rtex_height; i++) {
   1.216 +		for(int j=0; j<rtex_width; j++) {
   1.217 +			bool chess = ((i >> 4) & 1) == ((j >> 4) & 1);
   1.218 +
   1.219 +			int val = chess ? 64 : 48;
   1.220 +
   1.221 +			*ptr++ = val;
   1.222 +			*ptr++ = val;
   1.223 +			*ptr++ = val;
   1.224 +			*ptr++ = 255;
   1.225 +		}
   1.226 +	}
   1.227 +
   1.228 +	glBindTexture(GL_TEXTURE_2D, rtex);
   1.229 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   1.230 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   1.231 +	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, rtex_width, rtex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, defpix);
   1.232 +}
   1.233 +
   1.234 +static void update_rect(int x, int y, int xsz, int ysz, float *pixels)
   1.235 +{
   1.236 +	glBindTexture(GL_TEXTURE_2D, rtex);
   1.237 +	glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, xsz, ysz, GL_RGBA, GL_FLOAT, pixels);
   1.238 +}
   1.239 +
   1.240 +static void display()
   1.241 +{
   1.242 +	glBindTexture(GL_TEXTURE_2D, rtex);
   1.243 +	glEnable(GL_TEXTURE_2D);
   1.244 +
   1.245 +	float maxu = (float)width / (float)rtex_width;
   1.246 +	float maxv = (float)height / (float)rtex_height;
   1.247 +
   1.248 +	glBegin(GL_QUADS);
   1.249 +	glTexCoord2f(0, maxv); glVertex2f(-1, -1);
   1.250 +	glTexCoord2f(maxu, maxv); glVertex2f(1, -1);
   1.251 +	glTexCoord2f(maxu, 0); glVertex2f(1, 1);
   1.252 +	glTexCoord2f(0, 0); glVertex2f(-1, 1);
   1.253 +	glEnd();
   1.254 +
   1.255 +	glDisable(GL_TEXTURE_2D);
   1.256 +
   1.257 +	glutSwapBuffers();
   1.258 +	assert(glGetError() == GL_NO_ERROR);
   1.259 +}
   1.260 +
   1.261 +static void reshape(int x, int y)
   1.262 +{
   1.263 +	glViewport(0, 0, x, y);
   1.264 +	resize_rtarget(x, y);
   1.265 +}
   1.266 +
   1.267 +static void keyb(unsigned char key, int x, int y)
   1.268 +{
   1.269 +	switch(key) {
   1.270 +	case 27:
   1.271 +		exit(0);
   1.272 +	}
   1.273 +}
   1.274 +
   1.275 +static void mouse(int bn, int st, int x, int y)
   1.276 +{
   1.277 +}
   1.278 +
   1.279 +static int next_pow2(int x)
   1.280 +{
   1.281 +	int res = 2;
   1.282 +	while(res < x) {
   1.283 +		res <<= 1;
   1.284 +	}
   1.285 +	return res;
   1.286 +}