No subject
Mon Oct 10 16:13:32 PDT 2011
Is this a known issue? Any workarounds available? Anything else I
could do to help debug?
Thanks,
-tom
------- =_aaaaaaaaaa0
Content-Type: text/x-diff; charset="us-ascii"
Content-ID: <19164.1318285832.2 at shigeru.sci.utah.edu>
Content-Description: 0001-Add-new-test-program-to-test-gl_NormalMatrix-issue.patch
Content-Transfer-Encoding: quoted-printable
=46rom c4b3d54e4d0ca991b8d76a4391d9c820fbb22747 Mon Sep 17 00:00:00 2001
From: Tom Fogal <tfogal at alumni.unh.edu>
Date: Thu, 6 Oct 2011 14:37:40 -0600
Subject: [PATCH] Add new test program to test gl_NormalMatrix issue.
Play with the 'if' condition in fragShaderText to identify what
any given gl_NormalMatrix entry is.
---
src/demos/CMakeLists.txt | 1 +
src/demos/normal.c | 611 +++++++++++++++++++++++++++++++++++++++++=
+++++
2 files changed, 612 insertions(+), 0 deletions(-)
create mode 100644 src/demos/normal.c
diff --git a/src/demos/CMakeLists.txt b/src/demos/CMakeLists.txt
index f1bcc03..b35fae6 100644
--- a/src/demos/CMakeLists.txt
+++ b/src/demos/CMakeLists.txt
@@ -51,6 +51,7 @@ set (targets
lodbias
morph3d
multiarb
+ normal
paltex
pixeltest
pointblast
diff --git a/src/demos/normal.c b/src/demos/normal.c
new file mode 100644
index 0000000..04ae5c5
--- /dev/null
+++ b/src/demos/normal.c
@@ -0,0 +1,611 @@
+/**
+ * Test gl_NormalMatrix.
+ * Tom Fogal
+ * 5 Oct 2011
+ *
+ * Based on Test OpenGL 2.0 vertex/fragment shaders.
+ * Brian Paul
+ * 1 November 2006
+ *
+ * Based on ARB version by:
+ * Michal Krol
+ * 20 February 2006
+ *
+ * Based on the original demo by:
+ * Brian Paul
+ * 17 April 2003
+ */
+
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <GL/glew.h>
+#include "glut_wrap.h"
+
+
+#define TEXTURE 0
+
+static GLint CoordAttrib =3D 0;
+
+static char *FragProgFile =3D NULL;
+static char *VertProgFile =3D NULL;
+
+static GLfloat diffuse[4] =3D { 0.5f, 0.5f, 1.0f, 1.0f };
+static GLfloat specular[4] =3D { 0.8f, 0.8f, 0.8f, 1.0f };
+static GLfloat lightPos[4] =3D { 0.0f, 10.0f, 20.0f, 0.0f };
+static GLfloat delta =3D 1.0f;
+
+static GLuint fragShader;
+static GLuint vertShader;
+static GLuint program;
+
+static GLuint SphereList, RectList, CurList;
+static GLint win =3D 0;
+static GLboolean anim =3D GL_TRUE;
+static GLboolean wire =3D GL_FALSE;
+static GLboolean pixelLight =3D GL_TRUE;
+
+static GLint t0 =3D 0;
+static GLint frames =3D 0;
+
+static GLfloat xRot =3D 90.0f, yRot =3D 0.0f;
+
+
+static void
+normalize(GLfloat *dst, const GLfloat *src)
+{
+ GLfloat len =3D sqrt(src[0] * src[0] + src[1] * src[1] + src[2] * src[=
2]);
+ dst[0] =3D src[0] / len;
+ dst[1] =3D src[1] / len;
+ dst[2] =3D src[2] / len;
+ dst[3] =3D 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) {
+ glUseProgram(program);
+ glDisable(GL_LIGHTING);
+ }
+ else {
+ glUseProgram(0);
+ 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();
+ frames++;
+
+ if (anim) {
+ GLint t =3D glutGet(GLUT_ELAPSED_TIME);
+ if (t - t0 >=3D 5000) {
+ GLfloat seconds =3D(GLfloat)(t - t0) / 1000.0f;
+ GLfloat fps =3D frames / seconds;
+ printf("%d frames in %6.3f seconds =3D %6.3f FPS\n",
+ frames, seconds, fps);
+ fflush(stdout);
+ t0 =3D t;
+ frames =3D 0;
+ }
+ }
+}
+
+
+static void
+Idle(void)
+{
+ lightPos[0] +=3D delta;
+ if (lightPos[0] > 25.0f || lightPos[0] < -25.0f)
+ delta =3D -delta;
+ 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
+CleanUp(void)
+{
+ glDeleteShader(fragShader);
+ glDeleteShader(vertShader);
+ glDeleteProgram(program);
+ glutDestroyWindow(win);
+}
+
+
+static void
+Key(unsigned char key, int x, int y)
+{
+ (void) x;
+ (void) y;
+
+ switch(key) {
+ case ' ':
+ case 'a':
+ anim =3D !anim;
+ if (anim)
+ glutIdleFunc(Idle);
+ else
+ glutIdleFunc(NULL);
+ break;
+ case 'x':
+ lightPos[0] -=3D 1.0f;
+ break;
+ case 'X':
+ lightPos[0] +=3D 1.0f;
+ break;
+ case 'w':
+ wire =3D !wire;
+ if (wire)
+ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+ else
+ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+ break;
+ case 'o':
+ if (CurList =3D=3D SphereList)
+ CurList =3D RectList;
+ else
+ CurList =3D SphereList;
+ break;
+ case 'p':
+ pixelLight =3D !pixelLight;
+ if (pixelLight)
+ printf("Per-pixel lighting\n");
+ else
+ printf("Conventional lighting\n");
+ break;
+ case 27:
+ CleanUp();
+ exit(0);
+ break;
+ }
+ glutPostRedisplay();
+}
+
+
+static void
+SpecialKey(int key, int x, int y)
+{
+ const GLfloat step =3D 3.0f;
+
+ (void) x;
+ (void) y;
+
+ switch(key) {
+ case GLUT_KEY_UP:
+ xRot -=3D step;
+ break;
+ case GLUT_KEY_DOWN:
+ xRot +=3D step;
+ break;
+ case GLUT_KEY_LEFT:
+ yRot -=3D step;
+ break;
+ case GLUT_KEY_RIGHT:
+ yRot +=3D step;
+ break;
+ }
+ glutPostRedisplay();
+}
+
+
+static void
+TestFunctions(void)
+{
+ printf("Error 0x%x at line %d\n", glGetError(), __LINE__);
+
+ assert(glIsProgram(program));
+ assert(glIsShader(fragShader));
+ assert(glIsShader(vertShader));
+
+ /* attached shaders */
+ {
+ GLuint shaders[20];
+ GLsizei count;
+ int i;
+ glGetAttachedShaders(program, 20, &count, shaders);
+ for (i =3D 0; i < count; i++) {
+ printf("Attached: %u\n", shaders[i]);
+ assert(shaders[i] =3D=3D fragShader ||
+ shaders[i] =3D=3D vertShader);
+ }
+ }
+
+ {
+ GLchar log[1000];
+ GLsizei len;
+ glGetShaderInfoLog(vertShader, 1000, &len, log);
+ printf("Vert Shader Info Log: %s\n", log);
+ glGetShaderInfoLog(fragShader, 1000, &len, log);
+ printf("Frag Shader Info Log: %s\n", log);
+ glGetProgramInfoLog(program, 1000, &len, log);
+ printf("Program Info Log: %s\n", log);
+ }
+
+ /* active uniforms */
+ {
+ GLint n, max, i;
+ glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &n);
+ glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max);
+ printf("Num uniforms: %d Max name length: %d\n", n, max);
+ for (i =3D 0; i < n; i++) {
+ GLint size, len;
+ GLenum type;
+ char name[100];
+ glGetActiveUniform(program, i, 100, &len, &size, &type, name);
+ printf(" %d: %s nameLen=3D%d size=3D%d type=3D0x%x\n",
+ i, name, len, size, type);
+ }
+ }
+}
+
+
+#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 =3D 0; i < SZ0; i++) {
+ for (j =3D 0; j < SZ0; j++) {
+ for (k =3D 0; k < SZ0; k++) {
+ if ((i/8 + j/8 + k/8) & 1) {
+ image0[i][j][k][0] =3D =
+ image0[i][j][k][1] =3D =
+ image0[i][j][k][2] =3D 200;
+ }
+ else {
+ image0[i][j][k][0] =3D =
+ image0[i][j][k][1] =3D =
+ image0[i][j][k][2] =3D 100;
+ }
+ image0[i][j][k][3] =3D 255;
+ }
+ }
+ }
+
+ /* level 1: two-tone green checkboard */
+ for (i =3D 0; i < SZ1; i++) {
+ for (j =3D 0; j < SZ1; j++) {
+ for (k =3D 0; k < SZ1; k++) {
+ if ((i/8 + j/8 + k/8) & 1) {
+ image1[i][j][k][0] =3D 0;
+ image1[i][j][k][1] =3D 250;
+ image1[i][j][k][2] =3D 0;
+ }
+ else {
+ image1[i][j][k][0] =3D 0;
+ image1[i][j][k][1] =3D 200;
+ image1[i][j][k][2] =3D 0;
+ }
+ image1[i][j][k][3] =3D 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 =3D gluNewQuadric();
+ SphereList =3D glGenLists(1);
+ gluQuadricTexture(obj, GL_TRUE);
+ glNewList(SphereList, GL_COMPILE);
+ gluSphere(obj, 2.0f, 10, 5);
+ glEndList();
+ gluDeleteQuadric(obj);
+}
+
+static void
+VertAttrib(GLint index, float x, float y)
+{
+#if 1
+ glVertexAttrib2f(index, x, y);
+#else
+ glTexCoord2f(x, y);
+#endif
+}
+
+static void
+MakeRect(void)
+{
+ RectList =3D 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(shader, 1, (const GLchar **) &text, NULL);
+
+ glCompileShader(shader);
+
+ glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
+ if (!stat) {
+ GLchar log[1000];
+ GLsizei len;
+ glGetShaderInfoLog(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 =3D 100*1000;
+ int n;
+ char *buffer =3D (char*) malloc(max);
+ FILE *f =3D fopen(filename, "r");
+ if (!f) {
+ fprintf(stderr, "fslight: Unable to open shader file %s\n", filenam=
e);
+ exit(1);
+ }
+
+ n =3D fread(buffer, 1, max, f);
+ printf("fslight: read %d bytes from shader file %s\n", n, filename);
+ if (n > 0) {
+ buffer[n] =3D 0;
+ LoadAndCompileShader(shader, buffer);
+ }
+
+ fclose(f);
+ free(buffer);
+}
+
+
+static void
+CheckLink(GLuint prog)
+{
+ GLint stat;
+ glGetProgramiv(prog, GL_LINK_STATUS, &stat);
+ if (!stat) {
+ GLchar log[1000];
+ GLsizei len;
+ glGetProgramInfoLog(prog, 1000, &len, log);
+ fprintf(stderr, "Linker error:\n%s\n", log);
+ }
+}
+
+
+static void
+Init(void)
+{
+ static const char *fragShaderText =3D
+ "void main(void) {\n"
+ " if(-0.000001 <=3D gl_NormalMatrix[2].z && gl_NormalMatrix[2].z <=
=3D 0.0) {\n"
+ " gl_FragColor =3D vec4(1.0, 0.0, 0.0, 0.05);\n"
+ " } else if(0.0 <=3D gl_NormalMatrix[1].y && gl_NormalMatrix[1].y =
<=3D 0.5)"
+ " {\n"
+ " gl_FragColor =3D vec4(0.0, 1.0, 0.0, 0.05);\n"
+ " } else {\n"
+ " gl_FragColor =3D vec4(0.0, 0.0, 1.0, 0.05);\n"
+ " }\n"
+ "}\n";
+/* " gl_Position =3D gl_ModelViewProjectionMatrix * gl_Vertex;\n" =
*/
+ static const char *vertShaderText =3D
+ "void main() {\n"
+ " gl_Position =3D ftransform();\n"
+ "}\n";
+
+ if (!GLEW_VERSION_2_0) {
+ printf("This program requires OpenGL 2.x or higher\n");
+ exit(1);
+ }
+
+ GLfloat mview[16];
+ glGetFloatv(GL_MODELVIEW_MATRIX, mview);
+ printf("mview:\n"
+ "[%g %g %g %g]\n"
+ "[%g %g %g %g]\n"
+ "[%g %g %g %g]\n"
+ "[%g %g %g %g]\n",
+ mview[ 0], mview[ 1], mview[ 2], mview[ 3],
+ mview[ 4], mview[ 5], mview[ 6], mview[ 7],
+ mview[ 8], mview[ 9], mview[10], mview[11],
+ mview[12], mview[13], mview[14], mview[15]);
+
+ fragShader =3D glCreateShader(GL_FRAGMENT_SHADER);
+ if (FragProgFile)
+ ReadShader(fragShader, FragProgFile);
+ else
+ LoadAndCompileShader(fragShader, fragShaderText);
+
+
+ vertShader =3D glCreateShader(GL_VERTEX_SHADER);
+ if (VertProgFile)
+ ReadShader(vertShader, VertProgFile);
+ else
+ LoadAndCompileShader(vertShader, vertShaderText);
+
+ program =3D glCreateProgram();
+ glAttachShader(program, fragShader);
+ glAttachShader(program, vertShader);
+ glLinkProgram(program);
+ CheckLink(program);
+ glUseProgram(program);
+
+ if (CoordAttrib) {
+ int i;
+ glBindAttribLocation(program, CoordAttrib, "coord");
+ i =3D glGetAttribLocation(program, "coord");
+ assert(i >=3D 0);
+ if (i !=3D CoordAttrib) {
+ printf("Hmmm, NVIDIA bug?\n");
+ CoordAttrib =3D i;
+ }
+ else {
+ printf("Mesa bind attrib: coord =3D %d\n", i);
+ }
+ }
+
+ /*assert(glGetError() =3D=3D 0);*/
+
+ glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
+ glEnable(GL_DEPTH_TEST);
+ glEnable(GL_LIGHT0);
+ glEnable(GL_LIGHTING);
+ glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
+ glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
+ glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20.0f);
+
+ MakeSphere();
+ MakeRect();
+
+ CurList =3D SphereList;
+
+#if TEXTURE
+ MakeTexture();
+#endif
+
+ printf("GL_RENDERER =3D %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 =3D strlen(fragShaderText) + 1;
+ GLsizei lenOut;
+ GLchar *src =3D(GLchar *) malloc(len * sizeof(GLchar));
+ glGetShaderSource(fragShader, 0, NULL, src);
+ glGetShaderSource(fragShader, len, &lenOut, src);
+ assert(len =3D=3D lenOut + 1);
+ assert(strcmp(src, fragShaderText) =3D=3D 0);
+ free(src);
+ }
+
+ assert(glIsProgram(program));
+ assert(glIsShader(fragShader));
+ assert(glIsShader(vertShader));
+
+ glColor3f(1, 0, 0);
+
+ /* for testing state vars */
+ {
+ static GLfloat fc[4] =3D { 1, 1, 0, 0 };
+ static GLfloat amb[4] =3D { 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 =3D 1; i < argc; i++) {
+ if (strcmp(argv[i], "-fs") =3D=3D 0) {
+ FragProgFile =3D argv[++i];
+ }
+ else if (strcmp(argv[i], "-vs") =3D=3D 0) {
+ VertProgFile =3D argv[++i];
+ }
+ else {
+ fprintf(stderr, "unknown option %s\n", argv[i]);
+ break;
+ }
+ }
+}
+
+
+int
+main(int argc, char *argv[])
+{
+ glutInitWindowSize(200, 200);
+ glutInit(&argc, argv);
+ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
+ win =3D glutCreateWindow(argv[0]);
+ glewInit();
+ glutReshapeFunc(Reshape);
+ glutKeyboardFunc(Key);
+ glutSpecialFunc(SpecialKey);
+ glutDisplayFunc(Redisplay);
+ if (anim)
+ glutIdleFunc(Idle);
+ ParseOptions(argc, argv);
+ Init();
+ glutMainLoop();
+ return 0;
+}
+
+
-- =
1.7.3.4
------- =_aaaaaaaaaa0--
More information about the mesa-dev
mailing list