急需用C++编程画出一些三维形体,如球,立方体等的程序代码,希望某大神能够降临急救!!!!

急需用C++编程画出一些三维形体,如球,立方体等的程序代码,希望某大神能够降临急救!!!!,第1张

#include<d3d9.h>//Direct3D程序不需的头文件

#include<d3dx9.h>//如果要使用D3DX库就必须加入这个头文件

#include<mmsystem.h>

#include<windows.h>

#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 CUSTOMVERTEX

{

FLOAT 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]=0indices[1]=1indices[2]=2

indices[3]=0indices[4]=2indices[5]=3

//背面

indices[6]=4indices[7]=6indices[8]=5

indices[9]=4indices[10]=7indices[11]=6

//左面

indices[12]=4indices[13]=5indices[14]=1

indices[15]=4indices[16]=1indices[17]=0

//右面

indices[18]=3indices[19]=2indices[20]=6

indices[21]=3indices[22]=6indices[23]=7

//顶面

indices[24]=1indices[25]=5indices[26]=6

indices[27]=1indices[28]=6indices[29]=2

//底面

indices[30]=4indices[31]=0indices[32]=3

indices[33]=4indices[34]=3indices[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实现的

void far bar3d(int x1, int y1, int x2, int y2,int depth,int topflag)当

topflag为非0时, 画出一个三维的长方体。当topflag为0时,三维图形不封顶,

实际上很少这样使用。

void far setfillstyle(int pattern, int color)color的值是当前屏幕图形

模式时颜色的有效值,SOLID_FILL 1 以实填充

void far floodfill(int x, int y, int border)

其中:x, y为封闭图形内的任意一border为边界的颜色,也就是封闭图形轮廓的

颜色。调用了该函数后,将用规定的颜色和图模填满整个封闭图形。

#include<stdlib.h>

#include<graphics.h>

main()

{

int gdriver, gmode

struct fillsettingstype save

gdriver=DETECT

initgraph(&gdriver, &gmode, "")

setbkcolor(BLUE)

cleardevice()

setcolor(LIGHTRED)

setlinestyle(0,0,3)

setfillstyle(1,14)/*设置填充方式*/

bar3d(100,200,400,350,200,1)/*画长方体并租昌坦填充*/

floodfill(450,300,LIGHTRED)

/迅早*填弊桐充长方体另外两个面*/

floodfill(250,150, LIGHTRED)

getch()

closegraph()

}

语法也有错误:glVertex3fv(&vdata [&tindices[i][0][0]])

glVertex3fv(&vdata [tindices[i][1][0]])

glVertex3fv(&vdata [tindices[i][2][0]])

改为:

glVertex3fv(vdata [tindices[i][0]])

   

glVertex3fv(vdata [tindices[i][1]])

     

glVertex3fv(vdata [tindices[i][2]])

我的运行环境是在QT IDE上运行的。4.7.0版。

如果你是在VC上运行应该也要加入opengl32,glut,glut32库(32位WIN上的),

另外,这个程序你是看不到一个球体的,因为你没有使用光照,只能看到一个轮廓。

我的Qt上我加入的库是:opengl32,glut,glee5(glee5是glee是自己编译的替换glut32库)

头加入了:windown.h,gl/glee.h(glee.h就是glee的头文件在网上可以下载,比gl.h要高级,因为WIN上的gl.h只支持到opengl32的1.1版)

源文件:

#include <windows.h>///////////////////////////////

#include <GL/glee.h>

////////////////////////////////

#include <GL\glut.h>

#include <math.h>

#define x .52573

#define z .85965

void mydisplay(void)

{

 

static GLfloat vdata[12][3]={{-x,0.0,z},

{x,0.0,z},

                              {-x,0.0,-z},

                         喊纯     {x,0.0,-z},

{0.0,z,x},

{0.0,z,-x},

                              {0.0,-z,x},

                              {0.0,-z,-x},

                              {z,x,0.0},

{-z,x,0.0},

                              {z,-x,0.0},

{-z,-x,0.0}}

 static GLuint tindices[20][3]={{1,4,0},

{4,9,0},

                                {4,5,9},

                                {8,5,4},

                                {1,8,4},

{1,10,8},

{10,3,8},

 颂渗信                               {8,3,5},

{3,2,5},

{3,7,2},

{3,10,7},

                                {10,6,7},

                                {6,11,7},

{6,0,11},

{6,1,0},

{10,1,6},

                                {11,0,9},

                                {2,11,9},

                                {5,2,9},

                                {11,2,7}}

 int i

glBegin(GL_TRIANGLES)

 for(i=0i<20i++)

{

     

glVertex3fv(vdata [tindices[i][0]])//////////////////////////////////////////////

     glVertex3fv(vdata [tindices[i][1]])/////////////////////////////////////////////

     glVertex3fv(vdata [tindices[i][2]])////////////////////////////////////////////

 }

glEnd()

}

int main(int argc, char *argv[])

{

 glutInit(&argc, argv)

 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE)

 glutInitWindowPosition(100, 100)

 野轮glutInitWindowSize(400, 400)

 glutCreateWindow("第一个OpenGL程序")

 glutDisplayFunc(&mydisplay)

 glutMainLoop()

 return 0

}

后面加了///////////////////////////////////是我修改了的。

我用的连接库是:

LIBS=-lopengl32 -lfreeglut -lglee5


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12493108.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存