OpenGL顶点数组甚至没有被链接

时间:2016-11-14 05:27:55

标签: c++ opengl

我正在使用旧的opengl 3.0,我已经验证我的系统运行在3.0以上,并且出于某种原因,每当我使用这些函数/术语时,我都会收到错误:

glBindVertexArrays,
glGenBuffers,
GL_ARRAY_BUFFER,
glBindBuffers,
GL_STATIC_DRAW

为什么会这样???! 我在.cpp文件中编译C和C ++ 如果你喜欢它,我可以给你们整个项目,只是VAO的一个演示...但我认为这可能是人们经历的一个常见问题...... OpenGL一直对我非常沉重,似乎它有很多链接器错误和我从未与其他任何东西相关的问题。

如果我无法解决这个问题,我将不得不将我的项目转移到更高的opengl版本而忘记支持遗留系统。

编辑:这是代码

    #ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
// EXTRAS
#include <GL/gl.h> // Default OpenGL library
#include <GL/glu.h> // Utils
#include <stdlib.h>
#include <stdio.h>
#include "glext.h"
// EOF EXTRAS
#include <iostream>
#include <stdlib.h>
#define ARRAY_SIZE( array ) sizeof( array ) / sizeof( array[0] )
#define BUFFER_OFFSET(offset) ((GLvoid*) NULL + offset)
#define NumberOf(array)       (sizeof(array)/sizeof(array[0]))

typedef struct {
    GLfloat x, y, z;
} vec3;

typedef struct {
    vec3 xlate;
    GLfloat angle;
    vec3 axis;
} XForm;



enum { Cube, Cone, NumVAOs };
GLuint   VAO[NumVAOs];
GLenum   PrimType[NumVAOs];
GLsizei  NumElements[NumVAOs];
XForm Xform[NumVAOs] = {
    {{ -2.0, 0.0, 0.0 }, 0.0, { 0.0, 1.0, 0.0 }},
    {{ 0.0, 0.0, 2.0 }, 0.0, { 1.0, 0.0, 0.0 }}
};
GLfloat Angle = 0.0;

void init()
{
    glEnableClientState(GL_NORMAL_ARRAY);
    enum { Vertices, Colors, Elements, NumVBOs };
    GLuint buffers[NumVBOs];

    glGenVertexArrays(NumVAOs, VAO);

    {
        GLfloat cubeVerts[][3] = {
            { -1.0, -1.0, -1.0 },
            { -1.0, -1.0,  1.0 },
            { -1.0,  1.0, -1.0 },
            { -1.0,  1.0,  1.0 },
            {  1.0, -1.0, -1.0 },
            {  1.0, -1.0,  1.0 },
            {  1.0,  1.0, -1.0 },
            {  1.0,  1.0,  1.0 }
        };
        GLfloat cubeColors[][3] = {
            {  0.0,  0.0,  0.0 },
            {  0.0,  0.0,  1.0 },
            {  0.0,  1.0,  0.0 },
            {  0.0,  1.0,  1.0 },
            {  1.0,  0.0,  0.0 },
            {  1.0,  0.0,  1.0 },
            {  1.0,  1.0,  0.0 },
            {  1.0,  1.0,  1.0 }
        };

        GLubyte cubeIndices[] = {
            0, 1, 3, 2,
            4, 6, 7, 5,
            2, 3, 7, 6,
            0, 4, 5, 1,
            0, 2, 6, 4,
            1, 5, 7, 3
        };

        glBindVertexArray(VAO[Cube]);
        glGenBuffers(NumVBOs, buffers);
        glBindBuffer(GL_ARRAY_BUFFER, buffers[Vertices]);
        glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVerts),
            cubeVerts, GL_STATIC_DRAW);
        glVertexpointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
        glEnableClientState(GL_VERTEX_ARRAY);

        glBindBuffer(GL_ARRAY_BUFFER, buffers[Colors]);
        glBufferData(GL_ARRAY_BUFFER, sizeof(cubeColors),
            cubeColors, GL_STATIC_DRAW);
        glVertexpointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
        glEnableClientState(GL_COLOR_ARRAY);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Elements]);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cubeIndices), cubeIndices, GL_STATIC_DRAW);

        PrimType[Cube] = GL_QUADS;
        NumElements[Cube] = NumberOf(cubeIndices);
    }
    {
        int i, idx;
        float dTheta;
        #define NumConePoints 36
            //Add one for cone
            GLfloat coneVerts[NumConePoints+1][3] = {
                {0.0, 0.0, 1.0}
            };
            GLfloat coneColors[NumConePoints+1][3] = {
                {1.0,1.0,1.0}
            };
        GLubyte coneIndices[NumConePoints+1];

        dTheta = 2*M_PI / (NumConePoints - 1);
        idx = 1;
        for (i = 0; i < NumConePoints; ++i, ++idx){
            float theta = i*dTheta;
            coneVerts[idx][0] = cos(theta);
            coneVerts[idx][1] = sin(theta);
            coneVerts[idx][2] = 0.0;

            coneColors[idx][0] = cos(theta);
            coneColors[idx][1] = sin(theta);
            coneColors[idx][2] = 0.0;

            coneIndices[idx] = idx;
        }

        glBindVertexArray(VAO[Cone]);
        glGenBuffers(NumVBOs, buffers);
        glBindBuffer(GL_ARRAY_BUFFER, buffers[Vertices]);
        glBufferData(GL_ARRAY_BUFFER, sizeof(coneVerts), coneVerts, GL_STATIC_DRAW);
        glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
        glEnableClientState(GL_VERTEX_ARRAY);

        glBindBuffer(GL_ARRAY_BUFFER, buffers[Colors]);
        glBufferData(GL_ARRAY_BUFFER, sizeof(coneColors), coneColors, GL_STATIC_DRAW);
        glColorPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
        glEnableClientState(GL_COLOR_ARRAY);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Elements]);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(coneIndices), coneIndices, GL_STATIC_DRAW);

        PrimType[Cone] = GL_TRIANGLE_FAN;
        NumElements[Cone] = NumberOf(coneIndices);
    }
    glEnable(GL_DEPTH_TEST);
}
void display()
{
    int i;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glRotatef(Angle, 0.0, 1.0, 0.0);

    for (i = 0; i < NumVAOs; ++i)
    {
        glPushMatrix();
        glTranslatef(Xform[i].xlate.x, Xform[i].xlate.y, Xform[i].xlate.z);
        glBindVertexArray(VAO[i]);
        glDrawElements(PrimType[i], NumElements[i],
            GL_UNSIGNED_BYTE, BUFFER_OFFSET(0))
        glPopMatrix();
    }

    glPopMatrix();
    glutSwapBuffers();
}

0 个答案:

没有答案