# Thread: Frustration with drawing mesh

1. ## Re: Frustration with drawing mesh

Originally posted by beachboy1976:
The code is compiled fine. The only thing I can see on the screen is a plain with all messy points.
The really picture should like Terrain. Based on the different depth, you can see the land, the ocean, etc.

Thank you for your quick response.

Ok, I get it. I think the culprit is here:

Code :
```for (int i = 0; i < 300; i++)
{
int j = i + 1;
int k = i + 2;
int n = i + 3;

...
}```
As it is, the first time through your loop you get i = 0, j = 1, k = 2, n = 3, but the second time you get i = 1, j = 2, k = 3, n = 4, when that's probably not what quite what you were after... at the very least that i++ should be i += 2 so that you're getting at least two new points every time. Though at that poing you might just want to use a quad strip.

Of course, all of this is tied rather closely to how your data is organized.

2. ## Re: Frustration with drawing mesh

for (int i = 0; i < 300; i++)
{
int j, k, n;

if (i == 0)
{
j = i + 1;
k = i + 2;
n = i + 3;
}
else
{
i = i + 2;
j = i + 1;
k = k + 2;
n = n + 3;
}

The drawing on the screen is like big letter "X". How can I spread my drawing out so it will look like a terrain on the screen for the user.

Thank you

Originally posted by kabir:
Code :
```for (int i = 0; i < 300; i++)
{
int j = i + 1;
int k = i + 2;
int n = i + 3;

...
}```
As it is, the first time through your loop you get i = 0, j = 1, k = 2, n = 3, but the second time you get i = 1, j = 2, k = 3, n = 4, when that's probably not what quite what you were after... at the very least that i++ should be i += 2 so that you're getting at least two new points every time. Though at that poing you might just want to use a quad strip.

Of course, all of this is tied rather closely to how your data is organized.

3. ## Re: Frustration with drawing mesh

your for loop should go like this (sorry i don't know how to format code in here)

for( i = 0; i < 300; i+=2)
{
j = i+1;
k = i+2;
l = i+3;
}

this assumes you have data stored in a strip of quads. If not you may want to change i+=2 to i+=4

4. ## Re: Frustration with drawing mesh

I took time out to look at your code and I really have some problems with it. I might be wrong (since I don't read code very well but heres my take on it).

1) Every four cells in your array represents one quad. Thus every time you loop you should be jumping by 4, not 1. Otherwise you're changing only one corner of the quad each loop which should draw wack. I.e. your first quad is made by array[0], array[1], array[2], array[3]; your second by array[1], array[2], array[3], array[4]; your third by array[2], array[3], array[4], array[5]; etc, etc. One way to correct this is to change your for statement to...

for (int i = 0; i < 300; i += 4) {

which should give you array[0], array[1], array[2], array[3]; your second by array[4], array[5], array[6], array[7]; your third by array[8], array[9], array[10], array[11];

2) Since you are going progressively through the points it would be better to use ++i (or i++, I don't see a significant difference) to go through the array. Your code would look something like this...

for (int i = 0; i < 300; ++i) {
float xi = m_TotalPointsArray[i].x;
float yi = m_TotalPointsArray[i].y;
float zi = m_TotalPointsArray[i].z;
glVertex3f(xi, yi, zi);
}
glEnd();

3) If you have shared edges between quads you can use glBegin(GL_QUADSTRIP) instead of glBegin(GL_QUADS). It's faster, but it will mean changes that will be a bit more than I can deal with in detail. Your data will have to change. You're going to have to group quads that run along a line together, and then drop the duplicated edges in you data.

E.g. say quads 1, 2, 5, 6, 7 form a "line" and quads 3, 4, 8, 9, 10 form another line. Then you'll have to rearrange your data from 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 to 1, 2, 5, 6, 7, 3, 4, 8, 9, 10 and put glEnd(); glBegin() between lines of quadstrips in your loop to take full advantage of quadstrip drawing.

Also, say your quads have edges 1(A, B, C, D), 2(C, D, E, F), 3(E, F, G, H) etc. Then your data would be {A, B, C, D, E, F, G, H} and not {A, B, C, D, C, D, E, F, E, F, G, H}, which has duplicate edges. Then you could loop through and it would draw correctly with quadstrips.
PS. I might have gotten the egde order mixed up for successive quads.

4) This is just a speed tip. If you don't change the colour put the glColor call outside the for loop, so it isn't called unnecessarily.

5) This is just a speed tip (at least I hope it is). Use...

glVertex3f(m_TotalPointsArray[i].x, m_TotalPointsArray[i].y, m_TotalPointsArray[i].z);

float xi = m_TotalPointsArray[i].x;
float yi = m_TotalPointsArray[i].y;
float zi = m_TotalPointsArray[i].z;
glVertex3f(xi, yi, zi);

It's obviously faster (even if it's only trivially faster).

Originally posted by beachboy1976:
Thank you for your help again.
What I need to do is that I need use a set of data to draw a mesh on the screen. This is the first step of drawing. I have not able to do that. The set of data include over 300 vertex data. All the data look like this:
{ -76.3074 36.9634 -3.0000
-76.3297 36.9633 -4.8000
-76.3134 36.9632 -2.1000
-76.3014 36.9632 -2.7000
-76.3503 36.9632 -13.1000
-76.3169 36.9629 -2.7000
-76.3263 36.9629 -1.2000
-76.3186 36.9629 -0.9000
-76.3314 36.9627 -6.4000
-76.3134 36.9627 -2.1000
......
}

I used the following code to do the drawing mesh:
for (int i = 0; i < 300; i++)
{
int j = i + 1;
int k = i + 2;
int n = i + 3;
//m_TotalPointArray is vertex_data type
//the array holds all data
float xi = m_TotalPointsArray[i].x;
float yi = m_TotalPointsArray[i].y;
float zi = m_TotalPointsArray[i].z;

float xj = m_TotalPointsArray[j].x;
float yj = m_TotalPointsArray[j].y;
float zj = m_TotalPointsArray[j].z;

float xk = m_TotalPointsArray[k].x;
float yk = m_TotalPointsArray[k].y;
float zk = m_TotalPointsArray[k].z;

float xn = m_TotalPointsArray[n].x;
float yn = m_TotalPointsArray[n].y;
float zn = m_TotalPointsArray[n].z;

glColor3f (0.0f, 1.0f, 0.f);
glVertex3f(xi, yi, zi);
glVertex3f(-xj, yj, zj);
glVertex3f(-xk, -yk, zk);
glVertex3f(xn, yn, zn);

}
glEnd();

Thank you for your help here

[This message has been edited by Furrage (edited 03-28-2002).]

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•