Rodrigo

07-30-2010, 01:14 PM

Hi guys.

I'm new in OpenGL and I'm having some trouble showing the scene that I'm drawing.

My work is to create 20 cubes that affects each other by gravitional force, changing their speed and position in the space. That calculation is ok and working.

But in the function :

gluLookAt(0,0,400, 0,0,0, 0,1,0);

I choose the camera position at position 400 in z.

My cubes are being created respecting the range position of [-10,10] in z axe.

When I run my program a lot of cubes stay out the scene and then appear while they move over the xyz space as they were beyond the position 300 of the cam.

But the biggest problem is that those cubes get deformed as they get closer to the cam and look like parallelepipeds.

I don't know what the problem is, but I suspect that it's the values in gluLookAt and gluPerspective which receive the parameters (angle,w/h,2.0,700.0).

I've chosen values for angle between [45,150] but out this range it gets worse.

Does anybody have an idea of what that could be?

Here is my code if anybody wants to take a look:

#include <stdio.h>

#include <unistd.h>

#include <math.h>

#include <windows.h>

#include <gl/glut.h>

#define PTS 10

#define LIMITES 100

#define LIMITEV 10

#define LIMITEA 20

#define LIMITEM 200

#define RELOGIO 0.001

//CONST_GRAV 6,673 * exp10(-11) m^3 kg^-1 s^-2

struct ponto{

double x,y,z;

double xtmp,ytmp,ztmp;

double vx,vy,vz;

double vxtmp,vytmp,vztmp;

double ax,ay,az;

double fx,fy,fz;

double massa;

};

struct ponto p[PTS];

GLfloat angle, fAspect;

void Desenha(void)

{

int i;

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f, 0.0f, 0.0f);

for(i=0;i<PTS;i++)

{

glPushMatrix();

glTranslatef(p[i].x,p[i].y,p[i].z);

glutWireCube(10.0);

glPopMatrix();

}

glutSwapBuffers();

}

void Inicializa (void)

{

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

angle=150;

}

void EspecificaParametrosVisualizacao(void)

{

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(angle,fAspect,0.1,700.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt(0,0,400, 0,0,0, 0,1,0);

}

void Timer(int value)

{

int i,j;

double ftmp;

for(i=0;i<PTS;i++){

p[i].fx = p[i].fy = p[i].fz = 0;

for(j=0;j<PTS;j++){

double dij;

dij = sqrt((p[j].x - p[i].x)*(p[j].x - p[i].x) +

(p[j].y - p[i].y)*(p[j].y - p[i].y) +

(p[j].z - p[i].z)*(p[j].z - p[i].z));

if(i!=j){

ftmp = 100 * p[i].massa * p[j].massa/dij;

p[i].fx += (p[j].x - p[i].x)*ftmp/dij;

p[i].fy += (p[j].y - p[i].y)*ftmp/dij;

p[i].fz += (p[j].z - p[i].z)*ftmp/dij;

}

}

p[i].ax = p[i].fx/p[i].massa;

p[i].ay = p[i].fy/p[i].massa;

p[i].az = p[i].fz/p[i].massa;

}

for(i=0;i<PTS;i++){

p[i].vxtmp = p[i].vx + p[i].ax*RELOGIO;

p[i].vytmp = p[i].vy + p[i].ay*RELOGIO;

p[i].vztmp = p[i].vz + p[i].az*RELOGIO;

p[i].xtmp = p[i].x + p[i].vx*RELOGIO + p[i].ax*RELOGIO*RELOGIO/2;

p[i].ytmp = p[i].y + p[i].vy*RELOGIO + p[i].ay*RELOGIO*RELOGIO/2;

p[i].ztmp = p[i].z + p[i].vz*RELOGIO + p[i].az*RELOGIO*RELOGIO/2;

}

for(i=0;i<PTS;i++){

p[i].vx = p[i].vxtmp;

p[i].vy = p[i].vytmp;

p[i].vz = p[i].vztmp;

p[i].x = p[i].xtmp;

p[i].y = p[i].ytmp;

p[i].z = p[i].ztmp;

}

glutPostRedisplay();

glutTimerFunc(1,Timer, 1);

}

void AlteraTamanhoJanela(GLsizei w, GLsizei h)

{

if ( h == 0 ) h = 1;

glViewport(0, 0, w, h);

fAspect = (GLfloat)w/(GLfloat)h;

EspecificaParametrosVisualizacao();

}

int main(void)

{

int i;

srand(time(NULL));

for(i=0;i<PTS;i++){

p[i].x = (rand()%(2*LIMITES+1)) - LIMITES;

p[i].y = (rand()%(2*LIMITES+1)) - LIMITES;

p[i].z = (rand()%(2*LIMITES+1)) - LIMITES;

p[i].vx = (rand()%(2*LIMITEV+1)) - LIMITEV;

p[i].vy = (rand()%(2*LIMITEV+1)) - LIMITEV;

p[i].vz = (rand()%(2*LIMITEV+1)) - LIMITEV;

p[i].massa = (rand()%(LIMITEM)) + 200;

}

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(1000,800);

glutCreateWindow("Interação Gravitacional");

glutDisplayFunc(Desenha);

glutReshapeFunc(AlteraTamanhoJanela);

glutTimerFunc(1, Timer, 1);

Inicializa();

glutMainLoop();

return 0;

}

I'm new in OpenGL and I'm having some trouble showing the scene that I'm drawing.

My work is to create 20 cubes that affects each other by gravitional force, changing their speed and position in the space. That calculation is ok and working.

But in the function :

gluLookAt(0,0,400, 0,0,0, 0,1,0);

I choose the camera position at position 400 in z.

My cubes are being created respecting the range position of [-10,10] in z axe.

When I run my program a lot of cubes stay out the scene and then appear while they move over the xyz space as they were beyond the position 300 of the cam.

But the biggest problem is that those cubes get deformed as they get closer to the cam and look like parallelepipeds.

I don't know what the problem is, but I suspect that it's the values in gluLookAt and gluPerspective which receive the parameters (angle,w/h,2.0,700.0).

I've chosen values for angle between [45,150] but out this range it gets worse.

Does anybody have an idea of what that could be?

Here is my code if anybody wants to take a look:

#include <stdio.h>

#include <unistd.h>

#include <math.h>

#include <windows.h>

#include <gl/glut.h>

#define PTS 10

#define LIMITES 100

#define LIMITEV 10

#define LIMITEA 20

#define LIMITEM 200

#define RELOGIO 0.001

//CONST_GRAV 6,673 * exp10(-11) m^3 kg^-1 s^-2

struct ponto{

double x,y,z;

double xtmp,ytmp,ztmp;

double vx,vy,vz;

double vxtmp,vytmp,vztmp;

double ax,ay,az;

double fx,fy,fz;

double massa;

};

struct ponto p[PTS];

GLfloat angle, fAspect;

void Desenha(void)

{

int i;

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f, 0.0f, 0.0f);

for(i=0;i<PTS;i++)

{

glPushMatrix();

glTranslatef(p[i].x,p[i].y,p[i].z);

glutWireCube(10.0);

glPopMatrix();

}

glutSwapBuffers();

}

void Inicializa (void)

{

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

angle=150;

}

void EspecificaParametrosVisualizacao(void)

{

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(angle,fAspect,0.1,700.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt(0,0,400, 0,0,0, 0,1,0);

}

void Timer(int value)

{

int i,j;

double ftmp;

for(i=0;i<PTS;i++){

p[i].fx = p[i].fy = p[i].fz = 0;

for(j=0;j<PTS;j++){

double dij;

dij = sqrt((p[j].x - p[i].x)*(p[j].x - p[i].x) +

(p[j].y - p[i].y)*(p[j].y - p[i].y) +

(p[j].z - p[i].z)*(p[j].z - p[i].z));

if(i!=j){

ftmp = 100 * p[i].massa * p[j].massa/dij;

p[i].fx += (p[j].x - p[i].x)*ftmp/dij;

p[i].fy += (p[j].y - p[i].y)*ftmp/dij;

p[i].fz += (p[j].z - p[i].z)*ftmp/dij;

}

}

p[i].ax = p[i].fx/p[i].massa;

p[i].ay = p[i].fy/p[i].massa;

p[i].az = p[i].fz/p[i].massa;

}

for(i=0;i<PTS;i++){

p[i].vxtmp = p[i].vx + p[i].ax*RELOGIO;

p[i].vytmp = p[i].vy + p[i].ay*RELOGIO;

p[i].vztmp = p[i].vz + p[i].az*RELOGIO;

p[i].xtmp = p[i].x + p[i].vx*RELOGIO + p[i].ax*RELOGIO*RELOGIO/2;

p[i].ytmp = p[i].y + p[i].vy*RELOGIO + p[i].ay*RELOGIO*RELOGIO/2;

p[i].ztmp = p[i].z + p[i].vz*RELOGIO + p[i].az*RELOGIO*RELOGIO/2;

}

for(i=0;i<PTS;i++){

p[i].vx = p[i].vxtmp;

p[i].vy = p[i].vytmp;

p[i].vz = p[i].vztmp;

p[i].x = p[i].xtmp;

p[i].y = p[i].ytmp;

p[i].z = p[i].ztmp;

}

glutPostRedisplay();

glutTimerFunc(1,Timer, 1);

}

void AlteraTamanhoJanela(GLsizei w, GLsizei h)

{

if ( h == 0 ) h = 1;

glViewport(0, 0, w, h);

fAspect = (GLfloat)w/(GLfloat)h;

EspecificaParametrosVisualizacao();

}

int main(void)

{

int i;

srand(time(NULL));

for(i=0;i<PTS;i++){

p[i].x = (rand()%(2*LIMITES+1)) - LIMITES;

p[i].y = (rand()%(2*LIMITES+1)) - LIMITES;

p[i].z = (rand()%(2*LIMITES+1)) - LIMITES;

p[i].vx = (rand()%(2*LIMITEV+1)) - LIMITEV;

p[i].vy = (rand()%(2*LIMITEV+1)) - LIMITEV;

p[i].vz = (rand()%(2*LIMITEV+1)) - LIMITEV;

p[i].massa = (rand()%(LIMITEM)) + 200;

}

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(1000,800);

glutCreateWindow("Interação Gravitacional");

glutDisplayFunc(Desenha);

glutReshapeFunc(AlteraTamanhoJanela);

glutTimerFunc(1, Timer, 1);

Inicializa();

glutMainLoop();

return 0;

}