diff options
author | Dave Airlie <airlied@redhat.com> | 2009-02-13 01:11:25 +1000 |
---|---|---|
committer | Dave Airlie <airlied@redhat.com> | 2009-02-13 01:11:25 +1000 |
commit | 53116910b10e3b8a05f42970eff311c21808699f (patch) | |
tree | 6831e50a56a02fed324f8cb119e05d837e00d7cf /progs/glsl | |
parent | 7e104f9cde94279a902b408e8d1cf21779b393a8 (diff) | |
parent | 0ccbc3c905f0594a35d72887a1f115e148aaa596 (diff) |
Merge remote branch 'origin/master' into radeon-rewrite
Conflicts:
configure.ac
src/mesa/drivers/dri/r200/r200_context.c
src/mesa/drivers/dri/r300/r300_render.c
Diffstat (limited to 'progs/glsl')
-rw-r--r-- | progs/glsl/.gitignore | 10 | ||||
-rw-r--r-- | progs/glsl/CH11-bumpmap.frag | 2 | ||||
-rw-r--r-- | progs/glsl/Makefile | 115 | ||||
-rw-r--r-- | progs/glsl/convolutions.c | 27 | ||||
-rw-r--r-- | progs/glsl/fragcoord.c | 185 | ||||
-rw-r--r-- | progs/glsl/identity.c | 208 | ||||
-rw-r--r-- | progs/glsl/multinoise.c | 281 | ||||
-rw-r--r-- | progs/glsl/multitex.c | 88 | ||||
-rw-r--r-- | progs/glsl/multitex.vert | 10 | ||||
-rw-r--r-- | progs/glsl/noise.c | 1 | ||||
-rw-r--r-- | progs/glsl/samplers.c | 369 | ||||
-rw-r--r-- | progs/glsl/texdemo1.c | 4 | ||||
-rw-r--r-- | progs/glsl/twoside.c | 44 | ||||
-rw-r--r-- | progs/glsl/vert-or-frag-only.c | 191 | ||||
-rw-r--r-- | progs/glsl/vert-tex.c | 279 |
15 files changed, 1724 insertions, 90 deletions
diff --git a/progs/glsl/.gitignore b/progs/glsl/.gitignore index 1e0aecebef..7a51c8cf6f 100644 --- a/progs/glsl/.gitignore +++ b/progs/glsl/.gitignore @@ -1,17 +1,27 @@ bitmap brick bump +convolutions deriv extfuncs.h +fragcoord +identity mandelbrot +multinoise multitex noise +pointcoord points readtex.c readtex.h +samplers +samplers_array shaderutil.c shaderutil.h +skinning texdemo1 toyball trirast twoside +vert-or-frag-only +vert-tex diff --git a/progs/glsl/CH11-bumpmap.frag b/progs/glsl/CH11-bumpmap.frag index 063576f5a3..e12c5d374c 100644 --- a/progs/glsl/CH11-bumpmap.frag +++ b/progs/glsl/CH11-bumpmap.frag @@ -24,7 +24,7 @@ void main() float d, f; d = p.x * p.x + p.y * p.y; - f = 1.0 / sqrt(d + 1.0); + f = inversesqrt(d + 1.0); if (d >= BumpSize) { p = vec2(0.0); f = 1.0; } diff --git a/progs/glsl/Makefile b/progs/glsl/Makefile index 41d5849919..7099eeadbd 100644 --- a/progs/glsl/Makefile +++ b/progs/glsl/Makefile @@ -15,16 +15,23 @@ PROGS = \ bump \ convolutions \ deriv \ + identity \ + fragcoord \ mandelbrot \ + multinoise \ multitex \ noise \ points \ pointcoord \ + samplers \ + samplers_array \ skinning \ texdemo1 \ toyball \ twoside \ - trirast + trirast \ + vert-or-frag-only \ + vert-tex ##### RULES ##### @@ -35,7 +42,7 @@ PROGS = \ # make executable from .c file: .c: $(LIB_DEP) - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) $< $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) $< $(LIBS) -o $@ ##### TARGETS ##### @@ -57,7 +64,7 @@ readtex.h: $(TOP)/progs/util/readtex.h cp $< . readtex.o: readtex.c readtex.h - $(CC) -c -I$(INCDIR) $(CFLAGS) readtex.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) readtex.c shaderutil.c: $(TOP)/progs/util/shaderutil.c @@ -67,114 +74,152 @@ shaderutil.h: $(TOP)/progs/util/shaderutil.h cp $< . shaderutil.o: shaderutil.c shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) shaderutil.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) shaderutil.c bitmap.o: bitmap.c extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) bitmap.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) bitmap.c bitmap: bitmap.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) bitmap.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) bitmap.o shaderutil.o $(LIBS) -o $@ brick.o: brick.c extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) brick.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) brick.c brick: brick.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) brick.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) brick.o shaderutil.o $(LIBS) -o $@ bump.o: bump.c extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) bump.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) bump.c bump: bump.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) bump.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) bump.o shaderutil.o $(LIBS) -o $@ convolutions.o: convolutions.c readtex.h - $(CC) -c -I$(INCDIR) $(CFLAGS) convolutions.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) convolutions.c convolutions: convolutions.o readtex.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) convolutions.o readtex.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) convolutions.o readtex.o $(LIBS) -o $@ deriv.o: deriv.c extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) deriv.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) deriv.c deriv: deriv.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) deriv.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) deriv.o shaderutil.o $(LIBS) -o $@ + + +identity.o: identity.c extfuncs.h shaderutil.h + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) identity.c + +identity: identity.o shaderutil.o + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) identity.o shaderutil.o $(LIBS) -o $@ + + +fragcoord.o: fragcoord.c extfuncs.h shaderutil.h + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) fragcoord.c + +fragcoord: fragcoord.o shaderutil.o + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) fragcoord.o shaderutil.o $(LIBS) -o $@ mandelbrot.o: mandelbrot.c extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) mandelbrot.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) mandelbrot.c mandelbrot: mandelbrot.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) mandelbrot.o shaderutil.o $(LIBS) -o $@ - + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) mandelbrot.o shaderutil.o $(LIBS) -o $@ multitex.o: multitex.c extfuncs.h readtex.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) multitex.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) multitex.c multitex: multitex.o readtex.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) multitex.o readtex.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) multitex.o readtex.o shaderutil.o $(LIBS) -o $@ noise.o: noise.c extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) noise.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) noise.c noise: noise.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) noise.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) noise.o shaderutil.o $(LIBS) -o $@ points.o: points.c extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) points.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) points.c points: points.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) points.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) points.o shaderutil.o $(LIBS) -o $@ pointcoord.o: pointcoord.c readtex.h extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) pointcoord.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) pointcoord.c pointcoord: pointcoord.o readtex.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) pointcoord.o readtex.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) pointcoord.o readtex.o shaderutil.o $(LIBS) -o $@ + +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 $@ + +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 + +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 - $(CC) -c -I$(INCDIR) $(CFLAGS) skinning.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) skinning.c skinning: skinning.o readtex.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) skinning.o readtex.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) skinning.o readtex.o shaderutil.o $(LIBS) -o $@ texdemo1.o: texdemo1.c readtex.h extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) texdemo1.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) texdemo1.c texdemo1: texdemo1.o readtex.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) texdemo1.o readtex.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) texdemo1.o readtex.o shaderutil.o $(LIBS) -o $@ toyball.o: toyball.c extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) toyball.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) toyball.c toyball: toyball.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) toyball.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) toyball.o shaderutil.o $(LIBS) -o $@ twoside.o: twoside.c extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) twoside.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) twoside.c twoside: twoside.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) twoside.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) twoside.o shaderutil.o $(LIBS) -o $@ trirast.o: trirast.c extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) trirast.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) trirast.c trirast: trirast.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) trirast.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) trirast.o shaderutil.o $(LIBS) -o $@ + + +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 + +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 $@ + + +vert-tex.o: vert-tex.c extfuncs.h shaderutil.h + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) vert-tex.c +vert-tex: vert-tex.o shaderutil.o + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) vert-tex.o shaderutil.o $(LIBS) -o $@ diff --git a/progs/glsl/convolutions.c b/progs/glsl/convolutions.c index c5caa8ffed..ac71c68235 100644 --- a/progs/glsl/convolutions.c +++ b/progs/glsl/convolutions.c @@ -61,15 +61,15 @@ static void loadAndCompileShader(GLuint shader, const char *text) { GLint stat; - glShaderSourceARB(shader, 1, (const GLchar **) &text, NULL); + glShaderSource(shader, 1, (const GLchar **) &text, NULL); - glCompileShaderARB(shader); + glCompileShader(shader); - glGetObjectParameterivARB(shader, GL_COMPILE_STATUS, &stat); + glGetShaderiv(shader, GL_COMPILE_STATUS, &stat); if (!stat) { GLchar log[1000]; GLsizei len; - glGetInfoLogARB(shader, 1000, &len, log); + glGetShaderInfoLog(shader, 1000, &len, log); fprintf(stderr, "Problem compiling shader: %s\n", log); exit(1); } @@ -105,11 +105,11 @@ static void checkLink(GLuint prog) { GLint stat; - glGetObjectParameterivARB(prog, GL_LINK_STATUS, &stat); + glGetProgramiv(prog, GL_LINK_STATUS, &stat); if (!stat) { GLchar log[1000]; GLsizei len; - glGetInfoLogARB(prog, 1000, &len, log); + glGetProgramInfoLog(prog, 1000, &len, log); fprintf(stderr, "Linker error:\n%s\n", log); } else { @@ -200,12 +200,12 @@ static void setupConvolution() } loc = glGetUniformLocationARB(program, "KernelValue"); - glUniform4fvARB(loc, 9, vecKer); + glUniform4fv(loc, 9, vecKer); loc = glGetUniformLocationARB(program, "ScaleFactor"); - glUniform4fARB(loc, scale, scale, scale, scale); + glUniform4f(loc, scale, scale, scale, scale); loc = glGetUniformLocationARB(program, "BaseColor"); - glUniform4fARB(loc, baseColor[0], baseColor[1], - baseColor[2], baseColor[3]); + glUniform4f(loc, baseColor[0], baseColor[1], + baseColor[2], baseColor[3]); free(vecKer); free(kernel); @@ -229,10 +229,10 @@ static void createProgram(const char *vertProgFile, glAttachShader(program, fragShader); } - glLinkProgramARB(program); + glLinkProgram(program); checkLink(program); - glUseProgramObjectARB(program); + glUseProgram(program); /* assert(glIsProgram(program)); @@ -256,7 +256,7 @@ static void createProgram(const char *vertProgFile, 0.0 , -1.0 / texture.height, -1.0 / texture.width, -1.0 / texture.height }; GLuint offsetLoc = glGetUniformLocationARB(program, "Offset"); - glUniform2fvARB(offsetLoc, 9, offsets); + glUniform2fv(offsetLoc, 9, offsets); } setupConvolution(); @@ -331,6 +331,7 @@ static void init() fprintf(stderr, "Sorry, this program requires GL_ARB_shader_objects, GL_ARB_vertex_shader, and GL_ARB_fragment_shader\n"); exit(1); } + fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); diff --git a/progs/glsl/fragcoord.c b/progs/glsl/fragcoord.c new file mode 100644 index 0000000000..0b7561f3e4 --- /dev/null +++ b/progs/glsl/fragcoord.c @@ -0,0 +1,185 @@ +/** + * Test GLSL gl_FragCoord fragment program attribute. + * Color the quad's fragments according to their window position. + * + * Brian Paul + * 20 Nov 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" +#include "shaderutil.h" + + +static GLint WinWidth = 200, WinHeight = 200; +static char *FragProgFile = NULL; +static char *VertProgFile = NULL; +static GLuint fragShader; +static GLuint vertShader; +static GLuint program; +static GLint win = 0; +static GLboolean Anim = GL_TRUE; +static GLfloat PosX = 0.0, PosY = 0.0; + + +static void +Idle(void) +{ + float r = (WinWidth < WinHeight) ? WinWidth : WinHeight; + float a = glutGet(GLUT_ELAPSED_TIME) * 0.001; + r *= 0.25; + PosX = WinWidth / 2 + r * cos(a); + PosY = WinHeight / 2 + r * sin(a); + + glutPostRedisplay(); +} + + +static void +Redisplay(void) +{ + glClear(GL_COLOR_BUFFER_BIT); + + glPushMatrix(); + glTranslatef(PosX, PosY, 0.0); +#if 0 + glBegin(GL_POLYGON); + glVertex2f(-50, -50); + glVertex2f( 50, -50); + glVertex2f( 50, 50); + glVertex2f(-50, 50); + glEnd(); +#else + glutSolidSphere(50, 20, 10); +#endif + glPopMatrix(); + + glutSwapBuffers(); +} + + +static void +Reshape(int width, int height) +{ + glViewport(0, 0, width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, width, 0, height, -55, 55); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + WinWidth = width; + WinHeight = height; +} + + +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) +{ + (void) x; + (void) y; + + switch(key) { + case ' ': + case 'a': + Anim = !Anim; + glutIdleFunc(Anim ? Idle : NULL); + break; + case 27: + CleanUp(); + exit(0); + break; + } + glutPostRedisplay(); +} + + +static void +Init(void) +{ + static const char *fragShaderText = + "void main() { \n" + " vec4 scale = vec4(.005, 0.005, 0.5, 1.0);\n" + " gl_FragColor = gl_FragCoord * scale; \n" + "}\n"; + static const char *vertShaderText = + "void main() {\n" + " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" + "}\n"; + + if (!ShadersSupported()) + exit(1); + + GetExtensionFuncs(); + + vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText); + fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText); + program = LinkShaders(vertShader, fragShader); + + glUseProgram_func(program); + + /*assert(glGetError() == 0);*/ + + glClearColor(0.3f, 0.3f, 0.3f, 0.0f); + + printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); + + assert(glIsProgram_func(program)); + assert(glIsShader_func(fragShader)); + assert(glIsShader_func(vertShader)); + + glColor3f(1, 0, 0); +} + + +static void +ParseOptions(int argc, char *argv[]) +{ + int i; + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-fs") == 0) { + FragProgFile = argv[i+1]; + } + else if (strcmp(argv[i], "-vs") == 0) { + VertProgFile = argv[i+1]; + } + } +} + + +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(); + glutIdleFunc(Anim ? Idle : NULL); + glutMainLoop(); + return 0; +} diff --git a/progs/glsl/identity.c b/progs/glsl/identity.c new file mode 100644 index 0000000000..37579eb346 --- /dev/null +++ b/progs/glsl/identity.c @@ -0,0 +1,208 @@ +/** + * Test very basic glsl functionality (identity vertex and fragment shaders). + * Brian Paul & Stephane Marchesin + */ + + +#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" + + +static char *FragProgFile = NULL; +static char *VertProgFile = NULL; +static GLuint fragShader; +static GLuint vertShader; +static GLuint program; +static GLint win = 0; +static GLboolean anim = GL_FALSE; +static GLfloat xRot = 0.0f, yRot = 0.0f; +static int w,h; + + +static void +Redisplay(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glBegin(GL_TRIANGLES); + glColor3f(.8,0,0); + glVertex3f(-0.9, -0.9, 0.0); + glColor3f(0,.9,0); + glVertex3f( 0.9, -0.9, 0.0); + glColor3f(0,0,.7); + glVertex3f( 0.0, 0.9, 0.0); + glEnd(); + + glutSwapBuffers(); +} + + +static void +Idle(void) +{ + yRot = glutGet(GLUT_ELAPSED_TIME) * 0.1; + glutPostRedisplay(); +} + + +static void +Reshape(int width, int height) +{ + glViewport(0, 0, width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + w = width; + h = height; +} + + +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) +{ + (void) x; + (void) y; + + switch(key) { + case ' ': + case 'a': + anim = !anim; + if (anim) + glutIdleFunc(Idle); + else + glutIdleFunc(NULL); + break; + case 27: + CleanUp(); + exit(0); + break; + } + glutPostRedisplay(); +} + + +static void +SpecialKey(int key, int x, int y) +{ + const GLfloat step = 3.0f; + + (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) +{ + static const char *fragShaderText = + "void main() {\n" + " gl_FragColor = vec4(1.0,0.0,0.0,1.0);\n" + "}\n"; + static const char *vertShaderText = + "void main() {\n" + " gl_Position = gl_Vertex;\n" + "}\n"; + + if (!ShadersSupported()) + exit(1); + + GetExtensionFuncs(); + + if (FragProgFile) + fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile); + else + fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText); + + if (VertProgFile) + vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile); + else + vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText); + + program = LinkShaders(vertShader, fragShader); + + glUseProgram_func(program); + + /*assert(glGetError() == 0);*/ + + glClearColor(0.3f, 0.3f, 0.3f, 0.0f); + glEnable(GL_DEPTH_TEST); + + printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); + + assert(glIsProgram_func(program)); + assert(glIsShader_func(fragShader)); + assert(glIsShader_func(vertShader)); + + glColor3f(1, 0, 0); +} + + +static void +ParseOptions(int argc, char *argv[]) +{ + int i; + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-fs") == 0) { + FragProgFile = argv[i+1]; + } + else if (strcmp(argv[i], "-vs") == 0) { + VertProgFile = argv[i+1]; + } + } +} + + +int +main(int argc, char *argv[]) +{ + glutInit(&argc, argv); + glutInitWindowPosition( 0, 0); + glutInitWindowSize(200, 200); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); + win = glutCreateWindow(argv[0]); + glutReshapeFunc(Reshape); + glutKeyboardFunc(Key); + glutSpecialFunc(SpecialKey); + glutDisplayFunc(Redisplay); + if (anim) + glutIdleFunc(Idle); + ParseOptions(argc, argv); + Init(); + glutMainLoop(); + return 0; +} diff --git a/progs/glsl/multinoise.c b/progs/glsl/multinoise.c new file mode 100644 index 0000000000..2351863aff --- /dev/null +++ b/progs/glsl/multinoise.c @@ -0,0 +1,281 @@ +/** + * Another test for noise() functions (noise1 to noise4 tested independently). + * 13 Dec 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 const char *VertShaderText = + "void main() {\n" + " gl_TexCoord[0].xyz = gl_Vertex.xyz;\n" + " gl_TexCoord[0].w = gl_MultiTexCoord1.x;\n" + " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" + "}\n"; + +static const char *FragShaderText[ 4 ] = { + "void main()\n" + "{\n" + " gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].w ) * 0.5 + 0.5;\n" + " gl_FragColor.a = 1;\n" + "}\n", + "void main()\n" + "{\n" + " gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xw ) * 0.5 + 0.5;\n" + " gl_FragColor.a = 1;\n" + "}\n", + "void main()\n" + "{\n" + " gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xyw ) * 0.5 + 0.5;\n" + " gl_FragColor.a = 1;\n" + "}\n", + "void main()\n" + "{\n" + " gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xyzw ) * 0.5 + 0.5;\n" + " gl_FragColor.a = 1;\n" + "}\n" +}; + +struct uniform_info { + const char *name; + GLuint size; + GLint location; + GLfloat value[4]; +}; + +/* program/shader objects */ +static GLuint fragShader[ 4 ]; +static GLuint vertShader; +static GLuint program[ 4 ]; + +static GLint win = 0; +static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f; +static GLfloat Slice = 0.0; +static GLboolean Anim = GL_FALSE; + + +static void +Idle(void) +{ + Slice += 0.01; + glutPostRedisplay(); +} + + +static void +Redisplay(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glMultiTexCoord1f( GL_TEXTURE1, Slice ); + + 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); + + glutSolidTeapot( 1.0 ); + + glPopMatrix(); + + 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) +{ + GLint i; + + glDeleteShader_func(vertShader); + for( i = 0; i < 4; i++ ) { + glDeleteShader_func(fragShader[ i ]); + glDeleteProgram_func(program[ i ]); + } + glutDestroyWindow(win); +} + + +static void +Key(unsigned char key, int x, int y) +{ + const GLfloat step = 0.01; + (void) x; + (void) y; + + switch(key) { + case 'a': + Anim = !Anim; + glutIdleFunc(Anim ? Idle : NULL); + case 's': + Slice -= step; + break; + case 'S': + Slice += step; + break; + case 'z': + zRot -= 1.0; + break; + case 'Z': + zRot += 1.0; + break; + case '1': + case '2': + case '3': + case '4': + glUseProgram_func(program[ key - '1' ]); + break; + case 27: + CleanUp(); + exit(0); + break; + } + glutPostRedisplay(); +} + + +static void +SpecialKey(int key, int x, int y) +{ + const GLfloat step = 3.0f; + + (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 +LoadAndCompileShader(GLuint shader, const char *text) +{ + GLint stat; + + glShaderSource_func(shader, 1, (const GLchar **) &text, NULL); + + glCompileShader_func(shader); + + glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat); + if (!stat) { + GLchar log[1000]; + GLsizei len; + glGetShaderInfoLog_func(shader, 1000, &len, log); + fprintf(stderr, "noise: problem compiling shader: %s\n", log); + exit(1); + } + else { + printf("Shader compiled OK\n"); + } +} + + +static void +CheckLink(GLuint prog) +{ + GLint stat; + glGetProgramiv_func(prog, GL_LINK_STATUS, &stat); + if (!stat) { + GLchar log[1000]; + GLsizei len; + glGetProgramInfoLog_func(prog, 1000, &len, log); + fprintf(stderr, "Linker error:\n%s\n", log); + } + else { + fprintf(stderr, "Link success!\n"); + } +} + + +static void +Init(void) +{ + const char *version; + GLint i; + + version = (const char *) glGetString(GL_VERSION); + if (version[0] != '2' || version[1] != '.') { + printf("Warning: this program expects OpenGL 2.0\n"); + /*exit(1);*/ + } + + GetExtensionFuncs(); + + vertShader = glCreateShader_func(GL_VERTEX_SHADER); + LoadAndCompileShader(vertShader, VertShaderText); + + for( i = 0; i < 4; i++ ) { + fragShader[ i ] = glCreateShader_func(GL_FRAGMENT_SHADER); + LoadAndCompileShader(fragShader[ i ], FragShaderText[ i ]); + program[ i ] = glCreateProgram_func(); + glAttachShader_func(program[ i ], fragShader[ i ]); + glAttachShader_func(program[ i ], vertShader); + glLinkProgram_func(program[ i ]); + CheckLink(program[ i ]); + } + + glUseProgram_func(program[ 0 ]); + + assert(glGetError() == 0); + + glClearColor(0.4f, 0.4f, 0.8f, 0.0f); + + glColor3f(1, 0, 0); + + glFrontFace( GL_CW ); + glEnable( GL_CULL_FACE ); + glEnable( GL_DEPTH_TEST ); +} + + +int +main(int argc, char *argv[]) +{ + glutInit(&argc, argv); + glutInitWindowPosition( 0, 0); + glutInitWindowSize(400, 400); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); + win = glutCreateWindow(argv[0]); + glutReshapeFunc(Reshape); + glutKeyboardFunc(Key); + glutSpecialFunc(SpecialKey); + glutDisplayFunc(Redisplay); + Init(); + glutMainLoop(); + return 0; +} + diff --git a/progs/glsl/multitex.c b/progs/glsl/multitex.c index 096d40f64d..b4be463787 100644 --- a/progs/glsl/multitex.c +++ b/progs/glsl/multitex.c @@ -47,9 +47,12 @@ static const char *TexFiles[2] = static GLuint Program; -static GLfloat Xrot = -90.0, Yrot = .0, Zrot = 0.0; +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 GLint VertCoord_attr = -1, TexCoord0_attr = -1, TexCoord1_attr = -1; /* value[0] = tex unit */ @@ -60,32 +63,62 @@ static struct uniform_info Uniforms[] = { }; +static const GLfloat Tex0Coords[4][2] = { + { 0.0, 0.0 }, { 2.0, 0.0 }, { 2.0, 2.0 }, { 0.0, 2.0 } +}; + +static const GLfloat Tex1Coords[4][2] = { + { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 } +}; + +static const GLfloat VertCoords[4][2] = { + { -3.0, -3.0 }, { 3.0, -3.0 }, { 3.0, 3.0 }, { -3.0, 3.0 } +}; + + static void -DrawPolygon(GLfloat size) +DrawPolygonArray(void) { - glPushMatrix(); - glRotatef(90, 1, 0, 0); - glNormal3f(0, 0, 1); - glBegin(GL_POLYGON); + if (VertCoord_attr >= 0) { + glVertexAttribPointer_func(VertCoord_attr, 2, GL_FLOAT, GL_FALSE, + 0, VertCoords); + glEnableVertexAttribArray_func(VertCoord_attr); + } + else { + glVertexPointer(2, GL_FLOAT, 0, VertCoords); + glEnable(GL_VERTEX_ARRAY); + } - glMultiTexCoord2f(GL_TEXTURE0, 0, 0); - glMultiTexCoord2f(GL_TEXTURE1, 0, 0); - glVertex2f(-size, -size); + glVertexAttribPointer_func(TexCoord0_attr, 2, GL_FLOAT, GL_FALSE, + 0, Tex0Coords); + glEnableVertexAttribArray_func(TexCoord0_attr); - glMultiTexCoord2f(GL_TEXTURE0, 2, 0); - glMultiTexCoord2f(GL_TEXTURE1, 1, 0); - glVertex2f( size, -size); + glVertexAttribPointer_func(TexCoord1_attr, 2, GL_FLOAT, GL_FALSE, + 0, Tex1Coords); + glEnableVertexAttribArray_func(TexCoord1_attr); - glMultiTexCoord2f(GL_TEXTURE0, 2, 2); - glMultiTexCoord2f(GL_TEXTURE1, 1, 1); - glVertex2f( size, size); + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); +} - glMultiTexCoord2f(GL_TEXTURE0, 0, 2); - glMultiTexCoord2f(GL_TEXTURE1, 0, 1); - glVertex2f(-size, size); + +static void +DrawPolygonVert(void) +{ + GLuint i; + + glBegin(GL_TRIANGLE_FAN); + + for (i = 0; i < 4; i++) { + glVertexAttrib2fv_func(TexCoord0_attr, Tex0Coords[i]); + glVertexAttrib2fv_func(TexCoord1_attr, Tex1Coords[i]); + + if (VertCoord_attr >= 0) + glVertexAttrib2fv_func(VertCoord_attr, VertCoords[i]); + else + glVertex2fv(VertCoords[i]); + } glEnd(); - glPopMatrix(); } @@ -100,7 +133,10 @@ draw(void) glRotatef(Yrot, 0, 1, 0); glRotatef(Xrot, 1, 0, 0); - DrawPolygon(3.0); + if (UseArrays) + DrawPolygonArray(); + else + DrawPolygonVert(); glPopMatrix(); @@ -123,8 +159,11 @@ key(unsigned char k, int x, int y) (void) x; (void) y; switch (k) { - case ' ': case 'a': + UseArrays = !UseArrays; + printf("Arrays: %d\n", UseArrays); + break; + case ' ': Anim = !Anim; if (Anim) glutIdleFunc(idle); @@ -232,6 +271,13 @@ CreateProgram(const char *vertProgFile, const char *fragProgFile, InitUniforms(program, uniforms); + 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); + return program; } diff --git a/progs/glsl/multitex.vert b/progs/glsl/multitex.vert index 5518ca1ddd..4fae3b73fb 100644 --- a/progs/glsl/multitex.vert +++ b/progs/glsl/multitex.vert @@ -2,9 +2,13 @@ // Brian Paul +attribute vec4 TexCoord0, TexCoord1; +attribute vec4 VertCoord; + void main() { - gl_TexCoord[0] = gl_MultiTexCoord0; - gl_TexCoord[1] = gl_MultiTexCoord1; - gl_Position = ftransform(); + gl_TexCoord[0] = TexCoord0; + gl_TexCoord[1] = TexCoord1; + // note: may use gl_Vertex or VertCoord here for testing: + gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } diff --git a/progs/glsl/noise.c b/progs/glsl/noise.c index 9da71ac775..bd8f50036b 100644 --- a/progs/glsl/noise.c +++ b/progs/glsl/noise.c @@ -30,6 +30,7 @@ static const char *FragShaderText = " vec4 p;\n" " p.xy = gl_TexCoord[0].xy;\n" " p.z = Slice;\n" + " p.w = 0;\n" " vec4 n = noise4(p * scale);\n" " gl_FragColor = n * Scale + Bias;\n" "}\n"; diff --git a/progs/glsl/samplers.c b/progs/glsl/samplers.c new file mode 100644 index 0000000000..3fb8577d5e --- /dev/null +++ b/progs/glsl/samplers.c @@ -0,0 +1,369 @@ +/** + * Exercise all available GLSL texture samplers. + * + * Copyright (C) 2009 VMware, Inc. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/** + * We generate a fragment shader which uses the maximum number of supported + * texture samplers. + * For each sampler we create a separate texture. Each texture has a + * single strip of color at a different intensity. The fragment shader + * samples all the textures at the same coordinate and sums the values. + * The result should be a quad with rows of colors of increasing intensity + * from bottom to top. + * + * Brian Paul + * 1 Jan 2009 + */ + +#include <assert.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "GL/glut.h" +#include "readtex.h" +#include "extfuncs.h" +#include "shaderutil.h" + + +#define MAX_SAMPLERS 128 + + +static const char *Demo = "samplers"; + +static GLuint Program; +static GLint NumSamplers; +static GLuint Textures[MAX_SAMPLERS]; +static GLfloat Xrot = 0.0, Yrot = .0, Zrot = 0.0; +static GLfloat EyeDist = 10; +static GLboolean Anim = GL_FALSE; + + +static void +DrawPolygon(GLfloat size) +{ + glPushMatrix(); + glNormal3f(0, 0, 1); + glBegin(GL_POLYGON); + + glMultiTexCoord2f(GL_TEXTURE0, 0, 0); + glVertex2f(-size, -size); + + glMultiTexCoord2f(GL_TEXTURE0, 1, 0); + glVertex2f( size, -size); + + glMultiTexCoord2f(GL_TEXTURE0, 1, 1); + glVertex2f( size, size); + + glMultiTexCoord2f(GL_TEXTURE0, 0, 1); + glVertex2f(-size, size); + + glEnd(); + glPopMatrix(); +} + + +static void +draw(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glPushMatrix(); + glTranslatef(0.0, 0.0, -EyeDist); + glRotatef(Zrot, 0, 0, 1); + glRotatef(Yrot, 0, 1, 0); + glRotatef(Xrot, 1, 0, 0); + + DrawPolygon(3.0); + + glPopMatrix(); + + glutSwapBuffers(); +} + + +static void +idle(void) +{ + GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME); + Yrot = t; + glutPostRedisplay(); +} + + +static void +key(unsigned char k, int x, int y) +{ + (void) x; + (void) y; + switch (k) { + case ' ': + case 'a': + Anim = !Anim; + if (Anim) + glutIdleFunc(idle); + else + glutIdleFunc(NULL); + break; + case 'z': + EyeDist -= 0.5; + if (EyeDist < 3.0) + EyeDist = 3.0; + break; + case 'Z': + EyeDist += 0.5; + if (EyeDist > 90.0) + EyeDist = 90; + break; + case 27: + exit(0); + } + glutPostRedisplay(); +} + + +static void +specialkey(int key, int x, int y) +{ + 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(); +} + + +/* new window size or exposure */ +static void +Reshape(int width, int height) +{ + GLfloat ar = (float) width / (float) height; + glViewport(0, 0, (GLint)width, (GLint)height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + + +static void +InitTextures(void) +{ + const GLint size = MAX_SAMPLERS; + GLubyte *texImage; + GLenum filter = GL_NEAREST; + GLint stripeSize; + GLint s; + + texImage = (GLubyte *) malloc(size * size * 4); + + glGenTextures(NumSamplers, Textures); + + /* size of texels stripe */ + stripeSize = size / NumSamplers; + + /* create a texture for each sampler */ + for (s = 0; s < NumSamplers; s++) { + GLint x, y, ypos; + GLubyte intensity = 31 + s * (256-32) / (NumSamplers - 1); + + printf("Texture %d: color = %d, %d, %d\n", s, + (int) intensity, 0, (int) intensity ); + + /* initialize the texture to black */ + memset(texImage, 0, size * size * 4); + + /* set a stripe of texels to the intensity value */ + ypos = s * stripeSize; + for (y = 0; y < stripeSize; y++) { + for (x = 0; x < size; x++) { + GLint k = 4 * ((ypos + y) * size + x); + texImage[k + 0] = intensity; + texImage[k + 1] = intensity; + texImage[k + 2] = 0; + texImage[k + 3] = 255; + } + } + + glActiveTexture(GL_TEXTURE0 + s); + glBindTexture(GL_TEXTURE_2D, Textures[s]); + gluBuild2DMipmaps(GL_TEXTURE_2D, 4, size, size, + GL_RGBA, GL_UNSIGNED_BYTE, texImage); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter); + } + + free(texImage); +} + + +/** + * Generate a fragment shader that uses the given number of samplers. + */ +static char * +GenFragmentShader(GLint numSamplers) +{ + const int maxLen = 10 * 1000; + char *prog = (char *) malloc(maxLen); + char *p = prog; + int s; + + p += sprintf(p, "// Generated fragment shader:\n"); +#ifndef SAMPLERS_ARRAY + for (s = 0; s < numSamplers; s++) { + p += sprintf(p, "uniform sampler2D tex%d;\n", s); + } +#else + p += sprintf(p, "uniform sampler2D tex[%d];\n", numSamplers); +#endif + p += sprintf(p, "void main()\n"); + p += sprintf(p, "{\n"); + p += sprintf(p, " vec4 color = vec4(0.0);\n"); + for (s = 0; s < numSamplers; s++) { +#ifndef SAMPLERS_ARRAY + p += sprintf(p, " color += texture2D(tex%d, gl_TexCoord[0].xy);\n", s); +#else + p += sprintf(p, " color += texture2D(tex[%d], gl_TexCoord[0].xy);\n", s); +#endif + } + p += sprintf(p, " gl_FragColor = color;\n"); + p += sprintf(p, "}\n"); + + assert(p - prog < maxLen); + return prog; +} + + +/** Create & bind shader program */ +static GLuint +CreateProgram(void) +{ + GLuint fragShader, vertShader, program; + const char *vertShaderText = + "void main() \n" + "{ \n" + " gl_TexCoord[0] = gl_MultiTexCoord0; \n" + " gl_Position = ftransform(); \n" + "} \n"; + char *fragShaderText = GenFragmentShader(NumSamplers); + + printf("%s", fragShaderText); + + vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText); + fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText); + assert(vertShader); + program = LinkShaders(vertShader, fragShader); + + glUseProgram_func(program); + + free(fragShaderText); + + return program; +} + + +static void +InitProgram(void) +{ + GLint s; + + Program = CreateProgram(); + + /* init sampler uniforms */ + for (s = 0; s < NumSamplers; s++) { + char uname[10]; + GLint loc; + +#ifndef SAMPLERS_ARRAY + sprintf(uname, "tex%d", s); +#else + sprintf(uname, "tex[%d]", s); +#endif + loc = glGetUniformLocation_func(Program, uname); + assert(loc >= 0); + + glUniform1i_func(loc, s); + } +} + + +static void +InitGL(void) +{ + if (!ShadersSupported()) { + printf("GLSL not supported!\n"); + exit(1); + } + + printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER)); + + GetExtensionFuncs(); + + glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &NumSamplers); + if (NumSamplers > MAX_SAMPLERS) + NumSamplers = MAX_SAMPLERS; + printf("Testing %d samplers\n", NumSamplers); + + InitTextures(); + InitProgram(); + + glClearColor(.6, .6, .9, 0); + glColor3f(1.0, 1.0, 1.0); + + printf("Each color corresponds to a separate sampler/texture.\n"); +} + + +int +main(int argc, char *argv[]) +{ + glutInit(&argc, argv); + glutInitWindowSize(500, 400); + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); + glutCreateWindow(Demo); + glutReshapeFunc(Reshape); + glutKeyboardFunc(key); + glutSpecialFunc(specialkey); + glutDisplayFunc(draw); + if (Anim) + glutIdleFunc(idle); + InitGL(); + glutMainLoop(); + return 0; +} diff --git a/progs/glsl/texdemo1.c b/progs/glsl/texdemo1.c index 41010746ee..96ddca1f32 100644 --- a/progs/glsl/texdemo1.c +++ b/progs/glsl/texdemo1.c @@ -49,6 +49,7 @@ static GLfloat TexXrot = 0, TexYrot = 0; static GLfloat Xrot = 20.0, Yrot = 20.0, Zrot = 0.0; static GLfloat EyeDist = 10; static GLboolean Anim = GL_TRUE; +static int win = 0; static struct uniform_info ReflectUniforms[] = { @@ -159,6 +160,7 @@ key(unsigned char k, int x, int y) EyeDist = 90; break; case 27: + glutDestroyWindow(win); exit(0); } glutPostRedisplay(); @@ -423,7 +425,7 @@ main(int argc, char *argv[]) glutInit(&argc, argv); glutInitWindowSize(500, 400); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); - glutCreateWindow(Demo); + win = glutCreateWindow(Demo); glutReshapeFunc(Reshape); glutKeyboardFunc(key); glutSpecialFunc(specialkey); diff --git a/progs/glsl/twoside.c b/progs/glsl/twoside.c index 672a00491e..06488bd175 100644 --- a/progs/glsl/twoside.c +++ b/progs/glsl/twoside.c @@ -26,27 +26,39 @@ static GLuint fragShader; static GLuint vertShader; static GLuint program; static GLint win = 0; -static GLboolean anim = 0*GL_TRUE; -static GLboolean DetermineInFragProg = GL_TRUE; -static GLfloat Xrot = 30.0f; +static GLboolean anim; +static GLboolean DetermineFacingInFragProg; +static GLfloat Xrot; static GLint u_fragface; -static GLenum FrontWinding = GL_CCW; +static GLenum FrontWinding; static int prevTime = 0; -static const GLfloat Red[4] = {1, 0, 0, 0}; +static const GLfloat Red[4] = {1, 0, 0, 1}; static const GLfloat Green[4] = {0, 1, 0, 0}; static void +SetDefaults(void) +{ + DetermineFacingInFragProg = GL_TRUE; + FrontWinding = GL_CCW; + Xrot = 30; + anim = 0; + glutIdleFunc(NULL); +} + + +static void Redisplay(void) { + const int sections = 20; int i; float radius = 2; glFrontFace(FrontWinding); - if (DetermineInFragProg) { + if (DetermineFacingInFragProg) { glUniform1i_func(u_fragface, 1); glDisable(GL_VERTEX_PROGRAM_TWO_SIDE); } @@ -64,8 +76,8 @@ Redisplay(void) glBegin(GL_TRIANGLE_STRIP); glColor4fv(Red); glSecondaryColor3fv_func(Green); - for (i = 0; i < 20; i++) { - float a = i / 19.0 * M_PI * 2.0; + for (i = 0; i <= sections; i++) { + float a = (float) i / (sections) * M_PI * 2.0; float x = radius * cos(a); float y = radius * sin(a); glVertex3f(x, -1, y); @@ -139,17 +151,15 @@ Key(unsigned char key, int x, int y) break; case 'f': printf("Using frag shader gl_FrontFacing\n"); - DetermineInFragProg = GL_TRUE; + DetermineFacingInFragProg = GL_TRUE; break; case 'v': printf("Using vert shader Two-sided lighting\n"); - DetermineInFragProg = GL_FALSE; + DetermineFacingInFragProg = GL_FALSE; break; case 'r': /* reset */ - Xrot = 30; - anim = 0; - glutIdleFunc(NULL); + SetDefaults(); break; case 's': Xrot += 5; @@ -182,14 +192,16 @@ Init(void) static const char *fragShaderText = "uniform bool fragface; \n" "void main() { \n" -#if 0 +#if 1 " if (!fragface || gl_FrontFacing) { \n" " gl_FragColor = gl_Color; \n" " } \n" " else { \n" + " // note: dim green to help debug \n" " gl_FragColor = 0.8 * gl_SecondaryColor; \n" " } \n" #else + /* DEBUG CODE */ " bool f = gl_FrontFacing; \n" " if (f) { \n" " gl_FragColor = vec4(1.0, 0.0, 0.0, 0.0); \n" @@ -197,8 +209,6 @@ Init(void) " else { \n" " gl_FragColor = vec4(0.0, 1.0, 0.0, 0.0); \n" " } \n" - " //float g = float(gl_FrontFacing) * 0.5 + 0.5; \n" - " //gl_FragColor = vec4(g); \n" #endif "} \n"; static const char *vertShaderText = @@ -241,6 +251,8 @@ Init(void) assert(glIsShader_func(vertShader)); glEnable(GL_DEPTH_TEST); + + SetDefaults(); } diff --git a/progs/glsl/vert-or-frag-only.c b/progs/glsl/vert-or-frag-only.c new file mode 100644 index 0000000000..f6eedd8327 --- /dev/null +++ b/progs/glsl/vert-or-frag-only.c @@ -0,0 +1,191 @@ +/** + * Draw two quads, one using only a vertex shader, the other only with a + * fragment shader. They should appear the same. + * 17 Dec 2008 + * Brian Paul + */ + + +#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" + + +static char *FragProgFile = NULL; +static char *VertProgFile = NULL; +static GLuint FragShader; +static GLuint VertShader; +static GLuint VertProgram; /* w/out vertex shader */ +static GLuint FragProgram; /* w/out fragment shader */ +static GLint Win = 0; + + +static void +DrawQuadColor(void) +{ + glBegin(GL_QUADS); + glColor3f(1, 0, 0); glVertex2f(-1, -1); + glColor3f(0, 1, 0); glVertex2f( 1, -1); + glColor3f(0, 0, 1); glVertex2f( 1, 1); + glColor3f(1, 0, 1); glVertex2f(-1, 1); + glEnd(); +} + + +/** as above, but specify color via texcoords */ +static void +DrawQuadTex(void) +{ + glBegin(GL_QUADS); + glTexCoord3f(1, 0, 0); glVertex2f(-1, -1); + glTexCoord3f(0, 1, 0); glVertex2f( 1, -1); + glTexCoord3f(0, 0, 1); glVertex2f( 1, 1); + glTexCoord3f(1, 0, 1); glVertex2f(-1, 1); + glEnd(); +} + + +static void +Redisplay(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + /* render with vertex shader only */ + glUseProgram_func(VertProgram); + glPushMatrix(); + glTranslatef(-1.5, 0, 0); + DrawQuadTex(); + glPopMatrix(); + + /* render with fragment shader only */ + glUseProgram_func(FragProgram); + glPushMatrix(); + glTranslatef(+1.5, 0, 0); + DrawQuadColor(); + glPopMatrix(); + + glutSwapBuffers(); +} + + +static void +Reshape(int width, int height) +{ + glViewport(0, 0, width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-4, 4, -2, 2, -1, 1); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + + +static void +CleanUp(void) +{ + glDeleteShader_func(FragShader); + glDeleteShader_func(VertShader); + glDeleteProgram_func(VertProgram); + glDeleteProgram_func(FragProgram); + glutDestroyWindow(Win); +} + + +static void +Key(unsigned char key, int x, int y) +{ + (void) x; + (void) y; + switch(key) { + case 27: + CleanUp(); + exit(0); + break; + } + glutPostRedisplay(); +} + + +static void +Init(void) +{ + static const char *fragShaderText = + "void main() {\n" + " gl_FragColor = gl_Color;\n" + "}\n"; + static const char *vertShaderText = + "void main() {\n" + " gl_Position = ftransform();\n" + " gl_FrontColor = gl_MultiTexCoord0;\n" /* see DrawQuadTex() */ + "}\n"; + + if (!ShadersSupported()) + exit(1); + + GetExtensionFuncs(); + + if (FragProgFile) + FragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile); + else + FragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText); + + if (VertProgFile) + VertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile); + else + VertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText); + + VertProgram = LinkShaders(VertShader, 0); + FragProgram = LinkShaders(0, FragShader); + + glClearColor(0.3f, 0.3f, 0.3f, 0.0f); + glEnable(GL_DEPTH_TEST); + + printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); + + assert(glIsProgram_func(VertProgram)); + assert(glIsProgram_func(FragProgram)); + assert(glIsShader_func(FragShader)); + assert(glIsShader_func(VertShader)); + + glColor3f(1, 0, 0); +} + + +static void +ParseOptions(int argc, char *argv[]) +{ + int i; + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-fs") == 0) { + FragProgFile = argv[i+1]; + } + else if (strcmp(argv[i], "-vs") == 0) { + VertProgFile = argv[i+1]; + } + } +} + + +int +main(int argc, char *argv[]) +{ + glutInit(&argc, argv); + glutInitWindowPosition( 0, 0); + glutInitWindowSize(400, 200); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); + Win = glutCreateWindow(argv[0]); + glutReshapeFunc(Reshape); + glutKeyboardFunc(Key); + glutDisplayFunc(Redisplay); + ParseOptions(argc, argv); + Init(); + glutMainLoop(); + return 0; +} diff --git a/progs/glsl/vert-tex.c b/progs/glsl/vert-tex.c new file mode 100644 index 0000000000..9d00a61054 --- /dev/null +++ b/progs/glsl/vert-tex.c @@ -0,0 +1,279 @@ +/** + * Vertex shader texture sampling test. + * Brian Paul + * 2 Dec 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" +#include "shaderutil.h" + + +static const char *VertShaderText = + "uniform sampler2D tex1; \n" + "void main() \n" + "{ \n" + " vec4 pos = gl_Vertex; \n" + " pos.z = texture2D(tex1, gl_MultiTexCoord0.xy).x - 0.5; \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; + + +/* value[0] = tex unit */ +static struct uniform_info Uniforms[] = { + { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 }, + END_OF_UNIFORMS +}; + + + +static void +Idle(void) +{ + zRot = 90 + glutGet(GLUT_ELAPSED_TIME) * 0.05; + glutPostRedisplay(); +} + + +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++) { + glTexCoord2f(s, t); + glVertex2f(x, y); + glTexCoord2f(s, t + dt); + glVertex2f(x, y + dy); + x += dx; + s += ds; + } + glEnd(); + y += dy; + t += dt; + } +} + + +static void +Redisplay(void) +{ + if (WireFrame) + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + else + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + 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(); + + 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 +MakeTexture(void) +{ + const GLuint texWidth = 64, texHeight = 64; + GLfloat texImage[64][64]; + GLuint i, j; + + /* texture is basically z = f(x, y) */ + for (i = 0; i < texHeight; i++) { + GLfloat y = 2.0 * (i / (float) (texHeight - 1)) - 1.0; + for (j = 0; j < texWidth; j++) { + GLfloat x = 2.0 * (j / (float) (texWidth - 1)) - 1.0; + GLfloat z = 0.5 + 0.5 * (sin(4.0 * x) * sin(4.0 * y)); + texImage[i][j] = z; + } + } + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, 42); + glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, texWidth, texHeight, 0, + GL_LUMINANCE, GL_FLOAT, texImage); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); +} + + +static void +Init(void) +{ + GLint m; + + glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, &m); + if (m < 1) { + printf("Error: no vertex shader texture units supported.\n"); + exit(1); + } + + if (!ShadersSupported()) + exit(1); + + GetExtensionFuncs(); + + vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText); + fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText); + program = LinkShaders(vertShader, fragShader); + + glUseProgram_func(program); + + assert(glGetError() == 0); + + MakeTexture(); + + 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; +} + |