I need help!

Started by
12 comments, last by byrdJR 19 years, 7 months ago
I'm trying to build a sphere using the cylinder approximation algorithm that is in the lighting tutorial of the DX SDK. here's my code:
	FLOAT Y = 1.0f;
	FLOAT Y2 = 0.0f;
	Y2 = (FLOAT)(Y - 0.04);
	DWORD n = 0;
    for( DWORD i=1; i<26; i++ )
    {
		if( i>=13 )
		{ 
			cylinderSize = i - n;
			n = n + 2;
		}
		else 
		{
			
			cylinderSize = i;
			
		}

		if(i>=2)
		{
			Y = Y2;
			Y2 = (FLOAT)(Y - 0.04);
		}

		for( DWORD  j=0; j<50; j++)
		{
			FLOAT theta = (2*D3DX_PI*j)/(50-1);
			
				pVertices[2*j+((i-1)*48)].position = D3DXVECTOR3( cylinderSize*(sinf(theta)/13), Y, cylinderSize*(cosf(theta)/13) );
				
				pVertices[2*j+((i-1)*49)].position = D3DXVECTOR3( cylinderSize*(sinf(theta)/13), Y2, cylinderSize*(cosf(theta)/13) );
				
			
		}
This algorithm is basically trying to draw 25 cylinders stacked on top of each other. The variable cylinderSize is used to determine the size of the cylinder based on which cylinder is currently being drawn. The variable Y is used to determine the height of each cylinder, which I set to 0.04 for each cylinder. Also there are 50 even and 50 odd vertices being drawn for each cylinder. My problem is that i'm not getting a sphere. It is more like the shape of a Hershey's kiss. So I'm not exactly sure what the problem is but I think it could be how the triangles are being drawn between each vertice. I dunno. All I know is that I need someone to point me in the right direction. Thanx! [Edited by - byrdJR on August 18, 2004 11:44:23 PM]
Peace!
Advertisement
Your "cylinder size" is the radius of each cylinder at each step, and you're having it increase and then decrease linearly. That's not the shape a sphere has. You'll need to take the square root of the *normalized* cylinder radius, and rescale. (divide by max so you have something in the range 0..1; take square root; multiply again by the max so it's big enough.)
Thank you for your help, but I don't exactly understand what you mean by normalized. Could you elaborate please?

Thanx again!
Peace!
i think normalize means that it has the length 1 after normalization... if i remember correctly then you have to divide the vector by the magnitude.
read this
Now get down on your hands and knees and start repeating "Open Source Good, M$ Evil", smacking your head against the pavement after each repetition. Once you have completed your training you may change your first name to GNU/, to show that you are free from the slavery of the closed source world. -Michalson
Quote:Original post by BiGF00T
i think normalize means that it has the length 1 after normalization... if i remember correctly then you have to divide the vector by the magnitude.
read this


Yep, each component is divided by the square root of the squared components summed. The resultant vector has a magnitude of 1.
O.K I think I understand.

Thanx! I'm about to see if I can get it to work!
Peace!
I know nobody likes math but i'll try a better explanation for building a sphere.
The parametric equation of a sphere is:
x=R*cos(alpha)*sin(beta)
y=R*sin(alpha)*sin(beta)
z=R*sin(beta), where R is the radius and alpha and beta are angles in radians.
-alpha is from 0 to 2*PI (PI=3.1415f)
-beta is from 0 to PI

***at first you must choose a resolution(the number of steps needed to complete a meridian and a latitude orbit)--this is called biliniar interpolation*****

eg. resoulution=64--at each and every step you add PI/64 to beta and 2*PI/64 to alpha
----------------OpenGL code---------------------------------
du=2*PI/(double)resolution;dv=PI/(double)resolution;glBegin(GL_QUADS);for(i=0;i<resolution;i++){u=i*du;for(j=0;j<resolution;j++)v=j*dv;p1=Get3DPoint(u,v);p2=Get3DPoint(u+du,v);p3=Get3DPoint(u+du,v+dv);p4=Get3DPoint(u,v+dv);glVertex3f(p1.x,p1.y,p1.z);glVertex3f(p2.x,p2.y,p2.z);glVertex3f(p3.x,p3.y,p3.z);glVertex3f(p4.x,p4.y,p4.z)/*Get3DPoint-computes the coordinates by substitution in the parametric equation*/}glEnd();

Happy coding!












To normalize a vector...


v=a.x*i+a.y*j+a.z*k;(our vector)
vlength=sqrt(a.x*a.x+a.y*a.y+a.z*a.z);

vnormalized=(a.x/vlength)*i+(a.y/vlength)*j+(a.z/vlength)*k



i,j,k are unity vectors (length=1) i=(1,0,0)(on x axis),j=(0,1,0)(on y axis),k=(0,0,1)(on z axis)

the normal vector shows the direction of your plane facing.

why the normal should be devide with magnitude ?

coz we just want to have the derection of the plane facing.

no needs big number in normal vector..

if the normal vector normalized,and then we look for the vextor length. i,m sure the length number will be 1.

...
sorry for my bad english
....

you mean the normal vector... that is the upright vector on something. but we were talking about the normalized vector. they are different. good that we had vector math last semester :P
you take every element of your vector and devide it by the magnitude (length of the vector). then you get a vector to the same direction but with the length 1. very useful.
Now get down on your hands and knees and start repeating "Open Source Good, M$ Evil", smacking your head against the pavement after each repetition. Once you have completed your training you may change your first name to GNU/, to show that you are free from the slavery of the closed source world. -Michalson

This topic is closed to new replies.

Advertisement