2

My friend and I are working on a project using C++ and OpenGL. We've created a C++ class for a "ModelObject", and each ModelObject has a GLuint vao as a member variable. Then while initializing a ModelObject, we call

glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );

and at the end of the initializing function we call

glBindVertexArray(0);

to get out of the context. Now we're trying to render 2 objects, a train car and a cube. On his machine (Linux Mint) they both render fine, with their own textures, and querying the objects for their vaos returns 1 and 2 respectively.

On my machine however (a MacBook Pro), both objects render as a cube (though one has the texture of a train and the other the texture of the cube). Querying their vaos returns 0 and 0.

As an experiment we told glGenVertexArrays to create 5 vaos for each ModelObject. This resulted in the list 0, 1, 918273, 8, 7 (or something similar to that), and it was the same list for both ModelObjects.

So as far as I can tell the problem is that glGenVertexArrays is both a) using 0 as a valid address, and b) generating identical addresses on each call, even though we're never calling glDeleteVertexArray. Why would it be doing this on my machine and not his, and how do I stop it?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Nick
  • 6,900
  • 5
  • 45
  • 66

3 Answers3

4

Does your GPU support OpenGL 3.0? What does glview say about the entry point glGenVertexArrays? It is possible that your GPU/Driver doesn't support VAOs.

Ani
  • 10,826
  • 3
  • 27
  • 46
  • glxinfo says I've got OpenGL version 2.1. My partner's computer has an earlier version than that. I can't find glGenVertexArrays in glview anywhere – Nick Dec 13 '12 at 18:23
  • On OpenGL 2.1 - you could use VBOs instead. And - what GPU does your partner have? What does his glxInfo report? It is sometimes possible that the entry points exist even though the driver reports a lower version. – Ani Dec 13 '12 at 18:29
  • We're using 4 VBOs to actually store the data (vertices, color, normals, UVs), I was under the impression that the VBOs are packaged into a VAO to send to the GPU. I don't know the details of my partner's hardware, he's not around today unfortunately – Nick Dec 13 '12 at 18:32
  • VAOs encapsulate data about the VBOs without having to repeatedly issue glVertexAttribPointer calls each time - which is faster. See this (http://www.opentk.com/node/1619) thread on the OpenTK forums which has an extensive discussion about the subject with links to the OpenGL 3.1 spec for this feature as well. – Ani Dec 13 '12 at 18:36
  • My code seems to be following the process in that thread (Create VAO, bind it, create VBO, bind it, use glBufferData, call vertexattribpointer) – Nick Dec 13 '12 at 18:42
  • 4
    Since your GPU doesn't support VAOs - you should use the VBOs directly and set up the VertexAttribPointer each time is what I was getting at. – Ani Dec 13 '12 at 18:45
3

I had the same issue on an iMac. It turned out that apparently on MacOS, you need to use glGenVertexArrayAPPLE and glBindVertexArrayAPPLE. Replacing the call give consistent unique VAO.

Cédric
  • 156
  • 1
  • 6
  • That's probably true if you're using a 2.1 context, since VAOs were introduced in OpenGL 3.0. If you're using a 3.x context, `glGenVertexArrays()` will work fine. – Reto Koradi Sep 16 '14 at 13:04
2

VAOs were introduced in OpenGL 3.0, so they will only work in contexts that support 3.0 or later.

Mac OS only supports OpenGL 3.x and 4.x in Core Profile contexts. By default, you will get a context that supports OpenGL 2.1 with all the legacy features that are deprecated, and have been removed in the Core Profile. Mac OS does not support the Compatibility Profile, where features from 3.0 and later can be used in combination with legacy features.

How you create a Core Profile context depends on the window system interface you use. For two of the common ones:

  • With GLUT (which is marked as deprecated itself, but still works at the moment): Add GLUT_3_2_CORE_PROFILE to the flags passed to glutInitDisplayMode() (see Glut deprecation in Mac OSX 10.9, IDE: QT Creator for details).

  • With Cocoa, add this attribute/value pair to the pixel format attributes:

    NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
    

Once you have a Core Profile context, glGenVertexArrays() and glBindVertexArray() will work fine. This obviously requires a machine that can support at least OpenGL 3.x. The table on this page lists the version support for each machine: http://support.apple.com/kb/HT5942.

Community
  • 1
  • 1
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133