# HG changeset patch # User John Tsiombikas # Date 1398392453 -10800 # Node ID b326d53321f7528af1ad1e0b988bcf9896ca3c58 initial commit diff -r 000000000000 -r b326d53321f7 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,9 @@ +\.o$ +\.swp$ +\.sln$ +\.vcxproj$ +sdf$ +Debug/ +Release/ +\.suo$ +\.dll$ diff -r 000000000000 -r b326d53321f7 data/tiles.png Binary file data/tiles.png has changed diff -r 000000000000 -r b326d53321f7 src/camera.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/camera.cc Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,207 @@ +#include +#include +#include "camera.h" + +static void calc_sample_pos_rec(int sidx, float xsz, float ysz, float *pos); + +Camera::Camera() +{ + vfov = M_PI / 4.0; + cached_matrix_valid = false; + + rdir_cache_width = rdir_cache_height = 0; + rdir_cache = 0; +} + +Camera::Camera(const Vector3 &p) + : pos(p) +{ + vfov = M_PI / 4.0; + cached_matrix_valid = false; + + rdir_cache_width = rdir_cache_height = 0; + rdir_cache = 0; +} + +Camera::~Camera() +{ + delete [] rdir_cache; +} + +void Camera::set_fov(float vfov) +{ + this->vfov = vfov; + + // invalidate the dir cache + delete [] rdir_cache; +} + +float Camera::get_fov() const +{ + return vfov; +} + +void Camera::set_position(const Vector3 &pos) +{ + this->pos = pos; + cached_matrix_valid = false; // invalidate the cached matrix +} + +const Vector3 &Camera::get_position() const +{ + return pos; +} + +const Matrix4x4 &Camera::get_matrix() const +{ + if(!cached_matrix_valid) { + calc_matrix(&cached_matrix); + cached_matrix_valid = true; + } + return cached_matrix; +} + +Vector2 Camera::calc_sample_pos(int x, int y, int xsz, int ysz, int sample) const +{ + float ppos[2]; + float aspect = (float)xsz / (float)ysz; + + float pwidth = 2.0 * aspect / (float)xsz; + float pheight = 2.0 / (float)ysz; + + ppos[0] = (float)x * pwidth - aspect; + ppos[1] = 1.0 - (float)y * pheight; + + calc_sample_pos_rec(sample, pwidth, pheight, ppos); + return Vector2(ppos[0], ppos[1]); +} + +Ray Camera::get_primary_ray(int x, int y, int xsz, int ysz, int sample) const +{ +#pragma omp single + { + if(!rdir_cache || rdir_cache_width != xsz || rdir_cache_height != ysz) { + printf("calculating primary ray direction cache\n"); + + delete [] rdir_cache; + rdir_cache = new Vector3[xsz * ysz]; + +#pragma omp parallel for + for(int i=0; ix = ppos.x; + rdir->y = ppos.y; + rdir->z = 1.0 / tan(vfov / 2.0); + rdir->normalize(); + + rdir++; + } + } + rdir_cache_width = xsz; + rdir_cache_height = ysz; + } + } + + Ray ray; + ray.origin = pos; + ray.dir = rdir_cache[y * xsz + x]; + + // transform the ray direction with the camera matrix + Matrix4x4 mat = get_matrix(); + mat.m[0][3] = mat.m[1][3] = mat.m[2][3] = mat.m[3][0] = mat.m[3][1] = mat.m[3][2] = 0.0; + mat.m[3][3] = 1.0; + + ray.dir = ray.dir.transformed(mat); + return ray; +} + +TargetCamera::TargetCamera() {} + +TargetCamera::TargetCamera(const Vector3 &pos, const Vector3 &targ) + : Camera(pos), target(targ) +{ +} + +void TargetCamera::set_target(const Vector3 &targ) +{ + target = targ; + cached_matrix_valid = false; // invalidate the cached matrix +} + +const Vector3 &TargetCamera::get_target() const +{ + return target; +} + +void TargetCamera::calc_matrix(Matrix4x4 *mat) const +{ + Vector3 up(0, 1, 0); + Vector3 dir = (target - pos).normalized(); + Vector3 right = cross_product(up, dir); + up = cross_product(dir, right); + + *mat = Matrix4x4( + right.x, up.x, dir.x, pos.x, + right.y, up.y, dir.y, pos.y, + right.z, up.z, dir.z, pos.z, + 0.0, 0.0, 0.0, 1.0); +} + +void FlyCamera::input_move(float x, float y, float z) +{ + static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0); + + Vector3 k = vfwd.transformed(rot); + Vector3 i = vright.transformed(rot); + Vector3 j = cross_product(k, i); + + pos += i * x + j * y + k * z; + cached_matrix_valid = false; +} + +void FlyCamera::input_rotate(float x, float y, float z) +{ + Vector3 axis(x, y, z); + float axis_len = axis.length(); + if(fabs(axis_len) < 1e-5) { + return; + } + rot.rotate(axis / axis_len, -axis_len); + rot.normalize(); + + cached_matrix_valid = false; +} + +void FlyCamera::calc_matrix(Matrix4x4 *mat) const +{ + Matrix4x4 tmat; + tmat.set_translation(pos); + + Matrix3x3 rmat = rot.get_rotation_matrix(); + + *mat = tmat * Matrix4x4(rmat); +} + +/* generates a sample position for sample number sidx, in the unit square + * by recursive subdivision and jittering + */ +static void calc_sample_pos_rec(int sidx, float xsz, float ysz, float *pos) +{ + static const float subpt[4][2] = { + {-0.25, -0.25}, {0.25, -0.25}, {-0.25, 0.25}, {0.25, 0.25} + }; + + if(!sidx) { + return; + } + + /* determine which quadrant to recurse into */ + int quadrant = ((sidx - 1) % 4); + pos[0] += subpt[quadrant][0] * xsz; + pos[1] += subpt[quadrant][1] * ysz; + + calc_sample_pos_rec((sidx - 1) / 4, xsz / 2, ysz / 2, pos); +} diff -r 000000000000 -r b326d53321f7 src/camera.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/camera.h Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,61 @@ +#ifndef CAMERA_H_ +#define CAMERA_H_ + +#include "vmath/vmath.h" + +class Camera { +protected: + Vector3 pos; + float vfov; // vertical field of view in radians + + mutable Matrix4x4 cached_matrix; + mutable bool cached_matrix_valid; + + mutable Vector3 *rdir_cache; + mutable int rdir_cache_width, rdir_cache_height; + + virtual void calc_matrix(Matrix4x4 *mat) const = 0; + + Vector2 calc_sample_pos(int x, int y, int xsz, int ysz, int sample) const; + +public: + Camera(); + Camera(const Vector3 &pos); + virtual ~Camera(); + + virtual void set_fov(float vfov); + virtual float get_fov() const; + + virtual void set_position(const Vector3 &pos); + virtual const Vector3 &get_position() const; + virtual const Matrix4x4 &get_matrix() const; + + virtual Ray get_primary_ray(int x, int y, int xsz, int ysz, int sample = 0) const; +}; + +class TargetCamera : public Camera { +protected: + Vector3 target; + + void calc_matrix(Matrix4x4 *mat) const; + +public: + TargetCamera(); + TargetCamera(const Vector3 &pos, const Vector3 &targ); + + virtual void set_target(const Vector3 &targ); + virtual const Vector3 &get_target() const; +}; + +class FlyCamera : public Camera { +protected: + Quaternion rot; + + void calc_matrix(Matrix4x4 *mat) const; + +public: + void input_move(float x, float y, float z); + void input_rotate(float x, float y, float z); +}; + +#endif // CAMERA_H_ diff -r 000000000000 -r b326d53321f7 src/game.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/game.cc Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,180 @@ +#include "game.h" +#include "opengl.h" +#include "camera.h" +#include "texture.h" +#include "OVR_CAPI_GL.h" + +static void draw_scene(); + +static const float move_speed = 10.0f; + +static int fb_width, fb_height; +static FlyCamera cam; +static Texture floor_tex; +static bool keystate[256]; + +bool game_init() +{ + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + + glClearColor(0.1, 0.1, 0.1, 1); + + if(!floor_tex.load("data/tiles.png")) { + return false; + } + + cam.input_move(0, 0, 5); + return true; +} + +void game_cleanup() +{ + floor_tex.destroy(); +} + + +void game_update(unsigned int msec) +{ + static unsigned int prev_msec; + float dt = (msec - prev_msec) / 1000.0f; + float offs = dt * move_speed; + prev_msec = msec; + + Vector3 move; + float roll = 0.0f; + + if(keystate['d'] || keystate['D']) { + move.x += offs; + } + if(keystate['a'] || keystate['A']) { + move.x -= offs; + } + if(keystate['s'] || keystate['S']) { + move.z += offs; + } + if(keystate['w'] || keystate['W']) { + move.z -= offs; + } + if(keystate['e'] || keystate['E']) { + roll += dt; + } + if(keystate['q'] || keystate['Q']) { + roll -= dt; + } + + cam.input_move(move.x, move.y, move.z); + cam.input_rotate(0, 0, roll); +} + +void game_render(int eye) +{ + Matrix4x4 view_matrix = cam.get_matrix().inverse(); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glLoadTransposeMatrixf(view_matrix[0]); + + draw_scene(); +} + +void game_reshape(int x, int y) +{ + glViewport(0, 0, x, y); + fb_width = x; + fb_height = y; +} + +void game_keyboard(int key, bool pressed, int x, int y) +{ + if(pressed) { + switch(key) { + case 27: + exit(0); + } + } + + if(key < 256) { + keystate[key] = pressed; + } +} + +static int prev_x, prev_y; +static bool bnstate[32]; + +void game_mouse(int bn, bool pressed, int x, int y) +{ + bnstate[bn] = pressed; + prev_x = x; + prev_y = y; +} + +void game_motion(int x, int y) +{ + int dx = x - prev_x; + int dy = y - prev_y; + prev_x = x; + prev_y = y; + + if(!dx && !dy) return; + + if(bnstate[0]) { + float xrot = dy * 0.5; + float yrot = dx * 0.5; + cam.input_rotate(DEG_TO_RAD(xrot), 0, 0); + cam.input_rotate(0, DEG_TO_RAD(yrot), 0); + } +} + +void game_6dof_move(float x, float y, float z) +{ + cam.input_move(x, y, z); +} + +void game_6dof_rotate(float x, float y, float z) +{ + cam.input_rotate(x, y, z); +} + +static void draw_scene() +{ + glMatrixMode(GL_MODELVIEW); + glTranslatef(0, -1.5, 0); + + float lpos[] = {-20, 30, 10, 1}; + glLightfv(GL_LIGHT0, GL_POSITION, lpos); + + glEnable(GL_TEXTURE_2D); + floor_tex.bind(); + + glMatrixMode(GL_TEXTURE); + glScalef(8, 8, 8); + + glBegin(GL_QUADS); + glNormal3f(0, 1, 0); + glTexCoord2f(0, 0); glVertex3f(-25, 0, 25); + glTexCoord2f(1, 0); glVertex3f(25, 0, 25); + glTexCoord2f(1, 1); glVertex3f(25, 0, -25); + glTexCoord2f(0, 1); glVertex3f(-25, 0, -25); + glEnd(); + glDisable(GL_TEXTURE_2D); + glLoadIdentity(); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glTranslatef(0, 0.75, 0); + + glFrontFace(GL_CW); + glutSolidTeapot(1.0); + glFrontFace(GL_CCW); + + glPopMatrix(); +} \ No newline at end of file diff -r 000000000000 -r b326d53321f7 src/game.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/game.h Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,18 @@ +#ifndef GAME_H_ +#define GAME_H_ + +bool game_init(); +void game_cleanup(); + +void game_update(unsigned int msec); +void game_render(int eye); + +void game_reshape(int x, int y); +void game_keyboard(int key, bool pressed, int x, int y); +void game_mouse(int bn, bool pressed, int x, int y); +void game_motion(int x, int y); + +void game_6dof_move(float x, float y, float z); +void game_6dof_rotate(float x, float y, float z); + +#endif // GAME_H_ \ No newline at end of file diff -r 000000000000 -r b326d53321f7 src/image.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/image.cc Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,94 @@ +#include +#include "imago2.h" +#include "image.h" + +Image::Image() +{ + pixels = 0; + own_pixels = true; + width = height = 0; +} + +Image::~Image() +{ + destroy(); +} + +Image::Image(const Image &img) +{ + pixels = 0; + own_pixels = false; + + create(img.width, img.height, img.pixels); +} + +Image &Image::operator =(const Image &img) +{ + if(this != &img) { + destroy(); + create(img.width, img.height, img.pixels); + } + return *this; +} + +void Image::create(int xsz, int ysz, unsigned char *pixels) +{ + destroy(); + + this->pixels = new unsigned char[xsz * ysz * 4]; + if(pixels) { + memcpy(this->pixels, pixels, xsz * ysz * 4); + } else { + memset(this->pixels, 0, xsz * ysz * 4); + } + width = xsz; + height = ysz; + own_pixels = true; +} + +void Image::destroy() +{ + if(own_pixels) { + delete [] pixels; + } + pixels = 0; + width = height = 0; + own_pixels = true; +} + +int Image::get_width() const +{ + return width; +} + +int Image::get_height() const +{ + return height; +} + +void Image::set_pixels(int xsz, int ysz, unsigned char *pixels) +{ + destroy(); + + this->pixels = pixels; + width = xsz; + height = ysz; + own_pixels = false; +} + +unsigned char *Image::get_pixels() const +{ + return pixels; +} + +bool Image::load(const char *fname) +{ + int xsz, ysz; + unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz); + if(!pix) { + return false; + } + + create(xsz, ysz, pix); + return true; +} \ No newline at end of file diff -r 000000000000 -r b326d53321f7 src/image.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/image.h Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,29 @@ +#ifndef IMAGE_H_ +#define IMAGE_H_ + +class Image { +private: + int width, height; + unsigned char *pixels; + bool own_pixels; + +public: + Image(); + ~Image(); + + Image(const Image &img); + Image &operator =(const Image &img); + + void create(int xsz, int ysz, unsigned char *pix = 0); + void destroy(); + + int get_width() const; + int get_height() const; + + void set_pixels(int xsz, int ysz, unsigned char *pix); + unsigned char *get_pixels() const; + + bool load(const char *fname); +}; + +#endif // IMAGE_H_ \ No newline at end of file diff -r 000000000000 -r b326d53321f7 src/main.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main.cc Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,111 @@ +#include +#include +#include "opengl.h" +#include "game.h" + +static bool init(); +static void cleanup(); + +static void display(); +static void idle(); +static void reshape(int x, int y); +static void keyb(unsigned char key, int x, int y); +static void keyb_up(unsigned char key, int x, int y); +static void mouse(int bn, int st, int x, int y); +static void motion(int x, int y); +static void sball_motion(int x, int y, int z); +static void sball_rotate(int x, int y, int z); + +int main(int argc, char **argv) +{ + glutInitWindowSize(1024, 600); + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); + glutCreateWindow("VR Chess"); + + glutDisplayFunc(display); + glutIdleFunc(idle); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); + glutKeyboardUpFunc(keyb_up); + glutMouseFunc(mouse); + glutMotionFunc(motion); + glutSpaceballMotionFunc(sball_motion); + glutSpaceballRotateFunc(sball_rotate); + + if(!init()) { + return 1; + } + atexit(cleanup); + + glutMainLoop(); + return 0; +} + +static bool init() +{ + glewInit(); + + if(!game_init()) { + return false; + } + return true; +} + +static void cleanup() +{ + game_cleanup(); +} + +static void display() +{ + unsigned int msec = glutGet(GLUT_ELAPSED_TIME); + + game_update(msec); + game_render(0); + + glutSwapBuffers(); +} + +static void idle() +{ + glutPostRedisplay(); +} + +static void reshape(int x, int y) +{ + game_reshape(x, y); +} + +static void keyb(unsigned char key, int x, int y) +{ + game_keyboard(key, true, x, y); +} + +static void keyb_up(unsigned char key, int x, int y) +{ + game_keyboard(key, false, x, y); +} + +static void mouse(int bn, int st, int x, int y) +{ + game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y); +} + +static void motion(int x, int y) +{ + game_motion(x, y); +} + +#define SBALL_MOVE_SCALE 0.00025 +#define SBALL_ROT_SCALE 0.01 + +static void sball_motion(int x, int y, int z) +{ + game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE); +} + +static void sball_rotate(int x, int y, int z) +{ + game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE); +} \ No newline at end of file diff -r 000000000000 -r b326d53321f7 src/opengl.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/opengl.cc Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,61 @@ +#include "opengl.h" +#include + +void load_matrix(const Matrix4x4 &m) +{ +#ifdef SINGLE_PRECISION_MATH + if(glLoadTransposeMatrixfARB) { + glLoadTransposeMatrixfARB((float*)&m); + } else { + Matrix4x4 tmat = m.transposed(); + glLoadMatrixf((float*)&tmat); + } +#else + if(glLoadTransposeMatrixdARB) { + glLoadTransposeMatrixdARB((double*)&m); + } else { + Matrix4x4 tmat = m.transposed(); + glLoadMatrixd((double*)&tmat); + } +#endif +} + +void mult_matrix(const Matrix4x4 &m) +{ +#ifdef SINGLE_PRECISION_MATH + if(glMultTransposeMatrixfARB) { + glMultTransposeMatrixfARB((float*)&m); + } else { + Matrix4x4 tmat = m.transposed(); + glMultMatrixf((float*)&tmat); + } +#else + if(glMultTransposeMatrixdARB) { + glMultTransposeMatrixdARB((double*)&m); + } else { + Matrix4x4 tmat = m.transposed(); + glMultMatrixd((double*)&tmat); + } +#endif +} + +const char *strglerr(int err) +{ + static const char *errnames[] = { + "GL_INVALID_ENUM", + "GL_INVALID_VALUE", + "GL_INVALID_OPERATION", + "GL_STACK_OVERFLOW", + "GL_STACK_UNDERFLOW", + "GL_OUT_OF_MEMORY", + "GL_INVALID_FRAMEBUFFER_OPERATION" + }; + + if(!err) { + return "GL_NO_ERROR"; + } + if(err < GL_INVALID_ENUM || err > GL_OUT_OF_MEMORY) { + return ""; + } + return errnames[err - GL_INVALID_ENUM]; +} diff -r 000000000000 -r b326d53321f7 src/opengl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/opengl.h Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,29 @@ +#ifndef OPENGL_H_ +#define OPENGL_H_ + +#include + +#ifndef __APPLE__ +#include +#else +#include +#endif + +#define CHECKGLERR \ + do { \ + int err = glGetError(); \ + if(err) { \ + fprintf(stderr, "%s:%d: OpenGL error 0x%x: %s\n", __FILE__, __LINE__, err, strglerr(err)); \ + abort(); \ + } \ + } while(0) + + +class Matrix4x4; + +void load_matrix(const Matrix4x4 &m); +void mult_matrix(const Matrix4x4 &m); + +const char *strglerr(int err); + +#endif /* OPENGL_H_ */ diff -r 000000000000 -r b326d53321f7 src/sdr.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdr.c Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,427 @@ +/* +Printblobs - halftoning display hack +Copyright (C) 2013 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +#include +#include +#include +#include +#include +#include +#include + +#if defined(unix) || defined(__unix__) +#include +#include +#endif /* unix */ + +#include "sdr.h" + +static const char *sdrtypestr(unsigned int sdrtype); + +unsigned int create_vertex_shader(const char *src) +{ + return create_shader(src, GL_VERTEX_SHADER); +} + +unsigned int create_pixel_shader(const char *src) +{ + return create_shader(src, GL_FRAGMENT_SHADER); +} + +unsigned int create_tessctl_shader(const char *src) +{ + return create_shader(src, GL_TESS_CONTROL_SHADER); +} + +unsigned int create_tesseval_shader(const char *src) +{ + return create_shader(src, GL_TESS_EVALUATION_SHADER); +} + +unsigned int create_geometry_shader(const char *src) +{ + return create_shader(src, GL_GEOMETRY_SHADER); +} + +unsigned int create_shader(const char *src, unsigned int sdr_type) +{ + unsigned int sdr; + int success, info_len; + char *info_str = 0; + GLenum err; + + sdr = glCreateShader(sdr_type); + assert(glGetError() == GL_NO_ERROR); + glShaderSource(sdr, 1, &src, 0); + err = glGetError(); + assert(err == GL_NO_ERROR); + glCompileShader(sdr); + assert(glGetError() == GL_NO_ERROR); + + glGetShaderiv(sdr, GL_COMPILE_STATUS, &success); + assert(glGetError() == GL_NO_ERROR); + glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len); + assert(glGetError() == GL_NO_ERROR); + + if(info_len) { + if((info_str = malloc(info_len + 1))) { + glGetShaderInfoLog(sdr, info_len, 0, info_str); + assert(glGetError() == GL_NO_ERROR); + } + } + + if(success) { + fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str); + } else { + fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str); + glDeleteShader(sdr); + sdr = 0; + } + + free(info_str); + return sdr; +} + +void free_shader(unsigned int sdr) +{ + glDeleteShader(sdr); +} + +unsigned int load_vertex_shader(const char *fname) +{ + return load_shader(fname, GL_VERTEX_SHADER); +} + +unsigned int load_pixel_shader(const char *fname) +{ + return load_shader(fname, GL_FRAGMENT_SHADER); +} + +unsigned int load_tessctl_shader(const char *fname) +{ + return load_shader(fname, GL_TESS_CONTROL_SHADER); +} + +unsigned int load_tesseval_shader(const char *fname) +{ + return load_shader(fname, GL_TESS_EVALUATION_SHADER); +} + +unsigned int load_geometry_shader(const char *fname) +{ + return load_shader(fname, GL_GEOMETRY_SHADER); +} + +unsigned int load_shader(const char *fname, unsigned int sdr_type) +{ +#if defined(unix) || defined(__unix__) + struct stat st; +#endif + unsigned int sdr; + size_t filesize; + FILE *fp; + char *src; + + if(!(fp = fopen(fname, "r"))) { + fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno)); + return 0; + } + +#if defined(unix) || defined(__unix__) + fstat(fileno(fp), &st); + filesize = st.st_size; +#else + fseek(fp, 0, SEEK_END); + filesize = ftell(fp); + fseek(fp, 0, SEEK_SET); +#endif /* unix */ + + if(!(src = malloc(filesize + 1))) { + fclose(fp); + return 0; + } + fread(src, 1, filesize, fp); + src[filesize] = 0; + fclose(fp); + + fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname); + sdr = create_shader(src, sdr_type); + + free(src); + return sdr; +} + + +unsigned int get_vertex_shader(const char *fname) +{ + return get_shader(fname, GL_VERTEX_SHADER); +} + +unsigned int get_pixel_shader(const char *fname) +{ + return get_shader(fname, GL_FRAGMENT_SHADER); +} + +unsigned int get_tessctl_shader(const char *fname) +{ + return get_shader(fname, GL_TESS_CONTROL_SHADER); +} + +unsigned int get_tesseval_shader(const char *fname) +{ + return get_shader(fname, GL_TESS_EVALUATION_SHADER); +} + +unsigned int get_geometry_shader(const char *fname) +{ + return get_shader(fname, GL_GEOMETRY_SHADER); +} + +unsigned int get_shader(const char *fname, unsigned int sdr_type) +{ + unsigned int sdr; + if(!(sdr = load_shader(fname, sdr_type))) { + return 0; + } + return sdr; +} + + +/* ---- gpu programs ---- */ + +unsigned int create_program(void) +{ + unsigned int prog = glCreateProgram(); + assert(glGetError() == GL_NO_ERROR); + return prog; +} + +unsigned int create_program_link(unsigned int sdr0, ...) +{ + unsigned int prog, sdr; + va_list ap; + + if(!(prog = create_program())) { + return 0; + } + + attach_shader(prog, sdr0); + if(glGetError()) { + return 0; + } + + va_start(ap, sdr0); + while((sdr = va_arg(ap, unsigned int))) { + attach_shader(prog, sdr); + if(glGetError()) { + return 0; + } + } + va_end(ap); + + if(link_program(prog) == -1) { + free_program(prog); + return 0; + } + return prog; +} + +unsigned int create_program_load(const char *vfile, const char *pfile) +{ + unsigned int vs = 0, ps = 0; + + if(vfile && *vfile && !(vs = get_vertex_shader(vfile))) { + return 0; + } + if(pfile && *pfile && !(ps = get_pixel_shader(pfile))) { + return 0; + } + return create_program_link(vs, ps, 0); +} + +void free_program(unsigned int sdr) +{ + glDeleteProgram(sdr); +} + +void attach_shader(unsigned int prog, unsigned int sdr) +{ + glAttachShader(prog, sdr); + assert(glGetError() == GL_NO_ERROR); +} + +int link_program(unsigned int prog) +{ + int linked, info_len, retval = 0; + char *info_str = 0; + + glLinkProgram(prog); + assert(glGetError() == GL_NO_ERROR); + glGetProgramiv(prog, GL_LINK_STATUS, &linked); + assert(glGetError() == GL_NO_ERROR); + glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len); + assert(glGetError() == GL_NO_ERROR); + + if(info_len) { + if((info_str = malloc(info_len + 1))) { + glGetProgramInfoLog(prog, info_len, 0, info_str); + assert(glGetError() == GL_NO_ERROR); + } + } + + if(linked) { + fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str); + } else { + fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str); + retval = -1; + } + + free(info_str); + return retval; +} + +int bind_program(unsigned int prog) +{ + GLenum err; + + glUseProgram(prog); + if(prog && (err = glGetError()) != GL_NO_ERROR) { + /* maybe the program is not linked, try linking first */ + if(err == GL_INVALID_OPERATION) { + if(link_program(prog) == -1) { + return -1; + } + glUseProgram(prog); + return glGetError() == GL_NO_ERROR ? 0 : -1; + } + return -1; + } + return 0; +} + +/* ugly but I'm not going to write the same bloody code over and over */ +#define BEGIN_UNIFORM_CODE \ + int loc, curr_prog; \ + glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \ + if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \ + return -1; \ + } \ + if((loc = glGetUniformLocation(prog, name)) != -1) + +#define END_UNIFORM_CODE \ + if((unsigned int)curr_prog != prog) { \ + bind_program(curr_prog); \ + } \ + return loc == -1 ? -1 : 0 + +int set_uniform_int(unsigned int prog, const char *name, int val) +{ + BEGIN_UNIFORM_CODE { + glUniform1i(loc, val); + } + END_UNIFORM_CODE; +} + +int set_uniform_float(unsigned int prog, const char *name, float val) +{ + BEGIN_UNIFORM_CODE { + glUniform1f(loc, val); + } + END_UNIFORM_CODE; +} + +int set_uniform_float2(unsigned int prog, const char *name, float x, float y) +{ + BEGIN_UNIFORM_CODE { + glUniform2f(loc, x, y); + } + END_UNIFORM_CODE; +} + +int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z) +{ + BEGIN_UNIFORM_CODE { + glUniform3f(loc, x, y, z); + } + END_UNIFORM_CODE; +} + +int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w) +{ + BEGIN_UNIFORM_CODE { + glUniform4f(loc, x, y, z, w); + } + END_UNIFORM_CODE; +} + +int set_uniform_matrix4(unsigned int prog, const char *name, float *mat) +{ + BEGIN_UNIFORM_CODE { + glUniformMatrix4fv(loc, 1, GL_FALSE, mat); + } + END_UNIFORM_CODE; +} + +int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat) +{ + BEGIN_UNIFORM_CODE { + glUniformMatrix4fv(loc, 1, GL_TRUE, mat); + } + END_UNIFORM_CODE; +} + +int get_attrib_loc(unsigned int prog, const char *name) +{ + int loc, curr_prog; + + glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); + if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { + return -1; + } + + loc = glGetAttribLocation(prog, (char*)name); + + if((unsigned int)curr_prog != prog) { + bind_program(curr_prog); + } + return loc; +} + +void set_attrib_float3(int attr_loc, float x, float y, float z) +{ + glVertexAttrib3f(attr_loc, x, y, z); +} + +static const char *sdrtypestr(unsigned int sdrtype) +{ + switch(sdrtype) { + case GL_VERTEX_SHADER: + return "vertex"; + case GL_FRAGMENT_SHADER: + return "pixel"; + case GL_TESS_CONTROL_SHADER: + return "tessellation control"; + case GL_TESS_EVALUATION_SHADER: + return "tessellation evaluation"; + case GL_GEOMETRY_SHADER: + return "geometry"; + + default: + break; + } + return ""; +} diff -r 000000000000 -r b326d53321f7 src/sdr.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdr.h Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,76 @@ +/* +Printblobs - halftoning display hack +Copyright (C) 2013 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +#ifndef SDR_H_ +#define SDR_H_ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* ---- shaders ---- */ +unsigned int create_vertex_shader(const char *src); +unsigned int create_pixel_shader(const char *src); +unsigned int create_tessctl_shader(const char *src); +unsigned int create_tesseval_shader(const char *src); +unsigned int create_geometry_shader(const char *src); +unsigned int create_shader(const char *src, unsigned int sdr_type); +void free_shader(unsigned int sdr); + +unsigned int load_vertex_shader(const char *fname); +unsigned int load_pixel_shader(const char *fname); +unsigned int load_tessctl_shader(const char *fname); +unsigned int load_tesseval_shader(const char *fname); +unsigned int load_geometry_shader(const char *fname); +unsigned int load_shader(const char *src, unsigned int sdr_type); + +unsigned int get_vertex_shader(const char *fname); +unsigned int get_pixel_shader(const char *fname); +unsigned int get_tessctl_shader(const char *fname); +unsigned int get_tesseval_shader(const char *fname); +unsigned int get_geometry_shader(const char *fname); +unsigned int get_shader(const char *fname, unsigned int sdr_type); + +int add_shader(const char *fname, unsigned int sdr); +int remove_shader(const char *fname); + +/* ---- gpu programs ---- */ +unsigned int create_program(void); +unsigned int create_program_link(unsigned int sdr0, ...); +unsigned int create_program_load(const char *vfile, const char *pfile); +void free_program(unsigned int sdr); + +void attach_shader(unsigned int prog, unsigned int sdr); +int link_program(unsigned int prog); +int bind_program(unsigned int prog); + +int set_uniform_int(unsigned int prog, const char *name, int val); +int set_uniform_float(unsigned int prog, const char *name, float val); +int set_uniform_float2(unsigned int prog, const char *name, float x, float y); +int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z); +int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w); +int set_uniform_matrix4(unsigned int prog, const char *name, float *mat); +int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat); + +int get_attrib_loc(unsigned int prog, const char *name); +void set_attrib_float3(int attr_loc, float x, float y, float z); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* SDR_H_ */ diff -r 000000000000 -r b326d53321f7 src/texture.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/texture.cc Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,77 @@ +#include "texture.h" +#include "opengl.h" + +Texture::Texture() +{ + tex = 0; + type = GL_TEXTURE_2D; +} + +Texture::~Texture() +{ + destroy(); +} + +void Texture::create2d(int xsz, int ysz) +{ + destroy(); + + type = GL_TEXTURE_2D; + img.create(xsz, ysz); + + if(!tex) { + glGenTextures(1, &tex); + } + glBindTexture(type, tex); + glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(type, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); +} + +void Texture::destroy() +{ + if(tex) { + glDeleteTextures(1, &tex); + } +} + +void Texture::set_image(const Image &img) +{ + this->img = img; + create2d(img.get_width(), img.get_height()); + + glTexSubImage2D(type, 0, 0, 0, img.get_width(), img.get_height(), + GL_RGBA, GL_UNSIGNED_BYTE, img.get_pixels()); +} + +Image &Texture::get_image() +{ + return img; +} + +const Image &Texture::get_image() const +{ + return img; +} + +unsigned int Texture::get_texture_id() const +{ + return tex; +} + +void Texture::bind(int tunit) const +{ + glActiveTextureARB(GL_TEXTURE0_ARB + tunit); + glBindTexture(type, tex); + glActiveTextureARB(GL_TEXTURE0_ARB); +} + +bool Texture::load(const char *fname) +{ + Image image; + if(!image.load(fname)) { + return false; + } + set_image(image); + return true; +} \ No newline at end of file diff -r 000000000000 -r b326d53321f7 src/texture.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/texture.h Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,29 @@ +#ifndef TEXTURE_H_ +#define TEXTURE_H_ + +#include "image.h" + +class Texture { +private: + Image img; + unsigned int tex; + unsigned int type; + +public: + Texture(); + ~Texture(); + + void create2d(int xsz, int ysz); + void destroy(); + + void set_image(const Image &img); + Image &get_image(); + const Image &get_image() const; + + unsigned int get_texture_id() const; + void bind(int tunit = 0) const; + + bool load(const char *fname); +}; + +#endif // TEXTURE_H_ \ No newline at end of file diff -r 000000000000 -r b326d53321f7 vrchess.sln --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vrchess.sln Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "vrchess", "vrchess.vcxproj", "{714906B6-FD4C-4ECC-990C-247FA46B8801}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {714906B6-FD4C-4ECC-990C-247FA46B8801}.Debug|Win32.ActiveCfg = Debug|Win32 + {714906B6-FD4C-4ECC-990C-247FA46B8801}.Debug|Win32.Build.0 = Debug|Win32 + {714906B6-FD4C-4ECC-990C-247FA46B8801}.Release|Win32.ActiveCfg = Release|Win32 + {714906B6-FD4C-4ECC-990C-247FA46B8801}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff -r 000000000000 -r b326d53321f7 vrchess.vcxproj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vrchess.vcxproj Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,102 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {714906B6-FD4C-4ECC-990C-247FA46B8801} + Win32Proj + vrchess + + + + Application + true + v90 + Unicode + + + Application + false + v90 + true + Unicode + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + 4996;4244;4305 + + + Console + true + opengl32.lib;glut32.lib;glew32.lib;libvmath.lib;libimago2.lib;jpeglib.lib;libpng.lib;zlib.lib;libovrd.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + 4996;4244;4305 + + + Console + true + true + true + opengl32.lib;glut32.lib;glew32.lib;libvmath.lib;libimago2.lib;jpeglib.lib;libpng.lib;zlib.lib;libovr.lib;%(AdditionalDependencies) + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff -r 000000000000 -r b326d53321f7 vrchess.vcxproj.filters --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vrchess.vcxproj.filters Fri Apr 25 05:20:53 2014 +0300 @@ -0,0 +1,60 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file