游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2585|回复: 6

哪位好心人可以帮助把opengl的代码翻译成directx?

[复制链接]

83

主题

196

帖子

196

积分

注册会员

Rank: 2

积分
196
发表于 2007-9-6 20:36:00 | 显示全部楼层 |阅读模式
偶不懂CubeMap,
在看Samples中的HDRCubeMap代码,难度特大,
网上找了一份opengl的动态的CubeMap,粘贴上来,
求大大们翻译成directx的好么?

一个渲染到CubeMap的代码,因为要渲染六个面,所以场景要渲染六次, 所以动态CubeMap是十分需要性能的.

//Function to generate the cibemats for this frame
//PosX, PosY, PosZ 为需要采样的中心坐标,一般把他定在反射物体的中心.

const int CUBE_MAP_SIZE=64; //动态CubeMap的大小
void CreateCubeMap(GLfloat PosX, GLfloat PosY, GLfloat PosZ)
{
int i;
glViewport(0,0,CUBE_MAP_SIZE,CUBE_MAP_SIZE);
glScissor(0,0,CUBE_MAP_SIZE,CUBE_MAP_SIZE);
glEnable(GL_SCISSOR_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90,1,1,2000); //设定90度,每一面的图才能完整的拼在一起
glMatrixMode(GL_MODELVIEW);

for(i=0;i<=5;i++)
{
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//渲染每个面到CubeMap上面去
switch(i)
{
case 0:
glRotatef( 90,0,1,0);
glRotatef(180,1,0,0);
break;

case 1:
glRotatef(-90,0,1,0);
glRotatef(180,1,0,0);
break;

case 2:
glRotatef(-90,1,0,0);
break;

case 3:
glRotatef( 90,1,0,0);
break;

case 4:
glRotatef(180,1,0,0);
break;

case 5:
glRotatef(180,0,0,1);
break;
}

glTranslatef(PosX,PosY,PosZ);
glScalef(-1,-1,-1);
RenderScene(); //渲染场景, 可以换成你自己的场景渲染函数

glEnable(GL_TEXTURE_CUBE_MAP_ARB);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB,CubeMap_TEX);
glCopyTexSubImage2D(CubeMap_define,0,0,0,0,0,CUBE_MAP_SIZE,CUBE_MAP_SIZE);
glDisable(GL_TEXTURE_CUBE_MAP_ARB);
}

//恢复以前的设置
//SCREEN_WIDTH,SCREEN_HEIGHT是当前OGL程序的水平,垂直分辨率.

glScissor(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
glDisable(GL_SCISSOR_TEST);
glResizeWnd(SCREEN_WIDTH,SCREEN_HEIGHT);

}

glResizeWnd是一段Delphi代码,很容易翻译成C++的

procedure glResizeWnd(Width, Height : Integer);
begin
if (Height = 0) then // prevent divide by zero exception
Height := 1;
SCREEN_WIDTH := Width;
SCREEN_HEIGHT := Height;

glViewport(0, 0, Width, Height); // Set the viewport for the OpenGL window
glMatrixMode(GL_PROJECTION); // Change Matrix Mode to Projection
glLoadIdentity(); // Reset View
gluPerspective(45.0, Width/Height, 1.0, 200.0); // Do the perspective calculations. Last value = max clipping depth

glMatrixMode(GL_MODELVIEW); // Return to the modelview matrix
glLoadIdentity(); // Reset View
end;

1367

主题

1993

帖子

2118

积分

金牌会员

Rank: 6Rank: 6

积分
2118
发表于 2007-9-6 20:39:00 | 显示全部楼层

Re:哪位好心人可以帮助把opengl的代码翻译成directx?

好心人帮助

32

主题

1259

帖子

1351

积分

金牌会员

Rank: 6Rank: 6

积分
1351
发表于 2007-9-6 21:44:00 | 显示全部楼层

Re:哪位好心人可以帮助把opengl的代码翻译成directx?

怎么看怎么像我以前写过的一段代码.

10

主题

27

帖子

33

积分

注册会员

Rank: 2

积分
33
发表于 2007-9-7 15:33:00 | 显示全部楼层

Re:哪位好心人可以帮助把opengl的代码翻译成directx?

Creating Cubic Environment Map Surfaces (Direct3D 9)
You create a cubic environment map texture by calling the IDirect3DDevice9::CreateCubeTexture method. Cubic environment map textures must be square, with dimensions that are a power of two.

The following code example shows how your C++ application might create a simple cubic environment map.

// Init m_d3dDevice to point to an IDirect3DDevice9 interface

LPDIRECT3DCUBETEXTURE9 m_pCubeMap;

m_d3dDevice->CreateCubeTexture(256, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R8G8B8,
                                                                D3DPOOL_DEFAULT, &m_pCubeMap);

Accessing Cubic Environment Map Faces
You can navigate between faces of a cubic environment map by using the IDirect3DCubeTexture9::GetCubeMapSurface method.

The following code example uses IDirect3DCubeTexture9::GetCubeMapSurface to retrieve the cube-map surface used for the positive y-face (face 2).

// Init m_pCubeMap to point to an IDirect3DCubeTexture9 interface

LPDIRECT3DSURFACE9 pFace2;
m_pCubeMap->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_Y, 0, &pFace2);

The first parameter that IDirect3DCubeTexture9::GetCubeMapSurface accepts is a D3DCUBEMAP_FACES enumerated value that describes the attached surface that the method should retrieve. The second parameter tells Direct3D which level of a mipmapped cube texture to retrieve. The third parameter accepted is the address of the IDirect3DSurface9 interface, representing the returned cube texture surface. Because this cube-map is not mipmapped, 0 is used here.

Note   

After calling this method, the internal reference count on the IDirect3DSurface9 interface is increased. When you are done using this surface, be sure to call the IUnknown method on this IDirect3DSurface9 interface or you will have a memory leak.


Rendering to Cubic Environment Maps
You can copy images to the individual faces of the cube map just like you would any other texture or surface object. The most important thing to do before rendering to a face is set the transformation matrices so that the camera is positioned properly and points in the proper direction for that face: forward (+z), backward (-z), left (-x), right (+x), up (+y), or down (-y).

The following C++ code example prepares and sets a view matrix according to the face being rendered.

// Init pCubeMap to point to an IDirect3DCubeTexture9 interface
// Init d3dDevice to point to an IDirect3DDevice9 interface

void RenderFaces()
{
    // Save transformation matrices of the device
    D3DXMATRIX matProjSave, matViewSave;
    d3dDevice->GetTransform(D3DTS_VIEW,       &matViewSave ;
    d3dDevice->GetTransform(D3DTS_PROJECTION, &matProjSave);

    // Store the current back buffer and z-buffer
    LPDIRECT3DSURFACE9 pBackBuffer, pZBuffer;
    d3dDevice->GetRenderTarget(&pBackBuffer);
    d3dDevice->GetDepthStencilSurface(&pZBuffer);

Remember, each face of a cubic environment map represents a 90-degree field of view. Unless your application requires a different field of view angle - for special effects, for example - take care to set the projection matrix accordingly.

This code example creates and sets a projection matrix for the most common case.

    // Use 90-degree field of view in the projection
    D3DMATRIX matProj;
    D3DXMatrixPerspectiveFovLH(matProj, D3DX_PI/2, 1.0f, 0.5f, 1000.0f);
    d3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);

    // Loop through the six faces of the cube map
    for(DWORD i=0; i<6; i++)
    {
        // Standard view that will be overridden below
        D3DVECTOR vEnvEyePt = D3DVECTOR(0.0f, 0.0f, 0.0f);
        D3DVECTOR vLookatPt, vUpVec;

        switch(i)
        {
            case D3DCUBEMAP_FACE_POSITIVE_X:
                vLookatPt = D3DVECTOR(1.0f, 0.0f, 0.0f);
                vUpVec    = D3DVECTOR(0.0f, 1.0f, 0.0f);
                break;
            case D3DCUBEMAP_FACE_NEGATIVE_X:
                vLookatPt = D3DVECTOR(-1.0f, 0.0f, 0.0f);
                vUpVec    = D3DVECTOR( 0.0f, 1.0f, 0.0f);
                break;
            case D3DCUBEMAP_FACE_POSITIVE_Y:
                vLookatPt = D3DVECTOR(0.0f, 1.0f, 0.0f);
                vUpVec    = D3DVECTOR(0.0f, 0.0f,-1.0f);
                break;
            case D3DCUBEMAP_FACE_NEGATIVE_Y:
                vLookatPt = D3DVECTOR(0.0f,-1.0f, 0.0f);
                vUpVec    = D3DVECTOR(0.0f, 0.0f, 1.0f);
                break;
            case D3DCUBEMAP_FACE_POSITIVE_Z:
                vLookatPt = D3DVECTOR( 0.0f, 0.0f, 1.0f);
                vUpVec    = D3DVECTOR( 0.0f, 1.0f, 0.0f);
                break;
            case D3DCUBEMAP_FACE_NEGATIVE_Z:
                vLookatPt = D3DVECTOR(0.0f, 0.0f,-1.0f);
                vUpVec    = D3DVECTOR(0.0f, 1.0f, 0.0f);
                break;
        }

         D3DMATRIX matView;
         D3DXMatrixLookAtLH(matView, vEnvEyePt, vLookatPt, vUpVec);
         d3dDevice->SetTransform(D3DTS_VIEW, &matView);

When the camera is in position and the projection matrix set, you can render the scene. Each object in the scene should be positioned as you would normally position them. The following code example, provided for completeness, outlines this task.

        // Get pointer to surface in order to render to it
        LPDIRECT3DSURFACE9 pFace;
        pCubeMap->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0, &pFace);
        d3dDevice->SetRenderTarget (pFace , pZBuffer);
        SAFE_RELEASE(pFace);

        d3dDevice->BeginScene();
        // Render scene here
                ...
        d3dDevice->EndScene();
    }

    // Change the render target back to the main back buffer.
    d3dDevice->SetRenderTarget(pBackBuffer, pZBuffer);
    SAFE_RELEASE(pBackBuffer);
    SAFE_RELEASE(pZBuffer);

    // Restore the original transformation matrices
    d3dDevice->SetTransform(D3DTS_VIEW,       &matViewSave);
    d3dDevice->SetTransform(D3DTS_PROJECTION, &matProjSave);
}

Note the call to the IDirect3DDevice9::SetRenderTarget method. When rendering to the cube map faces, you must assign the face as the current render-target surface. Applications that use depth buffers can explicitly create a depth-buffer for the render-target, or reassign an existing depth-buffer to the render-target surface. The code sample above uses the latter approach.

10

主题

27

帖子

33

积分

注册会员

Rank: 2

积分
33
发表于 2007-9-7 15:38:00 | 显示全部楼层

Re:哪位好心人可以帮助把opengl的代码翻译成directx?

DX的SDK里有啊。。自己找出来看吧

83

主题

196

帖子

196

积分

注册会员

Rank: 2

积分
196
 楼主| 发表于 2007-9-9 15:50:00 | 显示全部楼层

Re:哪位好心人可以帮助把opengl的代码翻译成directx?

谢谢楼上的原来自己手头上已经有的.
但是不知贴图是怎么调用的.用
HRESULT D3DXCreateCubeTextureFromFile(
  LPDIRECT3DDEVICE9 pDevice,
  LPCTSTR pSrcFile,
  LPDIRECT3DCUBETEXTURE9 * ppCubeTexture
);
么?那要用DDS文件也,还要用D3dx9tex.h.

0

主题

228

帖子

285

积分

中级会员

Rank: 3Rank: 3

积分
285
发表于 2007-9-10 11:36:00 | 显示全部楼层

Re:哪位好心人可以帮助把opengl的代码翻译成directx?

dxsdk不是有动态cubemap的吗?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-6-21 22:13

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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