sanegl
changeset 0:00b315b6db1e tip
sanegl initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 23 Jun 2011 05:19:40 +0300 |
parents | |
children | |
files | Makefile sanegl.c sanegl.h sanegl_attr.h test.c |
diffstat | 5 files changed, 326 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/Makefile Thu Jun 23 05:19:40 2011 +0300 1.3 @@ -0,0 +1,20 @@ 1.4 +src = $(wildcard *.c) 1.5 +obj = $(src:.c=.o) 1.6 +bin = test 1.7 + 1.8 +CC = gcc 1.9 +CFLAGS = -pedantic -Wall -g 1.10 +LDFLAGS = $(libgl) 1.11 + 1.12 +ifeq ($(shell uname -s), Darwin) 1.13 + libgl = -framework OpenGL -framework GLUT 1.14 +else 1.15 + libgl = -lGL -lGLU -lglut 1.16 +endif 1.17 + 1.18 +$(bin): $(obj) 1.19 + $(CC) -o $@ $(obj) $(LDFLAGS) 1.20 + 1.21 +.PHONY: clean 1.22 +clean: 1.23 + rm -f $(obj) $(bin)
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/sanegl.c Thu Jun 23 05:19:40 2011 +0300 2.3 @@ -0,0 +1,73 @@ 2.4 +#include <stdio.h> 2.5 + 2.6 +#define SANEGL_IMPL_ 2.7 +#include "sanegl.h" 2.8 + 2.9 +struct vertex { 2.10 + float pos[4]; 2.11 + float color[4]; 2.12 + float tex[2]; 2.13 + float norm[3]; 2.14 +}; 2.15 + 2.16 +static const int mode_verts[] = { 2.17 + 1, /* GL_POINTS */ 2.18 + 2, /* GL_LINES */ 2.19 + -1, /* GL_LINE_LOOP TODO */ 2.20 + -1, /* GL_LINE_STRIP TODO */ 2.21 + 3, /* GL_TRIANGLES */ 2.22 + -1, /* GL_TRIANGLE_STRIP TODO */ 2.23 + -1, /* GL_TRIANGLE_FAN TODO */ 2.24 + 6, /* GL_QUADS (two triangles) */ 2.25 + -1, /* GL_QUAD_STRIP TODO */ 2.26 + -1 /* GL_POLYGON TODO */ 2.27 +}; 2.28 + 2.29 +static struct vertex vcur = { 2.30 + {0, 0, 0, 1}, /* position */ 2.31 + {1, 1, 1, 1}, /* color */ 2.32 + {0, 0}, /* tex-coord */ 2.33 + {0, 0, 0} /* normal */ 2.34 +}; 2.35 + 2.36 + 2.37 +void glBegin(GLenum mode) 2.38 +{ 2.39 + printf("glBegin\n"); 2.40 +} 2.41 + 2.42 +void glEnd(void) 2.43 +{ 2.44 + printf("glEnd\n"); 2.45 +} 2.46 + 2.47 +void glVertex4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w) 2.48 +{ 2.49 + vcur.pos[0] = x; 2.50 + vcur.pos[1] = y; 2.51 + vcur.pos[2] = z; 2.52 + vcur.pos[3] = w; 2.53 + 2.54 + varr[nvert++] = vcur; 2.55 +} 2.56 + 2.57 +void glColor4f(GLfloat r, GLfloat g, GLfloat, b, GLfloat a) 2.58 +{ 2.59 + vcur.color[0] = r; 2.60 + vcur.color[1] = g; 2.61 + vcur.color[2] = b; 2.62 + vcur.color[3] = a; 2.63 +} 2.64 + 2.65 +void glNormal3f(GLfloat x, GLfloat y, GLfloat z) 2.66 +{ 2.67 + vcur.normal[0] = x; 2.68 + vcur.normal[1] = y; 2.69 + vcur.normal[2] = z; 2.70 +} 2.71 + 2.72 +void glTexCoord4f(GLfloat x, GLfloat y) 2.73 +{ 2.74 + vcur.tex[0] = x; 2.75 + vcur.tex[1] = y; 2.76 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/sanegl.h Thu Jun 23 05:19:40 2011 +0300 3.3 @@ -0,0 +1,19 @@ 3.4 +#ifndef SANEGL_H_ 3.5 +#define SANEGL_H_ 3.6 + 3.7 +#ifndef __APPLE__ 3.8 +#include <GL/gl.h> 3.9 +#else 3.10 +#include <OpenGL/gl.h> 3.11 +#endif 3.12 + 3.13 +#ifndef GL_QUADS 3.14 +#define GL_QUADS 7 3.15 +#endif 3.16 + 3.17 +void glBegin(GLenum mode); 3.18 +void glEnd(void); 3.19 + 3.20 +#include "sanegl_attr.h" 3.21 + 3.22 +#endif /* SANEGL_H_ */
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/sanegl_attr.h Thu Jun 23 05:19:40 2011 +0300 4.3 @@ -0,0 +1,161 @@ 4.4 +#ifndef SANEGL_ATTR_H_ 4.5 +#define SANEGL_ATTR_H_ 4.6 + 4.7 +#ifndef SANEGL_IMPL_ 4.8 +/* in the header file just expand to the prototypes */ 4.9 +#define GLATTR1(name, type, suffix, conv) \ 4.10 + void gl##name##1##suffix(GL##type x); 4.11 + 4.12 +#define GLATTR2(name, type, suffix, conv) \ 4.13 + void gl##name##2##suffix(GL##type x, GL##type y); 4.14 + 4.15 +#define GLATTR3(name, type, suffix, conv) \ 4.16 + void gl##name##3##suffix(GL##type x, GL##type y, GL##type z); 4.17 + 4.18 +#define GLATTR4(name, type, suffix, conv) \ 4.19 + void gl##name##3##suffix(GL##type x, GL##type y, GL##type z, GL##type w); 4.20 + 4.21 +#define GLATTR1V(name, type, suffix, conv) \ 4.22 + void gl##name##1##suffix##v(const GL##type *p); 4.23 + 4.24 +#define GLATTR2V(name, type, suffix, conv) \ 4.25 + void gl##name##2##suffix##v(const GL##type *p); 4.26 + 4.27 +#define GLATTR3V(name, type, suffix, conv) \ 4.28 + void gl##name##3##suffix##v(const GL##type *p); 4.29 + 4.30 +#define GLATTR4V(name, type, suffix, conv) \ 4.31 + void gl##name##4##suffix##v(const GL##type *p); 4.32 +#else 4.33 +/* this full expansion will be performed in the implementation (sanegl.c) */ 4.34 +#define GLATTR1(name, type, suffix, conv) \ 4.35 + void gl##name##1##suffix(GL##type x) \ 4.36 + { gl##name##4f(conv(x), 0, 0, 1); } 4.37 + 4.38 +#define GLATTR2(name, type, suffix, conv) \ 4.39 + void gl##name##2##suffix(GL##type x, GL##type y) \ 4.40 + { gl##name##4f(conv(x), conv(y), 0, 1); } 4.41 + 4.42 +#define GLATTR3(name, type, suffix, conv) \ 4.43 + void gl##name##3##suffix(GL##type x, GL##type y, GL##type z) \ 4.44 + { gl##name##4f(conv(x), conv(y), conv(z), 1); } 4.45 + 4.46 +#define GLATTR4(name, type, suffix, conv) \ 4.47 + void gl##name##3##suffix(GL##type x, GL##type y, GL##type z, GL##type w) \ 4.48 + { gl##name##4f(conv(x), conv(y), conv(z), conv(w)); } 4.49 + 4.50 +#define GLATTR1V(name, type, suffix, conv) \ 4.51 + void gl##name##1##suffix##v(const GL##type *p) \ 4.52 + { gl##name##4f(conv(*p), 0, 0, 1); } 4.53 + 4.54 +#define GLATTR2V(name, type, suffix, conv) \ 4.55 + void gl##name##2##suffix##v(const GL##type *p) \ 4.56 + { gl##name##4f(conv(p[0]), conv(p[1]), 0, 1); } 4.57 + 4.58 +#define GLATTR3V(name, type, suffix, conv) \ 4.59 + void gl##name##3##suffix##v(const GL##type *p) \ 4.60 + { gl##name##4f(conv(p[0]), conv(p[1]), conv(p[2]), 1); } 4.61 + 4.62 +#define GLATTR4V(name, type, suffix, conv) \ 4.63 + void gl##name##4##suffix##v(const GL##type *p) \ 4.64 + { gl##name##4f(conv(p[0]), conv(p[1]), conv(p[2]), conv(p[3])); } 4.65 +#endif 4.66 + 4.67 +/* n-arg functions */ 4.68 +GLATTR2(Vertex, float, f, (float)) 4.69 +GLATTR3(Vertex, float, f, (float)) 4.70 + 4.71 +GLATTR2(Vertex, double, d, (float)) 4.72 +GLATTR3(Vertex, double, d, (float)) 4.73 +GLATTR4(Vertex, double, d, (float)) 4.74 + 4.75 +GLATTR2(Vertex, int, i, (float)) 4.76 +GLATTR3(Vertex, int, i, (float)) 4.77 +GLATTR4(Vertex, int, i, (float)) 4.78 + 4.79 +GLATTR2(Vertex, short, s, (float)) 4.80 +GLATTR3(Vertex, short, s, (float)) 4.81 +GLATTR4(Vertex, short, s, (float)) 4.82 + 4.83 +GLATTR3(Color, float, f, (float)) 4.84 +GLATTR3(Color, double, d, (float)) 4.85 +GLATTR4(Color, double, d, (float)) 4.86 + 4.87 +GLATTR3(Color, byte, b, (1.0f / CHAR_MAX) * (float)) 4.88 +GLATTR4(Color, byte, b, (1.0f / CHAR_MAX) * (float)) 4.89 + 4.90 +GLATTR3(Color, ubyte, ub, (1.0f / UCHAR_MAX) * (float)) 4.91 +GLATTR4(Color, ubyte, ub, (1.0f / UCHAR_MAX) * (float)) 4.92 + 4.93 +GLATTR3(Color, short, s, (1.0f / SHRT_MAX) * (float)) 4.94 +GLATTR4(Color, short, s, (1.0f / SHRT_MAX) * (float)) 4.95 + 4.96 +GLATTR3(Color, ushort, us, (1.0f / USHRT_MAX) * (float)) 4.97 +GLATTR4(Color, ushort, us, (1.0f / USHRT_MAX) * (float)) 4.98 + 4.99 +GLATTR3(Color, int, i, (1.0f / INT_MAX) * (float)) 4.100 +GLATTR4(Color, int, i, (1.0f / INT_MAX) * (float)) 4.101 + 4.102 +GLATTR3(Color, uint, ui, (1.0f / UINT_MAX) * (float)) 4.103 +GLATTR4(Color, uint, ui, (1.0f / UINT_MAX) * (float)) 4.104 + 4.105 +GLATTR1(TexCoord, float, f, (float)) 4.106 +GLATTR2(TexCoord, float, f, (float)) 4.107 +GLATTR3(TexCoord, float, f, (float)) 4.108 + 4.109 +GLATTR1(TexCoord, double, d, (float)) 4.110 +GLATTR2(TexCoord, double, d, (float)) 4.111 +GLATTR3(TexCoord, double, d, (float)) 4.112 +GLATTR4(TexCoord, double, d, (float)) 4.113 + 4.114 +GLATTR1(TexCoord, int, i, (float)) 4.115 +GLATTR2(TexCoord, int, i, (float)) 4.116 +GLATTR3(TexCoord, int, i, (float)) 4.117 +GLATTR4(TexCoord, int, i, (float)) 4.118 + 4.119 +GLATTR1(TexCoord, short, s, (float)) 4.120 +GLATTR2(TexCoord, short, s, (float)) 4.121 +GLATTR3(TexCoord, short, s, (float)) 4.122 +GLATTR4(TexCoord, short, s, (float)) 4.123 + 4.124 +/* vector functions */ 4.125 +GLATTR2V(Vertex, float, f, (float)) 4.126 +GLATTR3V(Vertex, float, f, (float)) 4.127 + 4.128 +GLATTR2V(Vertex, double, d, (float)) 4.129 +GLATTR3V(Vertex, double, d, (float)) 4.130 +GLATTR4V(Vertex, double, d, (float)) 4.131 + 4.132 +GLATTR2V(Vertex, int, i, (float)) 4.133 +GLATTR3V(Vertex, int, i, (float)) 4.134 +GLATTR4V(Vertex, int, i, (float)) 4.135 + 4.136 +GLATTR2V(Vertex, short, s, (float)) 4.137 +GLATTR3V(Vertex, short, s, (float)) 4.138 +GLATTR4V(Vertex, short, s, (float)) 4.139 + 4.140 +GLATTR3V(Color, float, f, (float)) 4.141 +GLATTR3V(Color, double, d, (float)) 4.142 +GLATTR4V(Color, double, d, (float)) 4.143 + 4.144 +GLATTR1V(TexCoord, float, f, (float)) 4.145 +GLATTR2V(TexCoord, float, f, (float)) 4.146 +GLATTR3V(TexCoord, float, f, (float)) 4.147 + 4.148 +GLATTR1V(TexCoord, double, d, (float)) 4.149 +GLATTR2V(TexCoord, double, d, (float)) 4.150 +GLATTR3V(TexCoord, double, d, (float)) 4.151 +GLATTR4V(TexCoord, double, d, (float)) 4.152 + 4.153 +GLATTR1V(TexCoord, int, i, (float)) 4.154 +GLATTR2V(TexCoord, int, i, (float)) 4.155 +GLATTR3V(TexCoord, int, i, (float)) 4.156 +GLATTR4V(TexCoord, int, i, (float)) 4.157 + 4.158 +GLATTR1V(TexCoord, short, s, (float)) 4.159 +GLATTR2V(TexCoord, short, s, (float)) 4.160 +GLATTR3V(TexCoord, short, s, (float)) 4.161 +GLATTR4V(TexCoord, short, s, (float)) 4.162 + 4.163 + 4.164 +#endif /* SANEGL_ATTR_H_ */
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test.c Thu Jun 23 05:19:40 2011 +0300 5.3 @@ -0,0 +1,53 @@ 5.4 +#include <stdlib.h> 5.5 +#include <GL/glut.h> 5.6 +#include "sanegl.h" 5.7 + 5.8 +void disp(void); 5.9 +void reshape(int x, int y); 5.10 +void keyb(unsigned char key, int x, int y); 5.11 + 5.12 + 5.13 +int main(int argc, char **argv) 5.14 +{ 5.15 + glutInit(&argc, argv); 5.16 + glutInitWindowSize(800, 600); 5.17 + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); 5.18 + glutCreateWindow("foo"); 5.19 + 5.20 + glutDisplayFunc(disp); 5.21 + glutReshapeFunc(reshape); 5.22 + glutKeyboardFunc(keyb); 5.23 + 5.24 + glutMainLoop(); 5.25 + return 0; 5.26 +} 5.27 + 5.28 +void disp(void) 5.29 +{ 5.30 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 5.31 + 5.32 + glBegin(GL_QUADS); 5.33 + glColor3f(1, 0, 0); 5.34 + glVertex2f(-0.5, -0.5); 5.35 + glColor3f(0, 1, 0); 5.36 + glVertex2f(0.5, -0.5); 5.37 + glColor3f(0, 0, 1); 5.38 + glVertex2f(0.5, 0.5); 5.39 + glColor3f(1, 1, 0); 5.40 + glVertex2f(-0.5, 0.5); 5.41 + glEnd(); 5.42 + 5.43 + glutSwapBuffers(); 5.44 +} 5.45 + 5.46 +void reshape(int x, int y) 5.47 +{ 5.48 + glViewport(0, 0, x, y); 5.49 +} 5.50 + 5.51 +void keyb(unsigned char key, int x, int y) 5.52 +{ 5.53 + if(key == 27) { 5.54 + exit(0); 5.55 + } 5.56 +}