PDA

View Full Version : ufo



prateek
05-04-2015, 11:46 AM
#include <stdlib.h>
#include <stdio.h>
#ifndef WIN32
#include <unistd.h>
#else
#define random rand
#endif
#include <math.h>
#include <GL/glut.h>


#ifndef M_PI
#define M_PI 3.14159265
#endif
#ifndef M_PI_2
#define M_PI_2 1.57079632
#endif

GLboolean moving = GL_FALSE;

#define MAX_UFO 15
#define ADD_UFO 1
#define REMOVE_UFO 2
#define MOTION_ON 3
#define MOTION_OFF 4
#define QUIT 5


struct {
float speed; /* zero speed means not flying */
float theta;
float x, y, z, angle;
} UFO[MAX_UFO];


void draw(void)
{
int i;
glClear(GL_DEPTH_BUFFER_BIT);
/* paint black to smooth shaded polygon for background */
glDisable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0,0.0);
glVertex3f(-20, 20, -19);
glVertex3f(20, 20, -19);
glColor3f(0.0, 0.5, 1.0);
glVertex3f(20, -20, -19);
glVertex3f(-20, -20, -19);
glEnd();
glEnable(GL_DEPTH_TEST); /* paint UFO */
glShadeModel(GL_FLAT);
for (i = 0; i < MAX_UFO; i++)
if (UFO[i].speed != 0.0) /*ufo in movement*/
{
glPushMatrix();
glTranslatef(UFO[i].x, UFO[i].y, UFO[i].z);
glRotatef(290.0, 1.0, 0.0, 0.0);
glRotatef(UFO[i].angle, 0.0, 0.0, 1.0);
glScalef(1.0/3.0, 1.0/4.0, 1.0/4.0);
glTranslatef(0.0, -4.0, -1.5);
glColor3f(1.0,1.0,1.0);
glutWireSphere(1.5,15,15);
glPopMatrix();

glPushMatrix();
glTranslatef(UFO[i].x, UFO[i].y, UFO[i].z);
glRotatef(290.0, 1.0, 0.0, 0.0);
glRotatef(UFO[i].angle, 0.0, 0.0, 1.0);
glScalef(1.0/3.0, 1.0 / 4.0, 1.0 / 4.0);
glTranslatef(0.75, -4.0, -1.5);
glColor3f(1.0,0.0,0.0);
glutSolidTeapot(1.15);
glPopMatrix();

glPushMatrix();
glTranslatef(UFO[i].x, UFO[i].y, UFO[i].z);
glRotatef(290.0, 1.0, 0.0, 0.0); /*rotates x axis with 290 degree*/
glRotatef(UFO[i].angle, 0.0, 0.0, 1.0);
glScalef(1.0 /3.0, 1.0 / 4.0, 1.0 / 4.0);
glTranslatef(0.95, -4.0,-1.5);
glColor3f(0.0,0.0,0.0);
glutSolidCone(3.75,0.5,14,5);
glPopMatrix();
}
glutSwapBuffers();
}

/*t positions the ufo with a function that is dependent on theta*/

void tick_per_UFO(int i) /*define the ufo position and speed incrementor*/
{
float theta = UFO[i].theta += UFO[i].speed;
UFO[i].z = -9 + 4 * cos(theta); /* z=length*cos(angle) */
UFO[i].x = 4 * sin(2 * theta);
UFO[i].y = sin(theta / 3.4) * 3;

/* degree=radians*180/pi */
UFO[i].angle = (((2.0) + M_PI_2) * sin(theta) - M_PI_2) * 180 / M_PI; /*calculates nor of degrees using nor of radians*/
if (UFO[i].speed < 0.0)
UFO[i].angle += 180;
}

void add_UFO(void)
{
int i;
for (i = 0; i < MAX_UFO; i++)
if (UFO[i].speed == 0)
{
UFO[i].speed = ((float) (random() % 20)) * 0.0001 + 0.02;
if (random() & 0x1)
UFO[i].speed *= -1;
UFO[i].theta = ((float) (random() % 257)) * 0.1111;
tick_per_UFO(i);
if (!moving)
glutPostRedisplay(); /*Request redisplay*/
return;
}
}

void remove_UFO(void)
{
int i;
for (i = MAX_UFO - 1; i >= 0; i--)
if (UFO[i].speed != 0)
{
UFO[i].speed = 0;
if (!moving)
glutPostRedisplay();
return;
}
}

void tick(void)
{
int i;
for (i = 0; i < MAX_UFO; i++)
if (UFO[i].speed != 0.0)
tick_per_UFO(i);
}

void animate(void)
{
tick();
glutPostRedisplay(); /*Request redisplay*/
}

void visible(int state)
{
if (state == GLUT_VISIBLE)
{
if (moving)
glutIdleFunc(animate);
}
else
{
if (moving)
glutIdleFunc(NULL);
}
}

void keyboard(unsigned char ch, int x, int y)
{
switch (ch)
{
case ' ':if (!moving)
{
tick();
glutPostRedisplay(); /*Request redisplay*/
}
break;

case 27:exit(0); /* ESC */
break;
}
}


void menu(int item)
{
switch (item) {
case ADD_UFO:add_UFO();
break;

case REMOVE_UFO:remove_UFO();
break;

case MOTION_ON:moving = GL_TRUE;
glutIdleFunc(animate);
break;

case MOTION_OFF:moving = GL_FALSE;
glutIdleFunc(NULL);
break;

case QUIT:exit(0);
break;
}
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutCreateWindow("UFO");
glutDisplayFunc(draw); /*Display Callback function*/
glutKeyboardFunc(keyboard); /*Keyboard Callback function*/
glutVisibilityFunc(visible);
glutCreateMenu(menu);
glutAddMenuEntry("Add UFO", ADD_UFO);
glutAddMenuEntry("Remove UFO", REMOVE_UFO);
glutAddMenuEntry("Motion on", MOTION_ON);
glutAddMenuEntry("Motion off",MOTION_OFF);
glutAddMenuEntry("Quit", QUIT);
glutAttachMenu(GLUT_RIGHT_BUTTON); /* setup OpenGL state */
glClearDepth(1.0);
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 20); /*Perspective viewing*/
glMatrixMode(GL_MODELVIEW);
add_UFO(); /* add three initial random UFO */
add_UFO();
add_UFO();
glutMainLoop(); /* start event processing */
return 0;
}


the above code is for 3D object ufo..in this I'm not getting the function void tick_per_UFO(int i) ,explain how it will work and what is the basic formula for those x,y,z axis (i.e, UFO[i].z = -9 + 4 * cos(theta); UFO[i].x = 4 * sin(2 * theta); UFO[i].y = sin(theta / 3.4)

Agent D
05-04-2015, 12:51 PM
To sumarize: You post a large blob of code, say that there is a 6 line function you don't understand and ask for an explanation.
You previously posted another snippet from the code above, demanding a "line by line" explanation in another thread.

Given the above sumary of events, I'm under the impression you have no idea what you are doing. Nobody here is going to do your
homework.

If you want to develop fancy graphics software (or any kind of software), you need to learn a programming language first (and understand
what is going on). You also need to learn about software development and design. If you make it that far and still want to do fancy graphics
programming stuff, you should have no problems learning about OpenGL(R). For using OpenGL(R), however, you should also learn some basic
linear algebra and trigonomentry, something that you apparently also have problems with, given the question at the end of your post.

Carmine
05-04-2015, 04:38 PM
Mr. Prateek -

I am wondering if you can compile and run this code?
If so, what shows up on the screen?

Can you make changes, recompile, and run again?
If so, you could always take the backward approach to understanding the code.
Make some small changes to 'tick_per_UFO' and see what happens.
This will get you a feel for the function.
Do you understand what trig functions are (sin, cos, etc)?
If not, it's going to be tough to understand what 'tick_per_UFO' does.

It's good that you posted the entire code.
Makes it more likely that someone will take the time to help you.
Even better - post the code inside of [ code] and [ \code] tags with proper indentation.

Good luck.

prateek
05-04-2015, 07:46 PM
I'm hvng problem with that only x,y,z axis..
that function is for positioning the object and to increment speed but i don't know basic formula to use...

prateek
05-04-2015, 07:50 PM
i know what I'm doing sir..
the function in which i hv problem is ,positions 3D object...
i only donn't know x,y,z axis line...because I donn't basic formula for those line..

Carmine
05-05-2015, 11:19 AM
Can you compile, link, and run this code?

Do you know what sin, cos functions are all about?

prateek
05-06-2015, 03:43 AM
yes ..I compiled it, it linked and i got required o/p...
sine and cos are for object speed in the above function..

Carmine
05-06-2015, 09:50 AM
For the fun of it, I tried to compile and run your code. Worked fine. Where did this code come from? Did you write it, or parts of it? The UFO's are propagated using trig functions which have cyclical behavior. This makes the UFOs appear to orbit a central point (the origin). I can see how it is not obvious why they move the way they do. If you have time to play with the code, I can make a few suggestions: 1) turn off all but one UFO and look at it's motion in detail, 2) draw and label the X, Y, and Z axes, 3) set the Y and Z coordinates of the UFO to 0.0 (so it moves only along the x axis), 4) print out theta and UFO.x of the UFO as it propagates, 5) change the formula that computes UFO.x and see how it affects the motion. Do all those things, and you'll build up an understanding of how those trig equations make the UFOs move the way they do. BTW - it sounds like you are not familiar with how trig functions work (sin, cos, etc). You may want to read up on that.

prateek
05-12-2015, 05:21 AM
hmm ...ty!!!
:cool: