PDA

View Full Version : compiles fine but...



11-13-2001, 10:02 AM
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;
}

jabe
11-13-2001, 12:57 PM
sorry, i dont want do the work for you. my time is short enough for my problems http://www.opengl.org/discussion_boards/ubb/smile.gif

but why dont you try to step through the code with a debugger? you can see easily what causes the trouble.

regards,
jan

RobM
11-13-2001, 01:44 PM
It works fine with me. Maybe your graphics drivers are at fault, or maybe get the latest version of glut.

ByteZero
11-13-2001, 09:35 PM
I have compiled it and all I can say is it works, the only thing I get is a waring about a unknow pragma in Line 217.

Inquisitor
11-14-2001, 06:25 AM
What compiler do you use? The #pragma looks like C++ Builder. When starting programs in debug code out of the IDE OpenGL programs tend to crash with a non-fatal exception. Non-fatal means you can continue execution. Use _control87(MCW_EM,MCW_EM) at the beginning of your code to make the FPU more friendly.

blood.angel
11-14-2001, 06:32 AM
Try assign GL data types instead of the compiler ones.
For example use GLfloat instead of float.