PDA

View Full Version : One alike object coming out from parent object



queries
11-21-2004, 11:41 PM
Hi....
I am have created two cubes(one parent one child) The child cube gets created when I do a mouse click.
I want that this child cube when created should give me an effect like as if its coming out from within the parent cube.(Like coming out from the base).
Can anybody suggest me what should I do to give this effect?

Thanks.
Queries

forth
11-22-2004, 04:52 AM
First I must tell you, Queries, that your English is not very good. But so is mine (I am Ukrainian) and I hope we will understand each other.
About cubes. It depends on the quality of animation you want to have.
If your application is not very havy at graphics you should simply enable z-buffer and just render child cube first inside of the parent (the child will not be seen, if it is smaller) and then slowly move it outside. Try next code. (The code is very messy, but it shows the idea).

#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>

struct cube
{
bool active;
float x[3];
float v[3];
float size[3];
float c[3];
float state;
};

/* Cubes positions, sizes an colors */
cube parent = {true, {0, 0, 0}, {0, 0, 0}, {2, 2, 2}, {0.9, 0.8, 0.3}};
cube child = {false, {0, 0, 0}, {0.01, 0.005, 0.005}, {1, 1, 1}, {1, 1, 1}};

/* Camera prefs */
float rotate_x = 0;
float rotate_y = 0;
float shift_x = 0;
float shift_y = 0;

/* Simple cube drawing func */
void draw_cube(const cube & c)
{
if(!c.active)return;
glPushMatrix();
glTranslatef(c.x[0], c.x[1], c.x[2]);
glBegin(GL_QUADS);
glColor3fv(c.c);

glNormal3f(0, 0, 1);
glVertex3f(c.size[0], +c.size[1], +c.size[2]); glVertex3f(-c.size[0], +c.size[1], +c.size[2]);
glVertex3f(-c.size[0], -c.size[1], +c.size[2]); glVertex3f(+c.size[0], -c.size[1], +c.size[2]);

glNormal3f(0, 0, -1);
glVertex3f(+c.size[0], +c.size[1], -c.size[2]); glVertex3f(+c.size[0], -c.size[1], -c.size[2]);
glVertex3f(-c.size[0], -c.size[1], -c.size[2]); glVertex3f(-c.size[0], +c.size[1], -c.size[2]);

glNormal3f(1, 0, 0);
glVertex3f(+c.size[0], +c.size[1], +c.size[2]); glVertex3f(+c.size[0], -c.size[1], +c.size[2]);
glVertex3f(+c.size[0], -c.size[1], -c.size[2]); glVertex3f(+c.size[0], +c.size[1], -c.size[2]);

glNormal3f(-1, 0, 0);
glVertex3f(-c.size[0], +c.size[1], +c.size[2]); glVertex3f(-c.size[0], +c.size[1], -c.size[2]);
glVertex3f(-c.size[0], -c.size[1], -c.size[2]); glVertex3f(-c.size[0], -c.size[1], +c.size[2]);

glNormal3f(0, 1, 0);
glVertex3f(+c.size[0], +c.size[1], +c.size[2]); glVertex3f(+c.size[0], +c.size[1], -c.size[2]);
glVertex3f(-c.size[0], +c.size[1], -c.size[2]); glVertex3f(-c.size[0], +c.size[1], +c.size[2]);

glNormal3f(0, -1, 0);
glVertex3f(+c.size[0], -c.size[1], +c.size[2]); glVertex3f(-c.size[0], -c.size[1], +c.size[2]);
glVertex3f(-c.size[0], -c.size[1], -c.size[2]); glVertex3f(+c.size[0], -c.size[1], -c.size[2]);

glEnd();
glPopMatrix();
}

/* Function plotting func */
void draw()
{
draw_cube(parent);
draw_cube(child);
};

/* Redrawing func */
void redraw(void)
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(shift_x, shift_y, 0);
glRotatef(rotate_x, 0, 1, 0);
glRotatef(rotate_y, 1, 0, 0);
draw();

glutSwapBuffers();
};

/* Idle proc. Redisplays, if called. */
void idle(void)
{
glutPostRedisplay();
};

/* Timer func */
void timer(int value)
{
if(child.active)
{
child.state += 0.08;
child.size[0] = 1 - 0.1 * sin(child.state);
child.size[1] = 1 + 0.1 * sin(child.state);
child.size[2] = 1 - 0.1 * sin(child.state);
child.x[0] += child.v[0];
child.x[1] += child.v[1];
child.x[2] += child.v[2];
};
parent.state += 0.07;
parent.size[0] = 2 + 0.2 * sin(parent.state);
parent.size[1] = 2 - 0.2 * sin(parent.state);
parent.size[2] = 2 + 0.2 * sin(parent.state);

parent.x[0] += parent.v[0];
parent.x[1] += parent.v[1];
parent.x[2] += parent.v[2];

glutTimerFunc(value, timer, value);
};

/* Key press processing */
void key(unsigned char c, int x, int y)
{
if(c == 27) exit(0);
if(c == 32)
{
child.active = true;
child.x[0] = 0;
child.x[1] = 0;
child.x[2] = 0;
child.v[0] = (float)(rand() % 4000) / 100000 + 0.003;
child.v[1] = (float)(rand() % 4000) / 100000 + 0.003;
child.v[2] = (float)(rand() % 4000) / 100000 + 0.003;
if(rand() % 2) child.v[0] = -child.v[0];
if(rand() % 2) child.v[1] = -child.v[1];
if(rand() % 2) child.v[2] = -child.v[2];
child.state = 0;
}
};

/* Window reashape */
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, (float)w/h, 20, 100);
gluLookAt(0.0, 0.0, 60.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.);
glMatrixMode(GL_MODELVIEW);
};

/* Current mouse state */
int mbutton = -1;
int mx = 0;
int my = 0;

/* Mouse func */
void mouse(int button, int state, int x, int y)
{
mbutton = button;
mx = x;
my = y;
}

/* Mouse motion func */
void motion(int x, int y)
{
if(mbutton == GLUT_LEFT_BUTTON)
{
rotate_x += (float) (x-mx) * 2;
rotate_y += (float) (y-my) * 2;
};

if(mbutton == GLUT_RIGHT_BUTTON)
{
shift_x += (float) (x-mx) / 10;
shift_y -= (float) (y-my) / 10;
};

mx = x;
my = y;
};

/* Main function */
int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Cube Maker - Press Space");

/* Register GLUT callbacks. */
glutDisplayFunc(redraw);
glutKeyboardFunc(key);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutTimerFunc(20, timer, 20);

/* Init the GL state */
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glLineWidth(1.0);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 1.0);
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);

float light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
float light_position[] = {-1.0, -1.0, 1.0, 0.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

/* Main loop */
glutMainLoop();
return 0;
}If you want something more advanced, then you can not use simple cubes: try the same technology but with something like a box, whitch can be opened (you'll have to render it in half-opened state).
It all depends on what do you need it for.