文档章节

D3D中的纹理映射(4)

rise-worlds
 rise-worlds
发布于 2016/06/20 13:35
字数 524
阅读 2
收藏 0

该例程演示了如何设置纹理寻址模式。

截图:

源程序:

/**************************************************************************************
  Allows the user to switch between the different texture address modes to see what they do.
  Use the following keys:
           'W' - Switches to Wrap mode
           'B' - Switches to Border mode
           'C' - Switches to Clamp mode
           'M' - Switches to Mirror mode 
**************************************************************************************/
#include "d3dUtility.h"
#pragma warning(disable : 4100)
const int WIDTH  = 640;
const int HEIGHT = 480;
IDirect3DDevice9*        g_d3d_device;
IDirect3DVertexBuffer9* g_quad_vb;
IDirect3DTexture9*        g_d3d_texture;
class cTextureVertex
{
public:
float m_x,  m_y,  m_z;
float m_nx, m_ny, m_nz;
float m_u, m_v; // texture coordinates   
    cTextureVertex() { }
    cTextureVertex(float x,  float y,  float z,
float nx, float ny, float nz,
float u,  float v)
    {
        m_x  = x;  m_y  = y;  m_z  = z;
        m_nx = nx; m_ny = ny; m_nz = nz;
        m_u  = u;  m_v  = v;
    }   
};
const DWORD TEXTURE_VERTEX_FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
////////////////////////////////////////////////////////////////////////////////////////////////////
bool setup()
{   
// create the quad vertex buffer and fill it with the quad geometry
    g_d3d_device->CreateVertexBuffer(6 * sizeof(cTextureVertex), D3DUSAGE_WRITEONLY, TEXTURE_VERTEX_FVF,
                                     D3DPOOL_MANAGED, &g_quad_vb, NULL);
    cTextureVertex* vertices;
    g_quad_vb->Lock(0, 0, (void**)&vertices, 0);
// quad built from two triangles, note texture coordinate.
    vertices[0] = cTextureVertex(-1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 3.0f);
    vertices[1] = cTextureVertex(-1.0f,  1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
    vertices[2] = cTextureVertex( 1.0f,  1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 3.0f, 0.0f);
    vertices[3] = cTextureVertex(-1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 3.0f);
    vertices[4] = cTextureVertex( 1.0f,  1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 3.0f, 0.0f);
    vertices[5] = cTextureVertex( 1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 3.0f, 3.0f);
    g_quad_vb->Unlock();
// create the texture and set filters
    D3DXCreateTextureFromFile(g_d3d_device, "dx5_logo.bmp", &g_d3d_texture);
    g_d3d_device->SetTexture(0, g_d3d_texture);
    g_d3d_device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    g_d3d_device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
    g_d3d_device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
// don't use lighting for this sample
    g_d3d_device->SetRenderState(D3DRS_LIGHTING, FALSE);
// set the projection matrix
    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.5f, (float)WIDTH/HEIGHT, 1.0f, 1000.0f);
    g_d3d_device->SetTransform(D3DTS_PROJECTION, &proj);
return true;
}
void cleanup()
{   
    safe_release<IDirect3DVertexBuffer9*>(g_quad_vb);
    safe_release<IDirect3DTexture9*>(g_d3d_texture);
}
bool display(float time_delta)
{
// set wrap address mode
if(GetAsyncKeyState('W') & 0x8000f)
    {
        g_d3d_device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
        g_d3d_device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
    }
// set border color address mode
if(GetAsyncKeyState('B') & 0x8000f)
    {
        g_d3d_device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
        g_d3d_device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
        g_d3d_device->SetSamplerState(0,  D3DSAMP_BORDERCOLOR, 0x000000ff);
    }
// set clamp address mode
if(GetAsyncKeyState('C') & 0x8000f)
    {
        g_d3d_device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
        g_d3d_device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
    }
// set mirror address mode
if(GetAsyncKeyState('M') & 0x8000f)
    {
        g_d3d_device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR);
        g_d3d_device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR);
    }   
// draw the scene
    g_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
    g_d3d_device->BeginScene();
    g_d3d_device->SetStreamSource(0, g_quad_vb, 0, sizeof(cTextureVertex));
    g_d3d_device->SetFVF(TEXTURE_VERTEX_FVF);
    g_d3d_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
    g_d3d_device->EndScene();
    g_d3d_device->Present(NULL, NULL, NULL, NULL);
return true;
}
LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM word_param, LPARAM long_param)
{
switch(msg)
    {
case WM_DESTROY:
        PostQuitMessage(0);
break;
case WM_KEYDOWN:
if(word_param == VK_ESCAPE)
            DestroyWindow(hwnd);
break;
    }
return DefWindowProc(hwnd, msg, word_param, long_param);
}
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, PSTR cmd_line, int cmd_show)
{
if(! init_d3d(inst, WIDTH, HEIGHT, true, D3DDEVTYPE_HAL, &g_d3d_device))
    {
        MessageBox(NULL, "init_d3d() - failed.", 0, MB_OK);
return 0;
    }
if(! setup())
    {
        MessageBox(NULL, "Steup() - failed.", 0, MB_OK);
return 0;
    }
    enter_msg_loop(display);
    cleanup();
    g_d3d_device->Release();
return 0;
}

下载源程序

本文转载自:http://www.cnblogs.com/flying_bat/archive/2008/03/20/1115369.html

rise-worlds

rise-worlds

粉丝 3
博文 1755
码字总数 0
作品 0
深圳
程序员
私信 提问
Nebula3绘制2D纹理

上次已经绘制过基本图元了, 这次只不过要贴张图而已..... 本来我想用Graphics的Model渲染流程来做, 不过这一层太高级了, 都是什么场景管理资源映射之类的 做低级的事情, 就要用低级的API嘛 ...

长平狐
2012/11/12
85
0
SDL2源代码分析6:复制到渲染器(SDL_RenderCopy())

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leixiaohua1020/article/details/40895593 ===================================================== SDL源代码......

雷霄骅
2014/11/08
0
0
SDL2源代码分析5:更新纹理(SDL_UpdateTexture())

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leixiaohua1020/article/details/40876089 ===================================================== SDL源代码......

雷霄骅
2014/11/07
0
0
HLSL 研究学习之五章六节

注:所有代码均由红孩儿编写或以Microsoft DX9 SDK Sample 代码修改而成,转载请注明. (5_6)3D三角形双纹理(外部设纹理,不用PS)) hlsl.fx: //(5_6)3D三角形双纹理(外部设纹理,不用PS)) // ---...

长平狐
2013/03/19
139
0
HLSL 研究学习之五章三节

注:所有代码均由红孩儿编写或以Microsoft DX9 SDK Sample 代码修改而成,转载请注明. (5_2)3D三角形纹理(fx中设纹理,不用PS) hlsl.fx: //(5_3)3D三角形纹理(fx中设纹理,不用PS) // --------...

长平狐
2013/03/19
46
0

没有更多内容

加载失败,请刷新页面

加载更多

关于谷歌浏览器崩溃,打不开任何界面

首先:谷歌浏览器右键打开属性,在箭头所指的位置复制粘贴 -no-sandbox。(需要空一格再写入 -no-sandbox) 其次:你打开谷歌浏览器可以看到如下提醒,提醒你,稳定性和安全性会有所下降,但...

Raphael98
16分钟前
2
0
java 删除文件夹下的文件

/** * 删除已经下载过的文件 * @param path * @return */ @ApiOperation(value = "删除已经下载过的Excel",httpMethod="",notes="") @GetMapping("/deleteExcel") public Object downLoad(@......

简小姐
16分钟前
3
0
如何安装GMP,MPFR,MPC,ELF,无需共享库?

如何使用当前版本, 使用正确版本的依赖关系,不使用包管理器(如yum,rpm,apt,dpkg)并且不使用共享库,来逐块安装GCC(GNU编译器集合)? 典型的开发人员可能希望以典型的方式安装GCC,使...

mskk
20分钟前
2
0
Rancher + VMware PKS实现全球数百站点的边缘K8S集群管理

Sovereign Systems是一家成立于2007年的技术咨询公司,帮助客户将传统数据中心技术和应用程序转换为更高效的、基于云的技术平台,以更好地应对业务挑战。曾连续3年提名CRN,并且在2012年到2...

RancherLabs
25分钟前
2
0
docker修改log-driver后启动失败问题解决

vi /etc/sysconfig/docker 去掉--log-driver=journald 重启docker,重新run一个容器

abowu
27分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部