Just to maybe help…
Here is a little program, it uses GLUT, mouse functions, glutIdleFunc, Keyboard, lighting, object maping via an array.
Over view of program…
Use the mouse to move your view and a triangle in your front view. Multiple triangle move in the back ground.
right/left mouse click moves you along the z-axis
ESC key exits the program.
Could use some commenting of the code…
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include “mystuff.h”
static int xsides = 0, rotateX = 0, rotateY = 0, rotateZ = 0;
static int WrotateX = 0, WrotateY = 0, WrotateZ = 0;
static float Worldz = 0,Worldzdir = 0.1, Mouse_x, Mouse_y, Win_x, Win_y;
static float xscale = 1, wscale = 1;
static int demo_state = 0;
GLfloat LightAmbient= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightPosition= { 0.0f, 0.0f, -5.0f, 1.0f };
GLfloat mat_specular = { 1.0, 1.0, 1.0, 1.0 };
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 Object_data My_Object[1001];
static void TimeEvent(int te)
{
int timent;
int i;
Worldz = Worldz + Worldzdir;
if (( Worldz > 25) | | (Worldz < 0)) Worldzdir = Worldzdir * -1;
if (My_Object[0].Ticks > My_Object[0].Ticks_per_frame)
{
if (demo_state == 0)
{
My_Object[0].Rotation_x = My_Object[0].Rotation_x + 5;
if (My_Object[0].Rotation_x > 360) My_Object[0].Rotation_x =0;
My_Object[0].Rotation_y = My_Object[0].Rotation_y + 5;
if (My_Object[0].Rotation_y > 360) My_Object[0].Rotation_y =0;
My_Object[0].Rotation_z = My_Object[0].Rotation_z + 5;
if (My_Object[0].Rotation_z > 360) My_Object[0].Rotation_z =0;
My_Object[0].Curent_point.xyz[0] = My_Object[0].Curent_point.xyz[0] + (0.1 * (My_Object[0].Curent_point.xyz[0] < My_Object[0].Int_point.xyz[0])) + (-0.1 * (My_Object[0].Curent_point.xyz[0] > My_Object[0].Int_point.xyz[0]));
My_Object[0].Curent_point.xyz[1] = My_Object[0].Curent_point.xyz[1] + (0.1 * (My_Object[0].Curent_point.xyz[1] < My_Object[0].Int_point.xyz[1])) + (-0.1 * (My_Object[0].Curent_point.xyz[1] > My_Object[0].Int_point.xyz[1]));
My_Object[0].Curent_point.xyz[2] = My_Object[0].Curent_point.xyz[2] + (0.1 * (My_Object[0].Curent_point.xyz[2] < My_Object[0].Int_point.xyz[2])) + (-0.1 * (My_Object[0].Curent_point.xyz[2] > My_Object[0].Int_point.xyz[2]));
for(i=1; i < 1000; i++)
{
My_Object[i].Rotation_x = My_Object[i-1].Rotation_x;
My_Object[i].Rotation_y = My_Object[i-1].Rotation_y;
My_Object[i].Rotation_z = My_Object[i-1].Rotation_z;
My_Object[i].Curent_point.xyz[0] = My_Object[i].Curent_point.xyz[0] + (0.1 * (My_Object[i].Curent_point.xyz[0] < My_Object[i].Int_point.xyz[0])) + (-0.1 * (My_Object[i].Curent_point.xyz[0] > My_Object[i].Int_point.xyz[0]));
My_Object[i].Curent_point.xyz[1] = My_Object[i].Curent_point.xyz[1] + (0.1 * (My_Object[i].Curent_point.xyz[1] < My_Object[i].Int_point.xyz[1])) + (-0.1 * (My_Object[i].Curent_point.xyz[1] > My_Object[i].Int_point.xyz[1]));
My_Object[i].Curent_point.xyz[2] = My_Object[i].Curent_point.xyz[2] + (0.1 * (My_Object[i].Curent_point.xyz[2] < My_Object[i].Int_point.xyz[2])) + (-0.1 * (My_Object[i].Curent_point.xyz[2] > My_Object[i].Int_point.xyz[2]));
}
}
if( demo_state ==1 )
{
My_Object[0].Rotation_x = My_Object[0].Rotation_x + 5;
if (My_Object[0].Rotation_x > 360) My_Object[0].Rotation_x =0;
My_Object[0].Rotation_y = My_Object[0].Rotation_y + 5;
if (My_Object[0].Rotation_y > 360) My_Object[0].Rotation_y =0;
My_Object[0].Rotation_z = My_Object[0].Rotation_z + 5;
if (My_Object[0].Rotation_z > 360) My_Object[0].Rotation_z =0;
for(i=1000; i > 0; i--)
{
My_Object[i].Rotation_x = My_Object[i-11].Rotation_x;
My_Object[i].Rotation_y = My_Object[i-1].Rotation_y;
My_Object[i].Rotation_z = My_Object[i-1].Rotation_z;
}
}
My_Object[0].Ticks = 0;
}else My_Object[0].Ticks++;
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);
glShadeModel (GL_SMOOTH);
glLightfv(GL_LIGHT1 ,GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1 ,GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1 ,GL_POSITION, LightPosition);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
glEnable(GL_DEPTH_TEST);
My_Object[0].Ticks_per_frame = 1;
My_Object[0].Ticks = 0;
for(iz=0; iz < 10; iz++)
{
for(iy=0; iy < 10; iy++)
{
for(ix=0; ix < 10; ix++)
{
My_Object[iz100+iy10+ix].Int_point.xyz[0] = ix * 2 - 9;
My_Object[iz100+iy10+ix].Int_point.xyz[1] = iy * 2 - 9;
My_Object[iz100+iy10+ix].Int_point.xyz[2] = iz * 2 - 34;
My_Object[iz100+iy10+ix].Curent_point.xyz[0] = 0;
My_Object[iz100+iy10+ix].Curent_point.xyz[1] = 0;
My_Object[iz100+iy10+ix].Curent_point.xyz[2] = -24;
My_Object[iz100+iy10+ix].Rotation_x = 0;
My_Object[iz100+iy10+ix].Rotation_y = 0;
My_Object[iz100+iy10+ix].Rotation_z = 0;
}
}
}
}
void display(void)
{
float i,j;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef( Mouse_x, Mouse_y, -15.0);
//glRotatef(270, 1.0, 0.0, 0.0);
glColorMaterial(GL_FRONT, GL_EMISSION);
glEnable(GL_COLOR_MATERIAL);
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINE_LOOP);
for(i=0;i <= 17;i++)
{
//glColor4f( pryamidc[(int)i].xyz[0], pryamidc[(int)i].xyz[1],pryamidc[(int)i].xyz[2], 0.2);
glNormal3fv( &pryamid[(int)i] );
glVertex3fv( &pryamid[(int)i] );
}
glEnd();
glPopMatrix();
glRotatef(WrotateX, 1.0f, 0.0f, 0.0f);
glRotatef(WrotateY, 0.0f, 1.0f, 0.0f);
glRotatef(WrotateZ, 0.0f, 0.0f, 1.0f);
glTranslatef( 0,0, Worldz);
for(j=0; j < 1000; j++ )
{
glPushMatrix();
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor3f(0.0, 1.0, 0.0);
glTranslatef(My_Object[(int)j].Curent_point.xyz[0], My_Object[(int)j].Curent_point.xyz[1], My_Object[(int)j].Curent_point.xyz[2]);
glRotatef(My_Object[(int)j].Rotation_x, 1.0f, 0.0f, 0.0f);
glRotatef(My_Object[(int)j].Rotation_y, 0.0f, 1.0f, 0.0f);
glRotatef(My_Object[(int)j].Rotation_z, 0.0f, 0.0f, 1.0f);
glScalef(xscale, xscale, xscale);
glBegin(GL_LINE_LOOP);
for(i=0;i <= 17;i++)
{
//glColor3f( pryamidc[(int)i].xyz[0], pryamidc[(int)i].xyz[1],pryamidc[(int)i].xyz[2]);
glNormal3fv( &pryamid[(int)i] );
glVertex3fv( &pryamid[(int)i] );
}
glEnd();
glPopMatrix();
}
glutSwapBuffers();
}
void reshape (int w, int h)
{
Win_x = w;
Win_y = h;
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0,(GLfloat) w/(GLfloat) h, 1.0, 50.0);
//gluLookAt (0.0, 0.0, wscale, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case ‘-’:
demo_state–;
if (demo_state < 0) demo_state = 0;
break;
case ‘+’:
demo_state++;
if (demo_state > 3) demo_state = 3;
break;
case ‘y’:
WrotateY = WrotateY + 15;
if (WrotateY > 360) WrotateY = 0;
glutPostRedisplay();
break;
case ‘Y’:
WrotateY = WrotateY - 15;
if (WrotateY < 0) WrotateY = 360;
glutPostRedisplay();
break;
case ‘x’:
WrotateX = WrotateX + 15;
if (WrotateX > 360) WrotateX = 0;
glutPostRedisplay();
break;
case ‘X’:
WrotateX = WrotateX - 15;
if (WrotateX < 0) WrotateX = 360;
glutPostRedisplay();
break;
case ‘z’:
WrotateZ = WrotateZ + 15;
if (WrotateZ > 360) WrotateZ = 0;
glutPostRedisplay();
break;
case ‘Z’:
WrotateZ = WrotateZ - 15;
if (WrotateZ < 0) WrotateZ = 360;
glutPostRedisplay();
break;
case ‘s’:
xscale = xscale -0.01f;
if (xscale < 0.01f ) xscale = 0.01f;
glutPostRedisplay();
break;
case ‘S’:
xscale = xscale + 0.01f;
if (xscale > 10) xscale = 10;
glutPostRedisplay();
break;
case ‘w’:
wscale = wscale - 0.01f;
if (wscale < 0.0f ) wscale = 10.0f;
glutPostRedisplay();
break;
case ‘W’:
wscale = wscale + 0.01f;
if (wscale > 10) wscale = 0;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
void mouse(int button, int state, int x, int y)
{
if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN)) Worldz++;
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) Worldz–;
}
void mouse_passive( int x, int y)
{
Mouse_x = -5 + 10 * (x/Win_x);
Mouse_y = 5 - 10 * (y/Win_y);
if ((Mouse_x < -1) && (Mouse_y < -1))
{
WrotateX++;
WrotateY–;
}
if ((Mouse_x > 1) && (Mouse_y > 1))
{
WrotateX–;
WrotateY++;
}
if ((Mouse_x > 1) && (Mouse_y < -1))
{
WrotateX++;
WrotateY++;
}
if ((Mouse_x < -1) && (Mouse_y > 1))
{
WrotateX–;
WrotateY–;
}
if ((Mouse_x < -1) && (Mouse_y > -1) && (Mouse_y < 1))
{
WrotateY–;
}
if ((Mouse_x > 1) && (Mouse_y > -1) && (Mouse_y < 1))
{
WrotateY++;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (10, 10);
glutCreateWindow (argv[0]);
glutSetWindowTitle(“GLbase”);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutPassiveMotionFunc(mouse_passive);
glutTimerFunc( 10, TimeEvent, 1);
glutMainLoop();
return 0;
}
/* Mystuff.h
by Eric Stringer
*/
typedef struct
{
GLfloat xyz[3];
}Point3F;
typedef struct
{
int Sides;
GLfloat x, y, z;
GLfloat Scale;
GLfloat Color3f[3];
Point3F* Vertex3f;
}Primitive3F;
typedef struct // this structure definded for the location of each pyramid
{
int object;
int sides;
Point3F Int_point;
Point3F Curent_point;
Point3F Target_point;
int Int_angle_x;
int Int_angle_y;
int Int_angle_z;
int Target_angle_x;
int Target_angle_y;
int Target_angle_z;
int Rotation_x;
int Rotation_y;
int Rotation_z;
int Rotation_rate_x;
int Rotation_rate_y;
int Rotation_rate_z;
int Speed_x;
int Speed_y;
int Speed_z;
int direction_x;
int direction_y;
int direction_z;
int Ticks_per_frame;
int Ticks;
}Object_data;
struct XYZ_BOUND
{
float ax;
float bx;
int timex;
float ay;
float by;
int timey;
float az;
float bz;
int timez;
};