如何用c++画c语言输出三角形形

DirectX 9.0 C++ 教程 绘制三角形 - 推酷
DirectX 9.0 C++ 教程 绘制三角形
注:开发环境设定参考 上篇文章
也可以参考下这篇
本例子效果图(来源 DirectX 9.0 SDK):
1.定义一个自定义的顶点(Vertex)类型
// A structure for our custom vertex type
struct CUSTOMVERTEX
FLOAT x, y, z, // The transformed position for the vertex
// The vertex color
//这里加入了颜色,后面设计到纹理(Texture)时,还可以加入纹理属性
struct CUSTOMVERTEX
FLOAT x,y,z,
FLOAT u,v;
//texture coordinate
2. 设置顶点缓存(Setting Up the Vertex Buffer)
// 初始化三个顶点
CUSTOMVERTEX Vertices[] =
50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255,0,0), }, // x, y, z, rhw, color //颜色为蓝色
{ 250.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,255,0), },//颜色为绿色
50.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,0,255), },//颜色为蓝色
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold Vertices
// 创建顶点缓存
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
&if( FAILED( g_pd3dDevice-&CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX), //空间大小,3个点故乘以3
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
return E_FAIL;
// 先锁定需要的内存块,再复制数据
if( FAILED( g_pVB-&Lock( 0, sizeof(Vertices), (void**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, Vertices, sizeof(Vertices) );
g_pVB-&Unlock();
3.绘制三角形(Rendering the Display)
// Clear the backbuffer to a blue color
g_pd3dDevice-&Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dDevice-&BeginScene() ) )
//Binds a vertex buffer to a device data stream
g_pd3dDevice-&SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice-&SetFVF( D3DFVF_CUSTOMVERTEX );//FVF 指flexible vertex format
&g_pd3dDevice-&DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 ); //D3DPT_TRIANGLELIST表示三角形
// End the scene
g_pd3dDevice-&EndScene();
// Present the backbuffer contents to the display
g_pd3dDevice-&Present( NULL, NULL, NULL, NULL );
4.上面设计到的一些函数解释
//CreateVertexBuffer 函数原型
HRESULT CreateVertexBuffer(
UINT Length, //长度,size of vertex buffer
DWORD Usage, //未使用D3DUSAGE_DYNAMIC,则为静态缓存,地形或城市建筑适合
DWORD FVF, //这里用了D3DFVF_XYZRHW和D3DFVF_DIFFUSE,因为顶点定义包含了rhw和颜色
D3DPOOL Pool,//D3DPOOL_DEFAULT
IDirect3DVertexBuffer9** ppVertexBuffer,
HANDLE* pSharedHandle//保留参数,未使用,设置为NULL);
//DrawPrimitive 函数原型
HRESULT&DrawPrimitive(&&&&&&
&&& D3DPRIMITIVETYPE&PrimitiveType,//&D3DPT_POINTLIST点,LINELIST线,D3DPT_TRIANGLELIST三角形 等
&&& UINT&StartVertex, //index of first vertex to load
&&& UINT&PrimitiveCount //数量
5.全部源码
//-----------------------------------------------------------------------------
// File: Vertices.cpp
// Desc: In this tutorial, we are rendering some Vertices. This introduces the
concept of the vertex buffer, a Direct3D object used to store
Vertices. Vertices can be defined any way we want by defining a
custom structure and a custom FVF (flexible vertex format). In this
tutorial, we are using Vertices that are transformed (meaning they
are already in 2D window coordinates) and lit (meaning we are not
using Direct3D lighting, but are supplying our own colors).
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include &d3d9.h&
#include &strsafe.h&
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9
= NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9
g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB
= NULL; // Buffer to hold Vertices
// 三角形顶点 结构体
struct CUSTOMVERTEX
FLOAT x, y, z, // The transformed position for the vertex
// The vertex color
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
// Create the D3D object.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// 初始化 D3DPRESENT_PARAMETERS 结构体
D3DPRESENT_PARAMETERS d3
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
// 创建 D3DDevice
if( FAILED( g_pD3D-&CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
return E_FAIL;
// Device state would normally be set here
return S_OK;
//-----------------------------------------------------------------------------
// Name: InitVB()
// Desc: Creates a vertex buffer and fills it with our Vertices. The vertex
buffer is basically just a chuck of memory that holds Vertices. After
creating it, we must Lock()/Unlock() it to fill it. For indices, D3D
also uses index buffers. The special thing about vertex and index
buffers is that they can be created in device memory, allowing some
cards to process them in hardware, resulting in a dramatic
performance gain.
//-----------------------------------------------------------------------------
HRESULT InitVB()
// 初始化三角形的三个顶点坐标
CUSTOMVERTEX Vertices[] =
50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255,0,0), }, // x, y, z, rhw, color
{ 250.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,255,0), },
50.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,0,255), },
// 创建 顶点buffer
if( FAILED( g_pd3dDevice-&CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
return E_FAIL;
// 锁定内存,复制
if( FAILED( g_pVB-&Lock( 0, sizeof(Vertices), (void**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, Vertices, sizeof(Vertices) );
g_pVB-&Unlock();
return S_OK;
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
if( g_pVB != NULL )
g_pVB-&Release();
if( g_pd3dDevice != NULL )
g_pd3dDevice-&Release();
if( g_pD3D != NULL )
g_pD3D-&Release();
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
// Clear the backbuffer to a blue color
g_pd3dDevice-&Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dDevice-&BeginScene() ) )
g_pd3dDevice-&SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice-&SetFVF( D3DFVF_CUSTOMVERTEX );
//绘制三角形
g_pd3dDevice-&DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
// End the scene
g_pd3dDevice-&EndScene();
// Present the backbuffer contents to the display
g_pd3dDevice-&Present( NULL, NULL, NULL, NULL );
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
switch( msg )
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return DefWindowProc( hWnd, msg, wParam, lParam );
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
&D3D Tutorial&, NULL };
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( &D3D Tutorial&, &D3D Tutorial 02: Vertices&,
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
// Create the vertex buffer
if( SUCCEEDED( InitVB() ) )
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Enter the message loop
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
TranslateMessage( &msg );
DispatchMessage( &msg );
UnregisterClass( &D3D Tutorial&, wc.hInstance );
文章源地址:
Project下载:
& (CSDN下载,免积分)
(本站下载)
相关文章:
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致君,已阅读到文档的结尾了呢~~
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
【Visual C++】游戏开发笔记二十九 一步一步教你用优雅的Direct3D11代码画一个三角形
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口rand()函数可以随机生成的点?但是怎么用rand()的函数在指定的三角形区域内产生点呢? 成功解决后,小女子必当感谢您(害羞)!
barycentric coordinate
&img src=&/b6f7c60a41e1903832edaa_b.png& data-rawwidth=&557& data-rawheight=&180& class=&origin_image zh-lightbox-thumb& width=&557& data-original=&/b6f7c60a41e1903832edaa_r.png&&在平行四边形中均匀地随机取点,然后把其中一半丢弃,或者矩阵变换旋转到想要的那一边来。&br&四面体的处理也是相似的,用八个等体积的四面体拼成一个平行六面体。&br&如果想要支持任意形状,有个词叫做三角化……
在平行四边形中均匀地随机取点,然后把其中一半丢弃,或者矩阵变换旋转到想要的那一边来。四面体的处理也是相似的,用八个等体积的四面体拼成一个平行六面体。如果想要支持任意形状,有个词叫做三角化……
假设三角形三个顶点为:A,B,C&br&&br&首先:&br&求得两个向量&br&ab = B - A&br&ac = C - A。&br&&br&然后:&br&使用rand()获得两个0~1之间的随机实数x, y&br&如果x+y&1, 那么令x'=1-x, y'=1-y&br&如果x+y&=1, 那么令x'=x, y'=y&br&&br&最后:&br&随机点 = A + x' * ab + y' * ac&br&&br&给个图像:&br&&img data-rawheight=&637& src=&/c03da0eed7e_b.jpg& data-rawwidth=&1065& class=&origin_image zh-lightbox-thumb& width=&1065& data-original=&/c03da0eed7e_r.jpg&&&br&----------------------&br&另见:&a href=&/question//answer/& class=&internal&&如何在三角形(比如正三角形)里随机取点? - 叶飞影的回答&/a&&br&还有个公式:&i&P=A*(1 - sqrt(r1)) + B*sqrt(r1)*(1 - r2) + C*r2*sqrt(r1)&/i&也能做到三角形内随机取点.&br&----------------------&br&至于将该问题扩展到三维空间中,即实现四面体内的随机取点.&br&假设四面体四个顶点为:A,B,C,D&br&&br&首先:&br&求得三个向量&br&ab = B - A&br&ac = C - A&br&ad = D - A&br&&br&然后:&br&使用rand()获得三个0~1之间的随机数u, v, w&br&如果u+v+w&2, 那么令u'=1-u, v'=1-v, w'=1-w &br&如果u+v+w&1, 那么令x'=x, y'=y, w'=w&br&否则重新生成u, v, w这三个随机数.&br&&br&最后:&br&随机点 = A + u' * ab + v‘ * ac + w'*ad&br&&br&给个图像:&br&&img src=&/557f7d518d3a22e10b1b8d9d1f985618_b.jpg& data-rawwidth=&483& data-rawheight=&429& class=&origin_image zh-lightbox-thumb& width=&483& data-original=&/557f7d518d3a22e10b1b8d9d1f985618_r.jpg&&
假设三角形三个顶点为:A,B,C首先:求得两个向量ab = B - Aac = C - A。然后:使用rand()获得两个0~1之间的随机实数x, y如果x+y&1, 那么令x'=1-x, y'=1-y如果x+y&=1, 那么令x'=x, y'=y最后:随机点 = A + x' * ab + y' * ac给个图像:----------------------…
已有帐号?
无法登录?
社交帐号登录
图形学相关,研发工程师下次自动登录
现在的位置:
& 综合 & 正文
OpenGL画三角形
修改paintGL函数:
void NeHeWidget::paintGL()
//清楚屏幕和深度缓存
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//重置当前的模型观察矩阵
glLoadIdentity();
glTranslatef(-1.5, 0.0, -6.0);
//开始绘制三角形
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
关于glLoadIdentity()与glTranslatef(x, y, z)函数:
glLoadIdentity()
将当前的用户坐标系的原点移到了屏幕中心:类似于一个复位操作
1.X坐标轴从左至右,Y坐标轴从下至上,Z坐标轴从里至外。
2.OpenGL屏幕中心的坐标值是X和Y轴上的0.0f点。
3.中心左面的坐标值是负值,右面是正值。
移向屏幕顶端是正值,移向屏幕底端是负值。
移入屏幕深处是负值,移出屏幕则是正值。
glTranslatef(x, y, z)
沿着 X, Y 和 Z 轴移动。
注意在glTranslatef(x, y, z)中,当您移动的时候,您并不是相对屏幕中心移动,而是相对与当前所在的屏幕位置。其作用就是将你绘点坐标的原点在当前原点的基础上平移一个(x,y,z)向量。
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
&&&&推荐文章:
【上篇】【下篇】}

我要回帖

更多关于 c 画三角形 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信