// moon.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include <d3dx9.h>
#include "Geometry.h"
#include "creatTeapot.h"
#include "Car.h"
#include "roadMap.h"
#include <windows.h>
#include <mmsystem.h>
#include "string.h"
#include "Ligth.h"
#include "CMySocket.h"
#include"Resource.h"
#define MAX_LOADSTRING 100
#define M_PI 3.14
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pD3DDevice = NULL;
CreatTeapot* teapot1=0;
CGeometry* tree = 0;
Car* car = 0;
Car* car1=0;
CRoad* road = 0;
CLight* light = 0;
D3DXVECTOR3 teapotPos[9];
float Xposi = 0.0f; //物体初始位置
float Xposi1 = 6.0f;
float Zposi1=-30.0;
float Zposi =-30.0f;
char pIp[32];
struct CarXZ
{
float X,Z;
D3DXVECTOR3 teapotPos[9];
};
// 此代码模块中包含的函数的前向声明:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
CMySocket mySocket;
//定义定点信息的结构体
//COM对象释放的宏定义
#define SafeRelease(pObject) if(pObject != NULL) {pObject->Release(); pObject=NULL;}
DWORD WINAPI RecvProc(LPVOID lpParameter);
//初始化D3D设备
HRESULT InitialiseD3D(HWND hWnd)
{
mySocket.InitSocket();
SOCKET socket=mySocket.m_socket;
HANDLE handle=CreateThread(NULL,0,RecvProc,NULL,0,NULL);
//得到一个D3D9的对象
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_pD3D == NULL)
{
return E_FAIL;
}
//得到当前的显示模式
//Get the current display mode
D3DDISPLAYMODE d3ddm;
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
{
return E_FAIL;
}
//建立一个存储设备设置的结构体
//Create a structure to hold the settings for our device
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.AutoDepthStencilFormat=D3DFMT_D16;
d3dpp.EnableAutoDepthStencil=TRUE;
//Fill the structure.
//We want our program to be windowed, and set the back buffer to a format
//that matches our current display mode
d3dpp.Windowed = TRUE;//为窗口还是全屏
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; //
d3dpp.BackBufferFormat = d3ddm.Format;//显示模式
//创建D3D设备
//Create a Direct3D device.
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
{
return E_FAIL;
}
tree = new CGeometry(g_pD3DDevice); //模型树载入
tree->InitGeometry(".\\model\\pine04.x");
car = new Car(g_pD3DDevice);
car->InitGeometry(".\\model\\car.x");
car1 = new Car(g_pD3DDevice);
car1->InitGeometry(".\\model\\car.x");
road = new CRoad(g_pD3DDevice);
road->InitialiseVertexBuffer();
road->InitIndexBuffer();
light = new CLight(g_pD3DDevice);
light->lightInit();
teapot1 = new CreatTeapot(g_pD3DDevice);
//设置渲染时的剔出模式
g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
//关闭灯光
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, true);
//深度测试
g_pD3DDevice->SetRenderState(D3DRS_ZENABLE,TRUE);
g_pD3DDevice->SetRenderState(D3DRS_ZFUNC,2);
g_pD3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE );
return S_OK;
}
DWORD WINAPI RecvProc(LPVOID lpParameter)
{
SOCKADDR_IN addrFrom;
int Len=sizeof(SOCKADDR) ;
CarXZ posi;
int retval;
while(true)
{
retval = recvfrom(mySocket.m_socket,&(char&)posi,sizeof(CarXZ),0,(SOCKADDR*)&addrFrom,&Len);
if(SOCKET_ERROR==retval)
{
MessageBox(NULL,"接受失败",NULL,MB_YESNO);
break;
}
Xposi1=posi.X;
Zposi1=posi.Z;
for(int i=0;i<9;i++)
{
teapotPos[i].x=posi.teapotPos[i].x;
teapotPos[i].y=posi.teapotPos[i].y;
teapotPos[i].z=posi.teapotPos[i].z;
}
}
return 0;
}
//设置旋转、移动和缩放,即设置世界变换
void SetupRotation()
{
static float y = 0.0f;
D3DXMATRIX matWorld;
D3DXMATRIX matWorldX;
D3DXMATRIX matWorldY;
D3DXMatrixRotationX(&matWorldX,0);
D3DXMatrixRotationY(&matWorldY,0);
D3DXMatrixMultiply(&matWorld,&matWorldX,&matWorldY);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
}
void SetTeapotPosi()
{
//D3DXVECTOR3
D3DXMATRIX matWorld;
D3DXMATRIX matScale;
D3DXMatrixScaling(&matScale, 2,2,2);//尺寸大小
teapot1->m_tMax[0] =(teapot1->m_vMaxbeg)*2;//包围盒随茶壶一起缩放
teapot1->m_tMin[0] =(teapot1->m_vMinbeg)*2;//同上
D3DXMatrixTranslation(&matWorld, 0.0, 2.0,0.0);
teapot1->m_tMax[0]=teapot1->m_vMaxbeg+D3DXVECTOR3(0.0,2.0,0.0);//包围盒随茶壶移动
teapot1->m_tMin[0]=teapot1->m_vMinbeg+D3DXVECTOR3(0.0,2.0,0.0);
D3DXMatrixMultiply(&matWorld,&matScale,&matWorld);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
}
void SetCarPosi()
{
D3DXMATRIX matWorld,MatrixR;
D3DXMATRIX matScale;
D3DXMatrixScaling(&matScale, 1,1,1);//尺寸大小
car->m_vMax =(car->m_vMaxbeg)*1;//包围盒随车缩放
car->m_vMin =(car->m_vMinbeg)*1;
D3DXMatrixTranslation(&matWorld, Xposi,1.6,Zposi);
car->m_vMax=car->m_vMaxbeg+D3DXVECTOR3(Xposi,1.6,Zposi);//包围盒随车移动
car->m_vMin=car->m_vMinbeg+D3DXVECTOR3(Xposi,1.6,Zposi);
D3DXMatrixRotationY(&MatrixR,M_PI);
D3DXMatrixMultiply(&MatrixR,&matScale,&MatrixR);
D3DXMatrixMultiply(&matWorld,&MatrixR,&matWorld);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
}
void SetCar1Posi()
{
D3DXMATRIX matWorld,MatrixR;
D3DXMATRIX matScale;
D3DXMatrixScaling(&matScale, 1,1,1);//尺寸大小
car1->m_vMax =(car1->m_vMaxbeg)*1;//包围盒随车缩放
car1->m_vMin =(car1->m_vMinbeg)*1;
D3DXMatrixTranslation(&matWorld, Xposi1,1.6,Zposi1);
car1->m_vMax=car1->m_vMaxbeg+D3DXVECTOR3(Xposi1,1.6,Zposi1);//包围盒随车移动
car1->m_vMin=car1->m_vMinbeg+D3DXVECTOR3(Xposi1,1.6,Zposi1);
D3DXMatrixRotationY(&MatrixR,M_PI);
D3DXMatrixMultiply(&MatrixR,&matScale,&MatrixR);
D3DXMatrixMultiply(&matWorld,&MatrixR,&matWorld);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
}
void Send()
{
SOCKADDR_IN addrto;
addrto.sin_family =AF_INET;
addrto.sin_port = htons(6000);
addrto.sin_addr.S_un.S_addr = inet_addr(pIp);
CarXZ posi;
posi.X=Xposi;
posi.Z=Zposi;
sendto(mySocket.m_socket,&(char&)posi,sizeof(CarXZ),0,(SOCKADDR*)&addrto,sizeof(SOCKADDR));
}
//小车的移动
void CarMove()
{
if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
{ Xposi-=0.2;
if(Xposi <= -15.0f)
Xposi = -15.0;
Send();
}
if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
{ Xposi+=0.2;
if(Xposi>=15.0f)
Xposi=15.0;
Send();
}
if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
{ Zposi+=0.2;
Send();
}
if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
{ Zposi-=0.2;
if(Zposi<=-45)
Zposi=-45;
Send();
}
}
//设置摄像机
void SetupCamera()
{
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 15.0f,-65.0f), //摄像机的位置
&D3DXVECTOR3(0.0f, 0.0f, 10.0f), //摄像机的朝向
&D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //摄像机的顶方向
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
}
void SetupPerspective()
{
//Here we specify the field of view, aspect ration and near and far clipping planes.
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 500.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
bool move=true;
void Render()
{
if(g_pD3DDevice == NULL)
{
return;
}
//将后备缓冲区全部清空为黑色
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(91, 183, 250), 1.0f, 0);
//开始绘制场景
g_pD3DDevice->BeginScene();
CarMove();//小车的移动
//设置世界变换、视图变换和投影变换
SetupRotation();
SetupCamera();
SetupPerspective();
road->RoadRender(move) ;//地图绘制
g_pD3DDevice->Set
kehuduan.rar_969316.com_Direct3D 小游戏_direct3d游戏
版权申诉
135 浏览量
2022-09-20
18:14:08
上传
评论
收藏 3.25MB RAR 举报
weixin_42651887
- 粉丝: 80
- 资源: 1万+
最新资源
- 基于Python的高德地图+58租房(源码+使用文档)
- C语言文件读写代码.docx
- python的概要介绍与分析
- module-0982.SyncTensorsGraph.9515.sm-8.0-gpu-after-optimizations
- ChromeSetup20220804.exe
- 讲解记录 基于Java的小区物业管理系统
- module-0004.SyncTensorsGraph.7578.sm-8.0-gpu-after-optimizations
- python39.dll
- 【Matlab 六自由度机器人】Fixed Angles(固定角度) 和 Euler Angles(欧拉角) 之间的区别
- STLINK下载器调试过程中常见问题排查
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈