0

以下是自述文件所说的这个例子的作用: “在一个多边形上使用旋转灯凹凸贴图”。让我补充一下:多边形是一个正方形,它上面的凹凸贴图应该在原始多边形正方形内产生一个凸起的正方形。main() 中的第二个“for”循环中的 i 和 j(像素迭代)的值表示这种内部正方形模式。

问题:当我运行它时,显示窗口出现但它是空白的。也许您可以尝试自己运行它:如果它有效,让我们在我们的设置中找到不同之处;如果是,请不要帮助找出代码有什么问题。代码在这里:http ://www.cs.unm.edu/~angel/BOOK/INTERACTIVE_COMPUTER_GRAPHICS/SIXTH_EDITION/ 。单击“代码”,然后单击“CHAPTER07/”。这是示例 7.3。此外,以下是供参考的代码。注意:我在 fshader73.glsl 中添加了“#version 150”以消除运行时的版本错误。

示例 3.cpp

/* sets up flat mesh */
/* sets up elapsed time parameter for use by shaders */

#include "Angel.h"

#define N 256

GLfloat normals[N][N][3];
GLuint         program;
GLuint        texMapLocation;
GLfloat tangent[3] = {1.0, 0.0, 0.0};

typedef Angel::vec4  point4;
typedef Angel::vec4  color4;

point4 points[6];
vec2 tex_coord[6];
mat4 ctm, projection;

vec4 normal = point4(0.0, 1.0, 0.0, 0.0);
color4 light_diffuse = color4(1.0, 1.0, 1.0, 1.0);
color4 material_diffuse = color4(0.7, 0.7, 0.7, 1.0);
point4  light_position = point4(0.0, 10.0, 0.0, 1.0);
vec4 eye =  vec4(2.0, 2.0, 2.0, 1.0);
vec4 at = vec4(0.5, 0.0, 0.5, 1.0);
vec4 up = vec4(0.0, 1.0, 0.0, 1.0);



GLuint loc, loc2;
GLuint buffers[2];

GLuint normal_loc;
GLuint diffuse_product_loc;
GLuint light_position_loc;
GLuint ctm_loc, projection_loc;
GLuint tangent_loc;


/* standard OpenGL initialization */

vec4 product(vec4 a, vec4 b)
{
  return vec4(a[0]*b[0], a[1]*b[1], a[2]*b[2], a[3]*b[3]);
}
static void init()
{
    const float meshColor[]     = {0.7f, 0.7f, 0.7f, 1.0f}; 

    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, meshColor);

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    glBindTexture(GL_TEXTURE_2D, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, N, N, 0, GL_RGB, GL_FLOAT, normals);
    glEnable(GL_TEXTURE_2D);


    glEnable(GL_DEPTH_TEST);

   loc = glGetAttribLocation(program, "vPosition");
   glEnableVertexAttribArray(loc);
   glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, 0, points);

   loc2 = glGetAttribLocation(program, "texcoord");
   glEnableVertexAttribArray(loc2);
   glVertexAttribPointer(loc2, 2, GL_FLOAT, GL_FALSE, 0, tex_coord);

   glGenBuffers(2, buffers);
   glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
   glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

   tangent_loc = glGetUniformLocation(program, "objTangent");
   glUniform3fv(tangent_loc, 3, tangent);

   normal_loc = glGetUniformLocation(program, "Normal");
   glUniform4fv(normal_loc, 4, normal);

   vec4 diffuse_product = product(light_diffuse, material_diffuse);
   diffuse_product_loc = glGetUniformLocation(program, "DiffuseProduct");
   glUniform4fv(diffuse_product_loc, 4, diffuse_product);

   light_position_loc = glGetUniformLocation(program, "LightPosition");
   glUniform4fv(light_position_loc, 4, light_position);

   ctm_loc = glGetUniformLocation(program, "ModelView");
   ctm = LookAt(eye, at , up);
   glUniformMatrix4fv(ctm_loc, 16, GL_TRUE, ctm);

   mat4 nm;
   GLfloat det;
   det = ctm[0][0]*ctm[1][1]*ctm[2][2]+ctm[0][1]*ctm[1][2]*ctm[2][1]
     -ctm[2][0]*ctm[1][1]*ctm[0][2]-ctm[1][0]*ctm[0][1]*ctm[2][2]-ctm[0][0]*ctm[1][2]*ctm[2][1];
   nm[0][0] = (ctm[1][1]*ctm[2][2]-ctm[1][2]*ctm[2][1])/det;
   nm[0][1] = -(ctm[0][1]*ctm[2][2]-ctm[0][2]*ctm[2][1])/det;
   nm[0][2] = (ctm[0][1]*ctm[2][0]-ctm[2][1]*ctm[2][2])/det;
   nm[1][0] = -(ctm[0][1]*ctm[2][2]-ctm[0][2]*ctm[2][1])/det;
   nm[1][1] = (ctm[0][0]*ctm[2][2]-ctm[0][2]*ctm[2][0])/det;
   nm[1][2] = -(ctm[0][0]*ctm[2][1]-ctm[2][0]*ctm[0][1])/det;
   nm[2][0] = (ctm[0][1]*ctm[1][2]-ctm[1][1]*ctm[0][2])/det;
   nm[2][1] = -(ctm[0][0]*ctm[1][2]-ctm[0][2]*ctm[1][0])/det;
   nm[2][2] = (ctm[0][0]*ctm[1][1]-ctm[1][0]*ctm[0][1])/det;

    GLuint nm_loc;
    nm_loc = glGetUniformLocation(program, "NormalMatrix");
    glUniformMatrix4fv(nm_loc, 16, GL_TRUE, nm);

    projection_loc = glGetUniformLocation(program, "Projection");
    projection = Ortho(-0.75,0.75,-0.75,0.75,-5.5,5.5);
    glUniformMatrix4fv(projection_loc, 16, GL_TRUE, projection);

    texMapLocation = glGetUniformLocation(program, "texMap");

}

    /* set up uniform parameter */

void mesh()
{
      point4 vertices[4] = {point4(0.0, 0.0, 0.0, 1.0), point4(1.0, 0.0, 0.0, 1.0),
         point4(1.0, 0.0, 1.0, 1.0), point4(0.0, 0.0, 1.0, 1.0)};

       points[0] = vertices[0];
       tex_coord[0] = vec2(0.0, 0.0);
       points[1] = vertices[1];
       tex_coord[1] = vec2(1.0, 0.0);
       points[2] = vertices[2];
       tex_coord[2] = vec2(1.0, 1.0);
       points[3] = vertices[2];
       tex_coord[3] = vec2(1.0, 1.0);
       points[4] = vertices[3];
       tex_coord[4] = vec2(0.0, 1.0);
       points[5] = vertices[0];
       tex_coord[5] = vec2(0.0, 0.0);
}

static void draw()
{

    glUniform1i(texMapLocation, 0);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    mesh(); 

    glDrawArrays(GL_TRIANGLES, 0, 6);

    glutSwapBuffers();
}

static void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glutPostRedisplay();
}

static void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
    case 27:
    case 'Q':
    case 'q':
        exit(EXIT_SUCCESS);
    }
}

void idle()
{
   int t;
   t = glutGet(GLUT_ELAPSED_TIME);
   light_position[0] = 5.5*sin(0.001*t);
   light_position[2] = 5.5*cos(0.001*t);
   glUniform4fv(light_position_loc, 4, light_position);
   glutPostRedisplay();
}

int main(int argc, char** argv)
{
    int i,j, k;
    float d;

    float data[N+1][N+1];
    for(i=0;i<N+1;i++) for(j=0;j<N+1;j++) data[i][j]=0.0;
    for(i=N/4; i< 3*N/4; i++) for(j=N/4;j<3*N/4;j++) data[i][j] = 1.0;

    for(i=0;i<N;i++) for(j=0;j<N;j++)
    {
       normals[i][j][0] = data[i][j]-data[i+1][j];
       normals[i][j][2] = data[i][j]-data[i][j+1];
       normals[i][j][1]= 1.0;
    }


    for(i=0;i<N;i++) for(j=0;j<N;j++)
    {
       d = 0.0;
       for(k=0;k<3;k++) d+=normals[i][j][k]*normals[i][j][k];
       d=sqrt(d);
       for(k=0;k<3;k++) normals[i][j][k]= 0.5*normals[i][j][k]/d+0.5;
    }

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(1024, 1024);

    glutInitContextVersion( 3, 2 );
    glutInitContextProfile( GLUT_CORE_PROFILE );

    glutCreateWindow("Simple GLSL example");
    glutDisplayFunc(draw);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(idle);

    glewInit();

    program = InitShader("vshader73.glsl", "fshader73.glsl");
    init();


    glutMainLoop();
    return 0;
}

vshader73.glsl

/* bump map vertex shader */

#version 150

out vec3 L; /* light vector in texture-space coordinates */
out vec3 V; /* view vector in texture-space coordinates */

in vec2 texcoord;
in vec4 vPosition;

uniform vec4 Normal;
uniform vec4 LightPosition;
uniform mat4 ModelView;
uniform mat4 Projection;
uniform mat4 NormalMatrix;
uniform vec3 objTangent; /* tangent vector in object coordinates */

out vec2 st;

void main()
{
    mat3 NM3;

    NM3[0][0] = NormalMatrix[0][0];
    NM3[0][1] = NormalMatrix[0][1];
    NM3[0][2] = NormalMatrix[0][2];
    NM3[1][0] = NormalMatrix[1][0];
    NM3[1][1] = NormalMatrix[1][1];
    NM3[1][2] = NormalMatrix[1][2];
    NM3[2][0] = NormalMatrix[2][0];
    NM3[2][1] = NormalMatrix[2][1];
    NM3[2][2] = NormalMatrix[2][2];

    gl_Position = Projection*ModelView*vPosition;


    st = texcoord;

    vec3 eyePosition = vec3(ModelView*vPosition);
    vec3 eyeLightPos = LightPosition.xyz;

   /* normal, tangent and binormal in eye coordinates */

    vec3 N = normalize(NM3*Normal.xyz);
    vec3 T  = normalize(NM3*objTangent);
    vec3 B = cross(N, T);

    /* light vector in texture space */

    L.x = dot(T, eyeLightPos-eyePosition);
    L.y = dot(B, eyeLightPos-eyePosition);
    L.z = dot(N, eyeLightPos-eyePosition);

    L = normalize(L);

    /* view vector in texture space */

    V.x = dot(T, -eyePosition);
    V.y = dot(B, -eyePosition);
    V.z = dot(N, -eyePosition);

    V = normalize(V);
}

fshader73.glsl

#version 150

in vec3 L;
in vec3 V;
uniform sampler2D texMap;
in vec2 st;
uniform vec4 DiffuseProduct;

out vec4 fColor;

void main()
{

   vec4 N = texture2D(texMap, st);
   vec3 NN =  normalize(2.0*N.xyz-1.0);
   vec3 LL = normalize(L);
   float Kd = max(dot(NN.xyz, LL), 0.0);
   fColor = Kd*DiffuseProduct;
}

编辑

在布雷特的评论之后,我glBindTexture(GL_TEXTURE_2D, 0)用下面的内容替换了——仍然得到一个空白的白色窗口:

GLuint texture;
glGenTextures( 1, &texture );
glBindTexture(GL_TEXTURE_2D, texture);
...
glActiveTexture(GL_TEXTURE0);
4

2 回答 2

1

我的评论刚刚超过字符的限制,所以希望这值得回答。

我能看到的唯一可能有问题的是你的glUniformMatrix4fv电话。根据OpenGL 4 语言参考,第二个参数将其描述为要更改的矩阵的数量。3.2 核心参考页面没有链接到他们的网站上,所以我无法在特定的上下文中查找它。因此,glUniformMatrix4fv(projection_loc, 16, GL_TRUE, projection);它打算在一个名为“Projection”的数组中找到 16 个矩阵,但您只定义了uniform mat4 Projection;. 这可能是也可能不是您的问题的一部分,而且我什至可能都不正确,特别是如果这是其他人已经编译/使用的已知教程。

此外,glEnable(GL_TEXTURE_2D)对于 3.2 版核心的 OpenGL 上下文不是必需的(甚至是可用的,我认为? ),它是在此处设置的:

glutInitContextVersion( 3, 2 );
glutInitContextProfile( GLUT_CORE_PROFILE );

如果编译器没有给您任何错误,请尝试glGetError()在您认为可能有问题的行之后添加。返回值映射到 OpenGL 头文件中的枚举值(glcorearb.h将列出它们),我认为文档也列出了它们。

除此之外,您可能只想检查“法线” 3 维数组是否按照您期望的方式在内存中布局,因为我从未尝试过手动构建一个数组以自己用作纹理(尽管无论如何图像基本上都是字节数据字符串,所以在这种情况下我看不出这是个问题)。

希望这对您有所帮助,或者至少为您指明了正确的方向。祝你好运。

编辑#1:我意识到我关于着色器检查的评论不是很完整,因为我没有提到一些 API 调用。这是一个例子:

      char * dataBuffer;
      struct _stat fileStat;
      long fLen;
      FILE * vSh;
      FILE * fSh;
      int desc;

      GLint status;
      GLint logLength;

      GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
      //errort = glGetError();
      GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
      GLuint program = glCreateProgram();
      /*__________________________________________________*/

      vSh = fopen("vShader.vSh", "r");
      desc = _fileno(vSh);
      _fstat(desc, &fileStat);

      fLen = fileStat.st_size;

      dataBuffer = (char *) calloc((fLen + 1), sizeof(char));
      if (dataBuffer == NULL)
      {
            MessageBox(NULL, "Malloc failure: VS", "Create Program", MB_OK | MB_ICONINFORMATION);
            return 1;
      }


      if(feof(vSh) == 0)
      {
            MessageBox(NULL, "Did not reach EoF for VS", "Create Program", MB_OK | MB_ICONINFORMATION);
            return 2;
      }
      fclose(vSh);

      glShaderSource(vShader, 1, &dataBuffer, NULL);
      glCompileShader(vShader);

      //Error checking
      glGetShaderiv(vShader, GL_COMPILE_STATUS, &status);
      if (status == GL_FALSE)
      {
            glGetShaderiv(vShader, GL_INFO_LOG_LENGTH, &logLength);
            char resultV[logLength+1];
            glGetShaderInfoLog(vShader, logLength, NULL, resultV);

            MessageBox(NULL, resultV, "Vertex Compile Error", MB_OK);
      }
      free(dataBuffer);

我得到我的着色器的方式并不是真正重要的部分,所以希望这不会让你感到困惑。标记的部分//Error checking是我在谈论检查着色器是否有错误时的意思。

于 2012-11-19T23:02:56.447 回答
0

亚历克斯,我的朋友,你很亲密!基本上,glUniform...那里的每一个电话,不仅仅是glUniformMatrix...那些,都需要1作为他们的第二个论点。

于 2012-11-27T20:42:53.560 回答