My program compiles fine but after I start it crashes… and don’t know why…
code below…
#include <windows.h>
#include <GL/glut.h>
#include <GL/glaux.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SPEED 0.09f
float angle, angle2,x;
int moving, startx, starty;
typedef struct {
float x, y, z;
}Vector ;
Vector mPosition;
Vector mView;
Vector mUpVector;
void Camera()
{
Vector vZero = {0.0, 0.0, 0.0};
Vector vView = {0.0, 1.0, 0.5};
Vector vUp = {0.0, 0.0, 1.0};
mPosition = vZero;
mView = vView;
mUpVector = vUp;
}
GLvoid PositionCamera(float positionX, float positionY, float positionZ,
float viewX, float viewY, float viewZ,
float upVectorX, float upVectorY, float upVectorZ)
{
Vector vPosition = {positionX, positionY, positionZ};
Vector vView = {viewX, viewY, viewZ};
Vector vUpVector = {upVectorX, upVectorY, upVectorZ};
mPosition = vPosition;
mView = vView;
mUpVector = vUpVector;
}
void RotateView(float X, float Y, float Z)
{
Vector vector;
vector.x = mView.x - mPosition.x;
vector.y = mView.y - mPosition.y;
vector.z = mView.z - mPosition.z;
if(X) {
mView.z = (float)(mPosition.z + sin(X)*vector.y + cos(X)*vector.z);
mView.y = (float)(mPosition.y + cos(X)*vector.y - sin(X)*vector.z);
}
if(Y) {
mView.z = (float)(mPosition.z + sin(Y)*vector.x + cos(Y)*vector.z);
mView.x = (float)(mPosition.x + cos(Y)*vector.x - sin(Y)*vector.z);
}
if(Z) {
mView.x = (float)(mPosition.x + sin(Z)*vector.y + cos(Z)*vector.x);
mView.y = (float)(mPosition.y + cos(Z)*vector.y - sin(Z)*vector.x);
}
}
void RotateAroundPoint(Vector vCenter, float X, float Y, float Z)
{
Vector vector;
vector.x = mPosition.x - vCenter.x;
vector.y = mPosition.y - vCenter.y;
vector.z = mPosition.z - vCenter.z;
if(X) {
mPosition.z = (float)(vCenter.z + sin(X)*vector.y + cos(X)*vector.z);
mPosition.y = (float)(vCenter.y + cos(X)*vector.y - sin(X)*vector.z);
}
if(Y) {
mPosition.z = (float)(vCenter.z + sin(Y)*vector.x + cos(Y)*vector.z);
mPosition.x = (float)(vCenter.x + cos(Y)*vector.x - sin(Y)*vector.z);
}
if(Z) {
mPosition.x = (float)(vCenter.x + sin(Z)*vector.y + cos(Z)*vector.x);
mPosition.y = (float)(vCenter.y + cos(Z)*vector.y - sin(Z)*vector.x);
}
}
void MoveCamera(float speed)
{
Vector vector={0};
vector.x = mView.x - mPosition.x;
vector.y = mView.y - mPosition.y;
vector.z = mView.z - mPosition.z;
mPosition.x += vector.x * speed;
mPosition.z += vector.z * speed;
mView.x += vector.x * speed;
mView.z += vector.z * speed;
}
void CreatePyramid(float x, float y, float z, int width, int height)
{
glBegin(GL_TRIANGLES);
glColor3ub(255, 0, 0); glVertex3f(x, y + height, z);
glColor3ub(0, 255, 255); glVertex3f(x - width, y - height, z - width);
glColor3ub(255, 0, 255); glVertex3f(x + width, y - height, z - width);
glColor3ub(255, 0, 0); glVertex3f(x, y + height, z);
glColor3ub(0, 255, 255); glVertex3f(x + width, y - height, z + width);
glColor3ub(255, 0, 255); glVertex3f(x - width, y - height, z + width);
glColor3ub(255, 0, 0); glVertex3f(x, y + height, z);
glColor3ub(255, 0, 255); glVertex3f(x - width, y - height, z + width);
glColor3ub(0, 255, 255); glVertex3f(x - width, y - height, z - width);
glColor3ub(255, 0, 0); glVertex3f(x, y + height, z);
glColor3ub(255, 0, 255); glVertex3f(x + width, y - height, z - width);
glColor3ub(0, 255, 255); glVertex3f(x + width, y - height, z + width);
glEnd();
glBegin(GL_QUADS);
glColor3ub(0, 0, 255); glVertex3f(x - width, y - height, z + width);
glColor3ub(0, 0, 255); glVertex3f(x + width, y - height, z + width);
glColor3ub(0, 0, 255); glVertex3f(x + width, y - height, z - width);
glColor3ub(0, 0, 255); glVertex3f(x - width, y - height, z - width);
glEnd();
}
void Draw3DSGrid()
{
glColor3ub(0, 255, 0);
for(float i = -50; i <= 50; i += 1)
{
glBegin(GL_LINES);
glVertex3f(-50, 0, i);
glVertex3f(50, 0, i);
glVertex3f(i, 0, -50);
glVertex3f(i, 0, 50);
glEnd();
}
}
int init( void )
{
glEnable(GL_DEPTH_TEST);
PositionCamera(0, 1.5f, 6, 0, 1.5f, 0, 0, 1, 0);
return TRUE;
}
int draw_earth(void)
{
glLoadIdentity();
gluLookAt(mPosition.x, mPosition.y, mPosition.z,
mView.x, mView.y, mView.z,
mUpVector.x, mUpVector.y, mUpVector.z);
Draw3DSGrid();
CreatePyramid(-6, 3, 6, 1, 6);
CreatePyramid(6, 3, 6, 1, 6);
CreatePyramid(6, 3, -6, 1, 6);
CreatePyramid(-6, 3, -6, 1, 6);
glTranslatef(mView.x, 0, mView.z);
CreatePyramid(0, 2, 0, 1, 1);
glRotatef(180, 1, 0, 0);
CreatePyramid(0, -1, 0, 1, 1);
}
void display ( void )
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ( );
draw_earth ( );
glPopMatrix ( );
glutSwapBuffers ( );
}
void reshape ( int w, int h )
{
glViewport ( 0, 0, w, h );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
if ( h==0 )
gluPerspective ( 80, ( float ) w, 1.0, 5000.0 );
else
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,150.0f);
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
}
void motion ( int x, int y )
{
glutPostRedisplay ( );
}
void idle_func ( void )
{
glutPostRedisplay ( );
}
#pragma argsused
void keyboard ( unsigned char key, int x, int y )
{
switch ( key ) {
case 'a':
RotateAroundPoint(mView, 0, -SPEED, 0);
break;
case 'd':
RotateAroundPoint(mView, 0, SPEED, 0);
break;
case 'w':
MoveCamera(SPEED);
break;
case 's':
MoveCamera(-SPEED);
break;
default:
break;
}
}
int main ( int argc, char** argv )
{
glutInit ( &argc, argv );
glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE );
glutInitWindowSize ( 640, 480 );
glutCreateWindow ( argv[0] );
init ( );
glutReshapeFunc ( reshape );
glutMotionFunc ( motion );
glutKeyboardFunc ( keyboard );
glutDisplayFunc ( display );
glutIdleFunc ( idle_func );
glutMainLoop ( );
return 0;
}