diff options
Diffstat (limited to 'progs')
-rw-r--r-- | progs/glsl/.gitignore | 2 | ||||
-rw-r--r-- | progs/glsl/Makefile | 14 | ||||
-rw-r--r-- | progs/glsl/samplers.c | 357 | ||||
-rw-r--r-- | progs/glsl/twoside.c | 44 |
4 files changed, 397 insertions, 20 deletions
diff --git a/progs/glsl/.gitignore b/progs/glsl/.gitignore index 5d954519b24..40df5bb9780 100644 --- a/progs/glsl/.gitignore +++ b/progs/glsl/.gitignore @@ -21,3 +21,5 @@ texdemo1 toyball trirast twoside +vert-or-frag-only +vert-tex diff --git a/progs/glsl/Makefile b/progs/glsl/Makefile index 34da3a5a709..a39170b8c9c 100644 --- a/progs/glsl/Makefile +++ b/progs/glsl/Makefile @@ -23,6 +23,7 @@ PROGS = \ noise \ points \ pointcoord \ + samplers \ skinning \ texdemo1 \ toyball \ @@ -119,10 +120,10 @@ identity: identity.o shaderutil.o fragcoord.o: fragcoord.c extfuncs.h shaderutil.h - $(CC) -c -I$(INCDIR) $(CFLAGS) fragcoord.c + $(APP_CC) -c -I$(INCDIR) $(CFLAGS) fragcoord.c fragcoord: fragcoord.o shaderutil.o - $(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) fragcoord.o shaderutil.o $(LIBS) -o $@ + $(APP_CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) fragcoord.o shaderutil.o $(LIBS) -o $@ mandelbrot.o: mandelbrot.c extfuncs.h shaderutil.h @@ -159,6 +160,13 @@ pointcoord: pointcoord.o readtex.o shaderutil.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 $@ + + skinning.o: skinning.c readtex.h extfuncs.h shaderutil.h $(APP_CC) -c -I$(INCDIR) $(CFLAGS) skinning.c @@ -201,8 +209,6 @@ 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 diff --git a/progs/glsl/samplers.c b/progs/glsl/samplers.c new file mode 100644 index 00000000000..d2140097297 --- /dev/null +++ b/progs/glsl/samplers.c @@ -0,0 +1,357 @@ +/** + * 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"); + for (s = 0; s < numSamplers; s++) { + p += sprintf(p, "uniform sampler2D tex%d;\n", s); + } + 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++) { + p += sprintf(p, " color += texture2D(tex%d, gl_TexCoord[0].xy);\n", s); + } + 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; + + sprintf(uname, "tex%d", s); + 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/twoside.c b/progs/glsl/twoside.c index 672a00491e6..06488bd1759 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(); } |