#include #include "main.h" #include "globals.h" #include "Entity.h" #include "CBitmapFont.h" #include "TextFactory.h" #include "CLight.h" #include "TextureFactory.h" #include "LightFactory.h" #include "Camera.h" #define MAIN #include "funcProto.h" #undef MAIN #define FLOAT_ERR .1 extern bool keys[]; extern ZVector mouse; ZVector lastMouse; extern HDC hDC; CEntity *pEntity=NULL; CBitmapFont *pFont = NULL; CCamera *pCamera = NULL; CLight *pLight = NULL; CLightFactory *pLightFactory = NULL; ZVector carPos; std::vector pointList; int nextPoint = 0; float speed=.01f; ZVector carDir; void UpdateCar() { static init=1; if(init==1) { carDir.x = 0; carDir.y = 0; carDir.z = 0; if(pointList.size() > 1) { carPos.x = pointList[0]; carPos.z = pointList[1]; } init = 0; nextPoint = 2; } ZVector dir; if(pointList.size() > 4) if(nextPoint != 0) { dir.x = pointList[nextPoint] - pointList[nextPoint-2]; dir.z = pointList[nextPoint+1] - pointList[nextPoint-1]; } else { dir.x = pointList[nextPoint] - pointList[pointList.size()-2]; dir.z = pointList[nextPoint+1] - pointList[pointList.size()-1]; } carPos.x += dir.x*speed; carPos.z += dir.z*speed; if(dir.z == 0) dir.z = 1; if(dir.z < 0.f) { carDir.y = ((180./3.14158)*atan(dir.x/dir.z))-180.0f; } else { carDir.y = (180./3.14158)*atan(dir.x/dir.z); } if(pointList.size() > 4) if((carPos.x <= pointList[nextPoint]+FLOAT_ERR) && (carPos.x >= pointList[nextPoint]-FLOAT_ERR) && (carPos.z <= pointList[nextPoint+1]+FLOAT_ERR) && (carPos.z >= pointList[nextPoint+1]-FLOAT_ERR)) { nextPoint+=2; if(nextPoint == pointList.size()) { nextPoint = 0; } } } void LoadTrack() { FILE *fp = fopen("c:\\data.txt","rt"); float p1,p2; while(!feof(fp)) { fscanf(fp,"%f %f",&p1,&p2); pointList.push_back(p1); pointList.push_back(p2); } fclose(fp); } void fMouseL() { pointList.push_back(pLight->GetPosition().x); pointList.push_back(pLight->GetPosition().z); } void fMouseR() { if(pointList.size()) { pointList.pop_back(); } } void DrawTrack() { glPushMatrix(); glColor3f(0,1,0); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D,1); glBegin(GL_QUADS); glVertex3f(100,0,100); glNormal3f(0,1,0); glTexCoord2d(0,0); glVertex3f(100,0,-100); glNormal3f(0,1,0); glTexCoord2d(0,1); glVertex3f(-100,0,-100); glNormal3f(0,1,0); glTexCoord2d(1,1); glVertex3f(-100,0,100); glNormal3f(0,1,0); glTexCoord2d(1,0); glEnd(); glDisable(GL_TEXTURE_2D); glPopMatrix(); } void DrawCar(float x,float y,float z) { glPushMatrix(); glColor3f(0,0,1); //lRotatef(carDir.x,carDir.y,carDir.z); //carDir.y += 1; //glRotatef(carDir.x,1,0,0); //glLoadIdentity(); glTranslatef(x,y,z); glRotatef(carDir.y,0,1,0); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D,2); glBegin(GL_QUADS); glVertex3f(1,0,1); glTexCoord2d(0,0); glNormal3f(0,1,0); glVertex3f(1,0,-1); glTexCoord2d(0,1); glNormal3f(0,1,0); glVertex3f(-1,0,-1); glTexCoord2d(1,1); glNormal3f(0,1,0); glVertex3f(-1,0,1); glTexCoord2d(1,0); glNormal3f(0,1,0); glEnd(); glDisable(GL_TEXTURE_2D); glPopMatrix(); } void DrawPoint(float x,float y,float z) { glPushMatrix(); glColor3f(1.,0.,0.); glTranslatef(x,y,z); glBegin(GL_LINES); glVertex3f(0,0,-1); glVertex3f(0,0,1); glVertex3f(-1,0,0); glVertex3f(1,0,0); glVertex3f(0,-1,0); glVertex3f(0,1,0); glEnd(); glPopMatrix(); } void DrawPoints() { for(int i=0;iSetLocation(0,100,50); pCamera->UpdatePerspective(); pLightFactory = new CLightFactory(); pLight = pLightFactory->CreateLight(); pLightFactory->EnableLighting(); pLight->Enable(); pFont = TextFactory::BuildBitmapFont(hDC,"arial"); TextureFactory::CreateTexture("c:\\test\\track.bmp", 0); TextureFactory::CreateTexture("c:\\test\\car.bmp", 1); pLight->SetPositionABS(0,50,0); carPos.x = 0; carPos.y = 1; carPos.z = 0; LoadTrack(); } void fDrawGLScene() { static float theta=0; theta++; if(lastMouse.x == -1) { lastMouse.x = mouse.x; lastMouse.y = mouse.y; } pLight->Move((mouse.x - lastMouse.x ) / 2.,0,(mouse.y - lastMouse.y ) / 2.); lastMouse.x = mouse.x; lastMouse.y = mouse.y; //pCamera->Enable(); pLight->Update(); pCamera->SetDestination(pLight->GetPosition().MakeZVector()); pCamera->Enable(); pLightFactory->EnableLighting(false); pLight->DrawLightMarker(); DrawPoints(); //pLightFactory->EnableLighting(true); DrawTrack(); UpdateCar(); DrawCar(carPos.x,carPos.y,carPos.z); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glColor3f(1,0,0); glRasterPos2f(-1,.9); pFont->Print("hello world %d",rand()%1000); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); } void SaveTrack() { FILE *fp=fopen("c:\\data.txt","wt"); for(int i=0;i