PDA

View Full Version : rotating a cone

lara
11-13-2002, 09:32 AM
All I want to do, is rotate the cone, but keep the pointed end of the cone fixed...
well lets see how I can explain this:

basically the cone needs to be fixed at the "pointed end" to a point in space. I have to move the "open end of the cone" (the mouth , if you will), up and down.

the following is the shape of the cone at start:
glRotatef(270, 0.0, 1.0, 0.0);
glutWireCone(0.5,1.0,8,8);

i hope someone gets the idea.
Thanks,
Lara

nexusone
11-13-2002, 05:44 PM
Use gltranslate to move it up and down.
Use translate then rotate.

Originally posted by lara:
All I want to do, is rotate the cone, but keep the pointed end of the cone fixed...
well lets see how I can explain this:

basically the cone needs to be fixed at the "pointed end" to a point in space. I have to move the "open end of the cone" (the mouth , if you will), up and down.

the following is the shape of the cone at start:
glRotatef(270, 0.0, 1.0, 0.0);
glutWireCone(0.5,1.0,8,8);

i hope someone gets the idea.
Thanks,
Lara

lara
11-15-2002, 05:50 AM
that is exactly what i do...
but still the cone seems to repositon as even thought the center of the cone remains the same, it tilts(rotates), and thus the pointed end seems to have moved too...
is there anyway i can avoid that:

[This message has been edited by lara (edited 11-16-2002).]

nexusone
11-15-2002, 07:40 AM
I am not clear but you want the cone to rotate on its axis at an angle, sort of like the earth?

or is it to rotate the cone off axis, like the bottom edge down, and cone tip pointing out, like a dial?

Try switching the order of rotation:

glRotatef(angle, 1.0, 0.0, 0.0);
glRotatef(290, 0.0, 1.0, 0.0);

to

glRotatef(290, 0.0, 1.0, 0.0);
glRotatef(angle, 1.0, 0.0, 0.0);

Originally posted by lara:
that is exactly what i do...
but still the cone seems to repositon as even thought the center of the cone remains the same, it tilts(rotates), and thus the pointed end seems to have moved too...
is there anyway i can avoid that:

to make things better here is the code:
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
// rotAngle is used in rotation
int angle =0;

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
gluLookAt (0.0, 0, 3, 0.0, 0.0, 0.0, 0.0, 1, 0.0);
glPushMatrix();

glTranslatef(0,0,0);
glRotatef(angle, 1.0, 0.0, 0.0);
glRotatef(290, 0.0, 1.0, 0.0);
glutWireCone(0.5,0.5,8,8);

glPopMatrix();
glFlush();
_sleep(100);
glutSwapBuffers();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glFrustum (-1.0, 1.0, -2.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}
// handles keyboard events
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'u':
case 'U':
angle += 45;
glutPostRedisplay();
break;
case 'd':
case 'D':
angle -= 45;
glutPostRedisplay();
break;
case 'S':
case 's':
break;
case 27: /* Escape Key */
exit(0); // closes window
break;
default:
break;
}
}

void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (300, 300);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}

lara
11-15-2002, 07:47 AM
Well like neither?
It needs to act like a hinge..how can i explain this:

i want it to bend up and down like a lamp attached to some entity with the 'pointed end ' of the cone..

so i need to be able to press keys so that the i keep it attached to the same point, and bend it up and down.

If i do what i am..it seems to shift a little on the pointed end too...i was thinking of ways to avoid that.

Lara

[This message has been edited by lara (edited 11-15-2002).]

nexusone
11-15-2002, 08:47 AM
Cone axis looks something like this:
Given you have a radius of 0.5 and hight of 0.5, which translates to a base being 1 unit wide.

0.5 Z-axis is top
/\
/__\__ 0,0,0 is at center of cone bottom

Give z is a positive number the cone is pointing at the screen when first drawn.

to rotate like a hing we must change the origin in which we rotate.

glTranslatef(0.5, 0.0, 0.0);// note this makes the bottom x side the origin, make z = -0.5 and the top tip becomes the origin
glScalef(2.0, 2.0, 2.0);
glutWireCone(0.5, 0.5, 8, 8);

Remember in openGL the last command is acted on first.

We add the glRotatef before the translate, to rotate on our new origin for the cone.

Also check out my usage of rotation in my ball demo on my web site or the clock demo.
http://www.angelfire.com/linux/nexusone/

Originally posted by lara:
Well like neither?
It needs to act like a hinge..how can i explain this:

i want it to bend up and down like a lamp attached to some entity with the 'pointed end ' of the cone..

so i need to be able to press keys so that the i keep it attached to the same point, and bend it up and down.

If i do what i am..it seems to shift a little on the pointed end too...i was thinking of ways to avoid that.

Lara

[This message has been edited by lara (edited 11-15-2002).]

[This message has been edited by nexusone (edited 11-15-2002).]

[This message has been edited by nexusone (edited 11-15-2002).]

[This message has been edited by nexusone (edited 11-15-2002).]

lara
11-15-2002, 08:57 AM
I am sorry..but I could not follow you at all this time...would you please reword it, so that I can understand better,
Thanks,
Lara

nexusone
11-15-2002, 09:05 AM
In simple terms...this will make the cone tip the rotation point.

glRotatef(angle, 1.0, 0.0, 0.0);
glTranslatef( 0.0, 0.0, -0.5);
glutWireCone(0.5, 0.5, 8, 8,);

http://www.opengl.org/discussion_boards/ubb/smile.gif

Originally posted by lara:
I am sorry..but I could not follow you at all this time...would you please reword it, so that I can understand better,
Thanks,
Lara

lara
11-15-2002, 11:25 AM
I like the clock example ...very interesting.

But, I still have a few queries

>to rotate like a hing we must change the >origin in which we rotate.
>
>glTranslatef(0.5, 0.0, 0.0);// note this makes the bottom x side the origin, make z = -0.5 and the top tip becomes the origin
>glutWireCone(0.5, 0.5, 8, 8);

so if I have my cone positioned like:
glRotatef(290, 0.0, 1.0, 0.0);
glutWireCone(0.2,0.3,8,8);

Then how Can I change the origin of rotation to be the tip of the cone... If i use a gltranslate command, it translates the cone to a new place...

Thanks

nexusone
11-15-2002, 11:45 AM
The order in which you call the rotate and translate commands are very important!!!!

Did you put them in the order of my last post?

Originally posted by lara:
I like the clock example ...very interesting.

But, I still have a few queries

>to rotate like a hing we must change the >origin in which we rotate.
>
>glTranslatef(0.5, 0.0, 0.0);// note this makes the bottom x side the origin, make z = -0.5 and the top tip becomes the origin
>glutWireCone(0.5, 0.5, 8, 8);

so if I have my cone positioned like:
glRotatef(290, 0.0, 1.0, 0.0);
glutWireCone(0.2,0.3,8,8);

Then how Can I change the origin of rotation to be the tip of the cone... If i use a gltranslate command, it translates the cone to a new place...

Thanks

[This message has been edited by nexusone (edited 11-15-2002).]

lara
11-15-2002, 11:49 AM
glRotatef(angle, 1.0, 0.0, 0.0);
glTranslatef( 0.0, 0.0, -0.5);
glutWireCone(0.5, 0.5, 8, 8,);
works absolutely fine

but rather than having my cone as glutWireCone(0.5, 0.5, 8, 8,);

I have my cone initially as glRotatef(290, 0.0, 1.0, 0.0);
glutWireCone(0.2,0.3,8,8);
I need to bend it from there ..and leave it facing just the way it is...

nexusone
11-15-2002, 11:50 AM
Here is some sample program... cone rotating around tip.

// Glutwindow.c
// By Eric Stringer 2002
// Simple examples of OpenGL and Glut usage.
// Keyboard input
// 'v' = view ortho/perspective
// 'l' = lighting on/off

//#include <windows.h> // This header file will be needed for some windows compilers
//#include <GL/gl.h> // gl.h and glu.h also maybe needed for some compilers
//#include <GL/glu.h>
#include <GL/glut.h> // glut (gl utility toolkit) basic windows functions, keyboard, mouse.
#include <stdio.h> // standard (I/O library)
#include <stdlib.h> // standard library (set of standard C functions
#include <math.h> // Math library (Higher math functions )

// lighting
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightPosition[]= { 5.0f, 25.0f, 15.0f, 1.0f };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };

static int view_state = 0, light_state = 0;

int spin;

// I use this to put text on the screen
void Sprint( int x, int y, char *st)
{
int l,i;

l=strlen( st ); // see how many characters are in text string.
glRasterPos2i( x, y); // location to start printing text
for( i=0; i < l; i++) // loop until i is greater then l
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]); // Print a character on the screen
}

}

// This creates the spinning of the cube.
static void TimeEvent(int te)
{

spin++; // increase cube rotation by 1
if (spin > 360) spin = 0; // if over 360 degress, start back at zero.
glutPostRedisplay(); // Update screen with new rotation data
glutTimerFunc( 100, TimeEvent, 1); // Reset our timmer.
}

// Setup our Opengl world, called once at startup.
void init(void)
{

glClearColor (0.0, 0.0, 0.0, 0.0); // When screen cleared, use black.
glShadeModel (GL_SMOOTH); // How the object color will be rendered smooth or flat
glEnable(GL_DEPTH_TEST); // Check depth when rendering
// Lighting is added to scene
glLightfv(GL_LIGHT1 ,GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1 ,GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1 ,GL_POSITION, LightPosition);
glEnable(GL_LIGHTING); // Turn on lighting
glEnable(GL_LIGHT1); // Turn on light 1

}

// Draw our world
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen

glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work
glOrtho(-8.0, 8.0, -8.0, 8.0, 0.0, 30.0); // Setup an Ortho view
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
glLoadIdentity(); // Clear the model matrix

// Setup view, and print view state on screen
if (view_state == 1)
{
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2, 4, "Perspective view");
glMatrixMode (GL_PROJECTION);
gluPerspective(60, 1, 1, 30);
glMatrixMode(GL_MODELVIEW);
}else
{
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2, 4, "Ortho view");
}

// Lighting on/off
if (light_state == 1)
{
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
}else
{
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
}

gluLookAt( 0, 0, 20, 0, 0, 0, 0, 1, 0);

glColor3f( 0.0, 0.0, 1.0); // Cube color
//glRotatef( 45, 1.0, 1.0, 1.0); // rotate cube
glRotatef( spin++, 0.0, 1.0, 0.0); // spin cube
glTranslatef(0.0,0.0, -10.0);
glutWireCone(5.0, 10.0, 8, 8); // Draw a cube

glutSwapBuffers();
}

// This is called when the window has been resized.
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
}

void keyboard (unsigned char key, int x, int y)
{
switch (key)
{

case 'v':
case 'V':
view_state = abs(view_state -1);
break;
case 'l':
case 'L':
light_state = abs(light_state -1);
break;
case 27:
exit(0); // exit program when [ESC] key presseed
break;
default:
break;
}

}

// Main program
int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (10, 10);
glutCreateWindow (argv[0]);
glutSetWindowTitle("GlutWindow");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutTimerFunc( 10, TimeEvent, 1);
glutMainLoop();
return 0;
}

nexusone
11-15-2002, 12:10 PM
I still not clear, do you want it to just spin at the angle of 290 rotated on the y axis?

To make it spin like the earth, do this

glRotatef(290, 0.0, 1.0, 0.0);
glTranslatef( 0.0, 0.0, -0.5);
glRotatef(spin, 0.0, 0.0, 1.0);
glutWireCone( 0.5, 0.5, 8, 8);

Originally posted by lara:
glRotatef(angle, 1.0, 0.0, 0.0);
glTranslatef( 0.0, 0.0, -0.5);
glutWireCone(0.5, 0.5, 8, 8,);
works absolutely fine

but rather than having my cone as glutWireCone(0.5, 0.5, 8, 8,);

I have my cone initially as glRotatef(290, 0.0, 1.0, 0.0);
glutWireCone(0.2,0.3,8,8);
I need to bend it from there ..and leave it facing just the way it is...

DennisMV
11-15-2002, 10:11 PM
I am new to OpenGL, but I know a lot about matrix algebra.

You might already know this, and the problem might be something else, but see if this helps:
1. Translate the Cone's tip to the origin. For this you will need to know the coordinates of the top of the cone.
2. Rotate the cone any which way you want. It will rotate any which way, but the tip will stay at the origin. That is, as long as you rotate around the origin or around any axis. You may apply several rotations here.
3. Undo the translation you did in number 1. That is, inverse translate your cone to where it was before.
4. You're done --> Display the cone.

Now, OpenGL might have some matrix operations that are more complex, like rotate about a point, for example, or a line, where the matrix might combine all the operations above.

Dennis

[This message has been edited by DennisMV (edited 11-15-2002).]

[This message has been edited by DennisMV (edited 11-15-2002).]

lara
11-16-2002, 09:52 AM
A cone can be used to look like many things:
an ice cream cone(opening facing up), a hat(opening facing down), it can lie flat facing the left side or it can lie facing the right side. And so many others ways.

The cone represented as follows:
glutWireCone(0.5, 0.5, 8, 8);
lies facing the negative z-axis and the tipe of the cone is at the point (0,0,0.5);

in order to make it behave like a hing, we tranlate the tip to the origin and then do the rotation:
glRotatef(angle, 1.0, 0.0, 0.0);
glTranslatef( 0.0, 0.0, -0.5);
glutWireCone(0.5, 0.5, 8, 8);

The way I want the cone to look is as folows:
glRotatef(290, 0.0, 1.0, 0.0);
glutWireCone(0.2,0.3,8,8);
This shows the cone lying facing the right side and slightly pointing back(negative z-axis).

Now assume that after the above position, I do not want to move the tip from the point in space where it is (in the above case the tip is at point(-0.3,0,0);
Nove leaving the tip there I want the cone to slightly face up and slightly face down, thus behave as a hing.

If I apply the math etc, and translate the cone tip to the origin (by saying gltranslate(0.3,0,0) ) and then do
glRotatef(angle,1,0,0) and then translate the tip back to the original point, it does not behave as a hing but moves slightly form its original position.

Now, I need to figure out why, does the tip move from its place:

following is the code put together, of what I am doing:
If you try the example you will get the exact idea of what I want to do. But not move the tip(in the example the tip shifts slightly)

[This message has been edited by lara (edited 11-17-2002).]

DennisMV
11-16-2002, 10:13 PM
What if you do this ?
glTranslatef(-0.3,0,0);
glRotatef (angle,1.0,0.0,0.0);
glRotatef(290, 0.0, 1.0, 0.0);
glTranslatef(0.3,0,0);
glutWireCone(0.2,0.3,8,8);

For the cone's tip to stay at one place all the rotations must be done in between the translates.
The glRotatef(290, 0.0, 1.0, 0.0); was after the translate, so it would move the tip while rotating.

humm, it's hard to see where the cone is exactly, there are no axis.. Again, I'm new to OpenGL, I hope I said something useful http://www.opengl.org/discussion_boards/ubb/smile.gif

Dennis

nexusone
11-17-2002, 03:43 AM

glTranslatef(-0.3,0,0);
glRotatef (angle,0.0,0.0,1.0);

glRotatef(270, 0.0, 1.0, 0.0);
glTranslatef(0,0,-0.3);
glutWireCone(0.2,0.3,8,8);

lara
11-17-2002, 08:39 AM
Exactly what I wanted to do http://www.opengl.org/discussion_boards/ubb/smile.gif

Thank you for all your help and patience,
Lara