急需用C++编程画出一些三维形体,如球,立方体等的程序代码,希望某大神能够降临急救!!!!
#include //Direct3D程序不需的头文件#include//如果要使用D3DX库就必须加入这个头文件#include#include#pragma comment(lib,"d3d9.lib")#pragma comment(lib,"d3dx9.lib")#pragma comment(lib,"winmm.lib")//4个本程序用到的全局变量LPDIRECT3D9 g_pD3D = NULL; //Direct3D指针LPDIRECT3DDEVICE9 g_pD3DDevice = NULL;//Direct3D设备LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;//顶点缓冲区指针LPDIRECT3DINDEXBUFFER9g_pIB = NULL;//索引缓冲区指针//定义使用到的顶点结构struct CUSTOMVERTEXFLOAT x,y,z;DWORD color;//定义上述顶点的FVF结构#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)//初始化Direct3D设备过程函数HRESULT InitD3D(HWND hWnd)//创建Direct3D对象if(NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))return E_FAIL;//填写创建Direct3D设备结构体D3DPRESENT_PARAMETERS d3dpp;ZeroMemory(&d3dpp,sizeof(d3dpp));d3dpp.Windowed = false;//运行在窗口模式d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;//最高效的工作方式d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;//屏幕缓冲区数据像素格式//创建Direct3D设备if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_pD3DDevice)))return E_FAIL;return S_OK;//初始化与本程序绘图相关的数据函数HRESULT InitDraw()//创建顶点缓冲区g_pD3DDevice->CreateVertexBuffer( 8 * sizeof(CUSTOMVERTEX),D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&g_pVB,0);//创建索引缓冲区g_pD3DDevice->CreateIndexBuffer(36 * sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&g_pIB,0);//创建立方体的8个顶点,注意每个顶点的颜色都不同//在绘制时,Direct3D会根据顶点颜色对三角形内部像素进行插值,所以立方体看起来是彩色的CUSTOMVERTEX source_vertices[] = {{-1.0f,-1.0f,-1.0f,D3DCOLOR_XRGB(255,0,0)},{-1.0f,1.0f,-1.0f,D3DCOLOR_XRGB(0,255,0)},{1.0f,1.0f,-1.0f,D3DCOLOR_XRGB(0,0,255)},{1.0f,-1.0f,-1.0f,D3DCOLOR_XRGB(255,255,0)},{-1.0f,-1.0f,1.0f,D3DCOLOR_XRGB(255,0,255)},{-1.0f,1.0f,1.0f,D3DCOLOR_XRGB(0,255,255)},{1.0f,1.0f,1.0f,D3DCOLOR_XRGB(0,0,0)},{1.0f,-1.0f,1.0f,D3DCOLOR_XRGB(255,255,255)},//把上面的顶点数据复制到创建的缓冲区内CUSTOMVERTEX * pVertices;if(FAILED(g_pVB->Lock(0,8*sizeof(CUSTOMVERTEX),(VOID **)&pVertices,0)))return E_FAIL;memcpy(pVertices,source_vertices,8 * sizeof(CUSTOMVERTEX));g_pVB->Unlock();//定义索引缓冲区内容,这里可以手动定义,也可以用数组设定好,然后内容复制,向上面顶点缓冲区一样WORD *indices = 0;g_pIB->Lock(0,0,(void **) & indices,0);//正面indices[0]=0;indices[1]=1;indices[2]=2;indices[3]=0;indices[4]=2;indices[5]=3;//背面indices[6]=4;indices[7]=6;indices[8]=5;indices[9]=4;indices[10]=7;indices[11]=6;//左面indices[12]=4;indices[13]=5;indices[14]=1;indices[15]=4;indices[16]=1;indices[17]=0;//右面indices[18]=3;indices[19]=2;indices[20]=6;indices[21]=3;indices[22]=6;indices[23]=7;//顶面indices[24]=1;indices[25]=5;indices[26]=6;indices[27]=1;indices[28]=6;indices[29]=2;//底面indices[30]=4;indices[31]=0;indices[32]=3;indices[33]=4;indices[34]=3;indices[35]=7;g_pIB->Unlock();//设置摄像机D3DXVECTOR3 position(0.0f,0.0f,-3.0f);D3DXVECTOR3 target(0.0f,0.0f,0.0f);D3DXVECTOR3 up(0.0f,1.0f,0.0f);D3DXMATRIX V;D3DXMatrixLookAtLH(&V,&position,&target,&up);g_pD3DDevice->SetTransform(D3DTS_VIEW,&V);//设置投影矩阵D3DXMATRIX proj;D3DXMatrixPerspectiveFovLH(& proj,D3DX_PI * 0.5f,(float)800/(float) 600,1.0f,1000.0f);g_pD3DDevice->SetTransform(D3DTS_PROJECTION,&proj);//因为没有使用材质,纹理信息,所以关闭灯光显示出本身色彩g_pD3DDevice->SetRenderState(D3DRS_LIGHTING,FALSE);return S_OK;void Cleanup()if(g_pVB != NULL)g_pVB->Release();if(g_pIB != NULL)g_pIB->Release();if(g_pD3DDevice != NULL)g_pD3DDevice->Release();if(g_pD3D != NULL)g_pD3D ->Release();void Render()//清除屏幕缓冲区到蓝色屏幕g_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);//开始绘制if(SUCCEEDED(g_pD3DDevice->BeginScene()))//创建沿3个轴渲染的矩阵D3DXMATRIXA16 matWorld_X;D3DXMatrixIdentity(&matWorld_X);D3DXMatrixRotationX(&matWorld_X,timeGetTime()/500.0f);D3DXMATRIXA16 matWorld_Y;D3DXMatrixIdentity(&matWorld_Y);D3DXMatrixRotationY(&matWorld_Y,timeGetTime()/500.0f);D3DXMATRIXA16 matWorld_Z;D3DXMatrixIdentity(&matWorld_Z);D3DXMatrixRotationZ(&matWorld_Z,timeGetTime()/500.0f);//设置物体的世界矩阵g_pD3DDevice->SetTransform(D3DTS_WORLD,&(matWorld_X*matWorld_Y*matWorld_Z));//挂接渲染流水线,设定FVF值,设置索引,渲染g_pD3DDevice->SetStreamSource(0,g_pVB,0,sizeof( CUSTOMVERTEX));g_pD3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);g_pD3DDevice->SetIndices(g_pIB);g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12);//结束绘制g_pD3DDevice->EndScene();//显示到屏幕g_pD3DDevice->Present(NULL,NULL,NULL,NULL);//消息回调函数。只处理退出消息,所以鼠标一直显示忙状态LRESULT WINAPI MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)switch(msg)case WM_DESTROY:Cleanup();PostQuitMessage(0);return 0;return DefWindowProc(hWnd,msg,wParam,lParam);INT WINAPI WinMain(HINSTANCE hInst,HINSTANCE , LPSTR,INT)//注册窗口WNDCLASSEX wc = {sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,"D3D Tutorical",NULL};RegisterClassEx(&wc);//创建窗口HWND hWnd = CreateWindow("D3D Tutorical","3D立方体",WS_OVERLAPPEDWINDOW,100,100,800,600,GetDesktopWindow(),NULL,wc.hInstance,NULL);//嵌套if保证全部初始化成功才可以进入循环运行if(SUCCEEDED (InitD3D(hWnd)))if(SUCCEEDED(InitDraw())){//显示窗口ShowWindow(hWnd,SW_SHOWDEFAULT);UpdateWindow(hWnd);//优化的消息循环MSG msg;ZeroMemory(&msg,sizeof(msg));while(msg.message != WM_QUIT){if(PeekMessage(&msg,NULL,0U,0U,PM_REMOVE)){TranslateMessage(&msg);DispatchMessage(&msg);}else{Render();}}}UnregisterClass("D3D Tutorical",wc.hInstance);return 0;这个是CSDN上的一个立方体的C++代码,用D3D实现的
如何编写代码进行简单的编程入门?
代码编程入门学习介绍如下所示。1、掌握编程思想很多人学习编程的时候一上来就阅读大量的书籍,死记硬背各种语法,然而到最后成效并不大。如果想成为一名优秀的程序员,最重要的是掌握编程思想、找到编程感觉,而不是死记硬背语言本身。一名程序员的高级境界,是在反复的实践、观察、分析、比较、总结中潜移默化积累的。要学会编程思想、找到编程感觉,必须从实实在在的编程实际工作中去实践和体验。不少朋友在学习编程时只简单地学习语法、结构,枯燥而且目的性也不强,会大大降低初学者的兴趣和学习效果。而一般的书籍,也只从语言本身去讲解语法和举一些针对这些语法的应用例子,跟使用说明书没有太大差别,没有起到灌输编程思想,培养编程感觉的作用。2、学习C语言或者python语言python语法更加简单简洁易学,而且有各种强大的库,扩展库,可过程可对象,如果是为了通过编程这一手段来迅速的做出有用、实用的程序和界面,可以学python。入门是从Basic语言开始的,大概熟悉程序的基础语法与逻辑后,进阶C语言。现代很多语言都是从C语言发展而来的,所以可以以C语言做跳板进阶其他面向对象的高级语言。3、学习JavaScript若想要见效快的话,大家可以从JavaScript开始学起,毕竟你安装的浏览器就已经自带了语言环境了,并且JS现在基本前后通吃,你既可以写前端界面也可以写后端服务,是一门性价比很高的语言。