PDA

View Full Version : Viewing a thousand spinning triangles.

nexusone
02-07-2002, 07:57 AM
I have got the basics of creating objects and want to create a more advance scene.
I have seen hundreds of spinning cudes, in demo's moveing around. So I have been trying to create this effect, but only can see some of the cubes. Here is the code, see if you can give me some suggestions on it:
// main.c
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include "mystuff.h"

static int xsides = 0, rotateX = 0, rotateY = 0, rotateZ = 0;
static float xscale = 1;

static Point3F pryamidc[18] = {{1.0f,1.0f,1.0f},{1.0f,1.0f,1.0f},{1.0f,1.0f,1.0f },
{0.5f,1.0f,0.5f},{0.5f,1.0f,0.5f},{0.5f,1.0f,0.5f} ,
{0.5f,0.5f,0.5f},{0.5f,0.5f,0.5f},{0.5f,0.5f,0.5f} ,
{0.0f,1.0f,0.0f},{0.0f,1.0f,0.0f},{0.0f,1.0f,0.0f} ,
{1.0f,0.0f,1.0f},{1.0f,0.0f,1.0f},{1.0f,0.0f,1.0f} ,
{1.0f,1.0f,0.0f},{1.0f,1.0f,0.0f},{1.0f,1.0f,0.0f} };

static Point3F pryamidl[1000];

static Primitive3F object[1];

static Point3F Vmemory[10][100];

void buildquad( int sides, Point3F polygon[])
{
float angle, xangle;
float x, y, z;
float r;
int i;

r = 2.0f;
xangle = 3.1415927f / sides;

for (i=0; i <= sides; i++)
{
angle = xangle * i * 2;
polygon[i].xyz[0] = r * cos( angle ); // X
polygon[i].xyz[1] = r * sin( angle ); // Y
polygon[i].xyz[2] = 0.0f; // Z
}
polygon[i] = polygon[0];
}

void init(void)
{
int ix,iy,iz;
glClearColor (0.0, 0.0, 0.0, 0.0);
object[0].Vertex3f = Vmemory;

object[0].Sides = 8;
object[0].Scale = 1;
object[0].x = 0.0f;
object[0].y = 0.0f;
object[0].z = -2.0f;
object[0].Color3f[0] = 1.0f;
object[0].Color3f[1] = 0.5f;
object[0].Color3f[2] = 1.0f;
for(iz=0; iz < 10; iz++)
{
for(iy=0; iy < 10; iy++)
{
for(ix=0; ix < 10; ix++)
{
pryamidl[iz*100+iy*10+ix].xyz[0] = ix * 4 - 5;
pryamidl[iz*100+iy*10+ix].xyz[1] = iy * 4 - 5;
pryamidl[iz*100+iy*10+ix].xyz[2] = iz * 4 - 20;
}
}
}

}

void display(void)
{
int i,j;

glClear (GL_COLOR_BUFFER_BIT);

for(j=1000; j > 0; j--)
{
glPushMatrix();
glScalef(xscale, xscale, xscale);
glTranslatef(pryamidl[j].xyz[0], pryamidl[j].xyz[1], pryamidl[j].xyz[2]);
glRotatef(rotateX, 1.0f, 0.0f, 0.0f);
glRotatef(rotateY, 0.0f, 1.0f, 0.0f);
glRotatef(rotateZ, 0.0f, 0.0f, 1.0f);

glBegin(GL_TRIANGLES);

for(i=0;i <= 17;i++)
{
glColor3f( pryamidc[i].xyz[0], pryamidc[i].xyz[1],pryamidc[i].xyz[2]);
glVertex3fv( &pryamid[i] );
}

glEnd();
glPopMatrix();
}

glutSwapBuffers();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
gluPerspective(60.0,(GLfloat) w/(GLfloat) h, 1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case '-':
xsides--;
if (xsides < 3) xsides = 3;
glutPostRedisplay();
break;
case '+':
xsides++;
if (xsides > 30) xsides = 30;
glutPostRedisplay();
break;
case 'y':
rotateY = rotateY + 15;
if (rotateY > 360) rotateY = 0;
glutPostRedisplay();
break;
case 'Y':
rotateY = rotateY - 15;
if (rotateY < 0) rotateY = 360;
glutPostRedisplay();
break;
case 'x':
rotateX = rotateX + 15;
if (rotateX > 360) rotateX = 0;
glutPostRedisplay();
break;
case 'X':
rotateX = rotateX - 15;
if (rotateX < 0) rotateX = 360;
glutPostRedisplay();
break;
case 'z':
rotateZ = rotateZ + 15;
if (rotateZ > 360) rotateZ = 0;
glutPostRedisplay();
break;
case 'Z':
rotateZ = rotateZ - 15;
if (rotateZ < 0) rotateZ = 360;
glutPostRedisplay();
break;
case 's':
xscale = xscale -0.01f;
if (xscale < 0.01f ) rotateZ = 0.01f;
glutPostRedisplay();
break;
case 'S':
xscale = xscale + 0.01f;
if (xscale > 10) xscale = 10;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}

object[0].Scale = xscale;
object[0].Sides = xsides;
object[0].Color3f[0] = 1.0f;
object[0].Color3f[1] = 0.5f;
object[0].Color3f[2] = 1.0f;
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glutSetWindowTitle("GLbase");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

// mystuff.h

typedef struct
{
GLfloat xyz[3];

}Point3F;

typedef struct
{
int Sides;
GLfloat x, y, z;
GLfloat Scale;
GLfloat Color3f[3];
Point3F* Vertex3f;
}Primitive3F;

Elixer
02-07-2002, 09:22 AM
Why are using using glScale(1,1,1)?? That does nothing.

Looks like you are rendering some object out of the screen view.

nexusone
02-07-2002, 10:04 AM
I do not have glScale(1,1,1)?
Do you mean the glScalef(xscale, xscale, xscale)?
That was to scale the objects, but using the "S" key.

Originally posted by Elixer:
Why are using using glScale(1,1,1)?? That does nothing.

Looks like you are rendering some object out of the screen view.