#### Archived

This topic is now archived and is closed to further replies.

# What do i do with this vector class then?

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

## Recommended Posts

So i have a vector class with + - / etc.. overloaded and crossproduct dot product and normalize functions. Now what do I do with it? With the following code for drawing a pyramid, how do i use the vector class to define a normal for lighting and passed to glNormal?

ogl.Begin(GL_TRIANGLES);

texture.TexCoord2f(0.0f,1.0f);
ogl.Vertex3f(0.0f,1.0f,0.0f);	//top

texture.TexCoord2f(0.0f,0.0f);
ogl.Vertex3f(-1.0f,-1.0f,1.0f);	//left

texture.TexCoord2f(1.0f,0.0f);
ogl.Vertex3f(1.0f,-1.0f,1.0f);	//right

texture.TexCoord2f(0.0f,1.0f);
ogl.Vertex3f(0.0f,1.0f,0.0f);	//top

texture.TexCoord2f(0.0f,0.0f);
ogl.Vertex3f(1.0f,-1.0f,1.0f);	//left

texture.TexCoord2f(1.0f,0.0f);
ogl.Vertex3f(1.0f,-1.0f,-1.0f);	//right

texture.TexCoord2f(0.0f,1.0f);
ogl.Vertex3f(0.0f,1.0f,0.0f);	//top

texture.TexCoord2f(0.0f,0.0f);
ogl.Vertex3f(1.0f,-1.0f,-1.0f);	//left

texture.TexCoord2f(1.0f,0.0f);
ogl.Vertex3f(-1.0f,-1.0f,-1.0f);	//right

texture.TexCoord2f(0.0f,1.0f);
ogl.Vertex3f(0.0f,1.0f,0.0f);	//top

texture.TexCoord2f(0.0f,0.0f);
ogl.Vertex3f(-1.0f,-1.0f,-1.0f);//left

texture.TexCoord2f(1.0f,0.0f);
ogl.Vertex3f(-1.0f,-1.0f,1.0f);	//right

ogl.End();


##### Share on other sites
What you could do is make an instance of your vector class and define its x,y,z positions before you draw Vertices like so
VECTOR normal;

ogl.Begin(GL_TRIANGLES);
texture.TexCoord2f(0.0f,1.0f);
ogl.Vertex3f(0.0f,1.0f,0.0f); //top
normal.Set(0.0f,1.0f,0.0f);
ogl.Normal3f(normal.x,normal.y,normal.z);

if that dont work Sorry;

##### Share on other sites
quote:
Original post by RedFTiger
What you could do is make an instance of your vector class and define its x,y,z positions before you draw Vertices like so
VECTOR normal;

ogl.Begin(GL_TRIANGLES);
texture.TexCoord2f(0.0f,1.0f);
ogl.Vertex3f(0.0f,1.0f,0.0f); //top
normal.Set(0.0f,1.0f,0.0f);
ogl.Normal3f(normal.x,normal.y,normal.z);

if that dont work Sorry;

But I thought to get the normal you have to find the cross product of two vectors and normalize it? So in this case the cross product of the vector left to top and the vector left to right.

##### Share on other sites
quote:
Original post by RedFTiger
What you could do is make an instance of your vector class and define its x,y,z positions before you draw Vertices like so
VECTOR normal;

ogl.Begin(GL_TRIANGLES);
texture.TexCoord2f(0.0f,1.0f);
ogl.Vertex3f(0.0f,1.0f,0.0f); //top
normal.Set(0.0f,1.0f,0.0f);
ogl.Normal3f(normal.x,normal.y,normal.z);

if that dont work Sorry;

But I thought to get the normal you have to find the cross product of two vectors and normalize it? So in this case the cross product of the vector left to top and the vector left to right.

##### Share on other sites
So no one knows what to do with a vector class?

##### Share on other sites
If you know enough about vectors to write a vector class, surely you know how to use it?

##### Share on other sites
quote:
Original post by Anonymous Poster
If you know enough about vectors to write a vector class, surely you know how to use it?

that''s the thing I know what vectors are having done it in maths, but what i''m confused about is how this translates to an OpenGL program. I know they''re used for lighting, collision detection etc.. But for example you specify the normal of a triangle with glNormal.

Problem 1: It takes a value from -1.0f to 1.0f. When I find the resulting x,y,z of the normal what do I do if it''s bigger then 1?

Problem 2: A vector v in my class has coordinates x,y,z. A vertex in the triangle goes from coordinates x1,y1,z1 to x2,y2,z2 so is the vector x2-x1,y2-y1 etc..?

Problem 3: Then to actually find the normal get the cross-product of the two sides of the triangle. So I assume I do as stated in problem 2 and use x2-x1 etc.. to make a vector a and a vector b for the other side, then use the crossProduct() function and put the resulting vector into glNormal?

That''s the problem I HAVE NO IDEA all the tutorials I could find explain the maths or how to make a vector class which I understand perfectly but not what to do with this class in actual programs. It''s like knowing how each part of a computer works but not knowing how to put it together.

So can someone just post an example please?

##### Share on other sites
Let''s say you have a triangle defined by A, B and C.

Vector3D A( ... );
Vector3D B( ... );
Vector3D C( ... );

First, let''s compute AB (the AB vector, not the distance) and BC.

Vector3D AB = B - A;
Vector3D BC = C - B;

Now, let''s compute the normal and normalize it :

Vector3D N = AB.cross( BC);
N.normalize();

To draw the triangle, you do :

  glBegin( GL_TRIANGLES);   glNormal3f( N.x, N.y, N.z);   glVertex3f( A.x, A.y, A.z);   glVertex3f( B.x, B.y, B.z);   glVertex3f( C.x, C.y, C.z);GLEnd();

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 13
• 86
• 11
• 10
• ### Forum Statistics

• Total Topics
632972
• Total Posts
3009613
• ### Who's Online (See full list)

There are no registered users currently online

×