# HG changeset patch # User John Tsiombikas # Date 1308795580 -10800 # Node ID 00b315b6db1e67a06c0073362ba17293e8f1cbbe sanegl initial commit diff -r 000000000000 -r 00b315b6db1e Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Thu Jun 23 05:19:40 2011 +0300 @@ -0,0 +1,20 @@ +src = $(wildcard *.c) +obj = $(src:.c=.o) +bin = test + +CC = gcc +CFLAGS = -pedantic -Wall -g +LDFLAGS = $(libgl) + +ifeq ($(shell uname -s), Darwin) + libgl = -framework OpenGL -framework GLUT +else + libgl = -lGL -lGLU -lglut +endif + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff -r 000000000000 -r 00b315b6db1e sanegl.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sanegl.c Thu Jun 23 05:19:40 2011 +0300 @@ -0,0 +1,73 @@ +#include + +#define SANEGL_IMPL_ +#include "sanegl.h" + +struct vertex { + float pos[4]; + float color[4]; + float tex[2]; + float norm[3]; +}; + +static const int mode_verts[] = { + 1, /* GL_POINTS */ + 2, /* GL_LINES */ + -1, /* GL_LINE_LOOP TODO */ + -1, /* GL_LINE_STRIP TODO */ + 3, /* GL_TRIANGLES */ + -1, /* GL_TRIANGLE_STRIP TODO */ + -1, /* GL_TRIANGLE_FAN TODO */ + 6, /* GL_QUADS (two triangles) */ + -1, /* GL_QUAD_STRIP TODO */ + -1 /* GL_POLYGON TODO */ +}; + +static struct vertex vcur = { + {0, 0, 0, 1}, /* position */ + {1, 1, 1, 1}, /* color */ + {0, 0}, /* tex-coord */ + {0, 0, 0} /* normal */ +}; + + +void glBegin(GLenum mode) +{ + printf("glBegin\n"); +} + +void glEnd(void) +{ + printf("glEnd\n"); +} + +void glVertex4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w) +{ + vcur.pos[0] = x; + vcur.pos[1] = y; + vcur.pos[2] = z; + vcur.pos[3] = w; + + varr[nvert++] = vcur; +} + +void glColor4f(GLfloat r, GLfloat g, GLfloat, b, GLfloat a) +{ + vcur.color[0] = r; + vcur.color[1] = g; + vcur.color[2] = b; + vcur.color[3] = a; +} + +void glNormal3f(GLfloat x, GLfloat y, GLfloat z) +{ + vcur.normal[0] = x; + vcur.normal[1] = y; + vcur.normal[2] = z; +} + +void glTexCoord4f(GLfloat x, GLfloat y) +{ + vcur.tex[0] = x; + vcur.tex[1] = y; +} diff -r 000000000000 -r 00b315b6db1e sanegl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sanegl.h Thu Jun 23 05:19:40 2011 +0300 @@ -0,0 +1,19 @@ +#ifndef SANEGL_H_ +#define SANEGL_H_ + +#ifndef __APPLE__ +#include +#else +#include +#endif + +#ifndef GL_QUADS +#define GL_QUADS 7 +#endif + +void glBegin(GLenum mode); +void glEnd(void); + +#include "sanegl_attr.h" + +#endif /* SANEGL_H_ */ diff -r 000000000000 -r 00b315b6db1e sanegl_attr.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sanegl_attr.h Thu Jun 23 05:19:40 2011 +0300 @@ -0,0 +1,161 @@ +#ifndef SANEGL_ATTR_H_ +#define SANEGL_ATTR_H_ + +#ifndef SANEGL_IMPL_ +/* in the header file just expand to the prototypes */ +#define GLATTR1(name, type, suffix, conv) \ + void gl##name##1##suffix(GL##type x); + +#define GLATTR2(name, type, suffix, conv) \ + void gl##name##2##suffix(GL##type x, GL##type y); + +#define GLATTR3(name, type, suffix, conv) \ + void gl##name##3##suffix(GL##type x, GL##type y, GL##type z); + +#define GLATTR4(name, type, suffix, conv) \ + void gl##name##3##suffix(GL##type x, GL##type y, GL##type z, GL##type w); + +#define GLATTR1V(name, type, suffix, conv) \ + void gl##name##1##suffix##v(const GL##type *p); + +#define GLATTR2V(name, type, suffix, conv) \ + void gl##name##2##suffix##v(const GL##type *p); + +#define GLATTR3V(name, type, suffix, conv) \ + void gl##name##3##suffix##v(const GL##type *p); + +#define GLATTR4V(name, type, suffix, conv) \ + void gl##name##4##suffix##v(const GL##type *p); +#else +/* this full expansion will be performed in the implementation (sanegl.c) */ +#define GLATTR1(name, type, suffix, conv) \ + void gl##name##1##suffix(GL##type x) \ + { gl##name##4f(conv(x), 0, 0, 1); } + +#define GLATTR2(name, type, suffix, conv) \ + void gl##name##2##suffix(GL##type x, GL##type y) \ + { gl##name##4f(conv(x), conv(y), 0, 1); } + +#define GLATTR3(name, type, suffix, conv) \ + void gl##name##3##suffix(GL##type x, GL##type y, GL##type z) \ + { gl##name##4f(conv(x), conv(y), conv(z), 1); } + +#define GLATTR4(name, type, suffix, conv) \ + void gl##name##3##suffix(GL##type x, GL##type y, GL##type z, GL##type w) \ + { gl##name##4f(conv(x), conv(y), conv(z), conv(w)); } + +#define GLATTR1V(name, type, suffix, conv) \ + void gl##name##1##suffix##v(const GL##type *p) \ + { gl##name##4f(conv(*p), 0, 0, 1); } + +#define GLATTR2V(name, type, suffix, conv) \ + void gl##name##2##suffix##v(const GL##type *p) \ + { gl##name##4f(conv(p[0]), conv(p[1]), 0, 1); } + +#define GLATTR3V(name, type, suffix, conv) \ + void gl##name##3##suffix##v(const GL##type *p) \ + { gl##name##4f(conv(p[0]), conv(p[1]), conv(p[2]), 1); } + +#define GLATTR4V(name, type, suffix, conv) \ + void gl##name##4##suffix##v(const GL##type *p) \ + { gl##name##4f(conv(p[0]), conv(p[1]), conv(p[2]), conv(p[3])); } +#endif + +/* n-arg functions */ +GLATTR2(Vertex, float, f, (float)) +GLATTR3(Vertex, float, f, (float)) + +GLATTR2(Vertex, double, d, (float)) +GLATTR3(Vertex, double, d, (float)) +GLATTR4(Vertex, double, d, (float)) + +GLATTR2(Vertex, int, i, (float)) +GLATTR3(Vertex, int, i, (float)) +GLATTR4(Vertex, int, i, (float)) + +GLATTR2(Vertex, short, s, (float)) +GLATTR3(Vertex, short, s, (float)) +GLATTR4(Vertex, short, s, (float)) + +GLATTR3(Color, float, f, (float)) +GLATTR3(Color, double, d, (float)) +GLATTR4(Color, double, d, (float)) + +GLATTR3(Color, byte, b, (1.0f / CHAR_MAX) * (float)) +GLATTR4(Color, byte, b, (1.0f / CHAR_MAX) * (float)) + +GLATTR3(Color, ubyte, ub, (1.0f / UCHAR_MAX) * (float)) +GLATTR4(Color, ubyte, ub, (1.0f / UCHAR_MAX) * (float)) + +GLATTR3(Color, short, s, (1.0f / SHRT_MAX) * (float)) +GLATTR4(Color, short, s, (1.0f / SHRT_MAX) * (float)) + +GLATTR3(Color, ushort, us, (1.0f / USHRT_MAX) * (float)) +GLATTR4(Color, ushort, us, (1.0f / USHRT_MAX) * (float)) + +GLATTR3(Color, int, i, (1.0f / INT_MAX) * (float)) +GLATTR4(Color, int, i, (1.0f / INT_MAX) * (float)) + +GLATTR3(Color, uint, ui, (1.0f / UINT_MAX) * (float)) +GLATTR4(Color, uint, ui, (1.0f / UINT_MAX) * (float)) + +GLATTR1(TexCoord, float, f, (float)) +GLATTR2(TexCoord, float, f, (float)) +GLATTR3(TexCoord, float, f, (float)) + +GLATTR1(TexCoord, double, d, (float)) +GLATTR2(TexCoord, double, d, (float)) +GLATTR3(TexCoord, double, d, (float)) +GLATTR4(TexCoord, double, d, (float)) + +GLATTR1(TexCoord, int, i, (float)) +GLATTR2(TexCoord, int, i, (float)) +GLATTR3(TexCoord, int, i, (float)) +GLATTR4(TexCoord, int, i, (float)) + +GLATTR1(TexCoord, short, s, (float)) +GLATTR2(TexCoord, short, s, (float)) +GLATTR3(TexCoord, short, s, (float)) +GLATTR4(TexCoord, short, s, (float)) + +/* vector functions */ +GLATTR2V(Vertex, float, f, (float)) +GLATTR3V(Vertex, float, f, (float)) + +GLATTR2V(Vertex, double, d, (float)) +GLATTR3V(Vertex, double, d, (float)) +GLATTR4V(Vertex, double, d, (float)) + +GLATTR2V(Vertex, int, i, (float)) +GLATTR3V(Vertex, int, i, (float)) +GLATTR4V(Vertex, int, i, (float)) + +GLATTR2V(Vertex, short, s, (float)) +GLATTR3V(Vertex, short, s, (float)) +GLATTR4V(Vertex, short, s, (float)) + +GLATTR3V(Color, float, f, (float)) +GLATTR3V(Color, double, d, (float)) +GLATTR4V(Color, double, d, (float)) + +GLATTR1V(TexCoord, float, f, (float)) +GLATTR2V(TexCoord, float, f, (float)) +GLATTR3V(TexCoord, float, f, (float)) + +GLATTR1V(TexCoord, double, d, (float)) +GLATTR2V(TexCoord, double, d, (float)) +GLATTR3V(TexCoord, double, d, (float)) +GLATTR4V(TexCoord, double, d, (float)) + +GLATTR1V(TexCoord, int, i, (float)) +GLATTR2V(TexCoord, int, i, (float)) +GLATTR3V(TexCoord, int, i, (float)) +GLATTR4V(TexCoord, int, i, (float)) + +GLATTR1V(TexCoord, short, s, (float)) +GLATTR2V(TexCoord, short, s, (float)) +GLATTR3V(TexCoord, short, s, (float)) +GLATTR4V(TexCoord, short, s, (float)) + + +#endif /* SANEGL_ATTR_H_ */ diff -r 000000000000 -r 00b315b6db1e test.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.c Thu Jun 23 05:19:40 2011 +0300 @@ -0,0 +1,53 @@ +#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(800, 600); + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); + glutCreateWindow("foo"); + + glutDisplayFunc(disp); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); + + glutMainLoop(); + return 0; +} + +void disp(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glBegin(GL_QUADS); + glColor3f(1, 0, 0); + glVertex2f(-0.5, -0.5); + glColor3f(0, 1, 0); + glVertex2f(0.5, -0.5); + glColor3f(0, 0, 1); + glVertex2f(0.5, 0.5); + glColor3f(1, 1, 0); + glVertex2f(-0.5, 0.5); + glEnd(); + + glutSwapBuffers(); +} + +void reshape(int x, int y) +{ + glViewport(0, 0, x, y); +} + +void keyb(unsigned char key, int x, int y) +{ + if(key == 27) { + exit(0); + } +}