PDA

View Full Version : help me



durandal9
09-04-2000, 09:33 AM
I have this code that is part of a program that makes a 3d piont circle around a square box. I was asked to put a "tail" on the point so that it looks loike a "comet". I have been sort of unable to do it properly and I was wondering if you could give me a hand.

The idea is that I modify the draw_model function. The tail consist of a line that connects the actual position of the point with the last "tail_size" positions that have been used (tail_size is a constant defined by me).

I had thought of using something like:

glBegin(GL_LINE_STRIP);

for (?; ?; ?)
{
x = ...
y = ...
z = ...
glColor3d(r, g, b);
glVertex3d(x, y, z);
}

glEnd();

Starting from the initial position the tail should start from red (r=1.0, g=0.0, b=0.0) and gradualy turn to black (r=0.0, g=0.0, g=0.0) when it reaches the last point.

The following is the .c file in charge of the process. (there are other files that are needed to compile the final program, but I asume you wouldn't need them).

Here is the .c :

#endif

#if defined(WIN32) | | defined(__CYGWIN32__)
/* OpenGL needs this to be included before gl.h. */
#include <windows.h>
#endif

#include <GL/gl.h>

#include <model.h>
#include <setup.h>

/* models variables. */
static double t = 0.0;
static double dt = 0.2;

/* Makes time advance in the model. */
void
time_step(void)
{
t += dt;
}

/* t scale from [0, period] to [0, 1]. */
static double
t_to_01(double t, double period)
{
return (t - floor(t/period)*period)/period;
}

void
draw_model(void)
{
static double x;
static double y;
static double z;
static double last_t = -1.0;

static const double r_period = 1000.0;
static const double theta_period = 100.0;
const double r = 0.4*(vv_xmax - vv_xmin);

#if defined(DEBUG)
fprintf(stderr, "draw_model t = %f.\n", t);
#endif

if (t != last_t)
{
double theta = 2 * M_PI * t_to_01(t, theta_period);

x = r*sin(theta);
y = r*cos(theta);
z = 2*r*t_to_01(t, r_period) - r;
}

glBegin(GL_POINTS);
glColor3d(1.0, 0.7, 0.7);
glVertex3d(x, y, z);
glEnd();
}

grady
09-04-2000, 11:54 AM
If you use GL_LINE_STRIP your tails won't actually be trailing you know; a real trail would be in an arc (if hte points are rotating that is). So put your initial points in a matrix like
points[num_of_points][3] and maybe this will work-

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

for(int x=0; x<num_of_points; x++)
{
glBegin(GL_LINE_STRIP)

//z=the number of line segments you want
//in a trail.
//angle also effects the length of trail
for(int y=0; y<z; y++)
{
//rotate around y axis for instance
glRotatef(angle, 0.0f, 1.0f, 0.0f);

//i abreviate matrix in next line
glVertex3f(points[x][0], [x][1],[x][2]);
}

//restore original matrix for drawing the
//next point
glPopMatrix();

//save it again
glPushMatrix();

glEnd()
}


Now this should work i think, it might not though and you might look at it and think i'm crazy( i started learning opengl a couple of weeks ago with no sort of prior experience http://www.opengl.org/discussion_boards/ubb/biggrin.gif). I don't know what all you can put between glBegin() and glEnd() so you might have to move stuff around but i hope it works.

p.s. if it does work the trails farther fromthe axis of rotation wont be as smooth as teh inner ones. so you might want to change 'z' or 'angle' for outer points to make more line segments.

grady
09-04-2000, 01:17 PM
Ok, that won't work because you can't put glRotatef() in between glBegin() adn glEnd(); http://www.opengl.org/discussion_boards/ubb/confused.gif but i think i got anohter way http://www.opengl.org/discussion_boards/ubb/biggrin.gif

grady
09-04-2000, 01:38 PM
Oops i pressed enter. before i finished last post. I got it, what you do is draw your points, rotate the GL_MODELVIEW, and draw them again, I made a program to do it and if you put enough points in you cant tell there is no line. if you want the program i'll send you the source code and the executable i did it in VC++ 6.0 (i hope you're running windows), but i'll post the RenderScene() here:

void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//================================================== ========
//move out into space
//================================================== ========
glTranslatef(0.0f, 0.0f, -55.0f);
//================================================== ========
//get out from in front of the points for better perspective
//================================================== ========
glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
//================================================== ===============================
//be sure to save the matrix you have now because THIS is your actual point of view.
//================================================== ===============================
glPushMatrix();

//================================================== ============
//Now the following draws three comets apart from each other and
//then draws there little tails by iterating through
//the for loop and drawing a new point just a little
//bit behind the old point (it might be a little in
//front but who cares? its the same effect http://www.opengl.org/discussion_boards/ubb/smile.gif )
//The picture it draws looks like the symbol for something that
//is atomic with the three points circling a nucleus
//================================================== ============


//=============================================
//Make the rotation for the actual comet
//NOTE: Each comet rotates on a different axis
//=============================================
glRotatef(x, 1.0f, 0.0f, 0.0f);
for(int y=0; y<100; y++)
{
glBegin(GL_POINTS);
//DRAW THE COMET IN ITS SPECIFIC POSITION
//NOTE: EACH COMET STARTS SOMEWHERE DIFFERENT
glVertex3f(0.0f, 0.0f, -20.0f);
glEnd();

//KEEP THE ANGLE OF ROTATION SMALL BY DIVIDING BY A NUMBER SMALLER THAN "Y"'S GREATEST
//VALUE SO YOUR POINTS LOOK LIKE A LINE. I CHOSE 75.0.
glRotatef(float(y)/75.0f, 1.0f, 0.0f, 0.0f);
}

//=====================================
//This rotates the next point
//=====================================

glPopMatrix();
glPushMatrix();
glRotatef(x, 0.0f, 0.0f, 1.0f);

for(y=0; y<100; y++)
{
glBegin(GL_POINTS);
glVertex3f(0.0f, -20.0f, 0.0f);
glEnd();

glRotatef(float(y)/75.0f, 0.0f, 0.0f, 1.0f);
}

//======================================
//and the next
//======================================
glPopMatrix();
glPushMatrix();
glRotatef(x, 1.0f, -1.0f, 0.0f);

for(y=0; y<100; y++)
{
glBegin(GL_POINTS);
glVertex3f(-14.14f, -14.14f, 0.0f);
glEnd();

glRotatef(float(y)/75.0f, 1.0f, -1.0f, 0.0f);
}

//================================================== ==================
//Load the original matrix, the angle of rotation is updated elsewhere
//so the current matrix can be discarded
//================================================== ==================
glPopMatrix();

//REDRAW
glutSwapBuffers();
}

durandal9
09-04-2000, 03:29 PM
Gee, thanks! I have a problem though. The thing is that I have been asked to modify the file so that it works. (that's why I had thought of using GL_LINE_STRIP). I'm totally new to this OpenGL thing and this is my first assignment. I'm not familiar with the code at all.
I don't fully understand your code. Is there a way you could fit that in the .c file I put? If so how would where would it go? Any chances you wrote all those functions and made them work in the .c file I put? Sorry if I may seem lost, but again I'm really new to this...

grady
09-04-2000, 06:30 PM
I might be able to modify your file if i can understand it http://www.opengl.org/discussion_boards/ubb/smile.gif I emailed you at your hotmail address. forward me back all your files for your assingment and i'll modify it if I can uderstand it http://www.opengl.org/discussion_boards/ubb/smile.gif just tell me how many points you want and where you want them and how you want them to rotate. Questions- Are you using the glut library? what version if you are? are you using Windows or linux (cause if you're using linux i probly cant help you much more)? By the way, all those funtions in my post are OpenGL functions, i dint have to right them, i'll explain them if you email me your source code(they're really simple). I'll put a good explanation of the ones you dont understand in the comments. What kind of assignment is this anyway? are you taking a class? I learned all my stuff out of this book called OpenGL Superbible. It's REALLY good. about $50. covers basics to advanced thouroughly you just gotta practice- thats why i want to help you out http://www.opengl.org/discussion_boards/ubb/biggrin.gif there isn't really a very effecient way to do what you wanna do without some advanced thing you do with a buffer- you can do this thing to where its like a motion blur, like a picture taken with the lens left open. but that stuffs advanced. so for now i can only think of ( and this is surely inefecient) drawing about a hundrend little points behind the original point. THATS 100 MATRIX ROTATIONS PER POINT http://www.opengl.org/discussion_boards/ubb/eek.gif .
But there just points, no lighting or anything so its cool http://www.opengl.org/discussion_boards/ubb/cool.gif i ran three points at once and they could really fly http://www.opengl.org/discussion_boards/ubb/smile.gif.

durandal9
09-04-2000, 09:37 PM
files sent, check your mail http://www.opengl.org/discussion_boards/ubb/smile.gif