PDA

View Full Version : Making a swing in 3d



hoganh
11-25-2003, 02:44 AM
Hi,Im wondering could anybody tell me how to make my swing into 3d or what i need to know.Thanks
#include "gl/glut.h"
#include "stdlib.h"
#include "stdio.h"
#include "math.h"


void draw(void);
void idle(void);
void key(unsigned char k, int x, int y);
void reshape(int width, int height);
void init_drawing(void);
void instructions(void);

int direction;
float angle=0.0;
float rota=0.0;
float rotb=0.0;
bool pause=false;

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("TEST3");
init_drawing();
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutDisplayFunc(draw);
glutInitWindowSize(600,600);
glutReshapeWindow(600,600);
glutMainLoop();
return 0;
}

void drawSwing()
{
glColor3f(1.0, 0.0, 1.0);
glLineWidth(5.0);

// Draw the ropes
glBegin(GL_LINES);
glVertex3d(-20, 30, 0);
glVertex3d(-20, -30, 0);
glVertex3d(20, 30, 0);
glVertex3d(20, -30, 0);
glEnd();

glColor3f(0.0, 1.0, 0.0);

// Draw the sides of the seat
glBegin(GL_QUAD_STRIP);
glVertex3d(20, -30, -10);
glVertex3d(20, -33, -10);
glVertex3d(-20, -30, -10);
glVertex3d(-20, -33, -10);
glVertex3d(-20, -30, 10);
glVertex3d(-20, -33, 10);
glVertex3d(20, -30, 10);
glVertex3d(20, -33, 10);
glVertex3d(20, -30, -10);
glVertex3d(20, -33, -10);
glEnd();

glColor3f(0.0, 0.0, 1.0);

// Draw the top and bottom of the seat
glBegin(GL_QUADS);
glVertex3d(-20, -33, -10);
glVertex3d(20, -33, -10);
glVertex3d(20, -33, 10);
glVertex3d(-20, -33, 10);

glVertex3d(-20, -30, -10);
glVertex3d(20, -30, -10);
glVertex3d(20, -30, 10);
glVertex3d(-20, -30, 10);
glEnd();
}

void drawBar()
{
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.0);

// Bottom
glVertex3d(-50, 30, -1);
glVertex3d(50, 30, -1);
glVertex3d(50, 30, 1);
glVertex3d(-50, 30, 1);
// Top
glVertex3d(-50, 33, -1);
glVertex3d(50, 33, -1);
glVertex3d(50, 33, 1);
glVertex3d(-50, 33, 1);

glColor3f(1.0, 0.65, 0.0);

// Front
glVertex3d(-50, 33, 1);
glVertex3d(-50, 30, 1);
glVertex3d(50, 30, 1);
glVertex3d(50, 33, 1);
// Back
glVertex3d(50, 33, -1);
glVertex3d(50, 30, -1);
glVertex3d(-50, 30, -1);
glVertex3d(-50, 33, -1);

glColor3f(1.0, 0.0, 0.0);

// Left
glVertex3d(-50, 33, -1);
glVertex3d(-50, 30, -1);
glVertex3d(-50, 30, 1);
glVertex3d(-50, 33, 1);
// Right
glVertex3d(50, 33, 1);
glVertex3d(50, 30, 1);
glVertex3d(50, 30, -1);
glVertex3d(50, 33, -1);
glEnd();



}

void draw(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Rotate the bar and swing
glRotatef(60.0, 0.0, 1.0, 0.0);

// Draw the bar
drawBar();

// Translate and rotate the swing to make it `swing'
glTranslatef(0.0, 30.0, 0.0);
glRotatef(angle, 1.0, 0.0, 0.0);
glTranslatef(0.0, -30.0, 0.0);

// Draw the swing
drawSwing();


//glPushMatrix();
//glTranslatef(1.4,0.5,-10.0);
// glRotatef(rota,0,0,1);
// drawSquare();

// glTranslatef(0.0,-1.5,0.0);
// glRotatef(rotb,0,0,1);
// glTranslatef(0.0,-1.5,0.0);
// drawSquare();

glFlush();
glutSwapBuffers();

// Make the it swing back or forth depending upon direction
if (direction == 1)
angle++;
else
angle--;

// Change the direction if 40 or -40 is reached
if (angle == -40.0) {
direction = 1;
}
if (angle == 40.0) {
direction = 0;
}

}

void idle(void)
{
if(pause==false)
{
}
}

void key(unsigned char k, int x, int y)
{
switch(k)
{
case 27: //27 is the ASCII code for the ESCAPE key
exit(0);
break;
case 'a': rota +=1;draw();break;
case 'b': rotb +=1;draw();break;
}
}

void reshape(int width, int height)
{
glViewport(0,0,width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0,50.0, -50.0, 50.0, -150.0, 150.0);
// gluPerspective(45.0, (float) width / (float) height, 1.0, 100.0);
draw();
}

void init_drawing(void)
{
glShadeModel(GL_SMOOTH);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);

glDisable(GL_LIGHTING);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
}

nexusone
11-25-2003, 05:53 AM
I don't have time to compile your code, but from the looks you need to use glPushMatrix/glPopMatrix.

I am guessing your problem is that the whole swing moves and you just want to swing to move.

Would look something like this:

// Set view

// Start swing set
glPushMatrix();
glTranslate(....) // location of swing set in world
glRotate(....); // Rotate whole swing set
// draw bars of the swing set
drawbars();

// Draw swing
glPushMatrix();
glRotate(...) // Rotate swing
drawswing()
// End swing
glPopMatrix();
// End Swing set
glPopMatrix();