doorbell

diff spycam/src/spycam.cc @ 0:a5755687dd75

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 08 Mar 2016 03:26:01 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/spycam/src/spycam.cc	Tue Mar 08 03:26:01 2016 +0200
     1.3 @@ -0,0 +1,98 @@
     1.4 +#include <GLES2/gl2.h>
     1.5 +#include <gmath/gmath.h>
     1.6 +#include "spycam.h"
     1.7 +#include "sdr.h"
     1.8 +
     1.9 +static unsigned int sdr_prog, tex;
    1.10 +static Mat4 world_matrix, view_matrix, proj_matrix;
    1.11 +static int win_width, win_height;
    1.12 +
    1.13 +bool app_init()
    1.14 +{
    1.15 +	glClearColor(0.2, 0.2, 0.2, 1);
    1.16 +
    1.17 +	if(!(sdr_prog = create_program_load("sdr/vertex.glsl", "sdr/pixel.glsl"))) {
    1.18 +		fprintf(stderr, "failed to load shader program\n");
    1.19 +		return false;
    1.20 +	}
    1.21 +	glUseProgram(sdr_prog);
    1.22 +	glBindAttribLocation(sdr_prog, 0, "a_vertex");
    1.23 +	glBindAttribLocation(sdr_prog, 1, "a_texcoord");
    1.24 +
    1.25 +	unsigned char *pixels = new unsigned char[256 * 256 * 3];
    1.26 +	unsigned char *pptr = pixels;
    1.27 +	for(int i=0; i<256; i++) {
    1.28 +		for(int j=0; j<256; j++) {
    1.29 +			bool chess = ((i >> 4) & 1) == ((j >> 4) & 1);
    1.30 +			*pptr++ = chess ? 255 : 64;
    1.31 +			*pptr++ = 128;
    1.32 +			*pptr++ = chess ? 64 : 255;
    1.33 +		}
    1.34 +	}
    1.35 +
    1.36 +	glGenTextures(1, &tex);
    1.37 +	glBindTexture(GL_TEXTURE_2D, tex);
    1.38 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    1.39 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.40 +	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
    1.41 +	glGenerateMipmap(GL_TEXTURE_2D);
    1.42 +	delete [] pixels;
    1.43 +
    1.44 +	return true;
    1.45 +}
    1.46 +
    1.47 +void app_cleanup()
    1.48 +{
    1.49 +}
    1.50 +
    1.51 +void app_draw()
    1.52 +{
    1.53 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.54 +
    1.55 +	glUseProgram(sdr_prog);
    1.56 +	set_uniform_matrix4(sdr_prog, "world_mat", world_matrix[0]);
    1.57 +	set_uniform_matrix4(sdr_prog, "view_mat", view_matrix[0]);
    1.58 +	set_uniform_matrix4(sdr_prog, "proj_mat", proj_matrix[0]);
    1.59 +
    1.60 +	glBindTexture(GL_TEXTURE_2D, tex);
    1.61 +
    1.62 +	static const float quad_verts[] = {-1, -1, 1, -1, 1, 1, -1, 1};
    1.63 +	static const float quad_texcoords[] = {0, 0, 1, 0, 1, 1, 0, 1};
    1.64 +	static const unsigned int quad_idx[] = {0, 1, 2, 0, 2, 3};
    1.65 +
    1.66 +	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, quad_verts);
    1.67 +	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, quad_texcoords);
    1.68 +	glEnableVertexAttribArray(0);
    1.69 +	glEnableVertexAttribArray(1);
    1.70 +
    1.71 +	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, quad_idx);
    1.72 +
    1.73 +	glDisableVertexAttribArray(0);
    1.74 +	glDisableVertexAttribArray(1);
    1.75 +}
    1.76 +
    1.77 +void app_reshape(int x, int y)
    1.78 +{
    1.79 +	win_width = x;
    1.80 +	win_height = y;
    1.81 +	glViewport(0, 0, x, y);
    1.82 +}
    1.83 +
    1.84 +void app_keyboard(int key, bool press)
    1.85 +{
    1.86 +	if(press) {
    1.87 +		switch(key) {
    1.88 +		case 27:
    1.89 +			app_quit();
    1.90 +			break;
    1.91 +		}
    1.92 +	}
    1.93 +}
    1.94 +
    1.95 +void app_mouse_button(int bn, bool press, int x, int y)
    1.96 +{
    1.97 +}
    1.98 +
    1.99 +void app_mouse_motion(int x, int y)
   1.100 +{
   1.101 +}