PDA

View Full Version : Frustration with drawing mesh



beachboy1976
03-27-2002, 08:27 AM
Hi,
I have been studying OpenGl for a while. I tried to draw a mesh on the screen for days. The mesh wasn't ceated right no matter how much I tried.
Anyone out there can tell me why OpenGl is so hard to learn.

DFrey
03-27-2002, 09:00 AM
Well, the difficulty of OpenGL is bound to vary from person to person, so this question may be unanswerable. I myself have no difficulty at all with OpenGL itself. It is just a simple state machine. For me the hardest part is understanding 3D graphics in general.

beachboy1976
03-27-2002, 09:07 AM
I was a math student in college. I have no problem to understand how the 3D works. However, when I started the project two weeks ago, I just had very hard time to even draw a mesh on the screen. I feel so frustrated right now. I even don't know how to continue my work next. I jst feel clueless.


Originally posted by DFrey:
Well, the difficulty of OpenGL is bound to vary from person to person, so this question may be unanswerable. I myself have no difficulty at all with OpenGL itself. It is just a simple state machine. For me the hardest part is understanding 3D graphics in general.

kabir
03-27-2002, 10:52 AM
http://nehe.gamedev.net

This might help.

beachboy1976
03-27-2002, 10:57 AM
Thank you.
I already looked the site and learned a lot of useful info from the site.
However, when I turned to my project, it wasn't working at all.
I was wondering if you can take a look my code and tell me what I did wrong.


Originally posted by kabir:
http://nehe.gamedev.net

This might help.

kabir
03-27-2002, 11:01 AM
What you should probably do first is describe the sytmptoms... so far all we know is that "it doesn't work" and you're frustrated. Not really enough info to help at all. Before you go posting loads of code just give a quick description of what you're trying to accomplish and where it seems to be failing. This will often be enough to allow someone to make a helpful suggestion or two which might help lead you to the answer.

beachboy1976
03-27-2002, 11:10 AM
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:
glBegin (GL_QUADS);
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



Originally posted by kabir:
What you should probably do first is describe the sytmptoms... so far all we know is that "it doesn't work" and you're frustrated. Not really enough info to help at all. Before you go posting loads of code just give a quick description of what you're trying to accomplish and where it seems to be failing. This will often be enough to allow someone to make a helpful suggestion or two which might help lead you to the answer.

kabir
03-27-2002, 11:21 AM
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.

What do you mean you have not been able to do that? Does the code compile? Do you see anything at all?

kabir
03-27-2002, 11:23 AM
I notice that your data seems to primarily consist of very small x & y variations and a larger z variation. Before drawing each frame do you translate the coordinate system such that what you're drawing will be in view? You may also need to rotate it so that you're not looking at it edge on (as you would be doing with the data as I see it now)

beachboy1976
03-27-2002, 11:25 AM
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.


Originally posted by kabir:
What do you mean you have not been able to do that? Does the code compile? Do you see anything at all?

kabir
03-27-2002, 11:37 AM
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:




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.

beachboy1976
03-27-2002, 11:47 AM
I made some cchanges based on your suggestion.
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:



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.

vTheHero
03-27-2002, 04:10 PM
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

Furrage
03-28-2002, 07:21 AM
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...

glBegin (GL_QUADS);
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);

instead of...

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:
glBegin (GL_QUADS);
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).]