3dphotoshoot
changeset 26:a460b1e5af4a
added GLUT frontend
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 18 Jun 2015 03:55:05 +0300 |
parents | ac80210d5fbe |
children | 3d082c566b53 |
files | .hgignore Makefile src/game.cc src/pc/assman.c src/pc/camera.c src/pc/main.c src/sanegl.c |
diffstat | 7 files changed, 259 insertions(+), 42 deletions(-) [+] |
line diff
1.1 --- a/.hgignore Thu Jun 18 03:12:30 2015 +0300 1.2 +++ b/.hgignore Thu Jun 18 03:55:05 2015 +0300 1.3 @@ -8,7 +8,8 @@ 1.4 ^android/res/ 1.5 ^android/gen/ 1.6 ^android/assets/ 1.7 -^data/ 1.8 +^data 1.9 +^photoshoot3d$ 1.10 \.properties$ 1.11 \.apk$ 1.12 ^android/proguard-project\.txt
2.1 --- a/Makefile Thu Jun 18 03:12:30 2015 +0300 2.2 +++ b/Makefile Thu Jun 18 03:55:05 2015 +0300 2.3 @@ -1,18 +1,18 @@ 2.4 root = . 2.5 include $(root)/proj.mk 2.6 2.7 -src += $(wildcard $(root)/src/glut/*.c) 2.8 +src += $(wildcard $(root)/src/pc/*.c) 2.9 obj = $(src:.c=.o) $(ccsrc:.cc=.o) 2.10 2.11 ifeq ($(shell uname -s), Darwin) 2.12 - libgl = -framework OpenGL -framework GLUT 2.13 + libgl = -framework OpenGL -framework GLUT -lGLEW 2.14 else 2.15 - libgl = -lGL -lGLU -lglut 2.16 + libgl = -lGL -lGLU -lglut -lGLEW 2.17 endif 2.18 2.19 CXXFLAGS = -pedantic -Wall -g $(defs) -I$(root)/src/glut $(incpaths) 2.20 CFLAGS = $(CXXFLAGS) 2.21 -LDFLAGS = $(libpaths) $(libs) $(libgl) 2.22 +LDFLAGS = $(libpaths) $(libs) $(libgl) -ldl -lpthread 2.23 2.24 $(bin): $(obj) 2.25 $(CXX) -o $@ $(obj) $(LDFLAGS)
3.1 --- a/src/game.cc Thu Jun 18 03:12:30 2015 +0300 3.2 +++ b/src/game.cc Thu Jun 18 03:55:05 2015 +0300 3.3 @@ -164,9 +164,9 @@ 3.4 gl_matrix_mode(GL_MODELVIEW); 3.5 gl_push_matrix(); 3.6 gl_mult_matrixf(rmat[0]); 3.7 - gl_apply_xform(sdr_debug->get_globj()); 3.8 3.9 sdr_debug->bind(); 3.10 + gl_apply_xform(sdr_debug->get_globj()); 3.11 3.12 mesh->draw(); 3.13
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/pc/assman.c Thu Jun 18 03:55:05 2015 +0300 4.3 @@ -0,0 +1,28 @@ 4.4 +#include <stdio.h> 4.5 +#include "assman.h" 4.6 + 4.7 +ass_file *ass_fopen(const char *fname, const char *mode) 4.8 +{ 4.9 + return (ass_file*)fopen(fname, mode); 4.10 +} 4.11 + 4.12 +void ass_fclose(ass_file *fp) 4.13 +{ 4.14 + fclose(fp); 4.15 +} 4.16 + 4.17 +long ass_fseek(ass_file *fp, long offs, int whence) 4.18 +{ 4.19 + fseek(fp, offs, whence); 4.20 + return ftell(fp); 4.21 +} 4.22 + 4.23 +long ass_ftell(ass_file *fp) 4.24 +{ 4.25 + return ftell(fp); 4.26 +} 4.27 + 4.28 +size_t ass_fread(void *buf, size_t size, size_t count, ass_file *fp) 4.29 +{ 4.30 + return fread(buf, size, count, fp); 4.31 +}
5.1 --- a/src/pc/camera.c Thu Jun 18 03:12:30 2015 +0300 5.2 +++ b/src/pc/camera.c Thu Jun 18 03:55:05 2015 +0300 5.3 @@ -1,42 +1,117 @@ 5.4 +#include <math.h> 5.5 +#include "opengl.h" 5.6 #include "camera.h" 5.7 5.8 +static unsigned int tex; 5.9 +static float tex_matrix[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; 5.10 + 5.11 +#define TESTPAT_WIDTH 256 5.12 +#define TESTPAT_HEIGHT 256 5.13 + 5.14 int cam_init(void *platform_data) 5.15 { 5.16 + unsigned char pal[2][2][3] = { 5.17 + {{255, 128, 64}, {64, 128, 255}}, 5.18 + {{128, 255, 64}, {128, 64, 255}} 5.19 + }; 5.20 + 5.21 + int i, j; 5.22 + unsigned char pixels[TESTPAT_WIDTH * TESTPAT_HEIGHT * 3]; 5.23 + unsigned char *pptr = pixels; 5.24 + 5.25 + for(i=0; i<TESTPAT_HEIGHT; i++) { 5.26 + for(j=0; j<TESTPAT_WIDTH; j++) { 5.27 + int chess = ((i >> 3) & 1) == ((j >> 3) & 1); 5.28 + float x = 2.0 * (float)j / (float)TESTPAT_WIDTH - 1.0; 5.29 + float y = 2.0 * (float)i / (float)TESTPAT_HEIGHT - 1.0; 5.30 + float len = sqrt(x * x + y * y); 5.31 + float wave = cos(len * 4.0 * M_PI); 5.32 + int band = wave >= 0 ? 0 : 1; 5.33 + 5.34 + *pptr++ = pal[band][chess][0]; 5.35 + *pptr++ = pal[band][chess][1]; 5.36 + *pptr++ = pal[band][chess][2]; 5.37 + } 5.38 + } 5.39 + 5.40 + glGenTextures(1, &tex); 5.41 + glBindTexture(GL_TEXTURE_2D, tex); 5.42 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 5.43 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 5.44 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TESTPAT_WIDTH, TESTPAT_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels); 5.45 + 5.46 + return 0; 5.47 +} 5.48 + 5.49 +void cam_shutdown(void) 5.50 +{ 5.51 + glDeleteTextures(1, &tex); 5.52 +} 5.53 + 5.54 +unsigned int cam_texture(void) 5.55 +{ 5.56 + return tex; 5.57 +} 5.58 + 5.59 +const float *cam_texture_matrix(void) 5.60 +{ 5.61 + return tex_matrix; 5.62 +} 5.63 + 5.64 +int cam_start_video(void) 5.65 +{ 5.66 + return 0; 5.67 +} 5.68 + 5.69 +int cam_stop_video(void) 5.70 +{ 5.71 + return 0; 5.72 +} 5.73 + 5.74 +int cam_update(void) 5.75 +{ 5.76 + return 0; 5.77 +} 5.78 + 5.79 +int cam_is_capturing(void) 5.80 +{ 5.81 + return 1; 5.82 +} 5.83 + 5.84 +int cam_video_size(int *xsz, int *ysz) 5.85 +{ 5.86 + *xsz = TESTPAT_WIDTH; 5.87 + *ysz = TESTPAT_HEIGHT; 5.88 + return 0; 5.89 +} 5.90 + 5.91 +int cam_take_picture(void) 5.92 +{ 5.93 return -1; 5.94 } 5.95 5.96 -void cam_shutdown(void) 5.97 +void cam_draw_preview(void) 5.98 { 5.99 + glPushAttrib(GL_ENABLE_BIT); 5.100 + glDisable(GL_DEPTH_TEST); 5.101 + glDisable(GL_LIGHTING); 5.102 + glEnable(GL_TEXTURE_2D); 5.103 + glDisable(GL_BLEND); 5.104 + 5.105 + glUseProgram(0); 5.106 + 5.107 + glBindTexture(GL_TEXTURE_2D, tex); 5.108 + 5.109 + glBegin(GL_QUADS); 5.110 + glTexCoord2f(0, 0); 5.111 + glVertex2f(-1, -1); 5.112 + glTexCoord2f(1, 0); 5.113 + glVertex2f(1, -1); 5.114 + glTexCoord2f(1, 1); 5.115 + glVertex2f(1, 1); 5.116 + glTexCoord2f(0, 1); 5.117 + glVertex2f(-1, 1); 5.118 + glEnd(); 5.119 + 5.120 + glPopAttrib(); 5.121 } 5.122 - 5.123 -unsigned int cam_texture(void) 5.124 -{ 5.125 -} 5.126 - 5.127 -const float *cam_texture_matrix(void) 5.128 -{ 5.129 -} 5.130 - 5.131 -int cam_start_video(void) 5.132 -{ 5.133 -} 5.134 - 5.135 -int cam_stop_video(void) 5.136 -{ 5.137 -} 5.138 - 5.139 -int cam_update(void) 5.140 -{ 5.141 -} 5.142 - 5.143 -int cam_is_capturing(void) 5.144 -{ 5.145 -} 5.146 - 5.147 -int cam_video_size(int *xsz, int *ysz) 5.148 -{ 5.149 -} 5.150 - 5.151 -int cam_take_picture(void) 5.152 -{ 5.153 -}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/pc/main.c Thu Jun 18 03:55:05 2015 +0300 6.3 @@ -0,0 +1,113 @@ 6.4 +#include <stdio.h> 6.5 +#include <stdlib.h> 6.6 +#include "opengl.h" 6.7 +#include <GL/glut.h> 6.8 +#include "game.h" 6.9 +#include "camera.h" 6.10 +#include "timer.h" 6.11 + 6.12 +static int init(void); 6.13 +static void cleanup(void); 6.14 +static void display(void); 6.15 +static void idle(void); 6.16 +static void reshape(int x, int y); 6.17 +static void keyb(unsigned char key, int x, int y); 6.18 +static void keyb_release(unsigned char key, int x, int y); 6.19 +static void mouse(int bn, int state, int x, int y); 6.20 +static void motion(int x, int y); 6.21 +static void sball_motion(int x, int y, int z); 6.22 +static void sball_rotate(int x, int y, int z); 6.23 + 6.24 +int main(int argc, char **argv) 6.25 +{ 6.26 + glutInit(&argc, argv); 6.27 + glutInitWindowSize(800, 600); 6.28 + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); 6.29 + glutCreateWindow("3dphotoshoot (PC)"); 6.30 + 6.31 + glutDisplayFunc(display); 6.32 + glutIdleFunc(idle); 6.33 + glutReshapeFunc(reshape); 6.34 + glutKeyboardFunc(keyb); 6.35 + glutKeyboardUpFunc(keyb_release); 6.36 + glutMouseFunc(mouse); 6.37 + glutMotionFunc(motion); 6.38 + glutSpaceballMotionFunc(sball_motion); 6.39 + glutSpaceballRotateFunc(sball_rotate); 6.40 + 6.41 + if(init() == -1) { 6.42 + return 1; 6.43 + } 6.44 + atexit(cleanup); 6.45 + 6.46 + glutMainLoop(); 6.47 + return 0; 6.48 +} 6.49 + 6.50 +static int init(void) 6.51 +{ 6.52 + glewInit(); 6.53 + 6.54 + if(cam_init(0) == -1) { 6.55 + return -1; 6.56 + } 6.57 + 6.58 + if(game_init() == -1) { 6.59 + return -1; 6.60 + } 6.61 + return 0; 6.62 +} 6.63 + 6.64 +static void cleanup(void) 6.65 +{ 6.66 + game_shutdown(); 6.67 +} 6.68 + 6.69 +static void display(void) 6.70 +{ 6.71 + game_display(get_time_msec()); 6.72 + glutSwapBuffers(); 6.73 +} 6.74 + 6.75 +static void idle(void) 6.76 +{ 6.77 + glutPostRedisplay(); 6.78 +} 6.79 + 6.80 +static void reshape(int x, int y) 6.81 +{ 6.82 + game_reshape(x, y); 6.83 +} 6.84 + 6.85 +static void keyb(unsigned char key, int x, int y) 6.86 +{ 6.87 + switch(key) { 6.88 + case 27: 6.89 + exit(0); 6.90 + } 6.91 + 6.92 + game_keyboard(key, 1); 6.93 +} 6.94 + 6.95 +static void keyb_release(unsigned char key, int x, int y) 6.96 +{ 6.97 + game_keyboard(key, 0); 6.98 +} 6.99 + 6.100 +static void mouse(int bn, int state, int x, int y) 6.101 +{ 6.102 + game_mouse_button(0, bn - GLUT_LEFT_BUTTON, state == GLUT_DOWN ? 1 : 0, x, y); 6.103 +} 6.104 + 6.105 +static void motion(int x, int y) 6.106 +{ 6.107 + game_mouse_motion(0, x, y); 6.108 +} 6.109 + 6.110 +static void sball_motion(int x, int y, int z) 6.111 +{ 6.112 +} 6.113 + 6.114 +static void sball_rotate(int x, int y, int z) 6.115 +{ 6.116 +}
7.1 --- a/src/sanegl.c Thu Jun 18 03:12:30 2015 +0300 7.2 +++ b/src/sanegl.c Thu Jun 18 03:55:05 2015 +0300 7.3 @@ -286,7 +286,7 @@ 7.4 double ndcy = 2.0 * (winy - viewp[1]) / viewp[3] - 1.0; 7.5 double ndcz = 2.0 * winz - 1.0; 7.6 7.7 - // calculate modelviewprojection 7.8 + /* calculate modelviewprojection */ 7.9 gl_matrix_mode(GL_MODELVIEW); 7.10 gl_push_matrix(); 7.11 gl_load_matrixd(proj); 7.12 @@ -294,10 +294,10 @@ 7.13 gl_get_doublev(GL_MODELVIEW_MATRIX, mvp); 7.14 gl_pop_matrix(); 7.15 7.16 - // invert modelviewprojection 7.17 + /* invert modelviewprojection */ 7.18 m4_inverse(inv_mvp, mvp); 7.19 7.20 - // transform ndc by modelview -> obj 7.21 + /* transform ndc by modelview -> obj */ 7.22 /**objx = inv_mvp[0] * ndcx + inv_mvp[4] * ndcy + inv_mvp[8] * ndcz + inv_mvp[12]; 7.23 *objy = inv_mvp[1] * ndcx + inv_mvp[5] * ndcy + inv_mvp[9] * ndcz + inv_mvp[13]; 7.24 *objz = inv_mvp[2] * ndcx + inv_mvp[6] * ndcy + inv_mvp[10] * ndcz + inv_mvp[14];*/