Nickyu0712 0 Newbie Poster

Every time I try to execute my program I get an error:"The instruction at "0x00401585" refereced memory at "0x00000028".The memory could not be "read"". Pleas help me!

I have linked -lagl -lalleg -luser32 -lgdi32 -lopengl32 -lglu32
I can't see the problem.
:(

#include <gl\gl.h>
#include <GL\glu.h>

#include <allegro.h>
#include <alleggl.h>

int timer;

void update_timer() {
	timer++;
}
END_OF_FUNCTION(update_timer);
bool light=true, fog=true;

void initGL(bool FULLSCREEN,int h,int w); // initializate openGL
void correctPerspective(GLfloat z1,GLfloat z2); // correct the perspective

void DrawScene(GLfloat X,GLfloat Y,GLfloat Z,GLfloat dist);
BITMAP* tex;

GLuint	filter;				// Which Filter To Use
GLuint	texture[3];	

const GLfloat ACC=0.003f;
const GLfloat DACC=0.002f;

void initRendering(); // in this function we enabble GL modules

int main() {
	float d = - 4,rtriX=0,rtriY=0,rtriZ=0,AX=0,AY=0,AZ=0;

	/* Initialize both Allegro and AllegroGL */
	allegro_init();	
	initGL(true,1366,768);
	install_keyboard();
	install_timer();
	LOCK_VARIABLE(timer);
	LOCK_FUNCTION(update_timer);
	install_int_ex(update_timer, BPS_TO_TIMER(30));
	tex=load_bitmap("Crate.bmp",NULL);
	GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f }; 
	GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
	GLfloat LightPosition[]= { 1.0f, 1.0f, 1.0f, 1.0f };	
	float fogColor[4]={0.3,0.3,0.3,0.6};
	
	glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);		// Setup The Ambient Light
	glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);		// Setup The Diffuse Light
	glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);	// Position The Light
	glEnable(GL_LIGHT1);
	
	//==============================================
	glFogfv(GL_FOG_COLOR, fogColor); 
	glHint(GL_FOG_HINT, GL_NICEST);   
    glFogi(GL_FOG_MODE, GL_LINEAR);
    glFogf(GL_FOG_DENSITY, 0.1);
    glFogf(GL_FOG_START, 1.0);
    glFogf(GL_FOG_END, 8.0);
	//=================================================
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth
	
	initRendering();
	/* Correct the perspective */
	correctPerspective(1.5f, 60.0f);
	glTexEnvi(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE, GL_DECAL);
	//============================
	glGenTextures(3, &texture[0]);					// Create Three Textures

		// Create Nearest Filtered Texture
		glBindTexture(GL_TEXTURE_2D, texture[0]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, tex[0].w, tex[0].h, 0, GL_RGB, GL_UNSIGNED_BYTE, tex[0].dat);

		// Create Linear Filtered Texture
		glBindTexture(GL_TEXTURE_2D, texture[1]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, tex[0].w, tex[0].h, 0, GL_RGB, GL_UNSIGNED_BYTE, tex[0].dat);

		// Create MipMapped Texture
		glBindTexture(GL_TEXTURE_2D, texture[2]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, tex[0].w, tex[0].h, GL_RGB, GL_UNSIGNED_BYTE, tex[0].dat);
	//============================
	while(!key[KEY_ESC]) {
		while( timer >0 ) {
			if( key[KEY_UP] )
			rtriX-=ACC;
			if( key[KEY_DOWN] )
				rtriX+=ACC;
			else if(!key[KEY_UP] && !key[KEY_DOWN] && rtriX !=0) {
				if( rtriX > 0 )
					rtriX-=DACC;
				if( rtriX < 0 )
					rtriX+=DACC;
			}
			if( key[KEY_RIGHT] )
				rtriY+=ACC;
			if( key[KEY_LEFT] )
				rtriY-=ACC;
			else if(!key[KEY_LEFT] && !key[KEY_RIGHT] && rtriY !=0) {
				if( rtriY > 0 )
					rtriY-=DACC;
				if( rtriY < 0 )
					rtriY+=DACC;
			}
			if( key[KEY_A] )
				rtriZ-=ACC;
			if( key[KEY_D]  )
				rtriZ+=ACC;
			if( !key[KEY_A] && !key[KEY_D] && rtriZ !=0) {
				if( rtriZ > 0 )
					rtriZ-=DACC;
				if( rtriZ < 0 )
					rtriZ+=DACC;
			}
			if(key[KEY_Z])
				d+=0.1f;
			if(key[KEY_X])
				d-=0.1f;
			if(key[KEY_I]&&key[KEY_L])
				light=true;
			if(key[KEY_O]&&key[KEY_L])
				light=false;
			if(key[KEY_I]&&key[KEY_F])
				fog=true;
			if(key[KEY_O]&&key[KEY_F])
				fog=false;
			if(key[KEY_T]&&key[KEY_1])
				filter=0;
			if(key[KEY_T]&&key[KEY_2])
				filter=1;
			if(key[KEY_T]&&key[KEY_3])
				filter=2;
			timer--;
			}
			AX=(AX+rtriX);
			AY=(AY+rtriY);
			AZ=(AZ+rtriZ);
			DrawScene(AX,AY,AZ,d);

	}
	/* Wait for the user to press a key */
	readkey();
	
	/* Finished. */
	return 0;
}
END_OF_MAIN();

void initRendering() {
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LEQUAL);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glEnable(GL_FOG);
	glEnable(GL_LIGHTING);
	glEnable(GL_POLYGON_SMOOTH);
	glEnable(GL_LINE_SMOOTH);
	// more modules...
}

void initGL(bool FULLSCREEN,int w,int h) {
	install_allegro_gl();
	allegro_gl_set( AGL_COLOR_DEPTH, 32 );
	allegro_gl_set( AGL_Z_DEPTH, 8 );
	allegro_gl_set( AGL_SUGGEST, AGL_COLOR_DEPTH | AGL_Z_DEPTH );
	if(FULLSCREEN)
		if(set_gfx_mode( GFX_OPENGL_FULLSCREEN, w, h, 0, 0 )) {
			allegro_message( "There was an error creating the window" );
		}
	if(!FULLSCREEN)
		if(set_gfx_mode( GFX_OPENGL_WINDOWED, w, h, 0, 0 )) {
			allegro_message( "There was an error creating the window" );
		}									

}

void correctPerspective(GLfloat z1,GLfloat z2) {
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	float	fMinX = -1.0f, fMaxX = 1.0f,
		fMinY = -1.0f, fMaxY = 1.0f;
	float	fAspectRatio	= SCREEN_W / (float)SCREEN_H;
	if ( fAspectRatio > 1.0f ) {
		fMinY	= fMinX / fAspectRatio;
		fMaxY	= fMaxX / fAspectRatio;
	}
	else {
		fMinX	= fMinY * fAspectRatio;
		fMaxX	= fMaxY * fAspectRatio;
	}
	glFrustum( fMinX, fMaxX, fMinY, fMaxY, z1, z2 );
}



void DrawScene(GLfloat X,GLfloat Y,GLfloat Z,GLfloat dist) {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	if( light==true )
		glEnable(GL_LIGHT1);
	if( light==false)
		glDisable(GL_LIGHT1);
	if( fog==true )
		glEnable(GL_FOG);
	if( fog==false)
		glDisable(GL_FOG);
	glPushMatrix();
	glTranslatef(0.0f,0.0f,dist);
	glBindTexture(GL_TEXTURE_2D, texture[filter]);
	glRotatef(X,1.0f,0.0f,0.0f);
	glRotatef(Y,0.0f,1.0f,0.0f);
	glRotatef(Z,0.0f,0.0f,1.0f);
	glBegin(GL_QUADS);
		glColor4f(1.0f,0.0f,0.0f,0.5f);
		glNormal3f( 0.0f, 0.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		// Back Face
		glNormal3f( 0.0f, 0.0f,1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		// Top Face
		glNormal3f( 0.0f, -1.0f, 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		// Bottom Face
		glNormal3f( 0.0f,1.0f, 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		// Right face
		glNormal3f( -1.0f, 0.0f, 0.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		// Left Face
		glNormal3f(1.0f, 0.0f, 0.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
	glEnd();
	glPopMatrix();
	glPushMatrix();
	glTranslatef(0.0f,0.0f,-20.0f);
	glBegin(GL_QUADS);
		glColor3f(0.0f,0.0f,0.0f);
		glVertex2f(-15.0f,10.0f);
		glVertex2f(15.0f,10.0f);
		glVertex2f(15.0f,-10.0f);
		glVertex2f(-15.0f,-10.0f);
	glEnd();
	glPopMatrix();
	allegro_gl_flip();
}

:icon_confused: