游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1670|回复: 5

Mix Two textures to One and Put to a rectangle

[复制链接]

414

主题

611

帖子

621

积分

高级会员

Rank: 4

积分
621
发表于 2006-2-23 20:09:00 | 显示全部楼层 |阅读模式
我想在一??物件上?上???texture(以一比一去混合)
OutputPixel=SourceTex*0.5+DestTex*0.5;
???一下,程式???如何??呢?
附上source code,From \3D??虺淌皆O?入?  使用Directx9.0??作(with project)\BookPart2Code\Chapter 7\MtrlAlpha (修改?)

#include "d3dUtility.h"

//
// Globals
//

IDirect3DDevice9* Device = 0;

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

ID3DXMesh*   Teapot = 0;
D3DMATERIAL9 TeapotMtrl;

IDirect3DVertexBuffer9* BkGndQuad = 0;
IDirect3DTexture9*      BkGndTex1  = 0;
IDirect3DTexture9*      BkGndTex2 = 0;

D3DMATERIAL9            BkGndMtrl;

//
// Classes and Structures
//
struct Vertex
{
Vertex(){}
Vertex(
float x, float y, float z,
float nx, float ny, float nz,
float u, float v)
{
_x  = x;  _y  = y;  _z  = z;
_nx = nx; _ny = ny; _nz = nz;
_u  = u;  _v  = v;
}
    float _x, _y, _z;
    float _nx, _ny, _nz;
    float _u, _v; // texture coordinates

static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;

//
// Framework Functions
//
bool Setup()
{
//
// Init Materials
//

TeapotMtrl = d3d::RED_MTRL;
TeapotMtrl.Diffuse.a = 0.5f; // set alpha to 50% opacity

BkGndMtrl = d3d::WHITE_MTRL;

//
// Create the teapot.
//

D3DXCreateTeapot(Device, &Teapot, 0);

//
// Create the background quad.
//

Device->CreateVertexBuffer(
6 * sizeof(Vertex),
D3DUSAGE_WRITEONLY,
Vertex::FVF,
D3DPOOL_MANAGED,
&BkGndQuad,
0);

Vertex* v;
BkGndQuad->Lock(0, 0, (void**)&v, 0);

v[0] = Vertex(-10.0f, -10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[1] = Vertex(-10.0f,  10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
v[2] = Vertex( 10.0f,  10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);

v[3] = Vertex(-10.0f, -10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[4] = Vertex( 10.0f,  10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[5] = Vertex( 10.0f, -10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);

BkGndQuad->Unlock();

//
// Setup a directional light.
//

D3DLIGHT9 dir;
::ZeroMemory(&dir, sizeof(dir));
dir.Type      = D3DLIGHT_DIRECTIONAL;
dir.Diffuse   = d3d::WHITE;
dir.Specular  = d3d::WHITE * 0.2f;
dir.Ambient   = d3d::WHITE * 0.6f;
dir.Direction = D3DXVECTOR3(0.707f, 0.0f, 0.707f);

Device->SetLight(0, &dir);
Device->LightEnable(0, true);

Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
Device->SetRenderState(D3DRS_SPECULARENABLE, true);

//
// Create texture and set texture filters.
//

D3DXCreateTextureFromFile(
Device,
"crate.jpg",
&BkGndTex1);
D3DXCreateTextureFromFile(
Device,
"chap6_0.jpg",
&BkGndTex2);

Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);

Device->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_POINT);

//
// Set alpha blending states.
//

// use alpha in material's diffuse component for alpha
Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);

Device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);
Device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_TEXTURE);
Device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);

// set blending factors so that alpha component determines transparency
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCCOLOR);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_DESTCOLOR);

//
// Set camera.
//

D3DXVECTOR3 pos(0.0f, 0.0f, -3.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &pos, &target, &up);

Device->SetTransform(D3DTS_VIEW, &V);

//
// Set projection matrix.
//

D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)Width / (float)Height,
1.0f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);

return true;
}

void Cleanup()
{
d3d::Release<IDirect3DVertexBuffer9*>(BkGndQuad);
d3d::Release<ID3DXMesh*>(Teapot);
d3d::Release<IDirect3DTexture9*>(BkGndTex1);
d3d::Release<IDirect3DTexture9*>(BkGndTex2);
}

bool Display(float timeDelta)
{
if( Device )
{
//
// Update
//

// increase/decrease alpha via keyboard input
if( ::GetAsyncKeyState('A') & 0x8000f )
TeapotMtrl.Diffuse.a += 0.01f;
if( ::GetAsyncKeyState('S') & 0x8000f )
TeapotMtrl.Diffuse.a -= 0.01f;

// force alpha to [0, 1] interval
if(TeapotMtrl.Diffuse.a > 1.0f)
TeapotMtrl.Diffuse.a = 1.0f;
if(TeapotMtrl.Diffuse.a < 0.0f)
TeapotMtrl.Diffuse.a = 0.0f;

//
// Render
//

Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
Device->BeginScene();

// Draw the background
D3DXMATRIX W;
D3DXMatrixIdentity(&W);
Device->SetTransform(D3DTS_WORLD, &W);
Device->SetFVF(Vertex::FVF);
Device->SetStreamSource(0, BkGndQuad, 0, sizeof(Vertex));
Device->SetMaterial(&BkGndMtrl);
Device->SetTexture(1, BkGndTex2);

Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

Device->EndScene();
Device-&gtresent(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 ::DefWindowProc(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;
}

sf_20062232094.zip

152.95 KB, 下载次数:

1367

主题

1993

帖子

2118

积分

金牌会员

Rank: 6Rank: 6

积分
2118
发表于 2006-2-23 20:22:00 | 显示全部楼层

Re:Mix Two textures to One and Put to a rectangle

hao

2

主题

141

帖子

141

积分

注册会员

Rank: 2

积分
141
发表于 2006-2-24 10:40:00 | 显示全部楼层

Re:Mix Two textures to One and Put to a rectangle

方法1:将顶点颜色的alpha值设为0xRRGGBB80(RR GG BB是你顶点的RGB颜色)
方法2: 将纹理混合alpha设为0x00000080
方法3:用PS最直接了当了
思路提供给你了,代码就自己写吧

414

主题

611

帖子

621

积分

高级会员

Rank: 4

积分
621
 楼主| 发表于 2006-3-5 20:33:00 | 显示全部楼层

Re:Mix Two textures to One and Put to a rectangle

有完整的?例??我用MFCTex所?得的ViewCode也是有???,不知道哪?少加了什??

由MFCTex所?得的View Code如下,我只是??tage1的Color Op切?Q成Modulate
?然????果是我要的,可是我?⒁韵碌?ode加入我的程式,?果?s是看不到第二材?BkGndTex2,而?面是?上第一??材??K呈??色
到底?少了什??
有?]有??墓?例有???材?混和的完整?例(除了用PS以外)

HRESULT SetShader( LPDIRECT3DDEVICE9 pd3dDevice )
{
   pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
   pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
   pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
   pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );

   pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_MODULATE );
   pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
   pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
   pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );

   pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
   pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );

   DWORD dwNumPasses;
   return pd3dDevice->ValidateDevice( &dwNumPasses );
}

//我的Code


IDirect3DDevice9* pd3dDevice=Device;
IDirect3DDevice9* m_pd3dDevice=Device;

pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );

pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_MODULATE );
pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );


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

// Draw the background
D3DXMATRIX W;
D3DXMatrixIdentity(&W);
Device->SetTransform(D3DTS_WORLD, &W);
Device->SetFVF(Vertex::FVF);
Device->SetStreamSource(0, BkGndQuad, 0, sizeof(Vertex));

Device->SetTexture(0, BkGndTex1);
Device->SetTexture(1, BkGndTex2);

Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

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

3

主题

72

帖子

72

积分

注册会员

Rank: 2

积分
72
发表于 2006-3-6 09:30:00 | 显示全部楼层

Re:Mix Two textures to One and Put to a rectangle

你的顶点格式有写tex2么?

414

主题

611

帖子

621

积分

高级会员

Rank: 4

积分
621
 楼主| 发表于 2006-3-6 22:08:00 | 显示全部楼层

Re:Mix Two textures to One and Put to a rectangle

?],同一??rectangle
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-23 19:54

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表