erebus

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main.cc	Sun Apr 27 16:02:47 2014 +0300
     1.3 @@ -0,0 +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