Here is my program!
Let me know how I can improve my pointer or data storage handeling.
/* main.c */
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include “mystuff.h”
static int year = 0, day = 0;
//static Point3F dataf[15]; //= {{1.0f,1.0f,0.0f}, {1.0f,0.0f,0.0f}, {0.0f,0.0f,0.0f}, {0.0f,1.0f,0.0f}, {1.0f,1.0f,0.0f}};
static Primitive3F object[1];
static Point3F Vmemory[100];
Point3F 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].xyz[0] = polygon[0].xyz[0];
polygon[i].xyz[1] = polygon[0].xyz[1];
polygon[i].xyz[2] = polygon[0].xyz[2];
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
object[0].Vertex3f = Vmemory;
buildquad( 5, object[0].Vertex3f);
object[0].Sides = 5;
object[0].Color3f[0] = 1.0f;
object[0].Color3f[1] = 1.0f;
object[0].Color3f[2] = 1.0f;
}
void display(void)
{
int i;
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glPushMatrix();
glBegin(GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);
for(i=0;i<=object[0].Sides;i++)
{
glVertex3fv( &object[0].Vertex3f[i] );
}
/* glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 0.0f, 0.0f);
glVertex3f( 0.0f, 0.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
*/
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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 ‘d’:
day = (day + 10) % 360;
glutPostRedisplay();
break;
case ‘D’:
day = (day - 10) % 360;
glutPostRedisplay();
break;
case ‘y’:
year = (year + 5) % 360;
glutPostRedisplay();
break;
case ‘Y’:
year = (year - 5) % 360;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
/* Mystuff.h
by Eric Stringer
*/
typedef struct
{
GLfloat xyz[3];
}Point3F;
typedef struct
{
int Sides;
GLfloat Color3f[3];
Point3F* Vertex3f;
}Primitive3F;