PDA

View Full Version : Urgent program description needed...



uvray90
05-26-2011, 10:50 AM
Helloo...
Iam new to the forum...I have only verry basic know hows on OPenGL..I need a little help with this program..Here...I couldnt figure out anything. :confused: Please try and tell a general description of what this program does...And if possible please comment on the lines...Will be a great help :) .....Thanks in advance.... :)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <GL/glut.h>

#undef PI /* Some systems may have this defined. */
#define PI 3.141592657

enum {
NORMAL = 0,
WEIRD = 1
};

enum {
STREAK = 0,
CIRCLE = 1
};

#define MAXSTARS 400
#define MAXPOS 10000
#define MAXWARP 10
#define MAXANGLES 6000

typedef struct _starRec {
GLint type;
float x[2], y[2], z[2];
float offsetX, offsetY, offsetR, rotation;
} starRec;

GLenum doubleBuffer;
GLint windW = 300, windH = 300;

GLenum flag = NORMAL;
GLint starCount = MAXSTARS / 2;
float speed = 1.0;
GLint nitro = 0;
starRec stars[MAXSTARS];
float sinTable[MAXANGLES];

float
Sin(float angle)
{
return (sinTable[(GLint) angle]);
}

float
Cos(float angle)
{
return (sinTable[((GLint) angle + (MAXANGLES / 4)) % MAXANGLES]);
}

void
NewStar(GLint n, GLint d)
{
if (rand() % 4 == 0) {
stars[n].type = CIRCLE;
} else {
stars[n].type = STREAK;
}
stars[n].x[0] = (float) (rand() % MAXPOS - MAXPOS / 2);
stars[n].y[0] = (float) (rand() % MAXPOS - MAXPOS / 2);
stars[n].z[0] = (float) (rand() % MAXPOS + d);
stars[n].x[1] = stars[n].x[0];
stars[n].y[1] = stars[n].y[0];
stars[n].z[1] = stars[n].z[0];
if (rand() % 4 == 0 &amp;&amp; flag == WEIRD) {
stars[n].offsetX = (float) (rand() % 100 - 100 / 2);
stars[n].offsetY = (float) (rand() % 100 - 100 / 2);
stars[n].offsetR = (float) (rand() % 25 - 25 / 2);
} else {
stars[n].offsetX = 0.0;
stars[n].offsetY = 0.0;
stars[n].offsetR = 0.0;
}
}

void
RotatePoint(float *x, float *y, float rotation)
{
float tmpX, tmpY;

tmpX = *x * Cos(rotation) - *y * Sin(rotation);
tmpY = *y * Cos(rotation) + *x * Sin(rotation);
*x = tmpX;
*y = tmpY;
}

void
MoveStars(void)
{
float offset;
GLint n;

offset = speed * 60.0;

for (n = 0; n < starCount; n++) {
stars[n].x[1] = stars[n].x[0];
stars[n].y[1] = stars[n].y[0];
stars[n].z[1] = stars[n].z[0];
stars[n].x[0] += stars[n].offsetX;
stars[n].y[0] += stars[n].offsetY;
stars[n].z[0] -= offset;
stars[n].rotation += stars[n].offsetR;
if (stars[n].rotation > MAXANGLES) {
stars[n].rotation = 0.0;
}
}
}

GLenum
StarPoint(GLint n)
{
float x0, y0;

x0 = stars[n].x[0] * windW / stars[n].z[0];
y0 = stars[n].y[0] * windH / stars[n].z[0];
RotatePoint(&amp;x0, &amp;y0, stars[n].rotation);
x0 += windW / 2.0;
y0 += windH / 2.0;

if (x0 >= 0.0 &amp;&amp; x0 < windW &amp;&amp; y0 >= 0.0 &amp;&amp; y0 < windH) {
return GL_TRUE;
} else {
return GL_FALSE;
}
}

void
ShowStar(GLint n)
{
float x0, y0, x1, y1, width;
GLint i;

x0 = stars[n].x[0] * windW / stars[n].z[0];
y0 = stars[n].y[0] * windH / stars[n].z[0];
RotatePoint(&amp;x0, &amp;y0, stars[n].rotation);
x0 += windW / 2.0;
y0 += windH / 2.0;

if (x0 >= 0.0 &amp;&amp; x0 < windW &amp;&amp; y0 >= 0.0 &amp;&amp; y0 < windH) {
if (stars[n].type == STREAK) {
x1 = stars[n].x[1] * windW / stars[n].z[1];
y1 = stars[n].y[1] * windH / stars[n].z[1];
RotatePoint(&amp;x1, &amp;y1, stars[n].rotation);
x1 += windW / 2.0;
y1 += windH / 2.0;

glLineWidth(MAXPOS / 100.0 / stars[n].z[0] + 1.0);
glColor3f(1.0, (MAXWARP - speed) / MAXWARP, (MAXWARP - speed) / MAXWARP);
if (fabs(x0 - x1) < 1.0 &amp;&amp; fabs(y0 - y1) < 1.0) {
glBegin(GL_POINTS);
glVertex2f(x0, y0);
glEnd();
} else {
glBegin(GL_LINES);
glVertex2f(x0, y0);
glVertex2f(x1, y1);
glEnd();
}
} else {
width = MAXPOS / 10.0 / stars[n].z[0] + 1.0;
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
for (i = 0; i < 8; i++) {
float x = x0 + width * Cos((float) i * MAXANGLES / 8.0);
float y = y0 + width * Sin((float) i * MAXANGLES / 8.0);
glVertex2f(x, y);
};
glEnd();
}
}
}

void
UpdateStars(void)
{
GLint n;

glClear(GL_COLOR_BUFFER_BIT);

for (n = 0; n < starCount; n++) {
if (stars[n].z[0] > speed || (stars[n].z[0] > 0.0 &amp;&amp; speed < MAXWARP)) {
if (StarPoint(n) == GL_FALSE) {
NewStar(n, MAXPOS);
}
} else {
NewStar(n, MAXPOS);
}
}
}

void
ShowStars(void)
{
GLint n;

glClear(GL_COLOR_BUFFER_BIT);

for (n = 0; n < starCount; n++) {
if (stars[n].z[0] > speed || (stars[n].z[0] > 0.0 &amp;&amp; speed < MAXWARP)) {
ShowStar(n);
}
}
}

static void
Init(void)
{
float angle;
GLint n;

srand((unsigned int) time(NULL));

for (n = 0; n < MAXSTARS; n++) {
NewStar(n, 100);
}

angle = 0.0;
for (n = 0; n <= MAXANGLES; n++) {
sinTable[n] = sin(angle);
angle += PI / (MAXANGLES / 2.0);
}

glClearColor(0.0, 0.0, 0.0, 0.0);

glDisable(GL_DITHER);
}

void
Reshape(int width, int height)
{
windW = width;
windH = height;

glViewport(0, 0, windW, windH);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-0.5, windW + 0.5, -0.5, windH + 0.5);
glMatrixMode(GL_MODELVIEW);
}

/* ARGSUSED1 */
static void
Key(unsigned char key, int x, int y)
{
switch (key) {
case ' ':
flag = (flag == NORMAL) ? WEIRD : NORMAL;
break;
case 't':
nitro = 1;
break;
case 27:
exit(0);
}
}

void
Idle(void)
{
MoveStars();
UpdateStars();
if (nitro > 0) {
speed = (float) (nitro / 10) + 1.0;
if (speed > MAXWARP) {
speed = MAXWARP;
}
if (++nitro > MAXWARP * 10) {
nitro = -nitro;
}
} else if (nitro < 0) {
nitro++;
speed = (float) (-nitro / 10) + 1.0;
if (speed > MAXWARP) {
speed = MAXWARP;
}
}
glutPostRedisplay();
}

void
Display(void)
{
ShowStars();
if (doubleBuffer) {
glutSwapBuffers();
} else {
glFlush();
}
}

void
Visible(int state)
{
if (state == GLUT_VISIBLE) {
glutIdleFunc(Idle);
} else {
glutIdleFunc(NULL);
}
}

static void
Args(int argc, char **argv)
{
GLint i;

doubleBuffer = GL_TRUE;

for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-sb") == 0) {
doubleBuffer = GL_FALSE;
} else if (strcmp(argv[i], "-db") == 0) {
doubleBuffer = GL_TRUE;
}
}
}

int
main(int argc, char **argv)
{
GLenum type;

glutInitWindowSize(windW, windH);
glutInit(&amp;argc, argv);
Args(argc, argv);

type = GLUT_RGB;
type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
glutInitDisplayMode(type);
glutCreateWindow("Stars");

Init();

glutReshapeFunc(Reshape);
glutKeyboardFunc(Key);
glutVisibilityFunc(Visible);
glutDisplayFunc(Display);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}

:eek: :)

Alfonse Reinheart
05-26-2011, 11:13 AM
So let me get this straight. You've posted a random blob of code, and you want us to figure out what it does. And to comment it for you. And this is an "urgent" problem.

And you didn't even bother to properly format the code with code tags, so that the code would be more readable.

OK, here's what the code does: it draws stuff to a window.

James Lovejoy
05-26-2011, 12:15 PM
Why don't you just compile it and see?

MaxH
05-26-2011, 03:54 PM
... tell a general description of what this program does... This sounds like a homework assignment. I'm guessing that you're supposed to compile and run this program, then answer the questions you asked us to answer for you. I have good news and bad news for you. The good news is that it compiles and runs fine. It's a nice demo. Thank you for posting the code. I always learn a little bit from looking at other people's code. The bad news is that that's all I have to say.

uvray90
05-26-2011, 07:49 PM
@alfonse
Sorry Mr.Alfonse, about the formattin of the code.Dont know much about the code tags and stuff.First time posting in a a forum.

@james.
I compiled it and saw.Its like going in a galaxy with stars and all around passing by and you can move here and there.You can change your view like you are in a space ship.
@maxH
Yup its kind of like somethin you said.I and my partner were supposed to do a project(our own) for our semester lab.He submitted this code without asking me.And now iam stuck without an explanation.Day after 2moro lab.And i just understood the main() function of the program...

I know my height of ignorance....:-( Please try to provide a brief description of how it works...The inbuilt functions i googled up and got some info.Please try to go through and find a description for it.Even if it is small and brief its ok.

BionicBytes
05-27-2011, 01:56 AM
I have to say this is a first :D
Usually people have a description of what they want, and ask us for help with the code.
You, on the other hand, have the code but want us to help with what the code does :confused:

Perhaps talk with your "lab partner" and get it from him. Presumably you did have input into this assignment didn't you and therefore the code is a joint effort? Or did you sit back and let him do all the work? Sounds like he's peed off with you and has submitted his *own* work!

Now that's a valuable lesson in life for you. Priceless.

mhagain
05-27-2011, 04:45 AM
Well I'll just back up what the others are saying: if this is a lab assignment, and if you're at the stage where you're expected to submit this kind of lab assignment, then ..... not that it's particularly advanced, but you really should know this stuff yourself by now.

uvray90
05-27-2011, 05:23 AM
:-( I admit my mistake...
@BionicBytes
Truth is that the even he doesnt know the working...Our college faculty is not that good..Even he didnt understand a bit of it.In exam i jus have to say a few words on the working.
....Only thing i know is....
Many stars around you passing by.
You can go in high speed or normal speed.

And few functions i tried to google up and work it out but still basic thing about its working remains a mystery....

I know the idea of this long code is much much...much more for my level...But Iam stuck with it..:( ...So guys please try a little....

BionicBytes
05-27-2011, 05:52 AM
You really need to spend the time investigating the problem your self; that's the essence of the problem here - you are relying upon others to do the work for you so you haven't gained any knowledge for yourself.
Best to admit defeat now and get help from your peers and teacher. Start to understand your failings and learn from the mistake.
You can help your self by disabling parts of the program to understand the purpose of each function. You can then document what that function does by knowing it's effect upon the program. Spend enough time and you WILL have your answer.

Nobody here will do this for you; I can assure you of that.

Dan Bartlett
05-27-2011, 06:05 AM
All the OpenGL functions used are described at http://www.opengl.org/sdk/docs/man/
It only uses basic OpenGL commands, and most of the code is the application logic. If you are still struggling with the OpenGL, try looking at tutorials at nehe.gamedev.net, for example the first couple of tutorials cover most of what you need http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=01

mhagain
05-27-2011, 06:10 PM
Bottom line. If you're learning OpenGL on your course, then the reason why is to learn OpenGL. Right now you're giving the impression that you sat on your ass all year and waited till the last moment before asking for a bail-out.

This is not a good thing.

There are already enough bad programmers in the world. Don't be another. If you have specific difficulty with something, then by all means ask - even if it's for an exam. So long as it seems that you at least tried yourself, you'll get answers. But you gotta show that much.

uvray90
05-27-2011, 09:56 PM
@Bionic
Thanks for the input ..Wil try ....

@dan
Ya i guess..Wil check out the tutorial..


Thanks guys...