istereo
changeset 1:4d25539806d2
bollocks
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Tue, 06 Sep 2011 12:48:39 +0300 |
parents | 1bb950d0976b |
children | bb68fac22579 |
files | .hgignore Makefile src/glutmain.c src/istereo.c src/opengl.h src/sanegl.c src/sanegl.h src/sdr.c |
diffstat | 8 files changed, 455 insertions(+), 2 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgignore Tue Sep 06 12:48:39 2011 +0300 1.3 @@ -0,0 +1,4 @@ 1.4 +\.o$ 1.5 +\.d$ 1.6 +\.swp$ 1.7 +^test$
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/Makefile Tue Sep 06 12:48:39 2011 +0300 2.3 @@ -0,0 +1,14 @@ 2.4 +src = $(wildcard src/*.c) 2.5 +obj = $(src:.c=.o) 2.6 +bin = test 2.7 + 2.8 +CC = gcc 2.9 +CFLAGS = -pedantic -Wall -g 2.10 +LDFLAGS = -lGL -lGLU -lglut -lGLEW -lm 2.11 + 2.12 +$(bin): $(obj) 2.13 + $(CC) -o $@ $(obj) $(LDFLAGS) 2.14 + 2.15 +.PHONY: clean 2.16 +clean: 2.17 + rm -f $(obj) $(bin)
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/glutmain.c Tue Sep 06 12:48:39 2011 +0300 3.3 @@ -0,0 +1,67 @@ 3.4 +#include <stdio.h> 3.5 +#include <stdlib.h> 3.6 +#include <GL/glew.h> 3.7 +#include <GL/glut.h> 3.8 +#include "sanegl.h" 3.9 + 3.10 +void disp(void); 3.11 +void reshape(int x, int y); 3.12 +void keyb(unsigned char key, int x, int y); 3.13 + 3.14 +int main(int argc, char **argv) 3.15 +{ 3.16 + glutInit(&argc, argv); 3.17 + glutInitWindowSize(960, 640); 3.18 + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); 3.19 + glutCreateWindow("test"); 3.20 + 3.21 + glutDisplayFunc(disp); 3.22 + glutIdleFunc(glutPostRedisplay); 3.23 + glutReshapeFunc(reshape); 3.24 + glutKeyboardFunc(keyb); 3.25 + 3.26 + glutMainLoop(); 3.27 + return 0; 3.28 +} 3.29 + 3.30 +void disp(void) 3.31 +{ 3.32 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 3.33 + 3.34 + glMatrixMode(GL_MODELVIEW); 3.35 + glLoadIdentity(); 3.36 + glTranslatef(0, 0, -8); 3.37 + 3.38 + glBegin(GL_QUADS); 3.39 + glColor3f(1, 0, 0); 3.40 + glVertex3f(-1, -1, 0); 3.41 + glColor3f(0, 1, 0); 3.42 + glVertex3f(1, -1, 0); 3.43 + glColor3f(0, 0, 1); 3.44 + glVertex3f(1, 1, 0); 3.45 + glColor3f(1, 1, 0); 3.46 + glVertex3f(-1, 1, 0); 3.47 + glEnd(); 3.48 + 3.49 + glutSwapBuffers(); 3.50 +} 3.51 + 3.52 +void reshape(int x, int y) 3.53 +{ 3.54 + glViewport(0, 0, x, y); 3.55 + 3.56 + glMatrixMode(GL_PROJECTION); 3.57 + glLoadIdentity(); 3.58 + gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0); 3.59 +} 3.60 + 3.61 +void keyb(unsigned char key, int x, int y) 3.62 +{ 3.63 + switch(key) { 3.64 + case 27: 3.65 + exit(0); 3.66 + 3.67 + default: 3.68 + break; 3.69 + } 3.70 +}
4.1 --- a/src/istereo.c Tue Sep 06 08:07:14 2011 +0300 4.2 +++ b/src/istereo.c Tue Sep 06 12:48:39 2011 +0300 4.3 @@ -1,4 +1,4 @@ 4.4 -#include <OpenGLES/ES2/gl.h> 4.5 +#include "opengl.h" 4.6 #include "istereo.h" 4.7 4.8 int init(void)
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/opengl.h Tue Sep 06 12:48:39 2011 +0300 5.3 @@ -0,0 +1,13 @@ 5.4 +#if TARGETIPHONESIMULATOR || TARGETOSIPHONE 5.5 +#include <OpenGLES/ES2/gl.h> 5.6 +#else 5.7 + 5.8 +#include <GL/glew.h> 5.9 + 5.10 +#ifdef __APPLE__ 5.11 +#include <GLUT/glut.h> 5.12 +#else 5.13 +#include <GL/glut.h> 5.14 +#endif /* __APPLE__ */ 5.15 + 5.16 +#endif
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/sanegl.c Tue Sep 06 12:48:39 2011 +0300 6.3 @@ -0,0 +1,241 @@ 6.4 +#include <math.h> 6.5 +#include <string.h> 6.6 +#include "sanegl.h" 6.7 + 6.8 +#define MMODE_IDX(x) ((x) - GL_MODELVIEW) 6.9 +#define MAT_STACK_SIZE 32 6.10 +#define MAT_IDENT {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1} 6.11 + 6.12 +#define MAX_VERTS 512 6.13 + 6.14 +typedef struct { float x, y; } vec2_t; 6.15 +typedef struct { float x, y, z; } vec3_t; 6.16 +typedef struct { float x, y, z, w; } vec4_t; 6.17 + 6.18 +static int mm_idx = 0; 6.19 +static float mat_stack[3][MAT_STACK_SIZE][16] = {{MAT_IDENT}, {MAT_IDENT}, {MAT_IDENT}}; 6.20 +static int stack_top[3]; 6.21 +static float mat_mvp[16]; 6.22 +static int mvp_valid; 6.23 +static int prim = -1; 6.24 + 6.25 +static vec3_t cur_normal; 6.26 +static vec4_t cur_color, cur_attrib; 6.27 +static vec2_t cur_texcoord; 6.28 + 6.29 +static vec4_t *vert_arr, *col_arr, *attr_arr; 6.30 +static vec3_t *norm_arr; 6.31 +static vec2_t *texc_arr; 6.32 +static int vloc, nloc, cloc, tloc, aloc; 6.33 + 6.34 + 6.35 +void gl_matrix_mode(int mm) 6.36 +{ 6.37 + mm_idx = MMODE_IDX(mm); 6.38 +} 6.39 + 6.40 +void gl_push_matrix(void) 6.41 +{ 6.42 + int top = stack_top[mm_idx]; 6.43 + 6.44 + memcpy(mat_stack[mm_idx][top + 1], mat_stack[mm_idx][top], 16 * sizeof(float)); 6.45 + stack_top[mm_idx]++; 6.46 + mvp_valid = 0; 6.47 +} 6.48 + 6.49 +void gl_pop_matrix(void) 6.50 +{ 6.51 + stack_top[mm_idx]--; 6.52 + mvp_valid = 0; 6.53 +} 6.54 + 6.55 +void gl_load_identity(void) 6.56 +{ 6.57 + static const float idmat[] = MAT_IDENT; 6.58 + int top = stack_top[mm_idx]; 6.59 + float *mat = mat_stack[mm_idx][top]; 6.60 + 6.61 + memcpy(mat, idmat, sizeof idmat); 6.62 + mvp_valid = 0; 6.63 +} 6.64 + 6.65 +void gl_load_matrixf(const float *m) 6.66 +{ 6.67 + int top = stack_top[mm_idx]; 6.68 + float *mat = mat_stack[mm_idx][top]; 6.69 + 6.70 + memcpy(mat, m, 16 * sizeof *mat); 6.71 + mvp_valid = 0; 6.72 +} 6.73 + 6.74 +#define M(i, j) ((i << 2) + j) 6.75 + 6.76 +void gl_mult_matrixf(const float *m2) 6.77 +{ 6.78 + int i, j; 6.79 + int top = stack_top[mm_idx]; 6.80 + float *m1 = mat_stack[mm_idx][top]; 6.81 + float res[16]; 6.82 + 6.83 + for(i=0; i<4; i++) { 6.84 + for(j=0; j<4; j++) { 6.85 + res[M(i,j)] = m1[M(i,0)] * m2[M(0,j)] + 6.86 + m1[M(i,1)] * m2[M(1,j)] + 6.87 + m1[M(i,2)] * m2[M(2,j)] + 6.88 + m1[M(i,3)] * m2[M(3,j)]; 6.89 + } 6.90 + } 6.91 + 6.92 + memcpy(m1, res, sizeof res); 6.93 + mvp_valid = 0; 6.94 +} 6.95 + 6.96 +void gl_translatef(float x, float y, float z) 6.97 +{ 6.98 + float mat[] = MAT_IDENT; 6.99 + 6.100 + mat[12] = x; 6.101 + mat[13] = y; 6.102 + mat[14] = z; 6.103 + 6.104 + gl_mult_matrixf(mat); 6.105 +} 6.106 + 6.107 +void gl_rotatef(float angle, float x, float y, float z) 6.108 +{ 6.109 + float mat[] = MAT_IDENT; 6.110 + 6.111 + float angle_rad = M_PI * angle / 180.0; 6.112 + float sina = sin(angle_rad); 6.113 + float cosa = cos(angle_rad); 6.114 + float one_minus_cosa = 1.0 - cosa; 6.115 + float nxsq = x * x; 6.116 + float nysq = y * y; 6.117 + float nzsq = z * z; 6.118 + 6.119 + mat[0] = nxsq + (1.0 - nxsq) * cosa; 6.120 + mat[4] = x * y * one_minus_cosa - z * sina; 6.121 + mat[8] = x * z * one_minus_cosa + y * sina; 6.122 + mat[1] = x * y * one_minus_cosa + z * sina; 6.123 + mat[5] = nysq + (1.0 - nysq) * cosa; 6.124 + mat[9] = y * z * one_minus_cosa - x * sina; 6.125 + mat[2] = x * z * one_minus_cosa - y * sina; 6.126 + mat[6] = y * z * one_minus_cosa + x * sina; 6.127 + mat[10] = nzsq + (1.0 - nzsq) * cosa; 6.128 + 6.129 + gl_mult_matrixf(mat); 6.130 +} 6.131 + 6.132 +void gl_scalef(float x, float y, float z) 6.133 +{ 6.134 + float mat[] = MAT_IDENT; 6.135 + 6.136 + mat[0] = x; 6.137 + mat[5] = y; 6.138 + mat[10] = z; 6.139 + 6.140 + gl_mult_matrixf(mat); 6.141 +} 6.142 + 6.143 +void gl_ortho(float left, float right, float bottom, float top, float near, float far) 6.144 +{ 6.145 + float mat[] = MAT_IDENT; 6.146 + 6.147 + float dx = right - left; 6.148 + float dy = top - bottom; 6.149 + float dz = far - near; 6.150 + 6.151 + float tx = -(right + left) / dx; 6.152 + float ty = -(top + bottom) / dy; 6.153 + float tz = -(far + near) / dz; 6.154 + 6.155 + float sx = 2.0 / dx; 6.156 + float sy = 2.0 / dy; 6.157 + float sz = -2.0 / dz; 6.158 + 6.159 + mat[0] = sx; 6.160 + mat[5] = sy; 6.161 + mat[10] = sz; 6.162 + mat[12] = tx; 6.163 + mat[13] = ty; 6.164 + mat[14] = tz; 6.165 + 6.166 + gl_mult_matrixf(mat); 6.167 +} 6.168 + 6.169 +void gl_frustum(float left, float right, float bottom, float top, float near, float far) 6.170 +{ 6.171 + float mat[] = MAT_IDENT; 6.172 + 6.173 + float dx = right - left; 6.174 + float dy = top - bottom; 6.175 + float dz = far - near; 6.176 + 6.177 + float a = (right + left) / dx; 6.178 + float b = (top + bottom) / dy; 6.179 + float c = -(far + near) / dz; 6.180 + float d = -2.0 * far * near / dz; 6.181 + 6.182 + mat[0] = 2.0 * near / dx; 6.183 + mat[5] = 2.0 * near / dy; 6.184 + mat[8] = a; 6.185 + mat[9] = b; 6.186 + mat[10] = c; 6.187 + mat[11] = -1.0; 6.188 + mat[14] = d; 6.189 + 6.190 + gl_mult_matrixf(mat); 6.191 +} 6.192 + 6.193 +void glu_perspective(float vfov, float aspect, float near, float far) 6.194 +{ 6.195 + float x = near * tan(vfov / 2.0); 6.196 + gl_frustum(-aspect * x, aspect * x, -x, x, near, far); 6.197 +} 6.198 + 6.199 +void gl_apply_xform(unsigned int prog) 6.200 +{ 6.201 + int loc, mvidx, pidx, tidx, mvtop, ptop, ttop; 6.202 + 6.203 + mvidx = MMODE_IDX(GL_MODELVIEW); 6.204 + pidx = MMODE_IDX(GL_PROJECTION); 6.205 + tidx = MMODE_IDX(GL_TEXTURE); 6.206 + 6.207 + mvtop = stack_top[mvidx]; 6.208 + ptop = stack_top[pidx]; 6.209 + ttop = stack_top[tidx]; 6.210 + 6.211 + if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) { 6.212 + glUniformMatrix4fv(loc, 16, 0, mat_stack[mvidx][mvtop]); 6.213 + } 6.214 + 6.215 + if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) { 6.216 + glUniformMatrix4fv(loc, 16, 0, mat_stack[pidx][ptop]); 6.217 + } 6.218 + 6.219 + if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) { 6.220 + glUniformMatrix4fv(loc, 16, 0, mat_stack[tidx][ttop]); 6.221 + } 6.222 + 6.223 + if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) { 6.224 + float nmat[9]; 6.225 + 6.226 + nmat[0] = mat_stack[mvidx][mvtop][0]; 6.227 + nmat[1] = mat_stack[mvidx][mvtop][1]; 6.228 + nmat[2] = mat_stack[mvidx][mvtop][2]; 6.229 + nmat[3] = mat_stack[mvidx][mvtop][4]; 6.230 + nmat[4] = mat_stack[mvidx][mvtop][5]; 6.231 + nmat[5] = mat_stack[mvidx][mvtop][6]; 6.232 + nmat[6] = mat_stack[mvidx][mvtop][8]; 6.233 + nmat[7] = mat_stack[mvidx][mvtop][9]; 6.234 + nmat[8] = mat_stack[mvidx][mvtop][10]; 6.235 + glUniformMatrix3fv(loc, 9, 0, nmat); 6.236 + } 6.237 + 6.238 + if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) { 6.239 + if(!mvp_valid) { 6.240 + /* TODO calc mvp */ 6.241 + } 6.242 + glUniformMatrix4fv(loc, 16, 0, mat_mvp); 6.243 + } 6.244 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/src/sanegl.h Tue Sep 06 12:48:39 2011 +0300 7.3 @@ -0,0 +1,114 @@ 7.4 +/* 7.5 +SaneGL - a small library to bring back sanity to OpenGL ES 2.x 7.6 +Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org> 7.7 + 7.8 +This program is free software: you can redistribute it and/or modify 7.9 +it under the terms of the GNU General Public License as published by 7.10 +the Free Software Foundation, either version 3 of the License, or 7.11 +(at your option) any later version. 7.12 + 7.13 +This program is distributed in the hope that it will be useful, 7.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 7.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 7.16 +GNU General Public License for more details. 7.17 + 7.18 +You should have received a copy of the GNU General Public License 7.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 7.20 +*/ 7.21 + 7.22 +#ifndef SANEGL_H_ 7.23 +#define SANEGL_H_ 7.24 + 7.25 +#include "opengl.h" 7.26 + 7.27 +#ifndef GL_MODELVIEW 7.28 +#define GL_MODELVIEW 0x1700 7.29 +#endif 7.30 +#ifndef GL_PROJECTION 7.31 +#define GL_PROJECTION 0x1701 7.32 +#endif 7.33 +#ifndef GL_TEXTURE 7.34 +#define GL_TEXTURE 0x1702 7.35 +#endif 7.36 + 7.37 +#ifndef GL_POINTS 7.38 +#define GL_POINTS 0 7.39 +#endif 7.40 +#ifndef GL_LINES 7.41 +#define GL_LINES 1 7.42 +#endif 7.43 +#ifndef GL_TRIANGLES 7.44 +#define GL_TRIANGLES 4 7.45 +#endif 7.46 +#ifndef GL_QUADS 7.47 +#define GL_QUADS 7 7.48 +#endif 7.49 + 7.50 +#ifdef GLDEF 7.51 + 7.52 +#define glMatrixMode gl_matrix_mode 7.53 +#define glPushMatrix gl_push_matrix 7.54 +#define glPopMatrix gl_pop_matrix 7.55 +#define glLoadIdentity gl_load_identity 7.56 +#define glLoadMatrixf gl_load_matrixf 7.57 +#define glMultMatrixf gl_mult_matrixf 7.58 +#define glTranslatef gl_translatef 7.59 +#define glRotatef gl_rotatef 7.60 +#define glScalef gl_scalef 7.61 +#define glOrtho gl_ortho 7.62 +#define glFrustum gl_frustum 7.63 +#define gluPerspective glu_perspective 7.64 + 7.65 +#define glBegin gl_begin 7.66 +#define glEnd gl_end 7.67 +#define glVertex2f gl_vertex2f 7.68 +#define glVertex3f gl_vertex3f 7.69 +#define glVertex4f gl_vertex4f 7.70 +#define glNormal3f gl_normal3f 7.71 +#define glColor3f gl_color3f 7.72 +#define glColor4f gl_color4f 7.73 +#define glTexCoord1f gl_texcoord1f 7.74 +#define glTexCoord2f gl_texcoord2f 7.75 +#define glVertexAttrib2f gl_vertex_attrib2f 7.76 +#define glVertexAttrib3f gl_vertex_attrib3f 7.77 +#define glVertexAttrib4f gl_vertex_attrib4f 7.78 +#endif 7.79 + 7.80 +/* matrix stuff */ 7.81 +void gl_matrix_mode(int mmode); 7.82 +void gl_push_matrix(void); 7.83 +void gl_pop_matrix(void); 7.84 +void gl_load_identity(void); 7.85 +void gl_load_matrixf(const float *mat); 7.86 +void gl_mult_matrixf(const float *mat); 7.87 +void gl_translatef(float x, float y, float z); 7.88 +void gl_rotatef(float angle, float x, float y, float z); 7.89 +void gl_scalef(float x, float y, float z); 7.90 +void gl_ortho(float left, float right, float bottom, float top, float near, float far); 7.91 +void gl_frustum(float left, float right, float bottom, float top, float near, float far); 7.92 +void glu_perspective(float vfov, float aspect, float near, float far); 7.93 + 7.94 +void gl_apply_xform(unsigned int prog); 7.95 + 7.96 + 7.97 +/* immediate mode rendering */ 7.98 +void gl_begin(int prim); 7.99 +void gl_end(void); 7.100 + 7.101 +void gl_vertex2f(float x, float y); 7.102 +void gl_vertex3f(float x, float y, float z); 7.103 +void gl_vertex4f(float x, float y, float z, float w); 7.104 + 7.105 +void gl_normal3f(float x, float y, float z); 7.106 + 7.107 +void gl_color3f(float r, float g, float b); 7.108 +void gl_color4f(float r, float g, float b, float a); 7.109 + 7.110 +void gl_texcoord1f(float s); 7.111 +void gl_texcoord2f(float s, float t); 7.112 + 7.113 +void gl_vertex_attrib2f(int loc, float x, float y); 7.114 +void gl_vertex_attrib3f(int loc, float x, float y, float z); 7.115 +void gl_vertex_attrib4f(int loc, float x, float y, float z, float w); 7.116 + 7.117 +#endif /* SANEGL_H_ */
8.1 --- a/src/sdr.c Tue Sep 06 08:07:14 2011 +0300 8.2 +++ b/src/sdr.c Tue Sep 06 12:48:39 2011 +0300 8.3 @@ -3,7 +3,7 @@ 8.4 #include <string.h> 8.5 #include <errno.h> 8.6 #include <assert.h> 8.7 -#include <OpenGLES/ES2/gl.h> 8.8 +#include "opengl.h" 8.9 8.10 #if defined(unix) || defined(__unix__) 8.11 #include <unistd.h>