0

I'm working with old opengl 3.0, I have verified my system runs above 3.0, and for some reason i'm getting an error whenever I use any of these functions/terms:

glBindVertexArrays,
glGenBuffers,
GL_ARRAY_BUFFER,
glBindBuffers,
GL_STATIC_DRAW

Why is this happening?!?! I am compiling in C and C++ in a .cpp file I can give you guys the entire project if you like it's just a demo for VAOs... but I thought that this might be a common problem that people experience... OpenGL has been very taxing on me, it seems it has LOTS of linker errors and problems that I've just never had with anything else.

If I can't fix this i'm going to have to move my project to a higher opengl version and forget supporting legacy systems.

EDIT: Here is the code

    #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();
}
  • 2
    What do the linker errors say? Do you use a library to handle the function-loading for you or do you load the functions yourself? – tkausl Nov 14 '16 at 05:32
  • to HANDLE it? Well, I mean, the functions i'm using are inside of libraries and headers. I have no idea how a library could HANDLE function loading. I honestly haven't the faintest idea what you are describing. – Geklmintendon't of Awesome Nov 14 '16 at 06:18
  • For people wondering: Compiler = gcc & g++ OPENGL version = 3.1, and everything else is in the code – Geklmintendon't of Awesome Nov 14 '16 at 06:19
  • Mind sharing what system you're working on? – Reto Koradi Nov 14 '16 at 06:58
  • 3
    The functions in modern OpenGL are not defined in headers and need to be loaded at runtime. There are libraries like GLEW that do that for you if you want. The functions that you can use are the basic OpenGL 1.2 ones defined in the header. – Banex Nov 14 '16 at 08:28
  • There is not enough information in your question but I'll try anyway. Have you tried using other functions of opengl? Do they work? Also, if you use glfw for window handling then you have to match its version to the compiler. You can also put breakpoints to see if the "init" functions returns correctly. – Efi Nov 14 '16 at 05:42
  • see [simple complete GL+VAO/VBO+GLSL+shaders example in C++](http://stackoverflow.com/a/31913542/2521214) on how to use GLEW. btw why do you include iostream and stdlib,stdio ...? too lazy to go through your code but their use seems pointless for OpenGL unless you are using them for File Access – Spektre Nov 14 '16 at 12:41
  • The functions are defined in `glext.h` ... but their pointers are set to `NULL` so any usage of them will cause exceptions ... The GLEW loads any available functions from your gfx driver into the GL function pointers so you can use them ... but before use you should make sure their supported either by GL version or by inspecting the pointers or by inspecting extension strings ... – Spektre Nov 14 '16 at 12:51

0 Answers0