diff options
author | Julien Cristau <[email protected]> | 2007-04-21 20:27:55 +0200 |
---|---|---|
committer | Julien Cristau <[email protected]> | 2007-04-21 20:27:55 +0200 |
commit | d890ebdd2591c5c09038c8986341da17127ba930 (patch) | |
tree | 46b3b277f2dca3a2ee7741be0f0ec41db7bd5313 /progs/demos/fslight.c | |
parent | 5ee9b3ce729c41dff80dc7ee2db8fdc30585c9e2 (diff) | |
parent | 5ca8d4ccf24af1c174ff791f15cf9f19defd9b7e (diff) |
Merge branch 'upstream-experimental' into debian-experimental
Conflicts:
doxygen/Makefile
doxygen/common.doxy
doxygen/doxy.bat
doxygen/glapi.doxy
doxygen/header.html
doxygen/main.doxy
doxygen/math.doxy
doxygen/shader.doxy
doxygen/swrast.doxy
doxygen/swrast_setup.doxy
doxygen/tnl.doxy
doxygen/tnl_dd.doxy
progs/tests/Makefile
progs/tests/fbotest1.c
progs/tests/fbotest2.c
progs/tests/fbotexture.c
progs/tests/mipmap_limits.c
progs/tests/sharedtex.c
progs/tools/trace/gltrace_support.cc
progs/trivial/Makefile
src/egl/docs/EGL_MESA_screen_surface
src/egl/main/egllog.c
src/glx/mini/Makefile
src/glx/mini/driver.h
src/mesa/drivers/allegro/amesa.c
src/mesa/drivers/windows/gldirect/dx7/gld_driver_dx7.c
src/mesa/drivers/windows/gldirect/dx7/gld_ext_dx7.c
src/mesa/drivers/windows/gldirect/dx7/gld_primitive_dx7.c
src/mesa/drivers/windows/gldirect/dx8/gld_driver_dx8.c
src/mesa/drivers/windows/gldirect/dx8/gld_ext_dx8.c
src/mesa/drivers/windows/gldirect/dx8/gld_primitive_dx8.c
src/mesa/drivers/windows/gldirect/dx9/gld_driver_dx9.c
src/mesa/drivers/windows/gldirect/dx9/gld_ext_dx9.c
src/mesa/drivers/windows/gldirect/dx9/gld_primitive_dx9.c
src/mesa/drivers/windows/gldirect/mesasw/gld_wgl_mesasw.c
Diffstat (limited to 'progs/demos/fslight.c')
-rw-r--r-- | progs/demos/fslight.c | 393 |
1 files changed, 341 insertions, 52 deletions
diff --git a/progs/demos/fslight.c b/progs/demos/fslight.c index 1c016cc75e9..493ce0e90f1 100644 --- a/progs/demos/fslight.c +++ b/progs/demos/fslight.c @@ -20,20 +20,30 @@ #include <GL/gl.h> #include <GL/glut.h> #include <GL/glext.h> +#include "extfuncs.h" + + +#define TEXTURE 0 + +static GLint CoordAttrib = 0; + +static char *FragProgFile = NULL; +static char *VertProgFile = NULL; static GLfloat diffuse[4] = { 0.5f, 0.5f, 1.0f, 1.0f }; static GLfloat specular[4] = { 0.8f, 0.8f, 0.8f, 1.0f }; -static GLfloat lightPos[4] = { 0.0f, 10.0f, 20.0f, 1.0f }; +static GLfloat lightPos[4] = { 0.0f, 10.0f, 20.0f, 0.0f }; static GLfloat delta = 1.0f; static GLuint fragShader; static GLuint vertShader; static GLuint program; -static GLint uLightPos; static GLint uDiffuse; static GLint uSpecular; +static GLint uTexture; +static GLuint SphereList, RectList, CurList; static GLint win = 0; static GLboolean anim = GL_TRUE; static GLboolean wire = GL_FALSE; @@ -42,24 +52,7 @@ static GLboolean pixelLight = GL_TRUE; static GLint t0 = 0; static GLint frames = 0; -static GLfloat xRot = 0.0f, yRot = 0.0f; - -static PFNGLCREATESHADERPROC glCreateShader_func = NULL; -static PFNGLSHADERSOURCEPROC glShaderSource_func = NULL; -static PFNGLGETSHADERSOURCEPROC glGetShaderSource_func = NULL; -static PFNGLCOMPILESHADERPROC glCompileShader_func = NULL; -static PFNGLCREATEPROGRAMPROC glCreateProgram_func = NULL; -static PFNGLDELETEPROGRAMPROC glDeleteProgram_func = NULL; -static PFNGLDELETESHADERPROC glDeleteShader_func = NULL; -static PFNGLATTACHSHADERPROC glAttachShader_func = NULL; -static PFNGLLINKPROGRAMPROC glLinkProgram_func = NULL; -static PFNGLUSEPROGRAMPROC glUseProgram_func = NULL; -static PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation_func = NULL; -static PFNGLISPROGRAMPROC glIsProgram_func = NULL; -static PFNGLISSHADERPROC glIsShader_func = NULL; -static PFNGLUNIFORM3FVPROC glUniform3fv_func = NULL; -static PFNGLUNIFORM3FVPROC glUniform4fv_func = NULL; - +static GLfloat xRot = 90.0f, yRot = 0.0f; static void @@ -69,31 +62,37 @@ normalize(GLfloat *dst, const GLfloat *src) dst[0] = src[0] / len; dst[1] = src[1] / len; dst[2] = src[2] / len; + dst[3] = src[3]; } static void Redisplay(void) { + GLfloat vec[4]; + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + /* update light position */ + normalize(vec, lightPos); + glLightfv(GL_LIGHT0, GL_POSITION, vec); if (pixelLight) { - GLfloat vec[3]; glUseProgram_func(program); - normalize(vec, lightPos); - glUniform3fv_func(uLightPos, 1, vec); glDisable(GL_LIGHTING); } else { glUseProgram_func(0); - glLightfv(GL_LIGHT0, GL_POSITION, lightPos); glEnable(GL_LIGHTING); } glPushMatrix(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); + /* glutSolidSphere(2.0, 10, 5); + */ + glCallList(CurList); glPopMatrix(); glutSwapBuffers(); @@ -174,6 +173,12 @@ Key(unsigned char key, int x, int y) else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); break; + case 'o': + if (CurList == SphereList) + CurList = RectList; + else + CurList = SphereList; + break; case 'p': pixelLight = !pixelLight; if (pixelLight) @@ -217,16 +222,248 @@ SpecialKey(int key, int x, int y) static void +TestFunctions(void) +{ + printf("Error 0x%x at line %d\n", glGetError(), __LINE__); + { + GLfloat pos[3]; + printf("Error 0x%x at line %d\n", glGetError(), __LINE__); + printf("Light pos %g %g %g\n", pos[0], pos[1], pos[2]); + } + + + { + GLfloat m[16], result[16]; + GLint mPos; + int i; + + for (i = 0; i < 16; i++) + m[i] = (float) i; + + mPos = glGetUniformLocation_func(program, "m"); + printf("Error 0x%x at line %d\n", glGetError(), __LINE__); + glUniformMatrix4fv_func(mPos, 1, GL_FALSE, m); + printf("Error 0x%x at line %d\n", glGetError(), __LINE__); + + glGetUniformfv_func(program, mPos, result); + printf("Error 0x%x at line %d\n", glGetError(), __LINE__); + + for (i = 0; i < 16; i++) { + printf("%8g %8g\n", m[i], result[i]); + } + } + + assert(glIsProgram_func(program)); + assert(glIsShader_func(fragShader)); + assert(glIsShader_func(vertShader)); + + /* attached shaders */ + { + GLuint shaders[20]; + GLsizei count; + int i; + glGetAttachedShaders_func(program, 20, &count, shaders); + for (i = 0; i < count; i++) { + printf("Attached: %u\n", shaders[i]); + assert(shaders[i] == fragShader || + shaders[i] == vertShader); + } + } + + { + GLchar log[1000]; + GLsizei len; + glGetShaderInfoLog_func(vertShader, 1000, &len, log); + printf("Vert Shader Info Log: %s\n", log); + glGetShaderInfoLog_func(fragShader, 1000, &len, log); + printf("Frag Shader Info Log: %s\n", log); + glGetProgramInfoLog_func(program, 1000, &len, log); + printf("Program Info Log: %s\n", log); + } +} + + +#if TEXTURE +static void +MakeTexture(void) +{ +#define SZ0 64 +#define SZ1 32 + GLubyte image0[SZ0][SZ0][SZ0][4]; + GLubyte image1[SZ1][SZ1][SZ1][4]; + GLuint i, j, k; + + /* level 0: two-tone gray checkboard */ + for (i = 0; i < SZ0; i++) { + for (j = 0; j < SZ0; j++) { + for (k = 0; k < SZ0; k++) { + if ((i/8 + j/8 + k/8) & 1) { + image0[i][j][k][0] = + image0[i][j][k][1] = + image0[i][j][k][2] = 200; + } + else { + image0[i][j][k][0] = + image0[i][j][k][1] = + image0[i][j][k][2] = 100; + } + image0[i][j][k][3] = 255; + } + } + } + + /* level 1: two-tone green checkboard */ + for (i = 0; i < SZ1; i++) { + for (j = 0; j < SZ1; j++) { + for (k = 0; k < SZ1; k++) { + if ((i/8 + j/8 + k/8) & 1) { + image1[i][j][k][0] = 0; + image1[i][j][k][1] = 250; + image1[i][j][k][2] = 0; + } + else { + image1[i][j][k][0] = 0; + image1[i][j][k][1] = 200; + image1[i][j][k][2] = 0; + } + image1[i][j][k][3] = 255; + } + } + } + + glActiveTexture(GL_TEXTURE2); /* unit 2 */ + glBindTexture(GL_TEXTURE_2D, 42); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SZ0, SZ0, 0, + GL_RGBA, GL_UNSIGNED_BYTE, image0); + glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, SZ1, SZ1, 0, + GL_RGBA, GL_UNSIGNED_BYTE, image1); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + glActiveTexture(GL_TEXTURE4); /* unit 4 */ + glBindTexture(GL_TEXTURE_3D, 43); + glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, SZ0, SZ0, SZ0, 0, + GL_RGBA, GL_UNSIGNED_BYTE, image0); + glTexImage3D(GL_TEXTURE_3D, 1, GL_RGBA, SZ1, SZ1, SZ1, 0, + GL_RGBA, GL_UNSIGNED_BYTE, image1); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 1); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); +} +#endif + + +static void +MakeSphere(void) +{ + GLUquadricObj *obj = gluNewQuadric(); + SphereList = glGenLists(1); + gluQuadricTexture(obj, GL_TRUE); + glNewList(SphereList, GL_COMPILE); + gluSphere(obj, 2.0f, 10, 5); + glEndList(); +} + +static void +VertAttrib(GLint index, float x, float y) +{ +#if 1 + glVertexAttrib2f_func(index, x, y); +#else + glTexCoord2f(x, y); +#endif +} + +static void +MakeRect(void) +{ + RectList = glGenLists(1); + glNewList(RectList, GL_COMPILE); + glNormal3f(0, 0, 1); + glBegin(GL_POLYGON); + VertAttrib(CoordAttrib, 0, 0); glVertex2f(-2, -2); + VertAttrib(CoordAttrib, 1, 0); glVertex2f( 2, -2); + VertAttrib(CoordAttrib, 1, 1); glVertex2f( 2, 2); + VertAttrib(CoordAttrib, 0, 1); glVertex2f(-2, 2); + glEnd(); /* XXX omit this and crash! */ + glEndList(); +} + + + +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, "fslight: problem compiling shader:\n%s\n", log); + exit(1); + } +} + + +/** + * Read a shader from a file. + */ +static void +ReadShader(GLuint shader, const char *filename) +{ + const int max = 100*1000; + int n; + char *buffer = (char*) malloc(max); + FILE *f = fopen(filename, "r"); + if (!f) { + fprintf(stderr, "fslight: Unable to open shader file %s\n", filename); + exit(1); + } + + n = fread(buffer, 1, max, f); + printf("fslight: read %d bytes from shader file %s\n", n, filename); + if (n > 0) { + buffer[n] = 0; + LoadAndCompileShader(shader, buffer); + } + + fclose(f); + free(buffer); +} + + +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); + } +} + + +static void Init(void) { static const char *fragShaderText = - "uniform vec3 lightPos;\n" "uniform vec4 diffuse;\n" "uniform vec4 specular;\n" "varying vec3 normal;\n" "void main() {\n" " // Compute dot product of light direction and normal vector\n" - " float dotProd = max(dot(lightPos, normalize(normal)), 0.0);\n" + " float dotProd = max(dot(gl_LightSource[0].position.xyz, \n" + " normalize(normal)), 0.0);\n" " // Compute diffuse and specular contributions\n" " gl_FragColor = diffuse * dotProd + specular * pow(dotProd, 20.0);\n" "}\n"; @@ -236,53 +473,63 @@ Init(void) " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" " normal = gl_NormalMatrix * gl_Normal;\n" "}\n"; - - const char *version; version = (const char *) glGetString(GL_VERSION); if (version[0] != '2' || version[1] != '.') { - printf("Warning: this program expects OpenGL 2.0\n"); - /*exit(1);*/ + printf("This program requires OpenGL 2.x, found %s\n", version); + exit(1); } - - glCreateShader_func = (PFNGLCREATESHADERPROC) glutGetProcAddress("glCreateShader"); - glDeleteShader_func = (PFNGLDELETESHADERPROC) glutGetProcAddress("glDeleteShader"); - glDeleteProgram_func = (PFNGLDELETEPROGRAMPROC) glutGetProcAddress("glDeleteProgram"); - glShaderSource_func = (PFNGLSHADERSOURCEPROC) glutGetProcAddress("glShaderSource"); - glGetShaderSource_func = (PFNGLGETSHADERSOURCEPROC) glutGetProcAddress("glGetShaderSource"); - glCompileShader_func = (PFNGLCOMPILESHADERPROC) glutGetProcAddress("glCompileShader"); - glCreateProgram_func = (PFNGLCREATEPROGRAMPROC) glutGetProcAddress("glCreateProgram"); - glAttachShader_func = (PFNGLATTACHSHADERPROC) glutGetProcAddress("glAttachShader"); - glLinkProgram_func = (PFNGLLINKPROGRAMPROC) glutGetProcAddress("glLinkProgram"); - glUseProgram_func = (PFNGLUSEPROGRAMPROC) glutGetProcAddress("glUseProgram"); - glGetUniformLocation_func = (PFNGLGETUNIFORMLOCATIONPROC) glutGetProcAddress("glGetUniformLocation"); - glIsProgram_func = (PFNGLISPROGRAMPROC) glutGetProcAddress("glIsProgram"); - glIsShader_func = (PFNGLISSHADERPROC) glutGetProcAddress("glIsShader"); - glUniform3fv_func = (PFNGLUNIFORM3FVPROC) glutGetProcAddress("glUniform3fv"); - glUniform4fv_func = (PFNGLUNIFORM3FVPROC) glutGetProcAddress("glUniform4fv"); + GetExtensionFuncs(); fragShader = glCreateShader_func(GL_FRAGMENT_SHADER); - glShaderSource_func(fragShader, 1, &fragShaderText, NULL); - glCompileShader_func(fragShader); + if (FragProgFile) + ReadShader(fragShader, FragProgFile); + else + LoadAndCompileShader(fragShader, fragShaderText); + vertShader = glCreateShader_func(GL_VERTEX_SHADER); - glShaderSource_func(vertShader, 1, &vertShaderText, NULL); - glCompileShader_func(vertShader); + if (VertProgFile) + ReadShader(vertShader, VertProgFile); + else + LoadAndCompileShader(vertShader, vertShaderText); program = glCreateProgram_func(); glAttachShader_func(program, fragShader); glAttachShader_func(program, vertShader); glLinkProgram_func(program); + CheckLink(program); glUseProgram_func(program); - uLightPos = glGetUniformLocation_func(program, "lightPos"); uDiffuse = glGetUniformLocation_func(program, "diffuse"); uSpecular = glGetUniformLocation_func(program, "specular"); + uTexture = glGetUniformLocation_func(program, "texture"); + printf("DiffusePos %d SpecularPos %d TexturePos %d\n", + uDiffuse, uSpecular, uTexture); glUniform4fv_func(uDiffuse, 1, diffuse); glUniform4fv_func(uSpecular, 1, specular); + /* assert(glGetError() == 0);*/ + glUniform1i_func(uTexture, 2); /* use texture unit 2 */ + /*assert(glGetError() == 0);*/ + + if (CoordAttrib) { + int i; + glBindAttribLocation_func(program, CoordAttrib, "coord"); + i = glGetAttribLocation_func(program, "coord"); + assert(i >= 0); + if (i != CoordAttrib) { + printf("Hmmm, NVIDIA bug?\n"); + CoordAttrib = i; + } + else { + printf("Mesa bind attrib: coord = %d\n", i); + } + } + + /*assert(glGetError() == 0);*/ glClearColor(0.3f, 0.3f, 0.3f, 0.0f); glEnable(GL_DEPTH_TEST); @@ -292,11 +539,20 @@ Init(void) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20.0f); + MakeSphere(); + MakeRect(); + + CurList = SphereList; + +#if TEXTURE + MakeTexture(); +#endif + printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); printf("Press p to toggle between per-pixel and per-vertex lighting\n"); /* test glGetShaderSource() */ - { + if (0) { GLsizei len = strlen(fragShaderText) + 1; GLsizei lenOut; GLchar *src =(GLchar *) malloc(len * sizeof(GLchar)); @@ -310,6 +566,37 @@ Init(void) assert(glIsProgram_func(program)); assert(glIsShader_func(fragShader)); assert(glIsShader_func(vertShader)); + + glColor3f(1, 0, 0); + + /* for testing state vars */ + { + static GLfloat fc[4] = { 1, 1, 0, 0 }; + static GLfloat amb[4] = { 1, 0, 1, 0 }; + glFogfv(GL_FOG_COLOR, fc); + glLightfv(GL_LIGHT1, GL_AMBIENT, amb); + } + +#if 0 + TestFunctions(); +#else + (void) TestFunctions; +#endif +} + + +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]; + } + } } @@ -327,8 +614,10 @@ main(int argc, char *argv[]) glutDisplayFunc(Redisplay); if (anim) glutIdleFunc(Idle); + ParseOptions(argc, argv); Init(); glutMainLoop(); return 0; } + |