# HG changeset patch # User John Tsiombikas # Date 1315302519 -10800 # Node ID 4d25539806d2bc2ed28e4424a563c93a442a5f22 # Parent 1bb950d0976b0b61fcabc3c971c9dd8686021196 bollocks diff -r 1bb950d0976b -r 4d25539806d2 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Tue Sep 06 12:48:39 2011 +0300 @@ -0,0 +1,4 @@ +\.o$ +\.d$ +\.swp$ +^test$ diff -r 1bb950d0976b -r 4d25539806d2 Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Tue Sep 06 12:48:39 2011 +0300 @@ -0,0 +1,14 @@ +src = $(wildcard src/*.c) +obj = $(src:.c=.o) +bin = test + +CC = gcc +CFLAGS = -pedantic -Wall -g +LDFLAGS = -lGL -lGLU -lglut -lGLEW -lm + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff -r 1bb950d0976b -r 4d25539806d2 src/glutmain.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/glutmain.c Tue Sep 06 12:48:39 2011 +0300 @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include "sanegl.h" + +void disp(void); +void reshape(int x, int y); +void keyb(unsigned char key, int x, int y); + +int main(int argc, char **argv) +{ + glutInit(&argc, argv); + glutInitWindowSize(960, 640); + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); + glutCreateWindow("test"); + + glutDisplayFunc(disp); + glutIdleFunc(glutPostRedisplay); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); + + glutMainLoop(); + return 0; +} + +void disp(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0, 0, -8); + + glBegin(GL_QUADS); + glColor3f(1, 0, 0); + glVertex3f(-1, -1, 0); + glColor3f(0, 1, 0); + glVertex3f(1, -1, 0); + glColor3f(0, 0, 1); + glVertex3f(1, 1, 0); + glColor3f(1, 1, 0); + glVertex3f(-1, 1, 0); + glEnd(); + + glutSwapBuffers(); +} + +void reshape(int x, int y) +{ + glViewport(0, 0, x, y); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0); +} + +void keyb(unsigned char key, int x, int y) +{ + switch(key) { + case 27: + exit(0); + + default: + break; + } +} diff -r 1bb950d0976b -r 4d25539806d2 src/istereo.c --- a/src/istereo.c Tue Sep 06 08:07:14 2011 +0300 +++ b/src/istereo.c Tue Sep 06 12:48:39 2011 +0300 @@ -1,4 +1,4 @@ -#include +#include "opengl.h" #include "istereo.h" int init(void) diff -r 1bb950d0976b -r 4d25539806d2 src/opengl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/opengl.h Tue Sep 06 12:48:39 2011 +0300 @@ -0,0 +1,13 @@ +#if TARGETIPHONESIMULATOR || TARGETOSIPHONE +#include +#else + +#include + +#ifdef __APPLE__ +#include +#else +#include +#endif /* __APPLE__ */ + +#endif diff -r 1bb950d0976b -r 4d25539806d2 src/sanegl.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sanegl.c Tue Sep 06 12:48:39 2011 +0300 @@ -0,0 +1,241 @@ +#include +#include +#include "sanegl.h" + +#define MMODE_IDX(x) ((x) - GL_MODELVIEW) +#define MAT_STACK_SIZE 32 +#define MAT_IDENT {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1} + +#define MAX_VERTS 512 + +typedef struct { float x, y; } vec2_t; +typedef struct { float x, y, z; } vec3_t; +typedef struct { float x, y, z, w; } vec4_t; + +static int mm_idx = 0; +static float mat_stack[3][MAT_STACK_SIZE][16] = {{MAT_IDENT}, {MAT_IDENT}, {MAT_IDENT}}; +static int stack_top[3]; +static float mat_mvp[16]; +static int mvp_valid; +static int prim = -1; + +static vec3_t cur_normal; +static vec4_t cur_color, cur_attrib; +static vec2_t cur_texcoord; + +static vec4_t *vert_arr, *col_arr, *attr_arr; +static vec3_t *norm_arr; +static vec2_t *texc_arr; +static int vloc, nloc, cloc, tloc, aloc; + + +void gl_matrix_mode(int mm) +{ + mm_idx = MMODE_IDX(mm); +} + +void gl_push_matrix(void) +{ + int top = stack_top[mm_idx]; + + memcpy(mat_stack[mm_idx][top + 1], mat_stack[mm_idx][top], 16 * sizeof(float)); + stack_top[mm_idx]++; + mvp_valid = 0; +} + +void gl_pop_matrix(void) +{ + stack_top[mm_idx]--; + mvp_valid = 0; +} + +void gl_load_identity(void) +{ + static const float idmat[] = MAT_IDENT; + int top = stack_top[mm_idx]; + float *mat = mat_stack[mm_idx][top]; + + memcpy(mat, idmat, sizeof idmat); + mvp_valid = 0; +} + +void gl_load_matrixf(const float *m) +{ + int top = stack_top[mm_idx]; + float *mat = mat_stack[mm_idx][top]; + + memcpy(mat, m, 16 * sizeof *mat); + mvp_valid = 0; +} + +#define M(i, j) ((i << 2) + j) + +void gl_mult_matrixf(const float *m2) +{ + int i, j; + int top = stack_top[mm_idx]; + float *m1 = mat_stack[mm_idx][top]; + float res[16]; + + for(i=0; i<4; i++) { + for(j=0; j<4; j++) { + res[M(i,j)] = m1[M(i,0)] * m2[M(0,j)] + + m1[M(i,1)] * m2[M(1,j)] + + m1[M(i,2)] * m2[M(2,j)] + + m1[M(i,3)] * m2[M(3,j)]; + } + } + + memcpy(m1, res, sizeof res); + mvp_valid = 0; +} + +void gl_translatef(float x, float y, float z) +{ + float mat[] = MAT_IDENT; + + mat[12] = x; + mat[13] = y; + mat[14] = z; + + gl_mult_matrixf(mat); +} + +void gl_rotatef(float angle, float x, float y, float z) +{ + float mat[] = MAT_IDENT; + + float angle_rad = M_PI * angle / 180.0; + float sina = sin(angle_rad); + float cosa = cos(angle_rad); + float one_minus_cosa = 1.0 - cosa; + float nxsq = x * x; + float nysq = y * y; + float nzsq = z * z; + + mat[0] = nxsq + (1.0 - nxsq) * cosa; + mat[4] = x * y * one_minus_cosa - z * sina; + mat[8] = x * z * one_minus_cosa + y * sina; + mat[1] = x * y * one_minus_cosa + z * sina; + mat[5] = nysq + (1.0 - nysq) * cosa; + mat[9] = y * z * one_minus_cosa - x * sina; + mat[2] = x * z * one_minus_cosa - y * sina; + mat[6] = y * z * one_minus_cosa + x * sina; + mat[10] = nzsq + (1.0 - nzsq) * cosa; + + gl_mult_matrixf(mat); +} + +void gl_scalef(float x, float y, float z) +{ + float mat[] = MAT_IDENT; + + mat[0] = x; + mat[5] = y; + mat[10] = z; + + gl_mult_matrixf(mat); +} + +void gl_ortho(float left, float right, float bottom, float top, float near, float far) +{ + float mat[] = MAT_IDENT; + + float dx = right - left; + float dy = top - bottom; + float dz = far - near; + + float tx = -(right + left) / dx; + float ty = -(top + bottom) / dy; + float tz = -(far + near) / dz; + + float sx = 2.0 / dx; + float sy = 2.0 / dy; + float sz = -2.0 / dz; + + mat[0] = sx; + mat[5] = sy; + mat[10] = sz; + mat[12] = tx; + mat[13] = ty; + mat[14] = tz; + + gl_mult_matrixf(mat); +} + +void gl_frustum(float left, float right, float bottom, float top, float near, float far) +{ + float mat[] = MAT_IDENT; + + float dx = right - left; + float dy = top - bottom; + float dz = far - near; + + float a = (right + left) / dx; + float b = (top + bottom) / dy; + float c = -(far + near) / dz; + float d = -2.0 * far * near / dz; + + mat[0] = 2.0 * near / dx; + mat[5] = 2.0 * near / dy; + mat[8] = a; + mat[9] = b; + mat[10] = c; + mat[11] = -1.0; + mat[14] = d; + + gl_mult_matrixf(mat); +} + +void glu_perspective(float vfov, float aspect, float near, float far) +{ + float x = near * tan(vfov / 2.0); + gl_frustum(-aspect * x, aspect * x, -x, x, near, far); +} + +void gl_apply_xform(unsigned int prog) +{ + int loc, mvidx, pidx, tidx, mvtop, ptop, ttop; + + mvidx = MMODE_IDX(GL_MODELVIEW); + pidx = MMODE_IDX(GL_PROJECTION); + tidx = MMODE_IDX(GL_TEXTURE); + + mvtop = stack_top[mvidx]; + ptop = stack_top[pidx]; + ttop = stack_top[tidx]; + + if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) { + glUniformMatrix4fv(loc, 16, 0, mat_stack[mvidx][mvtop]); + } + + if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) { + glUniformMatrix4fv(loc, 16, 0, mat_stack[pidx][ptop]); + } + + if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) { + glUniformMatrix4fv(loc, 16, 0, mat_stack[tidx][ttop]); + } + + if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) { + float nmat[9]; + + nmat[0] = mat_stack[mvidx][mvtop][0]; + nmat[1] = mat_stack[mvidx][mvtop][1]; + nmat[2] = mat_stack[mvidx][mvtop][2]; + nmat[3] = mat_stack[mvidx][mvtop][4]; + nmat[4] = mat_stack[mvidx][mvtop][5]; + nmat[5] = mat_stack[mvidx][mvtop][6]; + nmat[6] = mat_stack[mvidx][mvtop][8]; + nmat[7] = mat_stack[mvidx][mvtop][9]; + nmat[8] = mat_stack[mvidx][mvtop][10]; + glUniformMatrix3fv(loc, 9, 0, nmat); + } + + if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) { + if(!mvp_valid) { + /* TODO calc mvp */ + } + glUniformMatrix4fv(loc, 16, 0, mat_mvp); + } +} diff -r 1bb950d0976b -r 4d25539806d2 src/sanegl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sanegl.h Tue Sep 06 12:48:39 2011 +0300 @@ -0,0 +1,114 @@ +/* +SaneGL - a small library to bring back sanity to OpenGL ES 2.x +Copyright (C) 2011 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 SANEGL_H_ +#define SANEGL_H_ + +#include "opengl.h" + +#ifndef GL_MODELVIEW +#define GL_MODELVIEW 0x1700 +#endif +#ifndef GL_PROJECTION +#define GL_PROJECTION 0x1701 +#endif +#ifndef GL_TEXTURE +#define GL_TEXTURE 0x1702 +#endif + +#ifndef GL_POINTS +#define GL_POINTS 0 +#endif +#ifndef GL_LINES +#define GL_LINES 1 +#endif +#ifndef GL_TRIANGLES +#define GL_TRIANGLES 4 +#endif +#ifndef GL_QUADS +#define GL_QUADS 7 +#endif + +#ifdef GLDEF + +#define glMatrixMode gl_matrix_mode +#define glPushMatrix gl_push_matrix +#define glPopMatrix gl_pop_matrix +#define glLoadIdentity gl_load_identity +#define glLoadMatrixf gl_load_matrixf +#define glMultMatrixf gl_mult_matrixf +#define glTranslatef gl_translatef +#define glRotatef gl_rotatef +#define glScalef gl_scalef +#define glOrtho gl_ortho +#define glFrustum gl_frustum +#define gluPerspective glu_perspective + +#define glBegin gl_begin +#define glEnd gl_end +#define glVertex2f gl_vertex2f +#define glVertex3f gl_vertex3f +#define glVertex4f gl_vertex4f +#define glNormal3f gl_normal3f +#define glColor3f gl_color3f +#define glColor4f gl_color4f +#define glTexCoord1f gl_texcoord1f +#define glTexCoord2f gl_texcoord2f +#define glVertexAttrib2f gl_vertex_attrib2f +#define glVertexAttrib3f gl_vertex_attrib3f +#define glVertexAttrib4f gl_vertex_attrib4f +#endif + +/* matrix stuff */ +void gl_matrix_mode(int mmode); +void gl_push_matrix(void); +void gl_pop_matrix(void); +void gl_load_identity(void); +void gl_load_matrixf(const float *mat); +void gl_mult_matrixf(const float *mat); +void gl_translatef(float x, float y, float z); +void gl_rotatef(float angle, float x, float y, float z); +void gl_scalef(float x, float y, float z); +void gl_ortho(float left, float right, float bottom, float top, float near, float far); +void gl_frustum(float left, float right, float bottom, float top, float near, float far); +void glu_perspective(float vfov, float aspect, float near, float far); + +void gl_apply_xform(unsigned int prog); + + +/* immediate mode rendering */ +void gl_begin(int prim); +void gl_end(void); + +void gl_vertex2f(float x, float y); +void gl_vertex3f(float x, float y, float z); +void gl_vertex4f(float x, float y, float z, float w); + +void gl_normal3f(float x, float y, float z); + +void gl_color3f(float r, float g, float b); +void gl_color4f(float r, float g, float b, float a); + +void gl_texcoord1f(float s); +void gl_texcoord2f(float s, float t); + +void gl_vertex_attrib2f(int loc, float x, float y); +void gl_vertex_attrib3f(int loc, float x, float y, float z); +void gl_vertex_attrib4f(int loc, float x, float y, float z, float w); + +#endif /* SANEGL_H_ */ diff -r 1bb950d0976b -r 4d25539806d2 src/sdr.c --- a/src/sdr.c Tue Sep 06 08:07:14 2011 +0300 +++ b/src/sdr.c Tue Sep 06 12:48:39 2011 +0300 @@ -3,7 +3,7 @@ #include #include #include -#include +#include "opengl.h" #if defined(unix) || defined(__unix__) #include