Looking for guidance on where to research drawing a 2d ring.

Hello,

So i’m not looking for actual code but more so where i could go to research on how to draw a 2d ring of n sides using VAO and VBO’s.

i have managed to achieve this using:

glBegin(DRAW_TRIANGLE_STRIP) 
for every point {
    //math
    vertex2f(inner x, inner y);
    vertex2f(outer x, outer y);
    glEnd() 
}

and it works perfectly for any n > 2. I’m just really confused on how to do this using only generated vertices. My biggest roadblock at the moment is that the vertices i generate are not always between
-1 and 1 and im not sure how to handle that. As well as i dont understand how to put an inner and outer set of vertices together to join up so that it will render how i want.

This is what it looks like currently using glBegin(). I rendered one of the shapes in wire-frame to see that my triangles were accurate.
[ATTACH=CONFIG]1732[/ATTACH]

Any nudge in the right direction to understand how this works with VAO and VBO would be greatly appreciated!

Well, I am not sure if I got your question right, but maybe you should start with a simple Triangle tutorial with VBOs and VAOs to understand how they work. For example:

If you have done this, there are a lot of ways to render a ring. For example:

Combine 2 Triangles to a quad. This is one ring segment. Combine multiple segments to a ring. If you create the vertex coordinates, stick to a range from [-1,1]. You can scale and move the ring inside your vertex shader by passing transformation matrices and scaling parameters to it.

[QUOTE=ProgrammerX;1290794]
Combine 2 Triangles to a quad. This is one ring segment. Combine multiple segments to a ring. If you create the vertex coordinates, stick to a range from [-1,1]. You can scale and move the ring inside your vertex shader by passing transformation matrices and scaling parameters to it.[/QUOTE]

So at the moment im generating 4 vertices. Xinner Yinner Xouter Youter. And so if i pass those using a for loop into a float vertices[n+1][3] array it should technically create a ring segment? Is it possible to convert numbers like 1.2345661 to a value between -1 and 1?

Well okay. Lets go into the details a little bit more. A triangle has 3 vertices in a triangle. Each vertex itself contains usually 4 coordinates x,y,z,w. Since you just want to draw a 2d ring, you don’t need the z component and you should not care about w either as a beginner. A VBO is just a reserved block of memory, where you put your vertex data. The VAO actually is some kind of manual for the GPU how to interpret the data in your VBO. Please go through the tutorials I have posted earlier to understand how they need to be set up.

So lets say you set up your VBO that the vertex data is arranged in a row. For example [v1x, v1y, v2x, v2y, v3x, v3y]. Your Vertex Shader (OpenGL 3.3+ [might be wrong about the version number]) should look like this:


#version 430
layout(location=0) in vec2 in_Position;
void main(void)
{
    gl_Position = vec4(in_Position,0,1);
}

If you now make draw call for a single triangle, the following happens:
The vertex Shader is executed 3 times, cause you have 3 vertices. If you have setup the VAO correctly, the in_Position Variable of the first vertex shader run gets the values v1x and v1y. Then v2x and v2y and so on. The shown vertex shader itself just writes the x and y values to the internal gl_Position variable and adds the 2 missing values z and w. Writing to gl_Position sets the final position of your vertex. So try to create a triangle with this information and the tutorials. If you get that going, add a second triangle to your VBO by adding another 3 vertices to the VBO so that you have 12 entries. By copying two vertex coordinates (x,y) from the first triangle to the vertex coordinates of the second triangle you get a quad cause both triangles share two points. Also adjust your draw call, that it draws more than one triangle. The doubling of the entries is actually not best practice but lets keep it simple. Also watch out wich points they share, so that the triangles are not overlapping.

So if you are able to create a quad, then you can create a ring by connecting multiple quads by expanding your VBO and adding triangles to the draw call. You just need to set the coordinates of each triangle right.

If you want to draw a circle with a certain radius, your coordinate must fulfill the formula xx + yy = R*R. A ring is limited by a outer circle and a inner circle. I am sure you will figure out a routine to generate those vertex coordinates automatically.
If you finally got it, you can move the circle by adding the same x or y value to each vertex of your VBO. You can also scale it by multiplying every value in your VBO by a certain scaling value. But be aware. The circle should have x=0 and y=0 as its center and if you want to scale it, do that before moving it, otherwise you will also scale the movement.

Is it possible to convert numbers like 1.2345661 to a value between -1 and 1?

Probably, but first I need to know what the number is supposed to tell me. The rings diameter? its x-coordinate?

Just for giggles, I decided one day, that I had to draw a dodecahedron (12-sided polygon) !!!

Here’s the youtube link:

https://www.youtube.com/watch?v=bHMdzx8AvV0

Jeff