Jump to content

View more

Image of the Day

Working on an auto spawn system. #gamedev #indiedev #screenshotsaturday https://t.co/Mm2kfekz7b
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

opengl and reuse of vboarray

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 giugio   Members   


Posted 23 October 2012 - 03:23 AM

I don't understand the use in opengl of the
glVertexAttribPointer(glGetAttribLocation(shaderProgram1, "VertexPosition"), 3, GL_FLOAT, GL_FALSE, sizeof(Vertexes), BUFFER_OFFSET(sizeof(float) * 3));
and of the
glEnableVertexAttribArray(glGetAttribLocation(shaderProgram1, "VertexPosition"));
what do they precisely?
are relative to the shader or to the vbo?
because I have a vboarray and i wish use it for many render call
i would do:
//1) first use of the vboarray with shaderProgram1
//1) second use of the vboarray with shaderProgram2
,i must call glVertexAttribPointer and glEnableVertexAttribArray for each different shader?

#2 Kaptein   Prime Members   


Posted 23 October 2012 - 09:34 AM

no, vertex attribs should be part of vertex arrays, you call it a VBOarray, which it's not really
its an encapsulated thingamajig that contains all thats necessary to render your data, save for uniforms (and matrices)

to create a VAO:
generate vao, vbo
bindvertexarray vao
bind vbo, upload data
enable vertex attribs
set vertex attrib pointers
unbind vao

now VBO is gone, unless you are in compatibility mode, in which case only the vertex attribs are part of the object (VAO)
VAO used to be a state object to encapsulate vertex attributes, in modern openGL it is the recipe to render the model
so, if you are in core 3.x and later: only VAO remains!

to render:
gldrawarrays / gldrawelements

vertex attributes (glVertexAttrib*) are what makes you able to define your own vertex structure to openGL for usage in modern shaders
you specify a type, normalization, slot number (index), and offset from VBO data relative to the CURRENTLY bound VBO
this is because you can have 10 VBOs in a single VAO, all using different attributes in your shader
usually you have only 1 VBO with all attributes interleaved, still all using different attributes in your shader

let's look at:
for one, you are saying that your vertices are starting at byte position: 3 * sizeof(glfloat), which i don't believe
i think they start at byte number 0 in your vertex structure, which is the most common position for vertex position

let's say they are located at index 0 in your shader, and they start at byte 0 offset from your vertex structure,
then location = 0, and offset = 0

glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE, sizeof(Vertexes), (glvoid*) offset );

if that's the case, then the above line is saying:
i have bound a VAO, and i have bound a VBO. this VBO has an attribute that starts at the first byte of my data that openGL has no clue what looks like
it's located at slot: "location" because that's what openGL told me, or i told openGL (see below)
it has 3 values (vec3, or just vector), and is of type GL_FLOAT, which is GLfloat to your interface, meaning a 32bit's floating point variable of size 4 bytes

openGL now knows that to be able to access this data in your shader, it needs to access 4 * 3 bytes for each sizeof(Vertexes) just to get this data
however, the data isn't well defined yet, so it needs to know whether or not its normalized
imagine that you were passing a 4-byte color (R, G, B, A), then when you wanted to access it in your shader you got values from 0 to 255 (8 bits per channel), or 0 to 65535 (16 bits per channel), well then you'll need to convert that data to be able to properly work with it, since normalized values (0.0 to 1.0, or -1.0 to 1.0) are much easier to do calculations with!

normals is also an example that can normalized, unless you are using GL_FLOAT (if you are using floating point precision you might as well have them properly normalized yourself!)
if you were using GL_BYTE instead, then normalize should be GL_TRUE

back to your own code example:
since you specified GL_FALSE for your vertex position in the normalize parameter, openGL will not attempt to normalize your vertex position, and instead pass it as-is
however, there are many situations when you'd want to do that!

hope this helps!

as for the location of the attribs in your shader, you have 3 options:
1. use (layout = index) in vec4 in_vertex; in shader to hard-code the indexing
2. use glGetAttribLocation to get index from shader, which requires you to have the shader bound when doing this (not recommended)
3. use glBindAttribLocation(program, index, attrib_name) just before glLinkProgram(program)

i recommend the last one Posted Image

Edited by Kaptein, 23 October 2012 - 09:57 AM.

#3 Koehler   Members   


Posted 23 October 2012 - 09:44 AM

glEnableVertexAttribArray tells OpenGL to turn on a particular generic vertex attribute array. glVertexAttribPointer tells OpenGL where to start, and how to traverse memory when reading in vertex data.(either by way of a CPU memory pointer, or if a buffer is bound to GL_ARRAY_BUFFER an offset into that buffer)

For an example, let's assume you have the points of a triangle in a buffer that is laid out like so:
position = {[1.0,2.0,3.0 ][2.0,3.0,3.0 ][3.0,2.0,3.0 ]};

and a vertex shader with one of the following declared:

attribute vec3 VertexPosition; //OpenGL 2
in vec3 VertexPosition; //OpenGL 3

First off, you've got the glGetAttribLocation(shaderProgram1, "VertexPosition"); call. This is giving you the index of the generic array used to supply VertexPosition with data. Initially this array has no data bound to it, and is disabled. Suppose we do this first:

unsigned int positionLoc = glGetAttribLocation(shaderProgram1, "VertexPosition");

By calling glEnableVertexAttribArray(positionLoc), you're telling openGL to start pulling data from that generic attribute array. Currently there is no data bound to it though, so if you try to draw now you'll crash.

"glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 3, 0);" fixes this, by telling OpenGL where to start in the buffer object bound to GL_ARRAY_BUFFER (at offset 0), how to interpret the values there (as floats), whether to normalize them (in this case, no), and finally how to traverse the data (as 3 sets of 3 elements).

As the post above me points out, a VAO will encapsulate all of your enable / bind calls into one nice glBindVertexArray(vaoName); call. I recommend using it, as later versions of OpenGL will not draw without a VAO on Windows.

Edited by Koehler, 23 October 2012 - 09:45 AM.

#4 giugio   Members   


Posted 23 October 2012 - 09:57 AM

very thanks, but a question:
1)is possible to reuse a vao with another shader?
2)if I reuse the vao with another shader must call the glVertexAttribPointer and glEnableVertexAttribArray with the other shader?
i read about uniform buffers that can be bound to n shaders, this is they force
is the same things with vao?

#5 Kaptein   Prime Members   


Posted 23 October 2012 - 09:59 AM

a VAO isn't bound to a specific shader, but the VAO does specify the indexes or "slots" that it will ask openGL to use when rendering
that means that the indexes specified must be the same for all the shaders (see my post how to achieve this)
but it doesn't mean that the attributes have to be the same, because you can cheat and turn on and off certain attributes that you don't want to use with different shaders

#6 giugio   Members   


Posted 23 October 2012 - 10:05 AM

Then if i use the same shader program(string glsl source) but compiled and linked as another shader , the attributes(layout ) are the same(i use opengl 3.2), i can rebind the vao and draw with the second shader without problem?

#7 Koehler   Members   


Posted 23 October 2012 - 10:18 AM

That is correct, giugio. As long as you've either used glBindAttribLocation or layout specifiers in your shaders to make sure attributes in different shaders are bound to the same attribute indices, you can reuse a VAO without any problem. Remember that glBindAttribLocation must be used BEFORE linking your shader.

Edited by Koehler, 23 October 2012 - 10:18 AM.

#8 giugio   Members   


Posted 24 October 2012 - 02:56 AM

very thanks.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.