Back to openGL

Its been a long time since I tried openGL, been wandering around, and now I need to know…

Can you use a Vertex Array Object with a index Buffer? Or can you only use Index Buffers with VBOs?

My goal is to make a 2D game, no 3D plans as of now, I know this does not matter since everything is 3D. But I figure I’d mention it just encase, maybe VAOs are faster than a VBO and Index Buffer when it comes to making a Sprite Batcher

An index buffer IS a VBO. A Vertex Array Object doesn’t store vertex data, but holds state.

Maybe you should re-learn OpenGL from scratch :slight_smile:

I’m pretty much starting there, I have very scattered knowledge :frowning:

Can you explain a bit more? I’m looking at this mini tutorial

I don’t really see them as states (The VAOs). They just seem like I’m making some buffers and telling OpenGL “Hey these are by buffers and this is where they are at”. Where are with a VBO I actually have to bind them and declare a usage.

Also I see that there are 3 buffers: Vertex, Color, and index

It is possible to create a buffer that is a object and not primitive?
So I could do something like



class Vertex
{
    float x, y; z;
    float c1, c2, c3;
}


Nope, vertex shader input attributes cannot be objects. Attributes also don’t have to be only vertex, color, or index. That link you posted shows deprecated functions glVertexPointer and glColorPointer. Those were the old ways of doing things. Now, OpenGL doesn’t care what data is position or color, they’re all inputs to your vertex shader! You control how the data is sent and processed, and how the output results.

VAO’s just hold state: which VBO’s are used, which attributes they map to, and any index buffers if used.

I don’t know of any resources that teach 2D OpenGL only, as it’s best to usually learn how to do 3D with it and then 2D becomes simple.

A really good tutorial I recommend that covers modern core OpenGL would be this: www.arcsynthesis.org/gltut
That tutorial’s code is in C++ though, so I ported it to Java+LWJGL: www.bitbucket.org/ra4king/lwjgl-shader-tutorials/
If you’re not interested in focusing on 3D much, then I would recommend going through chapters 1-7 and 14-16 only.

Another excellent resource is our own Davedes’s tutorials: https://github.com/mattdesl/lwjgl-basics/wiki

Traditionally, state in OpenGL has been global. And when I say “state”, I’m talking about active textures, active buffers and so on. You can only have one VBO set as each active target, like GL_ARRAY_BUFFER and GL_ELEMENT_BUFFER, at any given time. So when using multiple buffers, you change the state for each target every time you need to render a different buffer. That means multiple calls to change state, which can become expensive.

VAOs allow you to make multiple state changes with one call. You associate a number of related VBOs (such as a vertex buffer and an index buffer) and their state with a VAO, then when its time to render you only activate the VAO. That way, you don’t need to make multiple calls to set up the state of each VBO target. The best way to understand it is to avoid thinking of a VAO as an object (vertex array object is a horribly confusing name). Think of it more as a marker, or in index into an array of states.

In C or C++, you could do this. The memory layout of a struct (or C++ class) declared like your Vertex class would be the same as that of an array of six floats. So you could use an array of Vertex objects and call glVertexAttribPointer to set up the offsets for each attribute. Then you would wind up with what they call “interleaved buffers”. In Java, you could declare a vertex class like this, but because of the nature of the OpenGL bindings like LWJGL, you still would have to fill a ByteBuffer with the individual values. You wouldn’t be able to pass the object directly to OpenGL.

This actually brings up a really rough spot with me. So I know that we are in the OpenGL 4.X range in terms of what is ‘up to date’. I know that 2.1 and down is legacy. And Then 3.0 becomes all shaders (No more fixed function pipeline). So in your opinion ( or anyone that wants to weigh in on this) I kinda want to support legacy hardware, but do you think I should just skip them and go on to 3.0? I mean is it a big impact?

No worries on this, my plan was to use textured quads all along. Yea I’m seeing a lot of tutorials are in C++, nothing to bad. But still a bit of a pain to ‘convert’

Dear god that is a horrible name… I thought these were separate objects the whole time

And the part about the struct, yeah I wish you could do this, when I made a SpriteBatcher in DX11 that was awesome. You can just pop them in on a Map call and you were good to go

Most computers today support OpenGL 2.1 and it’s pretty safe to make that your base supporting version. Shaders were introduced in 2.0, so you’re still fine. It’s only 3.1 that everything fixed function was deprecated. I would suggest you learn modern OpenGL 3.3 first, as it becomes much easier in my opinion to learn older versions.

They are separate objects. VBO’s store plain old data that you upload. VAO’s store which VBO’s are being used and how.

And structs can’t be used for input attributes. They can, however, be used as uniforms. :slight_smile:

I agree, seeing as hardware is getting better by the day and Win XP is getting its support dropped in April

Also is there some hint as to what is depreciated? I see that I need to import things like org.lwjgl.opengl.GL15; in order to use glGenBuffers() something that seems to be used in 3.X and up

LWJGL groups functions and enums by the version they were introduced in. OpenGL 1.5 (GL15) introduced VBO’s. OpenGL 3.X (GL3X) uses and builds on them.