24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 768  |  回复: 0

断点~+++

金虫 (小有名气)

[求助] DX 画个矩形问题 求大神

//////////////////////////////////////////////////////////////////////////////////////////////////
//
// File: triangle.cpp
//
// Author: Frank Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
//
// Desc: Renders a triangle in wireframe mode.  Demonstrates vertex buffers,
//       render states, and drawing commands.
//         
//////////////////////////////////////////////////////////////////////////////////////////////////

#include "d3dUtility.h"

//
// Globals
//

IDirect3DDevice9* Device = 0;

const int Width  = 640;
const int Height = 480;

IDirect3DVertexBuffer9* Triangle = 0; // vertex buffer to store
                                      // our triangle data.

//
// Classes and Structures
//

struct Vertex
{
        Vertex(){}

        Vertex(float x, float y, float z)
        {
        _x = x;         _y = y;  _z = z;
        }

        float _x, _y, _z;

        static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ;

//
// Framework Functions
//
bool Setup()
{
        //
        // Create the vertex buffer.
        //

        Device->CreateVertexBuffer(
                3 * sizeof(Vertex), // size in bytes
                D3DUSAGE_WRITEONLY, // flags
                Vertex::FVF,        // vertex format
                D3DPOOL_MANAGED,    // managed memory pool
                &Triangle,          // return create vertex buffer
                0);                 // not used - set to 0

        //
        // Fill the buffers with the triangle data.
        //

        Vertex* vertices;

        Triangle->Lock(0, 0, (void**)&vertices, 0);   

        vertices[0] = Vertex(0.0f, 0.5f, 2.0f); //left  
        vertices[1] = Vertex(-1.0f, 1.0f, 2.0f); //
        vertices[2] = Vertex( 1.0f, 1.0f, 2.0f); //right
        vertices[3] = Vertex( 1.0f, 0.0f, 2.0f);
        vertices[4] = Vertex( -1.0f, 0.0f, 2.0f);
        vertices[5] = Vertex( -1.0f, 1.0f, 2.0f);
        Triangle->Unlock();

        //
        // Set the projection matrix.
        //

        D3DXMATRIX proj;
        D3DXMatrixPerspectiveFovLH(
                        &proj,                        // result
                        D3DX_PI * 0.5f,               // 90 - degrees
                        (float)Width / (float)Height, // aspect ratio
                        1.0f,                         // near plane
                        1000.0f);                     // far plane
        Device->SetTransform(D3DTS_PROJECTION, &proj);

        //
        // Set wireframe mode render state.
        //

        Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

        return true;
}
void Cleanup()
{
        d3d::Release<IDirect3DVertexBuffer9*>(Triangle);
}

bool Display(float timeDelta)
{
        if( Device )
        {
                Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
                Device->BeginScene();

                Device->SetStreamSource(0, Triangle, 0, sizeof(Vertex));
                Device->SetFVF(Vertex::FVF);

                // Draw one triangle.

               
                        Device->DrawPrimitive(D3DPT_TRIANGLEFAN,0,4);

                Device->EndScene();
                Device->Present(0, 0, 0, 0);
        }
        return true;
}


//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        switch( msg )
        {
        case WM_DESTROY:
                :ostQuitMessage(0);
                break;
               
        case WM_KEYDOWN:
                if( wParam == VK_ESCAPE )
                        :estroyWindow(hwnd);
                break;
        }
        return :efWindowProc(hwnd, msg, wParam, lParam);
}

//
// WinMain
//
int WINAPI WinMain(HINSTANCE hinstance,
                                   HINSTANCE prevInstance,
                                   PSTR cmdLine,
                                   int showCmd)
{
        if(!d3d::InitD3D(hinstance,
                Width, Height, true, D3DDEVTYPE_HAL, &Device))
        {
                ::MessageBox(0, "InitD3D() - FAILED", 0, 0);
                return 0;
        }
               
        if(!Setup())
        {
                ::MessageBox(0, "Setup() - FAILED", 0, 0);
                return 0;
        }

        d3d::EnterMsgLoop( Display );

        Cleanup();

        Device->Release();

        return 0;
}

Device->DrawPrimitive(D3DPT_TRIANGLEFAN,0,4); 使用D3DPT_TRIANGLEFAN 系统使用顶点v2、v3和v1画第一个三角形;v3,v4,v1画第二个三角形;v4,v5,v1画第三个三角形;等等。启用平面着色时,系统色调颜色从第一个顶点的三角形 但是运行的时候只有第一个三角形画出来了  应该是四个三角形组成一个矩形啊
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 断点~+++ 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 一志愿复旦材料,英一专硕,总分357调剂 +4 1050389037 2026-04-02 5/250 2026-04-02 21:40 by dongzh2009
[考研] 294求调剂 +3 Grey_Ey 2026-04-02 5/250 2026-04-02 21:34 by swb0218
[考研] 343求调剂085601 +5 要努力学习x 2026-03-29 6/300 2026-04-02 21:18 by 1104338198
[考研] 一志愿北京科技大学材料工程085601,求调剂 +8 cdyw 2026-04-02 8/400 2026-04-02 21:16 by wwytracy
[考研] 319求调剂 +16 太容易1018 2026-04-01 16/800 2026-04-02 20:12 by seattle40
[考研] 085600,材料与化工321分求调剂 +11 大馋小子 2026-03-28 11/550 2026-04-02 15:59 by yunlongyang
[考研] 298求B区调剂 +4 zzz,,r 2026-04-02 5/250 2026-04-02 12:17 by 土木硕士招生
[考研] 重庆大学材料与化工085600,初试370+,求求调剂建议 +5 shzhou_ 2026-04-01 6/300 2026-04-02 10:05 by olim
[考研] 070300化学279求调剂 +15 哈哈哈^_^ 2026-03-31 17/850 2026-04-01 21:37 by 给你你注意休息
[考研] 建环,能源,土木老师路过看一看!!! +4 嘿嘿uu 2026-04-01 4/200 2026-04-01 20:42 by 无懈可击111
[考研] 求调剂 +4 图鉴212 2026-03-30 5/250 2026-04-01 15:32 by 图鉴212
[考研] 化学0703 调剂 306分 一志愿211 +12 26要上岸 2026-03-28 12/600 2026-04-01 11:10 by chemdavid
[考研] 288资源与环境专硕求调剂,不限专业,有学上就行 +25 lllllos 2026-03-30 26/1300 2026-04-01 09:52 by 一只好果子?
[考研] 土木304求调剂 +3 兔突突突, 2026-03-31 3/150 2026-04-01 09:42 by JourneyLucky
[考研] 0856调剂 +7 曲听筠 2026-03-30 7/350 2026-04-01 08:51 by JourneyLucky
[考研] 336材料求调剂 +10 陈滢莹 2026-03-26 12/600 2026-03-31 17:59 by jp9609
[考研] 求调剂 +8 11ggg 2026-03-30 8/400 2026-03-31 13:56 by nanaliuyun
[考研] 334求调剂 +7 Trying] 2026-03-31 7/350 2026-03-31 12:33 by 无际的草原
[考研] 313求调剂 +6 卖个关子吧 2026-03-31 6/300 2026-03-31 10:58 by Jaylen.
[考研] 312,生物学求调剂 +3 小译同学abc 2026-03-28 3/150 2026-03-28 15:32 by 落睿可思
信息提示
请填处理意见