PDA

View Full Version : Pryamid demo....

nexusone
02-15-2002, 11:44 AM
Here is a little demo I am working on.
I am using GLUT as to keep it platform independent. Does not do too much, spins 1000 pryamids on all axis at this point.

/* Pyramid1000 by Eric Stringer 2002 */
/* 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 pryamida[21] = {{0.0f,1.0f,0.0f},{-1.0f,-1.0f,1.0f},{1.0f,-1.0f,1.0f},{0.0f,1.0f,0.0f},
{0.0f,1.0f,0.0f},{1.0f,-1.0f,1.0f},{1.0f,-1.0f,-1.0f},{0.0f,1.0f,0.0f},
{0.0f,1.0f,0.0f},{1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,-1.0f},{0.0f,1.0f,0.0f},
{0.0f,1.0f,0.0f},{-1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,1.0f},{0.0f,1.0f,0.0f},
{-1.0f,-1.0f,1.0f},{1.0f,-1.0f,1.0f},{1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,1.0f}};

static Point3F pryamid[18] = {{0.0f,1.0f,0.0f},{-1.0f,-1.0f,1.0f},{1.0f,-1.0f,1.0f},
{0.0f,1.0f,0.0f},{1.0f,-1.0f,1.0f},{1.0f,-1.0f,-1.0f},
{0.0f,1.0f,0.0f},{1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,-1.0f},
{0.0f,1.0f,0.0f},{-1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,1.0f},
{-1.0f,-1.0f,1.0f},{1.0f,-1.0f,1.0f},{1.0f,-1.0f,-1.0f},
{1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,1.0f}};

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];

static void TimeEvent(int te)
{
int timent;
rotateX = rotateX + 5;
if (rotateX > 360) rotateX = 0;
rotateY = rotateY + 5;
if (rotateY > 360) rotateY = 0;
rotateZ = rotateZ + 5;
if (rotateZ > 360) rotateZ = 0;

glutPostRedisplay();
glutTimerFunc( 10, TimeEvent, 1);
}

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];
//polygon[i].xyz[1] = polygon[0].xyz[1];
//polygon[i].xyz[2] = polygon[0].xyz[2];

}

void init(void)
{
int ix,iy,iz;
glClearColor (0.0, 0.0, 0.0, 0.0);
//glDepthFunc(GL_EQUAL);
//glEnable(GL_DEPTH_TEST);
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 * 2 - 9;
pryamidl[iz*100+iy*10+ix].xyz[1] = iy * 2 - 9;
pryamidl[iz*100+iy*10+ix].xyz[2] = iz * 2 - 34;
}
}
}

}

void display(void)
{
int i,j;

glClear (GL_COLOR_BUFFER_BIT);

//glPushMatrix();
for(j=0; j < 1000; j++)
{
glPushMatrix();
glMatrixMode(GL_MODELVIEW);

//glTranslatef(1,1,-15);
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);
glScalef(xscale, xscale, xscale);
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, 50.0);
glMatrixMode(GL_MODELVIEW);
//glOrtho(-8.00,8.00,-8.00, 8.00,1.00, 25.00);
gluLookAt (0.0, 0.0, 30.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//glMatrixMode(GL_MODELVIEW);
}

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);
glutTimerFunc( 10, TimeEvent, 1);
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;

tempocrew
02-15-2002, 11:47 AM
I must have missed the question mark

nexusone
02-15-2002, 01:18 PM
Not really a question as much I want feedback on the demo so far. Check the code for things maybe that I am doing wrong or could improve on.

Originally posted by tempocrew:
I must have missed the question mark

satan
02-15-2002, 01:58 PM
didn't know that this is also a feature of this forum
may i sent my 2000 lines of code, too, so someone can check if it's ok and perhaps optimize it for me? http://www.opengl.org/discussion_boards/ubb/wink.gif

no offence meant

and by the way if it does what it should why changing it?

you may also take a look here
not really up to date but deals with some optimization stuff
http://www.mesa3d.org/brianp/sig97/perfopt.htm

[This message has been edited by satan (edited 02-15-2002).]