PDA

View Full Version : what I did wrong with my code?



beachboy1976
03-26-2002, 02:26 PM
Hi,
I have an vertex array to hold more than 300 vertex. I need use those data to draw a mesh on the screen.
I wrote the following code for doing that. However, I got almost nothing on the screen.
I really don't know what is going on?
My vertex array 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
......
}

...

glBegin (GL_QUAD_STRIP);
for (int i = 0; i < 300; i++)
{
int j = i + 1;
int k = i + 2;
int n = i + 3;

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 help

blood.angel
03-26-2002, 02:41 PM
1. Edit: Forget this. I thought you meant GL_QUAD. See my next post.

2. Why is the x negative in glVertex3f(-xj, yj, zj)?
Take all negatives out of glVertex definitions. Afterall the sign should be already in your vertex data.

3. How are you viewing the model?

[This message has been edited by blood.angel (edited 03-26-2002).]

[This message has been edited by blood.angel (edited 03-26-2002).]

blood.angel
03-26-2002, 03:09 PM
For GL_QUAD_STRIP you need to declare your vertexes differently.
You see you declare the first four to represent the first quad, but then only two for the next quad, two for the next, etc

All the order of the quad strip is important too. Take a look at the ordering diagram where the number represents the vertex number called.

0---2---4---6
|xxx|xxx|xxx|
|xxx|xxx|xxx|
|xxx|xxx|xxx|
1---3---5---7

So your code should be:

glColor3f (0.0f, 1.0f, 0.f);
glBegin (GL_QUAD_STRIP);
for (int i = 0; i < 300; i+=2)
{
int j = i + 1;

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;

glVertex3f(xi, yi, zi);
glVertex3f(xj, yj, zj);

}
glEnd();




[This message has been edited by blood.angel (edited 03-26-2002).]

beachboy1976
03-26-2002, 03:34 PM
Hi,
Thank you for your help.
I used your code to draw the mesh. There were only two lines on the screen. I didn't see any quads or mesh at all. Do I need do something else in order to make it work.

Thanks



Originally posted by blood.angel:
For GL_QUAD_STRIP you need to declare your vertexes differently.
You see you declare the first four to represent the first quad, but then only two for the next quad, two for the next, etc

All the order of the quad strip is important too. Take a look at the ordering diagram where the number represents the vertex number called.

0---2---4---6
|xxx|xxx|xxx|
|xxx|xxx|xxx|
|xxx|xxx|xxx|
1---3---5---7

So your code should be:

glColor3f (0.0f, 1.0f, 0.f);
glBegin (GL_QUAD_STRIP);
for (int i = 0; i < 300; i+=2)
{
int j = i + 1;

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;

glVertex3f(xi, yi, zi);
glVertex3f(xj, yj, zj);

}
glEnd();


[This message has been edited by blood.angel (edited 03-26-2002).]

blood.angel
03-26-2002, 04:26 PM
Im sorry I was away for a while.

How are you looking at the model? Please show us your code.

And you may have to reorganise your vertex data in m_TotalPointsArray so it conforms to the quad strip format (check my diagram to see how to order it)

beachboy1976
03-27-2002, 06:23 AM
Here is my code:

void COGLView::OnDraw(CDC* pDC)
{
//Clear the background and Z-buffer
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);

//Clean the modeling matrix (make it equal the unity matrix)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//At first set the light (otherwise it will rotate with the image)

SetLight();

//Change the modeling matrix coefficients in order
glTranslatef(m_xTrans,m_yTrans,m_zTrans);

// to shift
glRotatef (m_AngleX, 1.0f, 0.0f, 0.0f );

// and to rotate
glRotatef (m_AngleY, 0.0f, 1.0f, 0.0f );

//the following vertices coordinates (they are being multiplied by matrix)

glCallList(1);

//Switch back and front buffers (to show what happened)

SwapBuffers(m_hdc);

}

void COGLView: http://www.opengl.org/discussion_boards/ubb/biggrin.gifrawScene()
{

//Commented the following code ...

//Create the new list of OpenGL commands

glNewList(1, GL_COMPILE);

//Set the polygon filling mode

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

m_TotalPointsArray = new Vertex_Data[300];

glBegin (GL_QUAD_STRIP);

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

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;
glColor3f (0.0f, 1.0f, 0.f);
glVertex3f(xi, yi, zi);
glVertex3f(xj, yj, zj);
glEnd();
glEndList();
}

Only thing I want to do here is to create a mesh on the screen and the user can see it. I am not quite understand your question. Can you explain a little more?

Thank you for your help


Originally posted by blood.angel:
Im sorry I was away for a while.

How are you looking at the model? Please show us your code.

And you may have to reorganise your vertex data in m_TotalPointsArray so it conforms to the quad strip format (check my diagram to see how to order it)