(程序)未声明的错误

时间:2013-10-18 17:41:27

标签: opengl

我调试了这段代码,以尽我所能创建一个圆环,但仍然无法让它运行。我得到的错误特别是在我的(void init())中。它告诉我,我没有声明vPosition和vNormal,但我直接从工作代码中复制/粘贴它,我不明白为什么它不起作用。代码应该生成一个带阴影的圆环,并且可以使用鼠标旋转。

    #include "mat.h"
#include "vec.h"
#include "Angel.h"

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

GLuint  ModelView, Projection;
GLuint  torus;

int     click_button;
GLfloat click_rotation_x;
GLfloat click_rotation_y;
GLfloat click_position_z;
GLfloat click_x;
GLfloat click_y;
GLfloat rotation_x = 0.0;
GLfloat rotation_y = 0.0;
GLfloat position_z =  -5;

GLuint buffers[1];
GLuint loc;
GLint matrix_loc, projection_loc, color_loc;


void mouse(int button, int state, int x, int y)
{
    click_x = x;
    click_y = y;
    click_rotation_x = rotation_x;
    click_rotation_y = rotation_y;
    click_position_z = position_z;

}

void motion(int x, int y)
{
    GLfloat dx = GLfloat(x - click_x) / 512;
    GLfloat dy = GLfloat(y - click_y) / 512;

    if (click_button == GLUT_MIDDLE_BUTTON)
    {
        rotation_x = click_rotation_x + 90.0 * dy;
        rotation_y = click_rotation_y + 180.0 * dx;
        if (rotation_x > 90.0) rotation_x = 90.0;
        if (rotation_x < -90.0) rotation_x = -90.0;
        if (rotation_y > 180.0) rotation_y -= 360.0;
        if (rotation_y < -180.0) rotation_y += 360.0;
    }
    else
    {
        position_z = click_position_z + 5.0 * dy;
    }
    glutPostRedisplay();
}

struct vertex

{
    vec3 v;
    vec3 n;
};

const int n = 64;
const int m = 64;

struct vertex triangles[3*2*n*m];

struct vertex torus_vertex(float a, float b)
{
    const float R = 2.0f;
    const float r = 0.5f;

    struct vertex vertex;

    vertex.v.x = (R + r*cos(2.0*M_PI*a))* cos(2.0*M_PI*b);
    vertex.v.y = (R + r*cos(2.0*M_PI*a))* sin(2.0*M_PI*b);
    vertex.v.z =      r*sin(2.0*M_PI*a);

    vertex.n.x =        cos(2.0*M_PI*a)* cos(2.0*M_PI*b);
    vertex.n.y =        cos(2.0*M_PI*a)* sin(2.0*M_PI*b);
    vertex.n.z =        sin(2.0*M_PI*a);

    return vertex;
}

GLuint torus_create(GLuint vPosition, GLuint vNormal);

int i,j,k =0;


    for (int i=0, i<n, i++) 
        for (int j=0, j<m, j++)
        {

            triangles[k++] = torus_vertex(float (i    ) / n, float (j    ) / n);
            triangles[k++] = torus_vertex(float (i + 1) / n, float (j    ) / n);
            triangles[k++] = torus_vertex(float (i + 1) / n, float (j + 1) / n);
            triangles[k++] = torus_vertex(float (i    ) / n, float (j    ) / n);
            triangles[k++] = torus_vertex(float (i + 1) / n, float (j + 1) / n);
            triangles[k++] = torus_vertex(float (i    ) / n, float (j + 1) / n);
        }



void
init()
{     

    GLuint (vbo);
    glGenBuffers(1, &vbo);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glBufferData (GL_ARRAY_BUFFER, sizeof (struct vertex) * k, triangles, GL_STATIC_DRAW);


    GLuint program = InitShader("vshader53.glsl", "fshader53.glsl");
    glUseProgram( program );

    glEnableVertexAttribArray(vPosition);
    glEnableVertexAttribArray(vNormal);

    glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, sizeof (struct vertex), (GLvoid *)  0);
    glVertexAttribPointer(vNormal, 3, GL_FLOAT, GL_FALSE, sizeof (struct vertex), (GLvoid *) 12);

    GLuint vPosition = glGetAttribLocation( program, "vPosition" );
        glEnableVertexAttribArray( vPosition );
        glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
               BUFFER_OFFSET(0) );

     GLuint vNormal = glGetAttribLocation( program, "vNormal" ); 
        glEnableVertexAttribArray( vNormal );
        glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0,
               BUFFER_OFFSET(sizeof(vertex)) );

    point4 light_position( 0.0, 0.0, -1.0, 0.0);
    color4 light_ambient(   0.2, 0.2, 0.2, 1.0);
    color4 light_diffuse(   1.0, 1.0, 1.0, 1.0);
    color4 light_specular(  1.0, 1.0, 1.0, 1.0);

    color4 material_ambient(  1.0, 0.0, 1.0, 1.0);
    color4 material_diffuse(  1.0, 0.8, 0.0, 1.0);
    color4 material_specular( 1.0, 0.8, 0.0, 1.0);
    float material_shininess              = 100.0;

    color4 ambient_product = light_ambient   *  material_ambient;
    color4 diffuse_product = light_diffuse   *  material_diffuse;
    color4 specular_product = light_specular * material_specular;

    glUniform4fv( glGetUniformLocation(program, "AmbientProduct" ), 1,  ambient_product);
    glUniform4fv( glGetUniformLocation(program, "DiffuseProduct" ), 1,  diffuse_product);
    glUniform4fv( glGetUniformLocation(program, "SpecularProduct"), 1, specular_product);
    glUniform4fv( glGetUniformLocation(program, "LightPosition"  ), 1,   light_position);

    glUniform1f( glGetUniformLocation(program, "Shininess"), material_shininess);

    ModelView =  glGetUniformLocation(program,  "ModelView");
    Projection = glGetUniformLocation(program, "Projection");
}

int

 main()
 {
 printf("%d %d %d\n", n, m, k);
 glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH);
 glutInitWindowSize(512, 512);
 glutCreateWindow("Torus");
 glutMouseFunc(mouse);
 glutMotionFunc(motion);

 glEnable(GL_DEPTH_TEST);
 glutMainLoop();
 return 0;
}

1 个答案:

答案 0 :(得分:1)

在定义变量之前,您似乎正在使用变量。将以下内容从init()函数的中间移到init()函数的顶部:

GLuint vPosition = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( vPosition );
    glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
           BUFFER_OFFSET(0) );

 GLuint vNormal = glGetAttribLocation( program, "vNormal" ); 
    glEnableVertexAttribArray( vNormal );
    glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0,
           BUFFER_OFFSET(sizeof(vertex)) );