Jump to content

  • Log In with Google      Sign In   
  • Create Account


How to draw in modern OpenGL


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
4 replies to this topic

#1 knighce   Members   -  Reputation: 143

Like
0Likes
Like

Posted 20 October 2012 - 05:41 AM

Hi, I'm fairly new here and I'm also new to modern OpenGL. I am currently learning OpenGL 3 which I suppose is the starting point of the modern OpenGL.

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?

Many thanks,
Jonah

Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 2383

Like
3Likes
Like

Posted 20 October 2012 - 08:20 AM

The standard way is probably:

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:
-VBO(s)
-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
glDrawTrianggels(VAO)
glSetShaderVariableThing(glGetShaderVariableNumberThing(),mySecondMatrix) //Now with a different matrix, resulting in different position
glDrawTrianggels(VAO)


(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

Waterlimon (imagine this is handwritten please)


#3 Jebbs   Members   -  Reputation: 276

Like
2Likes
Like

Posted 21 October 2012 - 01:48 PM

This has a lot of basic information about 3D programming, but it should also give you some answers on OpenGL 3+ specifics.

http://www.arcsynthesis.org/gltut/

#4 knighce   Members   -  Reputation: 143

Like
0Likes
Like

Posted 22 October 2012 - 04:30 PM

Thanks for the replies. I think I get it now.

#5 L. Spiro   Crossbones+   -  Reputation: 12407

Like
0Likes
Like

Posted 22 October 2012 - 06:30 PM

I would be cautious before using VAO’s. They actually cause poor performance on some devices such as iOS. Of course that is OpenGL ES 2.0, but it could easily apply to desktop OpenGL depending on the driver implementations.


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.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




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.



PARTNERS