📄 runningmodel.cpp
字号:
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "define.h"
#include "runningmodel.h"
#include "terrain.h"
#include "sky.h"
#include "car.h"
#include "tree.h"
RUNNINGMODEL::RUNNINGMODEL(void)
{
int i;
bool b;
float aspect = (float) WIDTH/HEIGHT;
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glDisable(GL_TEXTURE_2D);
//The Terrain
G.SetWorldHeight(20);
G.LoadTerrainHeight(".\\Terrain.bmp");
G.LoadTerrainTexture(".\\Grass.bmp");
//The sky
S.LoadSkyTexture(".\\sky.bmp");
S.SetWorld(G.GetWorldWidth() , G.GetWorldLength() , G.GetWorldHeight());
//The Car object
C.CAR.SetScalerFactor(0.20f);
C.CAR.LoadCarFile(".\\55porsmx.obj");
C.vLocation[0] = 0;
C.vLocation[2] = 0;
C.vLocation[1] = G.GetPositionHeight(C.vLocation[0],C.vLocation[2]) + abs(C.CAR.GetMinHeightVertex());
C.vUp[0] = 0;
C.vUp[1] = 1;
C.vUp[2] = 0;
C.vForward[0] = 0;
C.vForward[1] = 0;
C.vForward[2] = 1;
C.Speed = 0;
//Tree Object
T.NumOfTree = 24;
T.x = new int[T.NumOfTree];
T.z = new int[T.NumOfTree];
T.Rot = new int[T.NumOfTree];
T.TreeSize = 0.1f;
srand((unsigned) time(NULL));
T.TREE.SetScalerFactor(0.8);
T.TREE.LoadTreeFile(".\\tree.obj");
i = 0;
while ( i < T.NumOfTree )
{
b = true;
T.x[i] = -G.GetWorldWidth() / 2 + ( abs(rand()) % (G.GetWorldWidth() - 5)) + 5;
T.z[i] = -G.GetWorldLength() / 2 + ( abs(rand()) % (G.GetWorldLength() - 5)) + 5;
T.Rot[i] = rand() % 360;
for ( int j = 0 ; ( j < i ) && b ; j++ )
if ( abs(T.x[i] - T.x[j]) < 2 || abs(T.z[i] - T.z[j]) < 2 ) b = false;
if (b) i++;
}
FieldList = glGenLists(1);
glNewList(FieldList,GL_COMPILE);
// Draw the GROUND
glColor3f(1.0,1.0,1.0);
G.Draw();
// Draw the Tree
for(int i = 0 ; i < T.NumOfTree ; i++) {
glPushMatrix();
glTranslatef(T.x[i] , G.GetPositionHeight(T.x[i],T.z[i]) , T.z[i]);
glRotatef(T.Rot[i], 0.0f, 1.0f, 0.0f);
glColor3f(1.0,1.0,1.0);
T.TREE.Draw();
glPopMatrix();
}
glEndList();
//cut the back face
glFrontFace(GL_CW);
glEnable(GL_CULL_FACE);
//the screen move and rotation
xRot = 0.0f;
yRot = 0.0f;
xTran = 0.0f;
yTran = 0.0f;
zTran = 0.0f;
//mouse event = NONE
beforeMEvent.MouseState = ::M_EVENT.NONE;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(15.0, aspect, 10.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
RUNNINGMODEL::~RUNNINGMODEL(void)
{
if (T.x) delete [] T.x;
if (T.z) delete [] T.z;
if (T.Rot) delete [] T.Rot;
glDeleteLists(FieldList,1);
}
void RUNNINGMODEL::Display()
{
double PI = 3.14159265;
double C_ = PI / 180.0f;
double MAX;
MAX = G.GetWorldWidth() > G.GetWorldLength() ? G.GetWorldWidth() : G.GetWorldLength();
MAX = MAX > G.GetWorldHeight() ? MAX : G.GetWorldHeight();
MAX = MAX;
GLfloat CarMatrix[16];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( C.vLocation[0] + C.vLocation[1] * tan( C_ * 45 ) , 20 + C.vLocation[1] , 60 + C.vLocation[2] + C.vLocation[1] * tan( C_ * 60 ) , C.vLocation[0] , C.vLocation[1] , C.vLocation[2] , 0, 1, 0);
glPushMatrix();
glTranslatef(xTran, 0.0f , 0.0f);
glTranslatef(0.0f, yTran , 0.0f);
glTranslatef(0.0f, 0.0f , zTran);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
// Draw the SKY
glPushMatrix();
glTranslatef(0.0, -MAX / 3.0f ,0.0);
glColor3f(1.0,1.0,1.0);
glFrontFace(GL_CCW);
glRotatef(180,1,0,0);
S.Draw();
glFrontFace(GL_CW);
glPopMatrix();
glCallList(FieldList);
// Draw the Car
glColor3f(1.0,1.0,1.0);
GetMatrix(&C,CarMatrix);
glMultMatrixf(CarMatrix);
glRotatef(-90, 1.0f, 0.0f, 0.0f);
C.CAR.Draw();
glPopMatrix();
glutSwapBuffers();
}
void RUNNINGMODEL::Mouse(M_EVENT mEvent)
{
if ( beforeMEvent.MouseButton == GLUT_MIDDLE_BUTTON && beforeMEvent.MouseState == GLUT_DOWN && mEvent.STATE == ::M_EVENT.MOTION ) {
if ( beforeMEvent.MouseY != mEvent.MouseY || beforeMEvent.MouseX != mEvent.MouseX ) {
yTran -= (beforeMEvent.MouseY - mEvent.MouseY) / 5.0f;
zTran -= (beforeMEvent.MouseY - mEvent.MouseY) / 1.0f;
beforeMEvent = mEvent;
beforeMEvent.MouseButton = GLUT_MIDDLE_BUTTON;
beforeMEvent.MouseState = GLUT_DOWN;
Display();
}
} else if ( beforeMEvent.MouseButton == GLUT_LEFT_BUTTON && beforeMEvent.MouseState == GLUT_DOWN && mEvent.STATE == ::M_EVENT.MOTION ) {
if ( beforeMEvent.MouseY != mEvent.MouseY || beforeMEvent.MouseX != mEvent.MouseX ) {
xTran -= (beforeMEvent.MouseX - mEvent.MouseX) / 5.0f;
zTran -= (beforeMEvent.MouseY - mEvent.MouseY) / 1.0f;
beforeMEvent = mEvent;
beforeMEvent.MouseButton = GLUT_LEFT_BUTTON;
beforeMEvent.MouseState = GLUT_DOWN;
Display();
}
} else if ( beforeMEvent.MouseButton == GLUT_RIGHT_BUTTON && beforeMEvent.MouseState == GLUT_DOWN && mEvent.STATE == ::M_EVENT.MOTION ) {
if ( beforeMEvent.MouseY != mEvent.MouseY || beforeMEvent.MouseX != mEvent.MouseX ) {
xRot += (beforeMEvent.MouseY - mEvent.MouseY) / 1;
yRot += (beforeMEvent.MouseX - mEvent.MouseX) / 1;
xRot = (GLfloat)((const int) xRot % 360);
yRot = (GLfloat)((const int) yRot % 360);
beforeMEvent = mEvent;
beforeMEvent.MouseButton = GLUT_RIGHT_BUTTON;
beforeMEvent.MouseState = GLUT_DOWN;
Display();
}
} else beforeMEvent = mEvent;
}
void RUNNINGMODEL::KeyBoard(K_EVENT kEvent)
{
}
void RUNNINGMODEL::Reshape(GLsizei Width , GLsizei Height)
{
SetWinSize( Width , Height );
Display();
}
void RUNNINGMODEL::GameTimer(int n)
{
Display();
}
void RUNNINGMODEL::GetMatrix(CarStruct *pC , GLfloat *pMatrix)
{
GLfloat vX[3];
//CrossProduct
vX[0] = pC->vUp[1] * pC->vForward[2] - pC->vForward[1] * pC->vUp[2];
vX[1] = -pC->vUp[0] * pC->vForward[2] + pC->vForward[0] * pC->vUp[2];
vX[2] = pC->vUp[0] * pC->vForward[1] - pC->vForward[0] * pC->vUp[1];
memcpy( pMatrix , vX , sizeof(GLfloat) * 3 );
pMatrix[3] = 0.0f;
memcpy( pMatrix + 4, pC->vUp , sizeof(GLfloat) * 3 );
pMatrix[7] = 0.0f;
memcpy( pMatrix + 8, pC->vForward , sizeof(GLfloat) * 3 );
pMatrix[11] = 0.0f;
memcpy( pMatrix + 12, pC->vLocation , sizeof(GLfloat) * 3 );
pMatrix[15] = 1.0f;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -