diff options
Diffstat (limited to 'progs/glsl')
-rw-r--r-- | progs/glsl/.gitignore | 3 | ||||
-rw-r--r-- | progs/glsl/Makefile | 263 | ||||
-rw-r--r-- | progs/glsl/array.c | 261 | ||||
-rw-r--r-- | progs/glsl/bump.c | 3 | ||||
-rw-r--r-- | progs/glsl/linktest.c | 2 | ||||
-rw-r--r-- | progs/glsl/multitex.c | 93 | ||||
-rw-r--r-- | progs/glsl/noise2.c | 201 | ||||
-rw-r--r-- | progs/glsl/texaaline.c | 369 |
8 files changed, 1055 insertions, 140 deletions
diff --git a/progs/glsl/.gitignore b/progs/glsl/.gitignore index d3e31163d8e..39d90c23ac5 100644 --- a/progs/glsl/.gitignore +++ b/progs/glsl/.gitignore @@ -1,3 +1,4 @@ +array bitmap brick bump @@ -11,6 +12,7 @@ mandelbrot multinoise multitex noise +noise2 pointcoord points readtex.c @@ -21,6 +23,7 @@ shaderutil.c shaderutil.h shadow_sampler skinning +texaaline texdemo1 toyball trirast diff --git a/progs/glsl/Makefile b/progs/glsl/Makefile index 0f1a299570f..f97cdb69429 100644 --- a/progs/glsl/Makefile +++ b/progs/glsl/Makefile @@ -5,46 +5,69 @@ include $(TOP)/configs/current INCDIR = $(TOP)/include -LIB_DEP = $(TOP)/$(LIB_DIR)/$(GL_LIB_NAME) $(TOP)/$(LIB_DIR)/$(GLU_LIB_NAME) $(TOP)/$(LIB_DIR)/$(GLUT_LIB_NAME) +LIB_DEP = \ + $(TOP)/$(LIB_DIR)/$(GL_LIB_NAME) \ + $(TOP)/$(LIB_DIR)/$(GLU_LIB_NAME) \ + $(TOP)/$(LIB_DIR)/$(GLUT_LIB_NAME) LIBS = -L$(TOP)/$(LIB_DIR) -l$(GLUT_LIB) -l$(GLU_LIB) -l$(GL_LIB) $(APP_LIB_DEPS) -PROGS = \ - bitmap \ - brick \ - bump \ - convolutions \ - deriv \ - identity \ - fragcoord \ - linktest \ - mandelbrot \ - multinoise \ - multitex \ - noise \ - points \ - pointcoord \ - samplers \ - samplers_array \ - shadow_sampler \ - skinning \ - texdemo1 \ - toyball \ - twoside \ - trirast \ - vert-or-frag-only \ - vert-tex +INCLUDE_DIRS = -I$(TOP)/progs/util + +DEMO_SOURCES = \ + array.c \ + bitmap.c \ + brick.c \ + bump.c \ + convolutions.c \ + deriv.c \ + fragcoord.c \ + identity.c \ + linktest.c \ + mandelbrot.c \ + multinoise.c \ + multitex.c \ + noise.c \ + noise2.c \ + pointcoord.c \ + points.c \ + samplers.c \ + shadow_sampler.c \ + skinning.c \ + texaaline.c \ + texdemo1.c \ + toyball.c \ + trirast.c \ + twoside.c \ + vert-or-frag-only.c \ + vert-tex.c + +UTIL_HEADERS = \ + extfuncs.h \ + shaderutil.h \ + readtex.h + +UTIL_SOURCES = \ + shaderutil.c \ + readtex.c + +UTIL_OBJS = $(UTIL_SOURCES:.c=.o) + + +PROGS = $(DEMO_SOURCES:%.c=%) + ##### RULES ##### -.SUFFIXES: -.SUFFIXES: .c +# make .o file from .c file: +.c.o: + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) $< -o $@ -# make executable from .c file: -.c: $(LIB_DEP) - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) $< $(LIBS) -o $@ +# make executable from .o files +.o: + $(APP_CC) $(INCLUDES) $(CFLAGS) $(LDFLAGS) $< $(UTIL_OBJS) $(LIBS) -o $@ ##### TARGETS ##### @@ -52,188 +75,164 @@ PROGS = \ default: $(PROGS) +clean: + -rm -f $(PROGS) + -rm -f *.o *~ + -rm -f extfuncs.h + -rm -f shaderutil.* + -rm -f readtex.* + + ##### Extra dependencies -extfuncs.h: $(TOP)/progs/util/extfuncs.h +extfuncs.h: $(TOP)/progs/util/extfuncs.h cp $< . - readtex.c: $(TOP)/progs/util/readtex.c cp $< . readtex.h: $(TOP)/progs/util/readtex.h cp $< . -readtex.o: readtex.c readtex.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) readtex.c - - shaderutil.c: $(TOP)/progs/util/shaderutil.c cp $< . shaderutil.h: $(TOP)/progs/util/shaderutil.h cp $< . -shaderutil.o: shaderutil.c shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) shaderutil.c +array.o: $(UTIL_HEADERS) -bitmap.o: bitmap.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) bitmap.c +array: array.o $(UTIL_OBJS) -bitmap: bitmap.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) bitmap.o shaderutil.o $(LIBS) -o $@ +bitmap.o: $(UTIL_HEADERS) -brick.o: brick.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) brick.c +bitmap: bitmap.o $(UTIL_OBJS) -brick: brick.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) brick.o shaderutil.o $(LIBS) -o $@ +brick.o: $(UTIL_HEADERS) -bump.o: bump.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) bump.c +brick: brick.o $(UTIL_OBJS) -bump: bump.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) bump.o shaderutil.o $(LIBS) -o $@ +bump.o: $(UTIL_HEADERS) -convolutions.o: convolutions.c readtex.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) convolutions.c +bump: bump.o $(UTIL_OBJS) -convolutions: convolutions.o readtex.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) convolutions.o readtex.o $(LIBS) -o $@ +convolutions.o: $(UTIL_HEADERS) -deriv.o: deriv.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) deriv.c +convolutions: convolutions.o $(UTIL_OBJS) -deriv: deriv.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) deriv.o shaderutil.o $(LIBS) -o $@ +deriv.o: deriv.c $(UTIL_HEADERS) -identity.o: identity.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) identity.c +deriv: deriv.o $(UTIL_OBJS) -identity: identity.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) identity.o shaderutil.o $(LIBS) -o $@ +identity.o: $(UTIL_HEADERS) -fragcoord.o: fragcoord.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) fragcoord.c +identity: identity.o $(UTIL_OBJS) -fragcoord: fragcoord.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) fragcoord.o shaderutil.o $(LIBS) -o $@ +fragcoord.o: $(UTIL_HEADERS) -linktest.o: linktest.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) linktest.c +fragcoord: fragcoord.o $(UTIL_OBJS) -linktest: linktest.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) linktest.o shaderutil.o $(LIBS) -o $@ -mandelbrot.o: mandelbrot.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) mandelbrot.c +linktest.o: $(UTIL_HEADERS) -mandelbrot: mandelbrot.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) mandelbrot.o shaderutil.o $(LIBS) -o $@ +linktest: linktest.o $(UTIL_OBJS) -multitex.o: multitex.c extfuncs.h readtex.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) multitex.c -multitex: multitex.o readtex.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) multitex.o readtex.o shaderutil.o $(LIBS) -o $@ +mandelbrot.o: $(UTIL_HEADERS) +mandelbrot: mandelbrot.o $(UTIL_OBJS) -noise.o: noise.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) noise.c -noise: noise.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) noise.o shaderutil.o $(LIBS) -o $@ +multinoise.o: $(UTIL_HEADERS) +multinoise: multinoise.o $(UTIL_OBJS) -points.o: points.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) points.c -points: points.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) points.o shaderutil.o $(LIBS) -o $@ +multitex.o: $(UTIL_HEADERS) +multitex: multitex.o $(UTIL_OBJS) -pointcoord.o: pointcoord.c readtex.h extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) pointcoord.c -pointcoord: pointcoord.o readtex.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) pointcoord.o readtex.o shaderutil.o $(LIBS) -o $@ +noise.o: $(UTIL_HEADERS) +noise: noise.o $(UTIL_OBJS) -samplers.o: samplers.c readtex.h extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) samplers.c -samplers: samplers.o readtex.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) samplers.o readtex.o shaderutil.o $(LIBS) -o $@ +noise2.o: $(UTIL_HEADERS) -samplers_array.o: samplers.c readtex.h extfuncs.h shaderutil.h - $(APP_CC) -c -DSAMPLERS_ARRAY -I$(INCDIR) $(CFLAGS) samplers.c -o samplers_array.o +noise2: noise2.o $(UTIL_OBJS) -samplers_array: samplers_array.o readtex.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) samplers_array.o readtex.o shaderutil.o $(LIBS) -o $@ -skinning.o: skinning.c readtex.h extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) skinning.c +points.o: $(UTIL_HEADERS) -skinning: skinning.o readtex.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) skinning.o readtex.o shaderutil.o $(LIBS) -o $@ +points: points.o $(UTIL_OBJS) -texdemo1.o: texdemo1.c readtex.h extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) texdemo1.c +pointcoord.o: $(UTIL_HEADERS) -texdemo1: texdemo1.o readtex.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) texdemo1.o readtex.o shaderutil.o $(LIBS) -o $@ +pointcoord: pointcoord.o $(UTIL_OBJS) -toyball.o: toyball.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) toyball.c +samplers.o: $(UTIL_HEADERS) -toyball: toyball.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) toyball.o shaderutil.o $(LIBS) -o $@ +samplers: samplers.o $(UTIL_OBJS) -twoside.o: twoside.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) twoside.c +samplers_array.o: $(UTIL_HEADERS) -twoside: twoside.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) twoside.o shaderutil.o $(LIBS) -o $@ +samplers_array: samplers_array.o $(UTIL_OBJS) -trirast.o: trirast.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) trirast.c +shadow_sampler.o: $(UTIL_HEADERS) -trirast: trirast.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) trirast.o shaderutil.o $(LIBS) -o $@ +shadow_sampler: shadow_sampler.o $(UTIL_OBJS) -vert-or-frag-only.o: vert-or-frag-only.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) vert-or-frag-only.c +skinning.o: $(UTIL_HEADERS) -vert-or-frag-only: vert-or-frag-only.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) vert-or-frag-only.o shaderutil.o $(LIBS) -o $@ +skinning: skinning.o $(UTIL_OBJS) -vert-tex.o: vert-tex.c extfuncs.h shaderutil.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) vert-tex.c +texaaline.o: $(UTIL_HEADERS) -vert-tex: vert-tex.o shaderutil.o - $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) vert-tex.o shaderutil.o $(LIBS) -o $@ +texaaline: texaaline.o $(UTIL_OBJS) +texdemo1.o: $(UTIL_HEADERS) +texdemo1: texdemo1.o $(UTIL_OBJS) -clean: - -rm -f $(PROGS) - -rm -f *.o *~ - -rm -f extfuncs.h - -rm -f shaderutil.* + +toyball.o: $(UTIL_HEADERS) + +toyball: toyball.o $(UTIL_OBJS) + + +twoside.o: $(UTIL_HEADERS) + +twoside: twoside.o $(UTIL_OBJS) + + +trirast.o: $(UTIL_HEADERS) + +trirast: trirast.o $(UTIL_OBJS) + + +vert-or-frag-only.o: $(UTIL_HEADERS) + +vert-or-frag-only: vert-or-frag-only.o $(UTIL_OBJS) + + +vert-tex.o: $(UTIL_HEADERS) + +vert-tex: vert-tex.o $(UTIL_OBJS) diff --git a/progs/glsl/array.c b/progs/glsl/array.c new file mode 100644 index 00000000000..46ef8043bcf --- /dev/null +++ b/progs/glsl/array.c @@ -0,0 +1,261 @@ +/** + * Test variable array indexing in a vertex shader. + * Brian Paul + * 17 April 2009 + */ + +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <GL/gl.h> +#include <GL/glut.h> +#include <GL/glext.h> +#include "extfuncs.h" +#include "shaderutil.h" + + +/** + * The vertex position.z is used as a (variable) index into an + * array which returns a new Z value. + */ +static const char *VertShaderText = + "uniform sampler2D tex1; \n" + "uniform float HeightArray[20]; \n" + "void main() \n" + "{ \n" + " vec4 pos = gl_Vertex; \n" + " int i = int(pos.z * 9.5); \n" + " pos.z = HeightArray[i]; \n" + " gl_Position = gl_ModelViewProjectionMatrix * pos; \n" + " gl_FrontColor = pos; \n" + "} \n"; + +static const char *FragShaderText = + "void main() \n" + "{ \n" + " gl_FragColor = gl_Color; \n" + "} \n"; + + +static GLuint fragShader; +static GLuint vertShader; +static GLuint program; + +static GLint win = 0; +static GLboolean Anim = GL_TRUE; +static GLboolean WireFrame = GL_TRUE; +static GLfloat xRot = -70.0f, yRot = 0.0f, zRot = 0.0f; + + +static void +Idle(void) +{ + zRot = 90 + glutGet(GLUT_ELAPSED_TIME) * 0.05; + glutPostRedisplay(); +} + + +/** z=f(x,y) */ +static float +fz(float x, float y) +{ + return fabs(cos(1.5*x) + cos(1.5*y)); +} + + +static void +DrawMesh(void) +{ + GLfloat xmin = -2.0, xmax = 2.0; + GLfloat ymin = -2.0, ymax = 2.0; + GLuint xdivs = 20, ydivs = 20; + GLfloat dx = (xmax - xmin) / xdivs; + GLfloat dy = (ymax - ymin) / ydivs; + GLfloat ds = 1.0 / xdivs, dt = 1.0 / ydivs; + GLfloat x, y, s, t; + GLuint i, j; + + y = ymin; + t = 0.0; + for (i = 0; i < ydivs; i++) { + x = xmin; + s = 0.0; + glBegin(GL_QUAD_STRIP); + for (j = 0; j < xdivs; j++) { + float z0 = fz(x, y), z1 = fz(x, y + dy); + + glTexCoord2f(s, t); + glVertex3f(x, y, z0); + + glTexCoord2f(s, t + dt); + glVertex3f(x, y + dy, z1); + x += dx; + s += ds; + } + glEnd(); + y += dy; + t += dt; + } +} + + +static void +Redisplay(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + if (WireFrame) + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + else + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + + glPushMatrix(); + glRotatef(xRot, 1.0f, 0.0f, 0.0f); + glRotatef(yRot, 0.0f, 1.0f, 0.0f); + glRotatef(zRot, 0.0f, 0.0f, 1.0f); + + glPushMatrix(); + DrawMesh(); + glPopMatrix(); + + glPopMatrix(); + + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + + glutSwapBuffers(); +} + + +static void +Reshape(int width, int height) +{ + glViewport(0, 0, width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0f, 0.0f, -15.0f); +} + + +static void +CleanUp(void) +{ + glDeleteShader_func(fragShader); + glDeleteShader_func(vertShader); + glDeleteProgram_func(program); + glutDestroyWindow(win); +} + + +static void +Key(unsigned char key, int x, int y) +{ + const GLfloat step = 2.0; + (void) x; + (void) y; + + switch(key) { + case 'a': + Anim = !Anim; + if (Anim) + glutIdleFunc(Idle); + else + glutIdleFunc(NULL); + break; + case 'w': + WireFrame = !WireFrame; + break; + case 'z': + zRot += step; + break; + case 'Z': + zRot -= step; + break; + case 27: + CleanUp(); + exit(0); + break; + } + glutPostRedisplay(); +} + + +static void +SpecialKey(int key, int x, int y) +{ + const GLfloat step = 2.0; + + (void) x; + (void) y; + + switch(key) { + case GLUT_KEY_UP: + xRot += step; + break; + case GLUT_KEY_DOWN: + xRot -= step; + break; + case GLUT_KEY_LEFT: + yRot -= step; + break; + case GLUT_KEY_RIGHT: + yRot += step; + break; + } + glutPostRedisplay(); +} + + +static void +Init(void) +{ + GLfloat HeightArray[20]; + GLint u, i; + + if (!ShadersSupported()) + exit(1); + + GetExtensionFuncs(); + + vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText); + fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText); + program = LinkShaders(vertShader, fragShader); + + glUseProgram_func(program); + + /* Setup the HeightArray[] uniform */ + for (i = 0; i < 20; i++) + HeightArray[i] = i / 20.0; + u = glGetUniformLocation_func(program, "HeightArray"); + glUniform1fv_func(u, 20, HeightArray); + + assert(glGetError() == 0); + + glClearColor(0.4f, 0.4f, 0.8f, 0.0f); + glEnable(GL_DEPTH_TEST); + glColor3f(1, 1, 1); +} + + +int +main(int argc, char *argv[]) +{ + glutInit(&argc, argv); + glutInitWindowSize(500, 500); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); + win = glutCreateWindow(argv[0]); + glutReshapeFunc(Reshape); + glutKeyboardFunc(Key); + glutSpecialFunc(SpecialKey); + glutDisplayFunc(Redisplay); + Init(); + if (Anim) + glutIdleFunc(Idle); + glutMainLoop(); + return 0; +} + diff --git a/progs/glsl/bump.c b/progs/glsl/bump.c index b93ab44b5b9..0ea1f8331ff 100644 --- a/progs/glsl/bump.c +++ b/progs/glsl/bump.c @@ -150,10 +150,11 @@ Redisplay(void) static void Reshape(int width, int height) { + float ar = (float) width / (float) height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); + glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -15.0f); diff --git a/progs/glsl/linktest.c b/progs/glsl/linktest.c index 601b24e893e..988d0823418 100644 --- a/progs/glsl/linktest.c +++ b/progs/glsl/linktest.c @@ -242,7 +242,7 @@ int main(int argc, char *argv[]) { glutInit(&argc, argv); - glutInitWindowPosition( 0, 0); + glutInitWindowSize(300, 300); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); Win = glutCreateWindow(argv[0]); glutReshapeFunc(Reshape); diff --git a/progs/glsl/multitex.c b/progs/glsl/multitex.c index b4be463787a..1a1c63aaf48 100644 --- a/progs/glsl/multitex.c +++ b/progs/glsl/multitex.c @@ -51,6 +51,8 @@ static GLfloat Xrot = 0.0, Yrot = .0, Zrot = 0.0; static GLfloat EyeDist = 10; static GLboolean Anim = GL_TRUE; static GLboolean UseArrays = GL_TRUE; +static GLboolean UseVBO = GL_TRUE; +static GLuint VBO = 0; static GLint VertCoord_attr = -1, TexCoord0_attr = -1, TexCoord1_attr = -1; @@ -76,28 +78,81 @@ static const GLfloat VertCoords[4][2] = { }; + +static void +SetupVertexBuffer(void) +{ + glGenBuffersARB_func(1, &VBO); + glBindBufferARB_func(GL_ARRAY_BUFFER_ARB, VBO); + + glBufferDataARB_func(GL_ARRAY_BUFFER_ARB, + sizeof(VertCoords) + + sizeof(Tex0Coords) + + sizeof(Tex1Coords), + NULL, + GL_STATIC_DRAW_ARB); + + /* non-interleaved vertex arrays */ + + glBufferSubDataARB_func(GL_ARRAY_BUFFER_ARB, + 0, /* offset */ + sizeof(VertCoords), /* size */ + VertCoords); /* data */ + + glBufferSubDataARB_func(GL_ARRAY_BUFFER_ARB, + sizeof(VertCoords), /* offset */ + sizeof(Tex0Coords), /* size */ + Tex0Coords); /* data */ + + glBufferSubDataARB_func(GL_ARRAY_BUFFER_ARB, + sizeof(VertCoords) + + sizeof(Tex0Coords), /* offset */ + sizeof(Tex1Coords), /* size */ + Tex1Coords); /* data */ + + glBindBufferARB_func(GL_ARRAY_BUFFER_ARB, 0); +} + + static void DrawPolygonArray(void) { + void *vertPtr, *tex0Ptr, *tex1Ptr; + + if (UseVBO) { + glBindBufferARB_func(GL_ARRAY_BUFFER_ARB, VBO); + vertPtr = (void *) 0; + tex0Ptr = (void *) sizeof(VertCoords); + tex1Ptr = (void *) (sizeof(VertCoords) + sizeof(Tex0Coords)); + } + else { + glBindBufferARB_func(GL_ARRAY_BUFFER_ARB, 0); + vertPtr = VertCoords; + tex0Ptr = Tex0Coords; + tex1Ptr = Tex1Coords; + } + if (VertCoord_attr >= 0) { glVertexAttribPointer_func(VertCoord_attr, 2, GL_FLOAT, GL_FALSE, - 0, VertCoords); + 0, vertPtr); glEnableVertexAttribArray_func(VertCoord_attr); } else { - glVertexPointer(2, GL_FLOAT, 0, VertCoords); + glVertexPointer(2, GL_FLOAT, 0, vertPtr); glEnable(GL_VERTEX_ARRAY); } glVertexAttribPointer_func(TexCoord0_attr, 2, GL_FLOAT, GL_FALSE, - 0, Tex0Coords); + 0, tex0Ptr); glEnableVertexAttribArray_func(TexCoord0_attr); glVertexAttribPointer_func(TexCoord1_attr, 2, GL_FLOAT, GL_FALSE, - 0, Tex1Coords); + 0, tex1Ptr); glEnableVertexAttribArray_func(TexCoord1_attr); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + + glBindBufferARB_func(GL_ARRAY_BUFFER_ARB, 0); } @@ -163,6 +218,10 @@ key(unsigned char k, int x, int y) UseArrays = !UseArrays; printf("Arrays: %d\n", UseArrays); break; + case 'v': + UseVBO = !UseVBO; + printf("Use VBO: %d\n", UseVBO); + break; case ' ': Anim = !Anim; if (Anim) @@ -271,9 +330,24 @@ CreateProgram(const char *vertProgFile, const char *fragProgFile, InitUniforms(program, uniforms); + VertCoord_attr = glGetAttribLocation_func(program, "VertCoord"); + if (VertCoord_attr > 0) { + /* We want the VertCoord attrib to have position zero so that + * the call to glVertexAttrib(0, xyz) triggers vertex processing. + * Otherwise, if TexCoord0 or TexCoord1 gets position 0 we'd have + * to set that attribute last (which is a PITA to manage). + */ + glBindAttribLocation_func(program, 0, "VertCoord"); + /* re-link */ + glLinkProgram_func(program); + /* VertCoord_attr should be zero now */ + VertCoord_attr = glGetAttribLocation_func(program, "VertCoord"); + assert(VertCoord_attr == 0); + } + TexCoord0_attr = glGetAttribLocation_func(program, "TexCoord0"); TexCoord1_attr = glGetAttribLocation_func(program, "TexCoord1"); - VertCoord_attr = glGetAttribLocation_func(program, "VertCoord"); + printf("TexCoord0_attr = %d\n", TexCoord0_attr); printf("TexCoord1_attr = %d\n", TexCoord1_attr); printf("VertCoord_attr = %d\n", VertCoord_attr); @@ -299,12 +373,19 @@ InitGL(void) /*exit(1);*/ } printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); - + printf("Usage:\n"); + printf(" a - toggle arrays vs. immediate mode rendering\n"); + printf(" v - toggle VBO usage for array rendering\n"); + printf(" z/Z - change viewing distance\n"); + printf(" SPACE - toggle animation\n"); + printf(" Esc - exit\n"); GetExtensionFuncs(); InitTextures(); InitPrograms(); + SetupVertexBuffer(); + glEnable(GL_DEPTH_TEST); glClearColor(.6, .6, .9, 0); diff --git a/progs/glsl/noise2.c b/progs/glsl/noise2.c new file mode 100644 index 00000000000..e972b62673f --- /dev/null +++ b/progs/glsl/noise2.c @@ -0,0 +1,201 @@ +/* + * GLSL noise demo. + * + * Michal Krol + * 20 February 2006 + * + * Based on the original demo by: + * Stefan Gustavson ([email protected]) 2004, 2005 + */ + +#ifdef WIN32 +#include <windows.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <GL/gl.h> +#include <GL/glut.h> +#include <GL/glext.h> + +#ifdef WIN32 +#define GETPROCADDRESS(F) wglGetProcAddress(F) +#else +#define GETPROCADDRESS(F) glutGetProcAddress(F) +#endif + +static GLhandleARB fragShader; +static GLhandleARB vertShader; +static GLhandleARB program; + +static GLint uTime; + +static GLint t0 = 0; +static GLint frames = 0; + +static GLfloat u_time = 0.0f; + +static PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB = NULL; +static PFNGLSHADERSOURCEARBPROC glShaderSourceARB = NULL; +static PFNGLCOMPILESHADERARBPROC glCompileShaderARB = NULL; +static PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB = NULL; +static PFNGLATTACHOBJECTARBPROC glAttachObjectARB = NULL; +static PFNGLLINKPROGRAMARBPROC glLinkProgramARB = NULL; +static PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB = NULL; +static PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB = NULL; +static PFNGLUNIFORM1FARBPROC glUniform1fARB = NULL; + +static void Redisplay (void) +{ + GLint t; + + glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glUniform1fARB (uTime, 0.5f * u_time); + + glPushMatrix (); + glutSolidSphere (2.0, 20, 10); + glPopMatrix (); + + glutSwapBuffers(); + frames++; + + t = glutGet (GLUT_ELAPSED_TIME); + if (t - t0 >= 5000) { + GLfloat seconds = (GLfloat) (t - t0) / 1000.0f; + GLfloat fps = frames / seconds; + printf ("%d frames in %6.3f seconds = %6.3f FPS\n", frames, seconds, fps); + fflush(stdout); + t0 = t; + frames = 0; + } +} + +static void Idle (void) +{ + u_time += 0.1f; + glutPostRedisplay (); +} + +static void Reshape (int width, int height) +{ + glViewport (0, 0, width, height); + glMatrixMode (GL_PROJECTION); + glLoadIdentity (); + glFrustum (-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); + glMatrixMode (GL_MODELVIEW); + glLoadIdentity (); + glTranslatef (0.0f, 0.0f, -15.0f); +} + +static void Key (unsigned char key, int x, int y) +{ + (void) x; + (void) y; + + switch (key) + { + case 27: + exit(0); + break; + } + glutPostRedisplay (); +} + +static void Init (void) +{ + static const char *fragShaderText = + "uniform float time;\n" + "varying vec3 position;\n" + "void main () {\n" + " gl_FragColor = vec4 (vec3 (0.5 + 0.5 * noise1 (vec4 (position, time))), 1.0);\n" + "}\n" + ; + static const char *vertShaderText = + "varying vec3 position;\n" + "void main () {\n" + " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" + " position = 4.0 * gl_Vertex.xyz;\n" + "}\n" + ; + + if (!glutExtensionSupported ("GL_ARB_fragment_shader")) + { + printf ("Sorry, this demo requires GL_ARB_fragment_shader\n"); + exit(1); + } + if (!glutExtensionSupported ("GL_ARB_shader_objects")) + { + printf ("Sorry, this demo requires GL_ARB_shader_objects\n"); + exit(1); + } + if (!glutExtensionSupported ("GL_ARB_shading_language_100")) + { + printf ("Sorry, this demo requires GL_ARB_shading_language_100\n"); + exit(1); + } + if (!glutExtensionSupported ("GL_ARB_vertex_shader")) + { + printf ("Sorry, this demo requires GL_ARB_vertex_shader\n"); + exit(1); + } + + glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) + GETPROCADDRESS("glCreateShaderObjectARB"); + glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) + GETPROCADDRESS("glShaderSourceARB"); + glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) + GETPROCADDRESS("glCompileShaderARB"); + glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) + GETPROCADDRESS("glCreateProgramObjectARB"); + glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) + GETPROCADDRESS("glAttachObjectARB"); + glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) + GETPROCADDRESS ("glLinkProgramARB"); + glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) + GETPROCADDRESS("glUseProgramObjectARB"); + + glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) + GETPROCADDRESS("glGetUniformLocationARB"); + glUniform1fARB = (PFNGLUNIFORM1FARBPROC) + GETPROCADDRESS("glUniform1fARB"); + + fragShader = glCreateShaderObjectARB (GL_FRAGMENT_SHADER_ARB); + glShaderSourceARB (fragShader, 1, &fragShaderText, NULL); + glCompileShaderARB (fragShader); + + vertShader = glCreateShaderObjectARB (GL_VERTEX_SHADER_ARB); + glShaderSourceARB (vertShader, 1, &vertShaderText, NULL); + glCompileShaderARB (vertShader); + + program = glCreateProgramObjectARB (); + glAttachObjectARB (program, fragShader); + glAttachObjectARB (program, vertShader); + glLinkProgramARB (program); + glUseProgramObjectARB (program); + + uTime = glGetUniformLocationARB (program, "time"); + + glClearColor (0.0f, 0.1f, 0.3f, 1.0f); + glEnable (GL_CULL_FACE); + glEnable (GL_DEPTH_TEST); + + printf ("GL_RENDERER = %s\n", (const char *) glGetString (GL_RENDERER)); +} + +int main (int argc, char *argv[]) +{ + glutInit (&argc, argv); + glutInitWindowPosition ( 0, 0); + glutInitWindowSize (200, 200); + glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); + glutCreateWindow (argv[0]); + glutReshapeFunc (Reshape); + glutKeyboardFunc (Key); + glutDisplayFunc (Redisplay); + glutIdleFunc (Idle); + Init (); + glutMainLoop (); + return 0; +} + diff --git a/progs/glsl/texaaline.c b/progs/glsl/texaaline.c new file mode 100644 index 00000000000..0b3cc849586 --- /dev/null +++ b/progs/glsl/texaaline.c @@ -0,0 +1,369 @@ +/** + * AA lines with texture mapped quads + * + * Brian Paul + * 9 Feb 2008 + */ + + +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <GL/gl.h> +#include <GL/glut.h> +#include <GL/glext.h> +#include "extfuncs.h" + + +static GLint WinWidth = 300, WinHeight = 300; +static GLint win = 0; +static GLfloat Width = 8.; + +/* + * Quad strip for line from v0 to v1: + * + 1 3 5 7 + +---+---------------------+---+ + | | + | *v0 v1* | + | | + +---+---------------------+---+ + 0 2 4 6 + */ +static void +QuadLine(const GLfloat *v0, const GLfloat *v1, GLfloat width) +{ + GLfloat dx = v1[0] - v0[0]; + GLfloat dy = v1[1] - v0[1]; + GLfloat len = sqrt(dx*dx + dy*dy); + float dx0, dx1, dx2, dx3, dx4, dx5, dx6, dx7; + float dy0, dy1, dy2, dy3, dy4, dy5, dy6, dy7; + + dx /= len; + dy /= len; + + width *= 0.5; /* half width */ + dx = dx * (width + 0.0); + dy = dy * (width + 0.0); + + dx0 = -dx+dy; dy0 = -dy-dx; + dx1 = -dx-dy; dy1 = -dy+dx; + + dx2 = 0+dy; dy2 = -dx+0; + dx3 = 0-dy; dy3 = +dx+0; + + dx4 = 0+dy; dy4 = -dx+0; + dx5 = 0-dy; dy5 = +dx+0; + + dx6 = dx+dy; dy6 = dy-dx; + dx7 = dx-dy; dy7 = dy+dx; + + /* + printf("dx, dy = %g, %g\n", dx, dy); + printf(" dx0, dy0: %g, %g\n", dx0, dy0); + printf(" dx1, dy1: %g, %g\n", dx1, dy1); + printf(" dx2, dy2: %g, %g\n", dx2, dy2); + printf(" dx3, dy3: %g, %g\n", dx3, dy3); + */ + + glBegin(GL_QUAD_STRIP); + glTexCoord2f(0, 0); + glVertex2f(v0[0] + dx0, v0[1] + dy0); + glTexCoord2f(0, 1); + glVertex2f(v0[0] + dx1, v0[1] + dy1); + + glTexCoord2f(0.5, 0); + glVertex2f(v0[0] + dx2, v0[1] + dy2); + glTexCoord2f(0.5, 1); + glVertex2f(v0[0] + dx3, v0[1] + dy3); + + glTexCoord2f(0.5, 0); + glVertex2f(v1[0] + dx2, v1[1] + dy2); + glTexCoord2f(0.5, 1); + glVertex2f(v1[0] + dx3, v1[1] + dy3); + + glTexCoord2f(1, 0); + glVertex2f(v1[0] + dx6, v1[1] + dy6); + glTexCoord2f(1, 1); + glVertex2f(v1[0] + dx7, v1[1] + dy7); + glEnd(); +} + + +static float Cos(float a) +{ + return cos(a * M_PI / 180.); +} + +static float Sin(float a) +{ + return sin(a * M_PI / 180.); +} + +static void +Redisplay(void) +{ + int i; + + glClear(GL_COLOR_BUFFER_BIT); + + glColor3f(1, 1, 1); + + glEnable(GL_BLEND); + glEnable(GL_TEXTURE_2D); + + for (i = 0; i < 360; i+=5) { + float v0[2], v1[2]; + v0[0] = 150 + 40 * Cos(i); + v0[1] = 150 + 40 * Sin(i); + v1[0] = 150 + 130 * Cos(i); + v1[1] = 150 + 130 * Sin(i); + QuadLine(v0, v1, Width); + } + + { + float v0[2], v1[2], x; + for (x = 0; x < 1.0; x += 0.2) { + v0[0] = 150 + x; + v0[1] = 150 + x * 40 - 20; + v1[0] = 150 + x + 5.0; + v1[1] = 150 + x * 40 - 20; + QuadLine(v0, v1, Width); + } + } + + glDisable(GL_BLEND); + glDisable(GL_TEXTURE_2D); + + glutSwapBuffers(); +} + + +static void +Reshape(int width, int height) +{ + glViewport(0, 0, width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, width, 0, height, -1, 1); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + + +static void +CleanUp(void) +{ + glutDestroyWindow(win); +} + + +static void +Key(unsigned char key, int x, int y) +{ + (void) x; + (void) y; + + switch(key) { + case 'w': + Width -= 0.5; + break; + case 'W': + Width += 0.5; + break; + case 27: + CleanUp(); + exit(0); + break; + } +#if 0 + if (Width < 3) + Width = 3; +#endif + printf("Width = %g\n", Width); + glutPostRedisplay(); +} + + +static float +ramp4(GLint i, GLint size) +{ + float d; + if (i < 4 ) { + d = i / 4.0; + } + else if (i >= size - 5) { + d = 1.0 - (i - (size - 5)) / 4.0; + } + else { + d = 1.0; + } + return d; +} + +static float +ramp2(GLint i, GLint size) +{ + float d; + if (i < 2 ) { + d = i / 2.0; + } + else if (i >= size - 3) { + d = 1.0 - (i - (size - 3)) / 2.0; + } + else { + d = 1.0; + } + return d; +} + +static float +ramp1(GLint i, GLint size) +{ + float d; + if (i == 0 || i == size-1) { + d = 0.0; + } + else { + d = 1.0; + } + return d; +} + + +/** + * Make an alpha texture for antialiasing lines. + * Just a linear fall-off ramp for now. + * Should have a number of different textures for different line widths. + * Could try a bell-like-curve.... + */ +static void +MakeTexture(void) +{ +#define SZ 8 + GLfloat tex[SZ][SZ]; /* alpha tex */ + int i, j; + for (i = 0; i < SZ; i++) { + for (j = 0; j < SZ; j++) { +#if 0 + float k = (SZ-1) / 2.0; + float dx = fabs(i - k) / k; + float dy = fabs(j - k) / k; + float d; + + dx = 1.0 - dx; + dy = 1.0 - dy; + d = dx * dy; + +#else + float d = ramp1(i, SZ) * ramp1(j, SZ); + printf("%d, %d: %g\n", i, j, d); +#endif + tex[i][j] = d; + } + } + + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, SZ, SZ, 0, GL_ALPHA, GL_FLOAT, tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); +#undef SZ +} + + +static void +MakeMipmap(void) +{ +#define SZ 64 + GLfloat tex[SZ][SZ]; /* alpha tex */ + int level; + + glPixelStorei(GL_UNPACK_ROW_LENGTH, SZ); + for (level = 0; level < 7; level++) { + int sz = 1 << (6 - level); + int i, j; + for (i = 0; i < sz; i++) { + for (j = 0; j < sz; j++) { + if (level == 6) + tex[i][j] = 1.0; + else if (level == 5) + tex[i][j] = 0.5; + else + tex[i][j] = ramp1(i, sz) * ramp1(j, sz); + } + } + + glTexImage2D(GL_TEXTURE_2D, level, GL_ALPHA, + sz, sz, 0, GL_ALPHA, GL_FLOAT, tex); + } + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 4); + ////glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 5); + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); +#undef SZ +} + + +static void +Init(void) +{ + const char *version; + + (void) MakeTexture; + (void) ramp4; + (void) ramp2; + + version = (const char *) glGetString(GL_VERSION); + if (version[0] != '2' || version[1] != '.') { + printf("This program requires OpenGL 2.x, found %s\n", version); + exit(1); + } + + GetExtensionFuncs(); + + glClearColor(0.3f, 0.3f, 0.3f, 0.0f); + + printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); + + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +#if 0 + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); +#elif 0 + MakeTexture(); +#else + MakeMipmap(); +#endif +} + + +static void +ParseOptions(int argc, char *argv[]) +{ +} + + +int +main(int argc, char *argv[]) +{ + glutInit(&argc, argv); + glutInitWindowPosition( 0, 0); + glutInitWindowSize(WinWidth, WinHeight); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); + win = glutCreateWindow(argv[0]); + glutReshapeFunc(Reshape); + glutKeyboardFunc(Key); + glutDisplayFunc(Redisplay); + ParseOptions(argc, argv); + Init(); + glutMainLoop(); + return 0; +} |