PDA

View Full Version : Rotating individual objects



mountainoflight
11-21-2008, 05:36 AM
Hi. I'm looking for a way to to rotate objects as I place them in particular locations on my screen (rotations and positions defined by an external file). My finished program should then be able to rotate all of the images by use of the arrow keys.

I've managed to form a code that places all of the objects (textured spheres) in a box, but all of them are in their starting orientations. I must have put code in the wrong order, or not called PushMatrix() and PopMatrix() correctly. Could anyone help? I've put my RenderScene code and subsidiary functions below.

A huge thank you in advance if you could help me out.

RenderScene:


// FUNCTION: Called to draw scene
void RenderScene(void)
{
FILE *fp;
char str[120];
float lat, lon, dep, ms, lat2, lon2, dep2, scalar;
float time;
long date;
int mmt, str2, dip2, rk2, count;

// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Make the cube a wire frame
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

// Save the matrix state
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0.0f, 0.0f, -200.0f);

// Rotate about x and y axes
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 0.0f, 1.0f);

vertices();


glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, corners);

// Using Drawarrays
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, indexes);

// Open file for reading
if((fp = fopen("search.txt", "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}

// Read the file
do {
fgets(str, 119, fp);
if(!feof(fp)) {
sscanf(str, "%ld %f %f %f %f %f %f %f %f %f %f e %d %d %d %d %d %d %d", \
&date, &time, &lat, &lon, &dep, &mb, &ms, &lat2, &lon2, &dep2, &scalar, &mmt, &strike, &dip, &rk, &str2, &dip2, &rk2);

if (lon < 0 ) lon = lon + 360;
x = (((lon - minlon) / (maxlon - minlon)) * 2 * i) - i;
y = (((lat - minlat) / (maxlat - minlat)) * 2 * j) - j;
z = (((maxdep-dep) / (maxdep - mindep)) * 2 * k) - k;

drawBeachball();

} ;
} while(!feof(fp));

fclose(fp);

// Restore the matrix state
glPopMatrix();

// Swap buffers
glutSwapBuffers();
}


drawBeachball


void drawBeachball(void)
{
glPushMatrix ( );
glTranslatef(x, y, z);
glRotatef ( rk , 1.0 , 0.0 , 0.0 ); //rake
glRotatef ( dip-90 , 0.0 , cos(rk*degtopi) , -sin(rk*degtopi)); //90-dip
glRotatef ( strike , sin((dip-90)*degtopi), -sin(rk*degtopi)*cos((dip-90)*degtopi), -cos(rk*degtopi)*cos((dip-90)*degtopi) ); //strike
glColor4f ( 1.0, 1.0, 1.0, 1.0 );

// Make the spheres solid
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
GLUquadricObj* q = gluNewQuadric ( );
gluQuadricDrawStyle ( q, GLU_FILL );
gluQuadricNormals ( q, GLU_SMOOTH );
gluQuadricTexture ( q, GL_TRUE );
gluSphere ( q, mb/3, 32, 32 );
gluDeleteQuadric ( q );
glPopMatrix ( );
}


Vertices:


//FUNCTION: Vertices
void vertices(void)
{
// Reinitialising array containing the six vertices of the cube
for (a = 0; a <= 23; a++)
{
switch(a)
{
/* Negative i */
case 0:
case 9:
case 12:
case 21:
corners[a] = (-i);
break;

/* Negative j */
case 7:
case 10:
case 19:
case 22:
corners[a] = (-j);
break;

/* Negative k */
case 14:
case 17:
case 20:
case 23:
corners[a] = (-k);
break;

/* Positive i */
case 3:
case 6:
case 15:
case 18:
corners[a] = i;
break;

/* Positive j */
case 1:
case 4:
case 13:
case 16:
corners[a] = j;
break;

/* Positive k */
case 2:
case 5:
case 8:
case 11:
corners[a] = k;
break;

default:
/* Should never hit this branch! */
break;
}
}
}

IneQuation.pl
11-21-2008, 07:10 AM
Do your transformations in this order: scale, rotate, translate.