Opengl (progam)未声明的错误

Opengl (progam)未声明的错误,opengl,Opengl,我已经尽我所能调试了这段代码来创建一个圆环体,但仍然无法让它运行。我得到的错误具体在我的(void init())中。它告诉我,我没有声明vPosition和vNormal,但我直接从工作代码复制/粘贴了它,我不明白它为什么不工作。代码应该生成一个着色的圆环,并且可以使用鼠标旋转 #include "mat.h" #include "vec.h" #include "Angel.h" typedef Angel::vec4 color4; typedef Angel::vec4 p

我已经尽我所能调试了这段代码来创建一个圆环体,但仍然无法让它运行。我得到的错误具体在我的(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;
}
#包括“mat.h”
#包括“vec.h”
#包括“Angel.h”
typedef angle::vec4 color4;
typedef angle::vec4 point4;
GLuint模型视图,投影;
胶环;
点击按钮;
GLfloat单击旋转;
GLfloat单击旋转;
GLfloat单击位置;
GLfloat click_x;
GLfloat单击y;
GLfloat旋转_x=0.0;
GLfloat旋转_y=0.0;
GLfloat位置_z=-5;
胶合缓冲区[1];
GLuint-loc;
闪烁矩阵、投影、颜色;
无效鼠标(int按钮、int状态、int x、int y)
{
点击_x=x;
单击y=y;
单击旋转=旋转;
单击旋转=旋转;
单击位置=位置;
}
无效运动(整数x,整数y)
{
GLfloat dx=GLfloat(x-点击x)/512;
GLfloat dy=GLfloat(y-点击y)/512;
如果(单击按钮==GLUT\U中间按钮)
{
旋转\u x=点击旋转\u x+90.0*dy;
旋转_y=单击_旋转_y+180.0*dx;
如果(旋转_x>90.0)旋转_x=90.0;
如果(旋转角x<-90.0)旋转角x=-90.0;
如果(旋转度y>180.0)旋转度y-=360.0;
如果(旋转y<-180.0)旋转y+=360.0;
}
其他的
{
位置_z=点击位置_z+5.0*dy;
}
再发现();
}
结构顶点
{
vec3v;
vec3n;
};
常数int n=64;
常数int m=64;
结构顶点三角形[3*2*n*m];
结构顶点圆环体顶点(浮点a、浮点b)
{
常数浮点R=2.0f;
常数浮点数r=0.5f;
结构顶点;
顶点v.x=(R+R*cos(2.0*M_-PI*a))*cos(2.0*M_-PI*b);
顶点v.y=(R+R*cos(2.0*M_-PI*a))*sin(2.0*M_-PI*b);
顶点v.z=r*sin(2.0*M_PI*a);
顶点n.x=cos(2.0*M_-PI*a)*cos(2.0*M_-PI*b);
顶点n.y=cos(2.0*M_-PI*a)*sin(2.0*M_-PI*b);
顶点n.z=sin(2.0*M_PI*a);
返回顶点;
}
GLuint圆环体(GLuint V位置,GLuint V正常);
int i,j,k=0;

对于(int i=0,i,在定义变量之前,您似乎正在使用这些变量。请将以下内容从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)) );

修正这篇文章的标题。它不能解释你的错误,因此你不会找到任何人来帮助你。