How to draw in modern OpenGL
Members - Reputation: 143
Posted 20 October 2012 - 05:41 AM
Now for my newb question, how do I draw multiple polygons (such as 2 separate triangles) in OpenGL. I remember in the old OpenGL, you could do something like multiple call to glBegin()~glEnd() translating them to different locations. But how do i this using OpenGL objects?(vao,vbo, etc.)
Sorry if I look brainless asking this question, but maybe someone could help me.
Thanks in advance.
Details below - Wall of text (you can skip it if you don't have time)
I have gone through different tutorials and could draw single triangle or even a cube. However for some reason I dont get how to draw multiple polygons in different location (not connected, as the cube is composed of multiple triangles). Maybe I just don't fully understand how OpenGL objects work. I believe it has to do with vertex buffer object. I drew my triangle or cube using a vertex buffer which is supplied with an array of vertices as the buffer data. But how do i draw another cube in different location resulting in having two cudes on the screen?
Crossbones+ - Reputation: 3165
Posted 20 October 2012 - 08:20 AM
1.Create VBO(s) which contain the vertex data for each vertex (position, texture coordinates)
for example you could have a VBO containing x,y,z,u,v or VBO for x,y,z and VBO for u,v
2.You create VAO
3.You bind the VBO and call a function named something like glVertexAttribPointer. It takes in the data so that when openGL wants the vertex properties for vertex number X, it knows which VBO it is in, how many components it has, what type it is, wether theres some offset between 2 of those properties (for example if you use x,y,z,u,v, the position would have u,v between it so there would be some offset called stride) for example in the x,y,z,u,v case to access the position opengl does something like VBO[(posComponentCount*posComponentSize+stride)*vertexToAccess]
You also tell which attribute it is (some integer, like position is usually 0 i believe, i think you can decide these yourself so it matches with your shaders)
So now the VAO has:
-For each attribute, data telling which VBO it is in, what type/size, and wether theres some offsets to access it in case theres multiple attributes in 1 VBO
4.You draw the VAO giving it the number of vertices etc. and for each vertex it gets each attribute of that vertex based on the data you provided and passes it to your shaders
I think your shaders will have inputs where it defines the variable name in your shader and which attribute it corresponds to (eg. vertex position would probably be attribute 0) but im not sure, i think you also can (or have to?) ask opengl which attribute number the shader variable name corrseponds to (so you can tell the VAO that the shader variable "blah" is found in this VBO and these offsets etc. by giving it the number opengl assigned to that shader variable)
You will have to code your own shaders (lighting, applying textures, matrix stuff...)
To draw lets say 2 cubes, you make a single VAO, with the vertex data of the cube and all that so opengl can draw a cube. But when drawing you set a variable like matrix (i dont know how this works, probably similiar to vertex attributes and those so you get the number corresponding to a shader variable and pass it to opengl along with the matrix so now your shader has a variable with the matrix as its current value)
So it goes something like
glSetShaderVariableThing(glGetShaderVariableNumberThing(),myMatrix) //each vertex in the cube will be translated using the matrix in your shader
glSetShaderVariableThing(glGetShaderVariableNumberThing(),mySecondMatrix) //Now with a different matrix, resulting in different position
(I hope that makes any sense because its probably kind of messy)
1.Stuff vertex attributes in VBOs
2.Tell VAO what attributes in which VBO and what offsets to apply to find the next one etc.
3.Set shader variables eg. matrix to use or texture to use
4.Draw VAO, opengl will get vertex datas from the VBO's, and will get matrix/texture data you previously set, and all are passed to your shader which will move the vertices using the matrix and color the pixels using the texture
Crossbones+ - Reputation: 19058
Posted 22 October 2012 - 06:30 PM
If you want to be sure you are not using old/deprecated features, run your program through gDEBugger. It will tell you not only which functions you are using that are deprecated, but also if you are using any deprecated enumerations, hints, or features at all.
If you address all the issues it reports, you can be sure you are using modern and efficient OpenGL.
My Art: http://l-spiro.deviantart.com/gallery/4844241/Realism My Music: https://soundcloud.com/l-spiro
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums