OpenGL Opengl grids

This topic is 3873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hy i'm a beginner in opengl ad i have this problem: i not understand this code: glPointSize(4.0); glBegin(GL_POINTS); for (int x = 0; x < 4; x++) for (int z = 0; z < 4; z++) glVertex3f(x, 0, z); glEnd(); Ths code create a points grid,but why? i'm not understand why use the z coordinate(the depth)and not the y coordinate! the grid Indeed appear in 2d. thanks I have the same problem with this grid: glBegin(GL_TRIANGLES); for (int x = 0; x < 3; x++) { for (int z = 0; z < 3; z++) { glVertex3f(x, 0.0, z); glVertex3f((x+1.0), 0.0, z); glVertex3f(x, 0.0, (z+1.0)); } } glEnd(); why the x+1 and z+1?(is a new point?) thanks.

Share on other sites
Actually this is very simple

Why y is 0 ? Well you don't want the grid to go upwards you want it on the lowest level of height.

Here is how i create a grid :

Gl.glBegin(Gl.GL_LINES);
for (int i = -5; i <= 5; ++i)
{
Gl.glVertex3i(i, 0, -5);
Gl.glVertex3i(i, 0, 5);

Gl.glVertex3i(5, 0, i);
Gl.glVertex3i(-5, 0, i);

}

Share on other sites
If you want a horizontal grid and not a vertical one, you need to lay it in the xz-plane (since y is up [smile]). So the y-value just sets the height of the grid.

The second loop inserts some points to make a triangle (see the GL_TRIANGLES?). Since most of those points are use more than once (in two iterations of the outer loop x and x+1 are the same: if x = 0 then x + 1 = 1, next iteration x = 1 so x + 1 = 2, you see x + 1 = 1 and x = 1 are the same in two iterations).
That loop basically draws a bunch of triangles like this:
|\|_\So the "grid" would look like this:|\ |\ |\ |_\|_\|_\|\ |\ |\ |_\|_\|_\|\ |\ |\ |_\|_\|_\

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 9
• 34
• 16
• Forum Statistics

• Total Topics
634123
• Total Posts
3015658
×