关于Directly Mapping Texels to Pixels的例子

2024-03-14 23:58

本文主要是介绍关于Directly Mapping Texels to Pixels的例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文:http://msdn.microsoft.com/en-us/library/bb219690%28v=vs.85%29.aspx

是关于在direct3d9中,对于屏幕空间中,将贴图映射到像素的问题

以下是pixel shader源代码(aaa.ps.txt)

texture MyTexture;
sampler2D MySampler = 
sampler_state 
{ 
Texture = <MyTexture>;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Clamp;
AddressV = Clamp;
};
float4 TextureLookupPS(float2 vTexCoord : TEXCOORD0) : COLOR
{
return tex2D(MySampler, vTexCoord);
} 
以下是主程序代码(main.cpp)

#include <d3d9.h>
#include <d3dx9.h>
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECT3DTEXTURE9 g_pTexture = NULL;
LPDIRECT3DPIXELSHADER9 g_pPS = NULL;
LPD3DXCONSTANTTABLE g_pPSConstantTable;
#define SAFE_RELEASE(p) if(p) {p->Release();}
struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw;
FLOAT tu, tv;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX1)
HRESULT Init(HWND hWnd)
{
if(NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
return E_FAIL;
D3DPRESENT_PARAMETERS d3dpp = {0};
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice)))
return E_FAIL;
if(FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, L"aaa.bmp", &g_pTexture)))
return E_FAIL;
LPD3DXBUFFER pShader;
LPD3DXBUFFER pErrorMsgs;
if(FAILED(D3DXCompileShaderFromFile(L"aaa.ps.txt", NULL, NULL, "TextureLookupPS", "ps_2_0", 0, &pShader, &pErrorMsgs, &g_pPSConstantTable)))
{
MessageBoxA(NULL, (LPSTR)pErrorMsgs->GetBufferPointer(), "Error", MB_OK);
SAFE_RELEASE(pErrorMsgs);
return E_FAIL;
}
if(FAILED(g_pd3dDevice->CreatePixelShader((DWORD *)pShader->GetBufferPointer(), &g_pPS)))
return E_FAIL;
SAFE_RELEASE(pShader);
SAFE_RELEASE(pErrorMsgs);
return S_OK;
}
VOID Render(void)
{
HRESULT hres;
hres = g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0);
if(SUCCEEDED(hres = g_pd3dDevice->BeginScene()))
{
CUSTOMVERTEX vertices[] =
{
{0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f},
{4.0f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f},
{4.0f, 4.0f, 0.5f, 1.0f, 1.0f, 1.0f},
{0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f},
{4.0f, 4.0f, 0.5f, 1.0f, 1.0f, 1.0f},
{0.0f, 4.0f, 0.5f, 1.0f, 0.0f, 1.0f},
};
//for(int i = 0; i < sizeof(vertices) / sizeof(CUSTOMVERTEX); i++)
//{
//	vertices[i].x -= 0.5f;
//	vertices[i].y -= 0.5f;
//}
hres = g_pd3dDevice->SetPixelShader(g_pPS);
UINT TextureSamplerIndex = g_pPSConstantTable->GetSamplerIndex("MySampler");
hres = g_pd3dDevice->SetTexture(TextureSamplerIndex, g_pTexture);
hres = g_pd3dDevice->SetSamplerState(TextureSamplerIndex, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
hres = g_pd3dDevice->SetSamplerState(TextureSamplerIndex, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
hres = g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
hres = g_pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, vertices, sizeof(CUSTOMVERTEX));
hres = g_pd3dDevice->EndScene();
}
hres = g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
}
VOID Cleanup(void)
{
SAFE_RELEASE(g_pPSConstantTable);
SAFE_RELEASE(g_pPS);
SAFE_RELEASE(g_pTexture);
SAFE_RELEASE(g_pd3dDevice);
SAFE_RELEASE(g_pD3D);
}
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 wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, INT)
{
WNDCLASSEX wc = {sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle( NULL ), NULL, NULL, NULL, NULL, L"MappingTexels", NULL};
RegisterClassEx(&wc);
HWND hWnd = CreateWindow(L"MappingTexels", L"MappingTexels", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, wc.hInstance, NULL);
if(SUCCEEDED(Init(hWnd)))
{
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
MSG msg = {0};
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
Render();
}
}
UnregisterClass(L"MappingTexels", wc.hInstance);
return 0;
}


这篇关于关于Directly Mapping Texels to Pixels的例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/810132

相关文章

JavaFX环境的搭建和一个简单的例子

之前在网上搜了很多与javaFX相关的资料,都说要在Eclepse上要安装sdk插件什么的,反正就是乱七八糟的一大片,最后还是没搞成功,所以我在这里写下我搭建javaFX成功的环境给大家做一个参考吧。希望能帮助到你们! 1.首先要保证你的jdk版本能够支持JavaFX的开发,jdk-7u25版本以上的都能支持,最好安装jdk8吧,因为jdk8对支持JavaFX有新的特性了,比如:3D等;

javaScript日期相加减例子

当前时间加上2天 var d = new Date(“2015-7-31”); d.setDate(d.getDate()+2); var addTwo=d.getFullYear()+”年”+(d.getMonth()+1)+”月”+d.getDate()+”日”; “控制台输出===============”+”当前日期加2天:”+addTwo; 使用这种方法,月份也会给你计算.

设计模式大全和详解,含Python代码例子

若有不理解,可以问一下这几个免费的AI网站 https://ai-to.cn/chathttp://m6z.cn/6arKdNhttp://m6z.cn/6b1quhhttp://m6z.cn/6wVAQGhttp://m6z.cn/63vlPw 下面是设计模式的简要介绍和 Python 代码示例,涵盖主要的创建型、结构型和行为型模式。 一、创建型模式 1. 单例模式 (Singleton

JSP 简单表单显示例子

<html><!--http://localhost:8080/test_jsp/input.html --><head><meta http-equiv="Content-Type" content="text/HTML; charset=utf-8"><title>input页面</title></head><body><form action="input.jsp" method

shell循环sleep while例子 条件判断

i=1# 小于5等于时候才执行while [ ${i} -le 5 ]doecho ${i}i=`expr ${i} + 1`# 休眠3秒sleep 3doneecho done 参考 http://c.biancheng.net/cpp/view/2736.html

【ReactJS】通过一个例子学习React组件的生命周期

源代码 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Reac

简单的android Listview使用例子

为了熟悉Listview的使用,做了一个小例子联系一下, 主要步骤: 1. 在MainActivity中,创建一个adapter对象(可以是android自带的ArrayAdapter,也可以是自定义的如SongAdapter) 2. 如果自定义,就要创建ListView的子项,如song_listview_item.xml 3. 创建ListView对象,并用setAdapter方法把a

【 python pymongo】使用pymongo的例子

MongoDB优点 MongoDB是一个为当代web应用而生的noSQL数据库,它有如下优点: 1、文档型存储。可以把关系型数据库的表理解为一个电子表格,列表示字段,每行的记录其实是按照列的字段顺序排列的值得元组。而存储在MongoDB中的文档被存储为键-值对的形式,值却可以是任意类型且可以嵌套。之前在用关系型数据库的时候,我们把产品信息打散到不同的表中,要通过关系表或者使用join拼接成复杂

c:if test=/c:if如何判断空(使用例子)

userName是登录的时候放到session中了 <c:if test="${ not empty userName }">这表示userName判断不为null `<c:if test="${empty userName }"> ` 这表示userName判断为null 使用案例 <c:if test="${ not empty userName }"><ul><li><a

【硬刚ES】ES基础(十二)多字段特性及Mapping中配置自定义Analyzer

本文是对《【硬刚大数据之学习路线篇】从零到大数据专家的学习指南(全面升级版)》的ES部分补充。