Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Direct X 9-三角形未显示_C++_Rendering_Directx_Directx 9_Direct3d9 - Fatal编程技术网

C++ Direct X 9-三角形未显示

C++ Direct X 9-三角形未显示,c++,rendering,directx,directx-9,direct3d9,C++,Rendering,Directx,Directx 9,Direct3d9,目前,在开始编写程序之前,我正在用DirectX做一些测试用例。现在,我已经把下面的代码弄乱了一天左右。我在网上查看类似问题时修改了代码。但最终,它也没有按照我想要的方式工作。有人能告诉我我到底做错了什么吗?也许其他人可以解决这个问题 // TetrisClone.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "TetrisClone.h" #include <d3d9

目前,在开始编写程序之前,我正在用DirectX做一些测试用例。现在,我已经把下面的代码弄乱了一天左右。我在网上查看类似问题时修改了代码。但最终,它也没有按照我想要的方式工作。有人能告诉我我到底做错了什么吗?也许其他人可以解决这个问题

// TetrisClone.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "TetrisClone.h"
#include <d3d9.h>
#include <d3dx9.h>


#define MAX_LOADSTRING 100
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

//for testing purposes
struct CUSTOMVERTEX
{
    FLOAT x, y, z; //position
    DWORD color; //Color
};

// global declarations for Direct3d
LPDIRECT3D9 d3d;    // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;

// function prototypes
void initD3D(HWND hWnd);    // sets up and initializes Direct3D
void render_frame(void);    // renders a single frame
void cleanD3D(void);    // closes Direct3D and releases memory
void init_graphics(void);    // 3D declarations

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.
    MSG msg;
    HACCEL hAccelTable;

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_TETRISCLONE, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TETRISCLONE));

    // Main message loop:
    while(TRUE)
    {
        // Check to see if any messages are waiting in the queue
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            // Translate the message and dispatch it to WindowProc()
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        // If the message is WM_QUIT, exit the while loop
        if(msg.message == WM_QUIT)
            break;

        render_frame();
    }

    cleanD3D();
    return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TETRISCLONE));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_GRAYTEXT);
    wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_TETRISCLONE);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, CW_USEDEFAULT, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);

   // set up and initialize Direct3D
   initD3D(hWnd);

   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

/***
    Direct3d functions

***/

// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
{
    d3d = Direct3DCreate9(D3D_SDK_VERSION);    // create the Direct3D interface

    D3DPRESENT_PARAMETERS d3dpp;    // create a struct to hold various device information

    ZeroMemory(&d3dpp, sizeof(d3dpp));    // clear out the struct for use
    d3dpp.Windowed = TRUE;    // program windowed, not fullscreen
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    // discard old frames
    d3dpp.hDeviceWindow = hWnd;    // set the window to be used by Direct3D

    // create a device class using this information and information from the d3dpp stuct
    d3d->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &d3dpp,
                      &d3ddev);

    init_graphics();
}

// this is the function used to render a single frame
void render_frame(void)
{

    // clear the window to a deep blue
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);

    d3ddev->BeginScene();    // begins the 3D scene

        //set FVF
    d3ddev->SetFVF(D3DFVF_CUSTOMVERTEX);

    //setting stream source
    d3ddev->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));

    // do 3D rendering on the back buffer here

    //drawing triangle
    d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

    d3ddev->EndScene();    // ends the 3D scene

    d3ddev->Present(NULL, NULL, NULL, NULL);    // displays the created frame
}

// this is the function that puts the 3D models into video RAM
void init_graphics(void)
{
    // create the vertices using the CUSTOMVERTEX struct
    CUSTOMVERTEX vertices[] =
    {
        { 400.0f, 62.5f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 650.0f, 500.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
        { 150.0f, 500.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
    };

    // create a vertex buffer interface called v_buffer
    d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
                               0,
                               D3DFVF_CUSTOMVERTEX,
                               D3DPOOL_MANAGED,
                               &g_pVB,
                               NULL);

    VOID* pVoid;    // a void pointer

    // lock v_buffer and load the vertices into it
    g_pVB->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    g_pVB->Unlock();
}

// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
    g_pVB->Release();
    d3ddev->Release();    // close and release the 3D device
    d3d->Release();    // close and release Direct3D
}
//TetrisClone.cpp:定义应用程序的入口点。
//
#包括“stdafx.h”
#包括“TetrisClone.h”
#包括
#包括
#定义最大加载字符串100
#定义屏幕宽度800
#定义屏幕高度600
#定义D3DFVF_自定义顶点(D3DFVF_XYZ | D3DFVF_漫反射)
//用于测试目的
结构自定义顶点
{
浮动x,y,z;//位置
DWORD color;//颜色
};
//Direct3d的全局声明
LPDIRECT3D9 d3d;//指向Direct3D界面的指针
LPDIRECT3DDEVICE9 d3ddev;//指向设备类的指针
LPDIRECT3DVERTEXBUFFER9 g_pVB=NULL;
//功能原型
void initD3D(HWND HWND);//设置并初始化Direct3D
void渲染_帧(void);//渲染单个帧
空隙清理3d(空隙);//关闭Direct3D并释放内存
void init_图形(void);//三维声明
//全局变量:
HINSTANCE hInst;//当前实例
TCHAR szTitle[MAX_LOADSTRING];//标题栏文本
TCHAR szWindowClass[最大加载字符串];//主窗口类名称
//转发此代码模块中包含的函数声明:
ATOM MyRegisterClass(HINSTANCE HINSTANCE);
BOOL InitInstance(HINSTANCE,int);
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
INT_PTR回调关于(HWND、UINT、WPARAM、LPARAM);
int APIENTH_tWinMain(HINSTANCE HINSTANCE),
HINSTANCE HPPrevenstance,
LPTSTR lpCmdLine,
国际展览(nCmdShow)
{
未引用的_参数(HPPreInstance);
未引用的_参数(lpCmdLine);
//TODO:将代码放在这里。
味精;
HACCEL hAccelTable;
//初始化全局字符串
加载字符串(hInstance、IDS\U APP\U TITLE、szTitle、MAX\U加载字符串);
LoadString(hInstance、IDC\u TETRISCLONE、szWindowClass、MAX\u LoadString);
MyRegisterClass(hInstance);
//执行应用程序初始化:
如果(!InitInstance(hInstance,nCmdShow))
{
返回FALSE;
}
hAccelTable=加载加速器(hInstance、MAKEINTRESOURCE(IDC_TETRISCLONE));
//主消息循环:
while(TRUE)
{
//检查队列中是否有消息等待
while(peek消息(&msg,NULL,0,0,PM_-REMOVE))
{
//翻译消息并将其分派到WindowProc()
翻译信息(&msg);
发送消息(&msg);
}
//如果消息为WM_QUIT,则退出while循环
如果(msg.message==WM\u退出)
打破
渲染_帧();
}
清洁d3d();
返回(int)msg.wParam;
}
//
//函数:MyRegisterClass()
//
//目的:注册窗口类。
//
//评论:
//
//只有在需要此代码时,才需要此函数及其用法
//与“RegisterClassEx”之前的Win32系统兼容
//添加到Windows 95的函数。调用此函数很重要
//因此,应用程序将获得相关的“格式良好”小图标
//用它。
//
ATOM MyRegisterClass(HINSTANCE HINSTANCE)
{
WNDCLASSEX wcex;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.style=CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc=WndProc;
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;
wcex.hInstance=hInstance;
wcex.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_TETRISCLONE));
wcex.hCursor=LoadCursor(空,IDC_箭头);
wcex.hbrBackground=(HBRUSH)(颜色为灰色文本);
wcex.lpszMenuName=MAKEINTRESOURCE(IDC_TETRISCLONE);
wcex.lpszClassName=szWindowClass;
wcex.hIconSm=LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_SMALL));
返回注册表类(&wcex);
}
//
//函数:InitInstance(HINSTANCE,int)
//
//用途:保存实例句柄并创建主窗口
//
//评论:
//
//在这个函数中,我们将实例句柄保存在一个全局变量中,然后
//创建并显示主程序窗口。
//
BOOL InitInstance(HINSTANCE HINSTANCE,int nCmdShow)
{
HWND-HWND;
hInst=hInstance;//将实例句柄存储在全局变量中
hWnd=CreateWindow(szWindowClass、szTitle、WS_OVERLAPPEDWINDOW、,
CW_USEDEFAULT、CW_USEDEFAULT、屏幕宽度、屏幕高度、NULL、NULL、hInstance、NULL);
如果(!hWnd)
{
返回FALSE;
}
显示窗口(hWnd、nCmdShow);
//设置并初始化Direct3D
initD3D(hWnd);
更新窗口(hWnd);
返回TRUE;
}
//
//功能:WndProc(HWND、UINT、WPARAM、LPARAM)
//
//用途:处理主窗口的消息。
//
//WM_命令-处理应用程序菜单
//WM_绘制-绘制主窗口
//WM_DESTROY-发布退出消息并返回
//
//
LRESULT回调WndProc(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
{
int wmId,wmEvent;
PAINTSTRUCT-ps;
HDC-HDC;
开关(信息)
{
case WM_命令:
wmId=LOWORD(wParam);
wmEvent=HIWORD(wParam);
//解析菜单选项:
交换机(wmId)
{
案例IDM_关于:
对话框(hInst、MAKEINTRESOURCE(IDD_ABOUTBOX)、hWnd、About);
打破
案例IDM_退出:
窗口(hWnd);
打破
违约:
返回DefWindowProc(hWnd、message、wParam、lParam);
}
打破
案例WM_油漆:
hdc=开始喷漆(hWnd和ps);
//TODO:在此处添加任何图形代码。。。
端漆(hWnd和ps);
打破
案例WM_销毁:
PostQuitMessage(0);
打破
违约:
返回
 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
struct CUSTOMVERTEX
{
    FLOAT x, y, z, w; //position
    DWORD color; //Color
};
CUSTOMVERTEX vertices[] =
{
    { 400.0f, 62.5f, 0.0f, 1, D3DCOLOR_XRGB(0,0,255) },
    { 650.0f, 500.0f, 0.0f, 1, D3DCOLOR_XRGB(0,255,0) },
    { 150.0f, 500.0f, 0.0f, 1, D3DCOLOR_XRGB(255,0,0) }
};
g_pVB->Lock(0, 3*sizeof(CUSTOMVERTEX), (void**)&pVoid, 0);
memcpy(pVoid, vertices, 3*sizeof(CUSTOMVERTEX));
-1 << 1 << 1
-1 << y << 1
 0 << z << 1
  CUSTOMVERTEX vertices[] =
    {
        { 0.0f, 0.0, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
        { 1.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
    };