请教:如何在视图内用direct3d绘图?

zxyin1 2003-12-28 07:10:56
是不是要用到CDC还是其他什么类来引入d3d?
...全文
287 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxyin1 2003-12-30
  • 打赏
  • 举报
回复
谢谢,解决了!!
zxyin1 2003-12-29
  • 打赏
  • 举报
回复
想在视图内用下面的render()函数实现背景的变化作为测试,哪位高手做过的指点一下呀!
// Function: render
// Whazzit:Clears the screen to a pseudo-random colour and then presents the results.
// If we were doing any real drawing, it would go in this function between
// the BeginScene() & EndScene().
/*
void CD3D_lesson1::render(HWND g_main_window)
{
int red=0;
int green=0;
int blue=0;

//These will safely overflow when the values go over 255, wrapping back to 0.
red++;
green+=2;
blue+=3;

//Clear the buffer to our new colour.
g_d3d_device->Clear(0, //Number of rectangles to clear, we're clearing everything so set it to 0
NULL, //Pointer to the rectangles to clear, NULL to clear whole display
D3DCLEAR_TARGET, //What to clear. We don't have a Z Buffer or Stencil Buffer
D3DCOLOR_XRGB(red,green,blue), //Colour to clear to
1.0f, //Value to clear ZBuffer to, doesn't matter since we don't have one
0); //Stencil clear value, again, we don't have one, this value doesn't matter

//Notify the device that we're ready to render
if(SUCCEEDED(g_d3d_device->BeginScene())){

//Put cool stuff here

//Notify the device that we're finished rendering for this frame
g_d3d_device->EndScene();
}

//Show the results
g_d3d_device->Present(NULL, //Source rectangle to display, NULL for all of it
NULL, //Destination rectangle, NULL to fill whole display
g_main_window, //Target window, if NULL uses device window set in CreateDevice
NULL );//Unused parameter, set it to NULL

}
zxyin1 2003-12-29
  • 打赏
  • 举报
回复
没有成功,有错。
我有一个初始化d3d函数和关闭函数如下:
// Function:init_d3d
// Whazzit:Sets up Direct3D and creates the device. The device is created differently
// if we're full-screen as opposed to running in a desktop window.
void CD3d_Init::init_d3d(HWND g_main_window)
{
HRESULT hr;
D3DPRESENT_PARAMETERS d3dpp;
D3DDISPLAYMODE display_mode;
// g_main_window=NULL;
g_D3D=NULL;
g_d3d_device=NULL;

//Create Direct3D8, this is the first thing you have to do in any D3D8 program
//Always pass D3D_SDK_VERSION to the function.
g_D3D = Direct3DCreate8( D3D_SDK_VERSION );
if(!g_D3D ){
// FatalError("Direct3DCreate8():Error creating Direct3D.");
}

//Get the current(desktop) display mode. This is only needed if
//we're running in a window.
hr=g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&display_mode);
if(FAILED(hr)){
// FatalError(hr,"GetAdapterDisplayMode():Error getting display mode");
}

//Clear out our D3DPRESENT_PARAMETERS structure. Even though we're going
//to set virtually all of its members, it's good practice to zero it out first.
ZeroMemory(&d3dpp,sizeof(d3dpp));

//Whether we're full-screen or windowed these are the same.
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // Throw away previous frames, we don't need them
d3dpp.hDeviceWindow = g_main_window; //This is our main (and only) window
d3dpp.BackBufferCount= 1; //We only need a single back buffer


//BackBufferWidth/Height have to be set for full-screen apps, these values are
//used (along with BackBufferFormat) to determine the display mode.
//They aren't needed in windowed mode since the size of the window will be used.
//BackBufferFormat is the pixel format we want.

//After filling in our D3DPRESENT_PARAMETERS structure, we're ready to create our device.
//Most of the options in how the device is created are set in the D3DPRESENT_PARAMETERS
//structure.
hr=g_D3D->CreateDevice(D3DADAPTER_DEFAULT, //The default adapter, on a multimonitor system
//there can be more than one.
//Use hardware acceleration rather than the software renderer
D3DDEVTYPE_HAL,
//Our Window
g_main_window,
//Process vertices in software. This is slower than in hardware,
//But will work on all graphics cards.
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
//Our D3DPRESENT_PARAMETERS structure, so it knows what we want to build
&d3dpp,
//This will be set to point to the new device
&g_d3d_device);
if(FAILED(hr)){
AfxMessageBox("ft.");
// FatalError(hr,"CreateDevice():Error creating device");
}
}

// Function:kill_d3d
// Whazzit:Releases all of our D3D resources in the opposite order from their creation.
// Note-Since we initially set the pointers to be NULL, we can safely test them
// for a non-NULL state and we know if they've been created. Thus we never Release
// something we didn't create (which causes bad things to happen).
void CD3d_Init::kill_d3d(void){

if(g_d3d_device){
g_d3d_device->Release();
g_d3d_device=NULL;
}

if(g_D3D){
g_D3D->Release();
g_D3D=NULL;
}
}
bluebohe 2003-12-29
  • 打赏
  • 举报
回复
#include <d3d9.h>
#include <d3dx9math.h>

void InitD3D(LPDIRECT3D9 * ppD3D,LPDIRECT3DDEVICE9 *pDevice,HWND hWnd);
void Render(LPDIRECT3D9 pD3D,LPDIRECT3DDEVICE9 pDevice);
void CleanD3d(LPDIRECT3D9 pD3D,LPDIRECT3DDEVICE9 pDevice);

void InitD3D(LPDIRECT3D9 * ppD3D,LPDIRECT3DDEVICE9 *ppDevice,HWND hWnd)
{
//创建Direct3D 对象,并获取接口IDirect3D9 的指针,
//我们将通过该指针操作Direct3D 对象。
*ppD3D = ::Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
::ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE; //创建窗口模式的Direct3D 程序
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
//调用方法IDirect3D9::CreateDevice 创建设备对象,并获取
//接口IDirect3DDevice9 的指针,我们将通过该指针操作设备对象
(*ppD3D)->CreateDevice(
D3DADAPTER_DEFAULT, //使用缺省的显卡
D3DDEVTYPE_HAL, //指定设备类型为HAL
hWnd, //Direct3D窗口的句柄
D3DCREATE_SOFTWARE_VERTEXPROCESSING,//软件顶点处理
&d3dpp, ppDevice);
}
void RenderD3d(LPDIRECT3D9 pD3D,LPDIRECT3DDEVICE9 pDevice)
{
//用指定颜色清除后备缓存区
pDevice->Clear(
0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(255,255,255), //指定使用蓝色
1.0f, 0);
//Direct3D 规定在渲染前必须调用方法IDirect3DDevice9::BeginScene,
//结束时要调用IDirect3DDevice9::EndScene。
pDevice->BeginScene();
//实际的渲染代码放在此处。因为本节只是为了演示如何初始化Direct3D,
//所以这里为空,生成的Direct3D 窗口将是一个蓝色背景的空白窗口
pDevice->EndScene();
//交换当前/后备缓存区,刷新窗口
pDevice->Present(NULL, NULL, NULL, NULL);
}
void CleanD3d(LPDIRECT3D9 pD3D,LPDIRECT3DDEVICE9 pDevice)
{
pDevice->Release(); //释放设备对象
pD3D->Release(); //释放Direct3D 对象
}

添加View类成员
LPDIRECT3D9 m_pD3D; //Direct3D 对象的接口指针
LPDIRECT3DDEVICE9 m_pDevice; //设备对象的接口指针
void CTextDxView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();

CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
InitD3D(&m_pD3D,&m_pDevice,m_hWnd);
}
void CTextDxView::OnPaint()
{
CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
RenderD3d(m_pD3D,m_pDevice);

// Do not call CScrollView::OnPaint() for painting messages
}
void CTextDxView::OnDestroy()
{
CScrollView::OnDestroy();

// TODO: Add your message handler code here
CleanD3d(m_pD3D,m_pDevice);

}

zxyin1 2003-12-29
  • 打赏
  • 举报
回复
sorry,写错了,是传递客户区句柄
zxyin1 2003-12-29
  • 打赏
  • 举报
回复
软模拟也不行,我跟踪了一下,是g_D3D->CreateDevice失败,同样这段程序在一个简单的用CreateWindow创建的窗口中有效,我估计是在传递视图客户区的指针时出错,还有其他获得客户区窗口指针的途经吗?
broadoceans 2003-12-29
  • 打赏
  • 举报
回复
1、窗口模式:
增加一句:d3dpp.Windowed = TRUE;

2、有可能是你的显卡不支持,改为软模拟
D3DDEVTYPE_HAL ---> D3DDEVTYPE_REF

hr=g_D3D->CreateDevice(D3DADAPTER_DEFAULT, //The default adapter, on a multimonitor system
//there can be more than one.
//Use hardware acceleration rather than the software renderer
D3DDEVTYPE_HAL, // 改为D3DDEVTYPE_REF
//Our Window
g_main_window,
//Process vertices in software. This is slower than in hardware,
//But will work on all graphics cards.
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
//Our D3DPRESENT_PARAMETERS structure, so it knows what we want to build
&d3dpp,
//This will be set to point to the new device
&g_d3d_device);
broadoceans 2003-12-28
  • 打赏
  • 举报
回复
this->m_hWnd
zxyin1 2003-12-28
  • 打赏
  • 举报
回复
谢谢,如何获得视图(单文档)的句柄呢?用this 好像不行
broadoceans 2003-12-28
  • 打赏
  • 举报
回复

首先要创建d3d对象
其次,用d3d对象创建d3d设备
创建设备的时候选定窗口模式,
对应一个窗口。
这时你就可以在设备上绘图,
然后调用设备的Present方法显示到窗口。

你需要安装d3d SDK 目前版本为9.0
头文件为 d3d9.h
库为: d3d9.lib
怎么绘图,请看SDK帮助。

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧