Download presentation
Presentation is loading. Please wait.
1
Computer Graphics Practical Lesson 8
Vertex Buffer Objects and selection
2
Display lists Last lesson we saw display lists which store sequence of commands. This mechanism was created before the invention of GPU and is based on CPU. Hence it doesn‘t improve performance too much.
3
Vertex Arrays Instead of drawing each triangle by the CPU separately, we send OpenGL arrays of vertices, normal's and triangles’ indices. The drawing process is more efficient. Its still not too efficient because on every frame drawing, it requires the transfer of the arrays to the video adapter.
4
Using Vertex Arrays To setup vertex array, we need to set the 3 arrays: Vertecies Normals ( not mandatory, but without it object won’t be visible) Elements array – the array of the triangles. We set the arrays with the following functions: glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer); glNormalPointer – the same signature. glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
5
Using Vertex Arrays(cont’)
Stride – is used for interleaved arrays, usually set for 0 – no interleaving. On normal and vertices array, we usually use float as type. The last parameter on all function is void pointer.
6
Vertex Buffer Objects(VBO)
Video adapters has a very large memory. We can copy our points and shapes into the video adapter’s memory. This is called vertex buffering. Drawing will be done in the video adapter, without implicitly draw by CPU for every shape.
7
Initialize VBO At first, we declare which types of arrays we want to use with buffers. glEnableClientState(GL_VERTEX_ARRAY) Options are: GL_VERTEX_ARRAY, GL_NORMAL_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY After finishing drawing, we should call glDisableClientState, to return to normal state.
8
Initialize VBO(cont’)
1. Create handler – similar to texture, we must create handler to array object. glGenBuffers(1,&handler_id); 2. Bind handler to be buffer array. Set the handler to be array of buffers. glBindBuffer(GL_ARRAY_BUFFER, handler_id);
9
Initialize VBO(cont’)
3. Copy the data into the video card memory. glBufferData(GL_ARRAY_BUFFER,Buffer_Size,Buffer _Pointer,GL_STATIC_DRAW) We repeat these commands for every array of the input – the vertex, normal and elements arrays.
10
Using VBO’s Now, we bind the arrays handlers to buffer: glBindBuffer(GL_ARRAY_BUFFER,g_nVerticesVBO); Now we call the same functions as vertex arrays, only giving NULL pointer instead of array vector. glVertexPointer(3,GL_FLOAT,0,0);
11
Objects Selection When user clicks on object in 2D GUI, it is easy to detect on which object the user clicked. In 3D GUI and with the transformations we can apply on objects, it can be very difficult to detect which object was clicked. Hence, OpenGL provides us a mechanism to detect which object was clicked.
12
Objects Selection(cont’)
OpenGL contains special working modes for selection: The normal mode is rendering mode – the standard mode, each polygon that is drawn is written to the frame buffer. (GL_RENDER) The selection mode – for selecting objects, in this mode the graphics hardware is used to create the objects for selection(GL_SELECT)
13
Objects Selection(cont’)
Feedback mode – The same idea as selection mode, but instead of simple id’s of objects, returns information like which type of object, color or polygon is drawn.(GL_FEEDBACK) After performing selection or feedback, it is important to return to render mode. Otherwise, frames won’t be drawn.
14
Performing Selection 1. defining buffer to store hits and bind it: glSelectBuffer(BUFSIZE, selectBuf); 2. Change to selection mode: glRenderMode(GL_SELECT); 3. Init names stack and set value in it – this stack is used to mark the created objects. glInitNames(); glPushName(0);
15
Performing Selection(cont’)
4. define selection region – gluPickMatrix( (GLdouble) mouse_x, // X center of viewport. (GLdouble) mouse_y, // Y 5.0, 5.0, // size of selection region viewport) // size of viewport. 5. draw the model regularly. But now, each time an object intersects the selection region, a hit record will be created. Unless the object is culled.
16
Performing Selection(cont’)
6. switch back to rendering mode. Num_of_hits = glRenderMode(GL_SELECT); The function returns the number of hits occurred. The hits are stored in the hits buffer.
17
Names stack When an object is drawn, a hit is created with the value on the top of the names stack. Before we draw object, we need to set different value on the top the stack. We can use glPushName and glPopName. We can also use glLoadName to replace the value on the top of the stack.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.