I am having trouble understanding how to use VAO's in OpenGL ES2 (on iOS) and getting them to work.
My current rendering setup looks like this (in pseudocode):
Initialization:
foreach VBO:
glGenBuffers();
Rendering a frame:
// Render VBO 1
glClear(color | depth);
glBindBuffer(GL_ARRAY_BUFFER, arrayVBO1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO1);
foreach vertex attribute:
glVertexAttribPointer();
glEnableVertexAttribArray();
glBufferData(GL_ARRAY_BUFFER, ...);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ...);
glDrawElements(GL_TRIANGLE_STRIP, ...);
// Render VBO 2
glClear(color | depth);
glBindBuffer(GL_ARRAY_BUFFER, arrayVBO2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO2);
foreach vertex attribute:
glVertexAttribPointer();
glEnableVertexAttribArray();
glBufferData(GL_ARRAY_BUFFER, ...);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ...);
glDrawElements(GL_TRIANGLE_STRIP, ...);
This works fine, however, both VBO's have exactly the same interleaved vertex attribute struct, and as you can see I'm setting up and enabling each attribute every frame for every VBO. Instruments complains about redundant calls to glVertexAttribPointer() and glEnableVertexAttribArray(), but when I move one of them or both to the initialization phase I either get a EXC_BAD_ACCESS when calling glDrawElements or nothing is drawn.
My question is whether I need to do this every frame, why it doesn't work if I don't, and how I would use VAO's to solve this.
Sorry for the dredge, but I'm procrastinating and you keep topping my google search. I'm sure you've solved it by now...
The correct way is to only update the buffers when the data changes, not every frame. Ideally, you would only update the part of the buffer that changed. Also, Attribute Pointers are offsets into the buffer if a buffer is bound.
Initialisation:
glGenBuffers()
foreach VBO:
glBufferData()
Updates / Animation, etc:
glMapBuffer() //or something like this
buffer->vertex = vec3(1,2,3) // etc
glUnmapBuffer()
And Render:
glBindFBO()
glClear(color | depth);
glBindBuffer(GL_ARRAY_BUFFER, arrayVBO1)
glVertexAttribPointer(GL_VERTEX,..., 0) // Buffer Offset = 0
glVertexAttribPointer(GL_TEXCOORD,..., sizeof(vertex)) // Buffer Offset = size of vertex
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO1);
glEnableVertexAttribArray(..., 0) // Buffer Offset = 0
glBindBuffer(0); // Unbind buffers, you don't need them here
glDrawElements(GL_TRIANGLE_STRIP, ...);
Hope that helps.
Related
I have a number of textures loaded using GLKTextureLoader. If I bind any of the loaded textures statically, each texture works as expected.
But I am trying to bind a random texture each glDrawArrays call, but the texture bound is always the same.
GLuint vbo = vboIDs[emitterNum];
GLKMatrix4 projectionMatrix = GLKMatrix4MakeScale(1.0f, aspectRatio, 1.0f);
glUseProgram(emitterShader[emitterNum].program);
glEnable(GL_TEXTURE_2D);
glActiveTexture (GL_TEXTURE0);
//Note: valid texture names are 0-31, but in my code I store texture names returned in an array and use them. Use arc4random here for simplicity
glBindTexture(GL_TEXTURE_2D, arc4random_uniform(31)); //use a random texture name
//glBindTexture(GL_TEXTURE_2D, 2); //If I use this line instead of the line above, it will draw texture 2, or any number I specify
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glUniformMatrix4fv(emitterShader[emitterNum].uProjectionMatrix, 1, 0, projectionMatrix.m);
//I set a number of uniforms such as:
glUniform1f(emitterShader[emitterNum].uTime, timeCurrentFrame);
glUniform1i(emitterShader[emitterNum].uTexture, 0);
//I set a number of vertex arrays such as:
glEnableVertexAttribArray(emitterShader[emitterNum].aShade);
glVertexAttribPointer(emitterShader[emitterNum].aShade, // Set pointer
4, // four components per particle (vec4)
GL_FLOAT, // Data is floating point type
GL_FALSE, // No fixed point scaling
sizeof(Particles), // No gaps in data
(void*)(offsetof(Particles, shade))); // Start from "shade" offset within bound buffer
GLsizei rowsToUse = emitters[emitterNum]->rows;
//Draw the arrays
glDrawArrays(GL_POINTS, 0, rowsToUse );
//Then clean up
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
glDisable(GL_TEXTURE_2D);
glBindBuffer(GL_ARRAY_BUFFER, 0);
I have tried putting the texture calls in various places, like where shown and direct before the glDrawArrays command, but no matter what - I can't make it bind to different textures unless done so statically.
Say we have an object and we want to create multiple objects and move them independently based some algorithm.
Here is what is the process I am using:
Create a structure with the geometry of the object
Create an array of vertice buffers using the geometry of the object
Now in the rendering routine, I need to go through each one of those objects and alter their position based on a specific algorithm.
To accomplish this I need to get the current location of the object to compute the new position.
How can I get the current location of a vertice buffer? Clearly, I do not want to store outside the program all locations of the object since they are inside the vertice buffer.
EDIT: This is the code I am using to store and retrieve data from the model matrix of each object
// Set up Code
- (void)setupGL
{
[EAGLContext setCurrentContext:self.context];
[self loadShaders];
glEnable(GL_DEPTH_TEST);
for( int i; i<num_objects; i++) {
glGenVertexArraysOES(1, &_objectArray[i]);
glBindVertexArrayOES(_objectArray[i]);
glGenBuffers(1, &_objectBuffer[i]);
glBindBuffer(GL_ARRAY_BUFFER, _objectBuffer[i]);
glBufferData(GL_ARRAY_BUFFER, sizeof(objectData), objectData, GL_STATIC_DRAW);
glEnableVertexAttribArray(....);
glVertexAttribPointer(......, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
}
glBindVertexArrayOES(0);
}
//********************************************************
// Rendering Code
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glUseProgram(_program);
for(int i=0; i<num_objects; i++) {
glBindVertexArrayOES(_objectArray[i]);
// Get Previous data
GLint uMatrix = glGetUniformLocation(_program, "modelMatrix");
glGetUniformfv(_program, uMatrix, dataOfCurrentObject);
// Get Previous data
... transform dataOfCurrentObject based on an algorithm and create newDataOfCurrentObject
// Update object with new data and draw
glUniformMatrix4fv(uMatrix, 1, 0, newDataOfCurrentObject);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
}
The problem I have now is that the dataOfCurrentObject for object 'i' is identical to the newDataOfCurrentObject for object 'i-1'. In other words it appears that the code keeps track of only one model matrix for all objects, or it does not read correctly the model matrix of a specific object. Any ideas?
The simplest method that gets used is to set the object's position in the Model Matrix, and upload that object's appropriate Model Matrix as a Uniform each time you draw a new object. The [pseudo-]code would look like this:
for(game_object object : game_object_list) {
glUniformMatrix4f(modelMatrixUniformLocation, 1, false, object->model_matrix);
object->draw();
}
And when you need to update the object's position:
for(game_object object : game_object_list) {
object->model_matrix = Matrix.identity();
/*Any transformations that need to take place here*/
object->model_matrix = object->model_matrix.transpose(/*x*/, /*y*/);
/*Any other transformations that need to take place*/
}
Exactly what you put in there will vary depending on your needs. If you're programming a 2D game, you probably don't need a 4x4 model matrix. But the basic logic should be identical to what you eventually use.
You don't have to read anything back from your vertex buffers. You just need to keep in your application the tranform you need and pass it as uniform for every object.
For example if you need to translate the object, keep the modelViewMatrix in and every frame apply trasformation to the previous matrix :
GLKMatrix4 modelViewMatrix;
...
modelViewMatrix = GLKMatrix4Identity;
...
-(void) render {
modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, deltaX, deltaY, deltaZ);
glUniformMatrix4fv(_modelViewUniform, 1,0, modelViewMatrix.m);
}
So you just need to keep reference to your objects modelView matrices and apply your transformations accordingly.
If you want to read data back from your vertex buffers (gpu) to your application you have to use Transform Feedback which is a bit more advanced technique and it is used when you modify your vertexes in the vertex shader and you need the results back. (It is not needed for your case).
Do a good deed and help get someone (me) out of their misery, since it's New Year's Eve soon. I'm working on an iOS app, a coloring book for kids and I haven't stumbled upon OpenGL before (more precisely OpenGLES 2.0) so there's a big chance there's stuff I don't actually get in my code.
One of the tasks is to not let the brush spill out of the contour in which the user started drawing.
After reading and understanding some OpenGL basics, I found that using the stencil buffer is the right solution. This is my stencil buffer setup:
glClearStencil(0);
//clear the stencil
glClear(GL_STENCIL_BUFFER_BIT);
//disable writing to color buffer
glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
//disable depth buffer
glDisable(GL_DEPTH_TEST);
//enable writing to stencil buffer
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_NEVER, 1, 0xFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
[self drawStencil];
//re-enable color buffer
glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
//only draw where there is a 1
glStencilFunc(GL_EQUAL, 1, 1);
//keep the pixels in the stencil buffer
glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
Right now, I'm just trying to draw a square in the stencil buffer and see if I can limit my drawing only to that square. This is the method drawing the square:
- (void)drawStencil
{
// Create a renderbuffer
GLuint renderbuffer;
glGenRenderbuffers(1, &renderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
// Create a framebuffer
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderbuffer);
// Clear
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
// Read vertex shader source
NSString *vertexShaderSource = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"VertexShader" ofType:#"vsh"] encoding:NSUTF8StringEncoding error:nil];
const char *vertexShaderSourceCString = [vertexShaderSource cStringUsingEncoding:NSUTF8StringEncoding];
// Create and compile vertex shader
GLuint _vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(_vertexShader, 1, &vertexShaderSourceCString, NULL);
glCompileShader(_vertexShader);
// Read fragment shader source
NSString *fragmentShaderSource = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FragmentShader" ofType:#"fsh"] encoding:NSUTF8StringEncoding error:nil];
const char *fragmentShaderSourceCString = [fragmentShaderSource cStringUsingEncoding:NSUTF8StringEncoding];
// Create and compile fragment shader
GLuint _fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(_fragmentShader, 1, &fragmentShaderSourceCString, NULL);
glCompileShader(_fragmentShader);
// Create and link program
GLuint program = glCreateProgram();
glAttachShader(program, _vertexShader);
glAttachShader(program, _fragmentShader);
glLinkProgram(program);
// Use program
glUseProgram(program);
// Define geometry
GLfloat square[] = {
-0.5, -0.5,
0.5, -0.5,
-0.5, 0.5,
0.5, 0.5};
//Send geometry to vertex shader
const char *aPositionCString = [#"a_position" cStringUsingEncoding:NSUTF8StringEncoding];
GLuint aPosition = glGetAttribLocation(program, aPositionCString);
glVertexAttribPointer(aPosition, 2, GL_FLOAT, GL_FALSE, 0, square);
glEnableVertexAttribArray(aPosition);
// Draw
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Present renderbuffer
[context presentRenderbuffer:GL_RENDERBUFFER];
}
So much code and nothing happens... I can draw relentlessly wherever I want without a single stencil test stopping me.
What can I do? How do I check if the stencil buffer has something drawn inside it? If there's a missing puzzle for any of you, I will happily share any other parts of the code.
Any help is greatly appreciated! This has been torturing me for a while now. I will be forever in your debt!
UPDATE
I got the contour thing to work but I didn't use the stencil buffer. I created masks for every drawing area and textures for each mask which I loaded in the fragment shader along with the brush texture. When I tap on an area, I iterate through the array of masks and see which one was selected and bind the mask texture. I will make another post on SO with a more appropriate title and explain it there.
The way you allocate the renderbuffer storage looks problematic:
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
The documentation says about this method:
The width, height, and internal color buffer format are derived from the characteristics of the drawable object.
The way I understand it, since your "drawable object" will normally be a color buffer, this will create a color renderbuffer. But you need a renderbuffer with stencil format in your case. I'm not sure if there's a way to do this with a utility method in the context class (the documentation says something about "overriding the internal color buffer format"), but the easiest way is probably to simply call the corresponding OpenGL function directly:
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height);
If you want to use your own FBO for this rendering, you will also need to create a color buffer for it, and attach it to the FBO. Otherwise you're not really producing any rendering output.
Instead of creating a new FBO, it might be easier to make sure that the default framebuffer has a stencil buffer, and render to it directly. To do this, you can request a stencil buffer for your GLKView derived view by making this call during setup:
[view setDrawableStencilFormat: GLKViewDrawableStencilFormat8];
Let me preface this by saying I’m fairly new at OpenGL ES and OpenGL in general.
I’ve got a model object that our 3D guy exported from zBrush. It doesn’t have any normals in the .obj file, and I think that’s what’s causing this behavior. I’m using obj2opengl to create a .h file containing the vertices and texture coordinates, then importing those and rendering. Here’s what the import looks like:
glGenBuffers(1, &mushroomVertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, mushroomVertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(mushroom01b_LowVerts), mushroom01b_LowVerts, GL_STATIC_DRAW);
glGenBuffers(1, &mushroomTextureBufferID);
glBindBuffer(GL_ARRAY_BUFFER, mushroomTextureBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(mushroom01b_LowTexCoords), mushroom01b_LowTexCoords, GL_STATIC_DRAW);
When it’s time to draw the object, here’s the code I’m using:
glBindBuffer(GL_ARRAY_BUFFER, mushroomVertexBufferID);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, NULL);
glBindBuffer(GL_ARRAY_BUFFER, mushroomTextureBufferID);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, NULL);
self.effect.texture2d0.name = _mushroomTextureInfo.name;
self.effect.texture2d0.target = _mushroomTextureInfo.target;
[self.effect prepareToDraw];
glDrawArrays(GL_TRIANGLES, 0, mushroom01b_LowNumVerts);
This works, and for models where I’ve opened them in Blender, fixed the normals, and re-exported as OBJ with normals (adding code above for a third array buffer), it looks pretty good. But if I don’t modify the exported-from-zBrush OBJ at all, here’s what I get:
Now, this is with face culling enabled with glEnable(GL_CULL_FACE);. If I don’t enable it, the model seems to be rendering inside-out.
Clearly the zBrush export is messed up somehow. My question is how to fix it.
The model isn't triangulated. Have your artist check that box on the output.
I went and looked into the script file for you.
Assuming the quad is
a -- c
| |
| |
b -- d
In the .pl you'll find
rectangle => second triangle
if($5 != "")
{
#d = split('/', $5);
$va_idx[$numFaces] = $a[0]-1;
$ta_idx[$numFaces] = $a[1]-1;
$na_idx[$numFaces] = $a[2]-1;
$vb_idx[$numFaces] = $d[0]-1;
$tb_idx[$numFaces] = $d[1]-1;
$nb_idx[$numFaces] = $d[2]-1;
$vc_idx[$numFaces] = $c[0]-1;
$tc_idx[$numFaces] = $c[1]-1;
$nc_idx[$numFaces] = $c[2]-1;
$face_line[$numFaces] = $line;
$numFaces++;
}
Try reordering to this to see if the second triangle's winding order needs to be reversed
rectangle => second triangle
if($5 != "")
{
#d = split('/', $5);
$va_idx[$numFaces] = $a[0]-1;
$ta_idx[$numFaces] = $a[1]-1;
$na_idx[$numFaces] = $a[2]-1;
$vb_idx[$numFaces] = $c[0]-1;
$tb_idx[$numFaces] = $c[1]-1;
$nb_idx[$numFaces] = $c[2]-1;
$vc_idx[$numFaces] = $d[0]-1;
$tc_idx[$numFaces] = $d[1]-1;
$nc_idx[$numFaces] = $d[2]-1;
$face_line[$numFaces] = $line;
$numFaces++;
}
Try change culling from clockwise to counterclockwise, if your tool respects orientation in obj and zBrush export respects it this might help.
I finally got some functioning code to draw lines (in Xamarin/monotouch)
//init calls
Context = new EAGLContext (EAGLRenderingAPI.OpenGLES2);
DrawableDepthFormat = GLKViewDrawableDepthFormat.Format24;
EAGLContext.SetCurrentContext (Context);
effect = new GLKBaseEffect ();
effect.UseConstantColor = true;
effect.ConstantColor = new Vector4 (1f, 1f, 1f, 1f); //white
GL.ClearColor (0f, 0f, 0f, 1f);//black
public void DrawLine(float[] pts) {
//generate, bind, init
GL.GenBuffers (1, out vertexBuffer);
GL.BindBuffer (BufferTarget.ArrayBuffer, vertexBuffer);
GL.BufferData (BufferTarget.ArrayBuffer, (IntPtr) (pts.Length * sizeof (float)), pts, BufferUsage.DynamicDraw);
// RENDER //
effect.PrepareToDraw ();
//describe what's going to happen
GL.EnableVertexAttribArray ((int) GLKVertexAttrib.Position);
GL.VertexAttribPointer ((int) GLKVertexAttrib.Position, 2, VertexAttribPointerType.Float, false, sizeof(float) * 2, 0);
GL.DrawArrays (BeginMode.LineStrip, 0, pts.Length/2);
}
I have a couple questions.
Is this approach for drawing lines optimal? Are there any suggested improvements (i.e. antialiasing, etc..)
GL.Clear (ClearBufferMask.ColorBufferBit);
effect.ConstantColor = new Vector4 (1f, 1f, 1f, 1f);
DrawLine (line);
effect.ConstantColor = new Vector4 (1f, 0f, 1f, 1f);
DrawLine (line2);
Does all the memory associated with the line disappear when I call GL.Clear()? i.e. do I have to do any memory cleanup, or can I just keep calling GL.Clear() followed by DrawLine() and not worry about memory management?
I'm planning on using these functions for graphing. If the underlying data changes (but I have the same number of lines, is there a subset of functions that I can call to more efficiently update the lines?
GL.GenBuffers (1, out vertexBuffer) creates a buffer on the GPU and has to be deleted after the usage. In most cases you create buffer to push data to GPU which will not be updated frequently and are used to draw those data many times. There is probably a flag to stream the data (instead of DynamicDraw) for constant updating though. You could use that to reuse the same buffer but it would probably be best to just push the data pointer directly from the CPU: Lose all 3 lines concerning the buffer and insert pts into VertexAttribPointer instead of 0 for the last argument.
You say you will be using this for graph drawing. If the graph data will not be modified every frame and you can compute all the points you still might want to benefit from buffers. Instead of trying to push every line to its own buffer try pushing all the lines to a single buffer (even axis can be there). Use GL.DrawArrays (BeginMode.LineStrip, 0, pts.Length/2) to draw specific lines as last 2 arguments control the range in current buffer to draw (to draw 5th line only you would write GL.DrawArrays(BeginMode.LineStrip, 5*2, 2)). So when the graph data should update; delete the current buffer, create a new buffer, push the data to buffer, bind the buffer, set the vertex pointer and then just keep calling the draw method.
GLClear has nothing to do with memory cleanup at all. It will only clear (set values) the buffers attached to your frame buffer, in your case it will set all the pixels in your render buffer to the color you set in ClearColor. Nothing more. Other common cases are to also clear depth buffer, stencil buffer...
As for all the optimization and anti-aliasing it all depends on what you are doing, there is no general answer. Though if your scene gets too edgy try to search around for multisampling.