Click here to Skip to main content
15,942,449 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello programmers!

I have a question!

I've already managed to draw a line. Here is the code:

C#
Gl.glBegin(Gl.GL_LINES);
Gl.glVertex3f(100.0f, 100.0f, 0.0f); // origin of the line
Gl.glVertex3f(200.0f, 140.0f, 5.0f); // ending point of the line
Gl.glEnd();

So my question is, how can i managed to draw a line or a terain (called what u want) within a loop! So i would change the values every time the loop goes around and at the end it would come up with a weird drawing or...whatever. Its just, i cant manage to draw using a loop!

Help!

[edit]
Well i got gauss krueger coordinates stored in a txt file, from a "walk" i made. After reading the txt file i store these coordinates in a two dimensional array.

C#
Example:
coordinate[0,0] = x
coordinate[0,1] = y
coordinate[0,2] = z

// And then
coordinate[1,0] = x
coordinate[1,1] = y
coordinate[1,2] = z

...and so on!

So i want now to place every single 'dot' of this into Gl.glVertex3f(float x, float y, float z)

I use Gl.glBegin(Gl.GL_LINE_STRIP)

And after all this done in a loop, a terain connected with all these dots and lines should show up.

I hope i've give more information then before! Thnx for replying!

[/edit]
Posted
Updated 24-Oct-12 4:05am
v3
Comments
Richard MacCutchan 24-Oct-12 7:26am    
Are you saying that you cannot figure out how to write a for or while statement in C#?
tokano 24-Oct-12 8:54am    
I do know, but when i make my while statement it doesnt want to draw the result. It just makes one line, not all of them
Richard MacCutchan 24-Oct-12 8:58am    
OK, so your code is wrong, but we cannot guess what it's doing. Please edit your question and add some more details, and please use <pre> tags around your code, as I have done above.
tokano 24-Oct-12 9:26am    
Well i got gauss krueger coordinates stored in a txt file, from a "walk" i made. After reading the txt file i store these coordinates in a two dimensional array.

Example:
<pre lang='cs'>
coordinate[0,0] = x
coordinate[0,1] = y
coordinate[0,2] = z</pre>

And then
<pre lang='cs'>
coordinate[1,0] = x
coordinate[1,1] = y
coordinate[1,2] = z</pre> ...and so on!

So i want now to place every single 'dot' of this into <pre lang='cs'>Gl.glVertex3f(float x, float y, float z)</pre>

I use <pre>Gl.glBegin(Gl.GL_LINE_STRIP);<pre>

And after all this done in a loop, a terain connected with all these dots and lines should show up.

I hope i've give more information then before! Thnx for replying!

1 solution

OK, so you have a list of points which you need to iterate and draw lines between, something like:
C#
Gl.glBegin(Gl.GL_LINES);
for (int i = 0; i < coordinate.size; i +=2)
{
    Gl.glVertex3f(coordinate[i][0], coordinate[i][1], coordinate[i][2]); // origin of the line
    Gl.glVertex3f(coordinate[i+1][0], coordinate[i+1][1], coordinate[i+1][2]); // ending point of the line
}
Gl.glEnd();

Which basically draws a line between each pair of vertices. If the ending point of one vertex is the same as the beginning of the next, then change the increment value of the loop to 1 rather than 2.
 
Share this answer
 
v2
Comments
fjdiewornncalwe 24-Oct-12 11:04am    
You are certainly on the right track, but for some reason the Gl.glBegin... and GL.glEnd existing within the loop looks strange to me. Should the Begin and End not happen outside of the loop and the loop contain only the glVertex3f calls?
Richard MacCutchan 24-Oct-12 11:25am    
Quite possibly. I don't know GL so I was just trying to show how the loop should be constructed, as that seemed to be OP's problem.
tokano 24-Oct-12 11:27am    
Here is the picture, what is drawn when going trough the loop.

http://postimage.org/image/mqf9h21op/

It's definitly not what i'm looking for. It should be more then one line:)) And you can see on the picture how i need to change the camera (translate) to see my drawn object.

Now when i've read the answer from Marcus Kramer, i found out that something has happened! I'm on the right track now! Here are the results!

http://postimage.org/image/urhg4phbj/

I think the whole problem was, that i had initiated the begin and end in the loop, and that's why it always "RESET" the other lines..and eventually showed just the last one.
fjdiewornncalwe 24-Oct-12 11:31am    
So then Richard's answer is almost correct if you take my comment along with it. Move the Begin and End calls out of the loop and just make the drawing calls within the loop.
tokano 24-Oct-12 11:33am    
Exactly!

Thank you both very much, for help! I hope i'll be meeting you guys more with answers and questions:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900