📄 openglapp.~dpr
字号:
glTexCoord(0, 0); glVertex3f(DeltaX + V2 - V1, 0, DeltaY);
glEnd();
end;
// Roght Faces
I :=1;
For K :=0 to PosZ+1 do
For J :=0 to Length(Face[i, k])-1 do
with face[i][k][j] do
begin
DeltaX := V1 - 1;
DeltaY := K - 1;
glBindTexture(GL_TEXTURE_2D, Texture[TextureIndex]);
glBegin(GL_QUADS);
glTexCoord(U, 0); glVertex3f(DeltaX, 0, DeltaY);
glTexCoord(U, V); glVertex3f(DeltaX, 1, DeltaY);
glTexCoord(0, V); glVertex3f(DeltaX + V2 - V1, 1, DeltaY);
glTexCoord(0, 0); glVertex3f(DeltaX + V2 - V1, 0, DeltaY);
glEnd();
end;
// Up Faces
I :=2;
For K :=PosX to Length(Face[i])-1 do
For J :=0 to Length(Face[i, k])-1 do
with face[i][k][j] do
begin
DeltaX := K - 1;
DeltaY := V1 - 1;
glBindTexture(GL_TEXTURE_2D, Texture[TextureIndex]);
glBegin(GL_QUADS);
glTexCoord(U, 0); glVertex3f(DeltaX, 0, DeltaY);
glTexCoord(U, V); glVertex3f(DeltaX, 1, DeltaY);
glTexCoord(0, V); glVertex3f(DeltaX , 1, DeltaY+ V2 - V1);
glTexCoord(0, 0); glVertex3f(DeltaX , 0, DeltaY+ V2 - V1);
glEnd();
end;
// Down Faces
I :=3;
For K :=0 to PosX+1 do
For J :=0 to Length(Face[i, k])-1 do
with face[i][k][j] do
begin
DeltaX := K - 1;
DeltaY := V1 - 1;
glBindTexture(GL_TEXTURE_2D, Texture[TextureIndex]);
glBegin(GL_QUADS);
glTexCoord(U, 0); glVertex3f(DeltaX, 0, DeltaY);
glTexCoord(U, V); glVertex3f(DeltaX, 1, DeltaY);
glTexCoord(0, V); glVertex3f(DeltaX , 1, DeltaY+ V2 - V1);
glTexCoord(0, 0); glVertex3f(DeltaX , 0, DeltaY+ V2 - V1);
glEnd();
end;
end;
procedure ShowCeiling;
var DeltaX, DeltaY : integer;
EdgeWidth : integer;
begin
EdgeWidth := 4;
if DrawFloor then
begin
DeltaX := 0 - 1;
DeltaY := 0 - 2;
// DrawSurface(0,0,MapWidth,MapHeight,0);
glBindTexture(GL_TEXTURE_2D, Texture[0]);
glBegin(GL_QUADS);
glTexCoord(MapWidth+EdgeWidth, 0); glVertex3f(DeltaX, 0, DeltaY );
glTexCoord(MapWidth+EdgeWidth, MapHeight+EdgeWidth); glVertex3f(DeltaX, 0, DeltaY + MapHeight+EdgeWidth );
glTexCoord(0, MapHeight+EdgeWidth); glVertex3f(DeltaX + MapWidth+EdgeWidth, 0, DeltaY + MapHeight+EdgeWidth );
glTexCoord(0, 0); glVertex3f(DeltaX + MapWidth+EdgeWidth, 0, DeltaY);
glEnd();
end;
// Draw Ceiling
if DrawCeiling then
begin
DeltaX := 0 - InitX;
DeltaY := 0 - InitY;
glBindTexture(GL_TEXTURE_2D, Texture[0]);
glBegin(GL_QUADS);
glTexCoord(MapWidth, 0); glVertex3f(DeltaX, 1, DeltaY );
glTexCoord(MapWidth, MapHeight); glVertex3f(DeltaX, 1, DeltaY + MapHeight );
glTexCoord(0, MapHeight); glVertex3f(DeltaX + MapWidth, 1, DeltaY + MapHeight );
glTexCoord(0, 0); glVertex3f(DeltaX + MapWidth, 1, DeltaY);
glEnd();
end;
end;
procedure ShowGates;
var i : integer;
DeltaX, DeltaY : integer;
begin
for i:= 0 to Length(Gates)-1 do
with Gates[i] do
begin
DeltaX := PosY -2;
DeltaY := PosX -2;
if ((not Opened and UpDown) or (Opened and not UpDown)) then
begin
DrawSurface(DeltaX, DeltaY, DeltaX, DeltaY+1, 2);
DrawSurface(DeltaX+1, DeltaY, DeltaX+1, DeltaY+1, 2);
end else
begin
DrawSurface(DeltaX, DeltaY, DeltaX+1, DeltaY, 2);
DrawSurface(DeltaX, DeltaY+1, DeltaX+1, DeltaY+1, 2);
end;
end;
end;
procedure ShowLights;
var LightPos : array [0 .. 3 ] of GLFloat ;
begin
if not LightOn then exit;
glDisable(GL_LIGHT1);
// LightPos[0]:= Z;
// LightPos[1]:= X;
LightPos[0]:= 0.5;
LightPos[1]:= 0.5;
LightPos[2]:= 0.5;
LightPos[3]:= 0.5;
glLightfv(GL_LIGHT0, GL_POSITION, @LightPos);
glEnable(GL_LIGHT1); // Enable Light One
end;
procedure ShowLittleMap;
const
UpperLeft = 0;
UpperTop = 0;
LittleAlpha = 0.6;
Depth = -0.2;
BlockWidth = 0.01;
Scope = 4;
Len = 0.005;
var PosX, PosY : integer;
ManX, ManY, HeadAngle : glFloat;
i, j : integer;
procedure ShowLittleBlock(dX, dY, pX, pY : integer);
begin
if ((dX<0) or (dX>MapWidth) or (dY<0) or (dY>MapHeight) or (Map[dX, dY]=floor)) then
exit;
if Map[dX, dY]=Wall then glColor(0, 0, 1.0, LittleAlpha);
if Map[dX, dY]=Door then glColor(0, 1.0, 0, LittleAlpha);
if Map[dX, dY]=Lift then glColor(1.0, 0, 0, LittleAlpha);
glBegin(GL_QUADS);
glVertex3f(UpperLeft+pX*BlockWidth, UpperTop+pY* BlockWidth, Depth);
glVertex3f(UpperLeft+pX*BlockWidth, UpperTop+pY* BlockWidth + BlockWidth, Depth);
glVertex3f(UpperLeft+pX*BlockWidth+BlockWidth, UpperTop+pY* BlockWidth + BlockWidth, Depth);
glVertex3f(UpperLeft+pX*BlockWidth+BlockWidth, UpperTop+pY* BlockWidth, Depth);
glEnd();
end;
begin
if not LittleMapOn then exit;
glLoadIdentity(); // Reset The View
glEnable(gl_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
PosX := Trunc(-(Z-2));
PosY := Trunc(-(X-2));
For i:= -Scope to Scope do
For j:= -Scope to Scope do
ShowLittleBlock(PosX+i, PosY +j, i, j);
GLDisAble(gl_Blend);
ManX:= UpperLeft + BlockWidth/4;
ManY:= UpperTop + BlockWidth/4;
HeadAngle:= (270-Heading)*pi/180;
glColor(1, 1, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex3f(ManX+ Len*sin(pi/2-HeadAngle)/4, ManY-Len*cos(pi/2-HeadAngle)/4, depth+0.1);
glVertex3f(ManX- Len*sin(pi/2-HeadAngle)/4, ManY+Len*cos(pi/2-HeadAngle)/4, depth+0.1);
glVertex3f(ManX+ Len*sin(HeadAngle), ManY+ Len*cos(HeadAngle), depth+0.1);
glEnd;
end;
{------------------------------------------------------------------}
{ Function to draw the actual scene }
{------------------------------------------------------------------}
procedure glDraw();
var DeltaX, DeltaY : integer;
d : glFloat;
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glColor(1.0, 1.0, 1.0, 1.0);
glRotate(-Heading, 0, 1, 0);
glTranslatef(X, -0.5, Z);
ShowLights;
ShowCeiling;
ShowFaces;
ShowGates;
ShowLittleMap;
end;
{------------------------------------------------------------------}
{ Initialise OpenGL }
{------------------------------------------------------------------}
procedure glInit();
begin
LoadMap;
glClearColor(0.0, 0.0, 0.0, 0.0); // Black Background
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glClearDepth(1.0); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enable Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glMatrixMode(GL_MODELVIEW);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Realy Nice perspective calculations
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_Nearest); // 线形滤波
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); // 线形滤波
// Load the textures
LoadTexture(ExtractFilePath(Application.ExeName)+'roomFloor.bmp', Texture[0], FALSE);
LoadTexture(ExtractFilePath(Application.ExeName)+'Wall2.bmp', Texture[1], FALSE);
LoadTexture(ExtractFilePath(Application.ExeName)+'Door.bmp', Texture[2], FALSE);
LoadTexture(ExtractFilePath(Application.ExeName)+'roomCeiling.bmp', Texture[3], FALSE);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
// 启动光源
glLightfv(GL_LIGHT1, GL_AMBIENT, @LightAmbient[0]); // Setup The Ambient Light
glLightfv(GL_LIGHT1, GL_DIFFUSE, @LightDiffuse[0]); // Setup The Diffuse Light
glLightfv(GL_LIGHT1, GL_POSITION,@LightPosition[0]); // Position The Light
glEnable(GL_LIGHT1); // Enable Light One
// glEnable(GL_LIGHTING);
LightOn := False;
// glLineWidth(4.0);
LittleMapOn := True;
Heading :=180;
end;
{------------------------------------------------------------------}
{ Handle window resize }
{------------------------------------------------------------------}
procedure glResizeWnd(Width, Height : Integer);
begin
if (Height = 0) then // prevent divide by zero exception
Height := 1;
glViewport(0, 0, Width, Height); // Set the viewport for the OpenGL window
glMatrixMode(GL_PROJECTION); // Change Matrix Mode to Projection
glLoadIdentity(); // Reset View
gluPerspective(45.0, Width/Height, 0.1, 100.0); // Do the perspective calculations. Last value = max clipping depth
glMatrixMode(GL_MODELVIEW); // Return to the modelview matrix
glLoadIdentity(); // Reset View
end;
{------------------------------------------------------------------}
{ Processes all the keystrokes }
{------------------------------------------------------------------}
procedure ProcessKeys;
const
MinDIs = 0.2;
var X1, Z1 : GLFloat;
SignX, SignZ : integer;
i : integer;
IndexX, IndexY : integer;
begin
if Keys[VK_UP] then
begin
if sin(Heading*pi/180)>0 then SignX:=1 else SignX :=-1;
if cos(Heading*pi/180)>0 then SignZ:=1 else SignZ :=-1;
X1 := X + sin(Heading*pi/180)*StepGo;
IndexX := trunc(-(Z-2+SignZ*MinDis));
IndexY := trunc(-(X1-2+SignX*MinDis));
if ((Map[IndexX, IndexY] = Floor) or ((Map[IndexX, IndexY] = Door) and GateOpened(IndexX, IndexY))) then
X := X1;
Z1 := Z + cos(Heading*pi/180)*StepGo; //FrameTime/600; // FrameTime/600=movement speed
IndexX := trunc(-(Z1-2+SignZ*MinDis));
IndexY := trunc(-(X-2+SignX*MinDis));
if ((Map[IndexX, IndexY] = Floor) or ((Map[IndexX, IndexY] = Door) and GateOpened(IndexX, IndexY))) then
Z := Z1;
end;
if Keys[VK_DOWN] then
begin
if sin(Heading*pi/180)>0 then SignX:=1 else SignX :=-1;
if cos(Heading*pi/180)>0 then SignZ:=1 else SignZ :=-1;
X1 := X - sin(Heading*pi/180)*StepGo;
IndexX := trunc(-(Z-2-SignZ*MinDis));
IndexY := trunc(-(X1-2-SignX*MinDis));
if ((Map[IndexX, IndexY] = Floor) or ((Map[IndexX, IndexY] = Door) and GateOpened(IndexX, IndexY))) then
X := X1;
Z1 := Z - cos(Heading*pi/180)*StepGo;
IndexX := trunc(-(Z1-2-SignZ*MinDis));
IndexY := trunc(-(X-2-SignX*MinDis));
if ((Map[IndexX, IndexY] = Floor) or ((Map[IndexX, IndexY] = Door) and GateOpened(IndexX, IndexY))) then
Z := Z1;
end;
if Keys[VK_LEFT] then
begin
Heading := Heading + StepHeading; //(mpos.x - 400)/100 * MouseSpeed;
end;
if Keys[VK_RIGHT] then
begin
Heading := Heading - StepHeading; // (mpos.x - 400)/100 * MouseSpeed;
end;
if Keys[VK_SPACE] then
begin
for i:= 0 to length(Gates)-1 do
if GateNear(i, trunc(-(Z-2)), trunc(-(X-2))) then
begin
Gates[i].Opened:= not Gates[i].Opened;
end;
Keys[VK_SPACE]:= False;
end;
if Keys[VK_F1] then
begin
LightOn := not LightOn;
if LightOn then
glEnable(GL_LIGHTING)
else
glDisable(GL_LIGHTING);
Keys[VK_F1]:=False;
end;
if Keys[VK_TAB] then
begin
LittleMapOn := not LittleMapOn;
Keys[VK_TAB]:= False;
end;
end;
{------------------------------------------------------------------}
{ Determines the application抯 response to the messages received }
{------------------------------------------------------------------}
function WndProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -