PDA

View Full Version : f1 segment



opengluser
10-25-2002, 01:51 PM
im trying with one of the tutorial on the OpenGL book, and trying to modify it, but how come when make the segment of f1, it give me a big cube. and is outsite of the elbow cube

void display (void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix ();
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix ();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix ();

glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix ();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix ();

glClear (GL_COLOR_BUFFER_BIT);
glTranslatef (0.0, 1.0, 0.0);
glRotatef ((GLfloat) f1, 0.0, 1.0, 0.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix ();
glScalef (1.0, 0.2, 0.5);
glutWireCube (0.25);
glPopMatrix ();

glPopMatrix ();
glutSwapBuffers ();
}

nexusone
10-25-2002, 03:20 PM
Here a few things I see:

1. glClear.... only call once at the start of the display routine.

2. I think your problem is in how you have setup your glPush/pop matrix.

3. Explain more about what you trying to do and can give you more of an idea of what to do.


Originally posted by opengluser:
im trying with one of the tutorial on the OpenGL book, and trying to modify it, but how come when make the segment of f1, it give me a big cube. and is outsite of the elbow cube

void display (void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix ();
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix ();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix ();

glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix ();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix ();

glClear (GL_COLOR_BUFFER_BIT);
glTranslatef (0.0, 1.0, 0.0);
glRotatef ((GLfloat) f1, 0.0, 1.0, 0.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix ();
glScalef (1.0, 0.2, 0.5);
glutWireCube (0.25);
glPopMatrix ();

glPopMatrix ();
glutSwapBuffers ();
}

opengluser
10-25-2002, 03:31 PM
actually im trying to do is a robot arm with fingers, f1 will be the fingers on the wrist, which is end of the elbow point.


Originally posted by nexusone:
Here a few things I see:

1. glClear.... only call once at the start of the display routine.

2. I think your problem is in how you have setup your glPush/pop matrix.

3. Explain more about what you trying to do and can give you more of an idea of what to do.

nexusone
10-25-2002, 04:14 PM
Should look something like this:


// hand
glPushMatrix(); // Save matrix to stack
glTranslatef(.....); // location of hand
glRotatef(......); // Rotation of whole hand
draw_base_of_hand();
// Draw first finger
glPushMatrix(); // Save matrix to stack
glTranslatef(.....); location of finger on hand.
glRotatef(......); rotation of finger.
draw_finger_seg1();
// Draw next finger segment.
glPushMatrix();
glTranslatef(.....);
glRotatef(......);
draw_finger_seg2();
glPopMatrix();
// If you wanted another finger segment would go here..
glPopMatrix();

// Draw Second finger would repeat for each finger.
glPushMatrix(); // Save matrix to stack
glTranslatef(.....); location of finger on hand.
glRotatef(......); rotation of finger.
draw_finger_seg1();
// Draw next finger segment.
glPushMatrix();
glTranslatef(.....);
glRotatef(......);
draw_finger_seg2();
glPopMatrix();
// If you wanted another finger segment would go here..
glPopMatrix();

glPopMatrix();


Note unless a glRotate/Translate/scale is between a glPush/PopMatrix, it will effect everything after it.

glTranslatef(.....)
// 1
glPushMatrix()
glTranslatef(.....)
glPopMatrix()
// 2
glPushMatrix()
glTranslatef(.....)
glPopMatrix()

note that both 1 and 2 are both effected by the first translate, but because of the use of push/pop matrix, 1 does not effect 2.


Originally posted by opengluser:
actually im trying to do is a robot arm with fingers, f1 will be the fingers on the wrist, which is end of the elbow point.

opengluser
10-25-2002, 05:53 PM
humm..actually im still confuse with it...below is my initial code. so now i need to add the fingers segment ast the end of the elbow cube.

coding :
#include <Gl/glut.h>
#include <stdlib.h>

static int shoulder = -35, elbow = 30;
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}

void display (void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);


glPushMatrix ();
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 0.1);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix ();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix ();

glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix ();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix ();
glPopMatrix ();
glutSwapBuffers ();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.0, 0.0, -5.0);
}

void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 's':
shoulder = (shoulder + 5) % 360;
glutPostRedisplay ();
break;

case 'S':
shoulder = (shoulder - 5) % 360;
glutPostRedisplay ();
break;

case 'e':
elbow = (elbow + 5) % 360;
glutPostRedisplay ();
break;

case 'E':
elbow = (elbow - 5) % 360;
glutPostRedisplay ();
break;
default:
break;
}
}
int main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutMainLoop ();
return 0;
}


can you plase explain why you use the draw_base_of_hand(); & draw_finger_seg1();
thanks.