D3D学习之一:Surface Texture例子

现在有一个远期目标:写一个带融合的播放器。播放器已经用mpc-hc编译了出来,但融合部分没有。打算中重新学习D3D的知识,在深化学习后,实现融合。

学习Dxdirect SDK版本为Microsoft DirectX SDK (June 2010),SDK中的所有例子已经全部用VS2010重新编译跑过一次。然后,自己尝试实现一些例子。我的目标是实现播放器,用不到3D场景,就想着用LPDIRECT3DSURFACE9来显示一张图片。在网上也很容易找到了相应的例子,但编译后发现并没有能正常的显示出图片。(图片大小为1280*800,窗口创建为1280*800)

    D3DXIMAGE_INFO info;
    if( FAILED(D3DXGetImageInfoFromFile(TEX_FILE, &info)))
    {
        return S_FALSE;
    }

D3DPOOL pool = D3DPOOL_SYSTEMMEM;
    result =g_pd3dDevice->CreateOffscreenPlainSurface(info.Width, info.Height, info.Format, pool, &g_surface, NULL);
    if( result == S_FALSE )
    {
        return result;
    }

//

 

继续查找资料并实现调试后发现,窗口创建后,GetBackBuffer获取到的surface远小于1280*800,所以CreateOffscreenPlainSurface传入的宽高参数应当是GetBackBuffer中获取的surface的实际大小

实现代码如下:

//-----------------------------------------------------------------------------
// File: Surface.cpp
//
// Desc: 演示LPDIRECT3DSURFACE9的运用
//         在调用CreateOffscreenPlainSurface宽和高并不能是窗口的大小,也不能是图片的大小,而应当是BackSurface的大小
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#pragma warning( disable : 4996 ) // disable deprecated warning 
#include <strsafe.h>
#pragma warning( default : 4996 )

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9             g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device

LPDIRECT3DSURFACE9        g_surface=NULL;

#define TEX_FILE L"./无标题.png"

//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice. Since we are now
    // using more complex geometry, we will create a device with a zbuffer.
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof( d3dpp ) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
        &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    // Turn off culling
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

    // Turn off D3D lighting
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

    // Turn on the zbuffer
    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );

    return S_OK;
}


//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Create the textures and vertex buffers
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
    D3DXIMAGE_INFO info;
    if( FAILED(D3DXGetImageInfoFromFile(TEX_FILE, &info)))
    {
        return S_FALSE;
    }
    HRESULT result;

    LPDIRECT3DSURFACE9        win_surface=NULL;

    result = g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &win_surface);
    if( result == S_FALSE )
    {
        return result;
    }
    D3DSURFACE_DESC desc;
    win_surface->GetDesc(&desc);  //win_surface大小并不是创建窗口时的大小
    win_surface->Release();

    D3DPOOL pool = D3DPOOL_SYSTEMMEM;
    result =g_pd3dDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, info.Format, pool, &g_surface, NULL);
    if( result == S_FALSE )
    {
        return result;
    }
    result = D3DXLoadSurfaceFromFile(g_surface, NULL, NULL, TEX_FILE, NULL, D3DX_FILTER_NONE, 0xFF000000, NULL);
    if( result == S_FALSE )
    {
        return result;
    }
    return S_OK;
}


//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{

    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )
        g_pD3D->Release();

    if( g_surface != NULL )
    {
        g_surface->Release();
    }
}


//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    // Clear the backbuffer and the zbuffer
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
        D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );

    // Begin the scene
    //if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {

        LPDIRECT3DSURFACE9 backBuffer = NULL;
        HRESULT result = g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
        if( result == S_FALSE )
        {
            return ;
        }
        D3DSURFACE_DESC desc;
        backBuffer->GetDesc(&desc);

        result = g_pd3dDevice->UpdateSurface(g_surface, NULL, backBuffer, NULL);
        if( result == S_FALSE )
        {
            return;
        }
        backBuffer->Release();
        // End the scene
        //g_pd3dDevice->EndScene();
    }

    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}


//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
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 );
}


//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
    UNREFERENCED_PARAMETER( hInst );

    // Register the window class
    WNDCLASSEX wc =
    {
        sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
        GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
        L"D3D Surface", NULL
    };
    RegisterClassEx( &wc );

    // Create the application's window
    HWND hWnd = CreateWindow( L"D3D Surface", L"D3D Surface",
        WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800,
        NULL, NULL, wc.hInstance, NULL );

    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        // Create the scene geometry
        if( SUCCEEDED( InitGeometry() ) )
        {
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );

            // Enter the message loop
            MSG msg;
            ZeroMemory( &msg, sizeof( msg ) );
            while( msg.message != WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                    Render();
            }
        }
    }

    UnregisterClass( L"D3D Surface", wc.hInstance );
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用OpenGL ES在Android中将一个SurfaceTexture的内容复制到另一个SurfaceTexture中。以下是实现此操作的基本步骤: 1.创建一个新的SurfaceTexture和相关的Surface。 ```java SurfaceTexture surfaceTexture1 = new SurfaceTexture(0); Surface surface1 = new Surface(surfaceTexture1); SurfaceTexture surfaceTexture2 = new SurfaceTexture(0); Surface surface2 = new Surface(surfaceTexture2); ``` 2.使用第一个SurfaceTexture作为纹理绑定到OpenGL ES程序中。 ```java int textureId; GLES20.glGenTextures(1, textureId, 0); GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE); GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE); surfaceTexture1.setDefaultBufferSize(width, height); surfaceTexture1.setOnFrameAvailableListener(new MyOnFrameAvailableListener()); Surface surface = new Surface(surfaceTexture1); ``` 3.创建一个OpenGL ES程序,并将第一个SurfaceTexture作为输入纹理。 ```java int program = GLES20.glCreateProgram(); int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER); int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER); GLES20.glAttachShader(program, vertexShader); GLES20.glAttachShader(program, fragmentShader); GLES20.glLinkProgram(program); GLES20.glUseProgram(program); int aPosition = GLES20.glGetAttribLocation(program, "aPosition"); int aTextureCoordinates = GLES20.glGetAttribLocation(program, "aTextureCoordinates"); int uTextureMatrix = GLES20.glGetUniformLocation(program, "uTextureMatrix"); int uTextureSampler = GLES20.glGetUniformLocation(program, "uTextureSampler"); FloatBuffer vertexBuffer = ByteBuffer.allocateDirect(4 * 2 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); vertexBuffer.put(new float[]{-1, -1, 1, -1, -1, 1, 1, 1}).position(0); FloatBuffer textureCoordinatesBuffer = ByteBuffer.allocateDirect(4 * 2 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); textureCoordinatesBuffer.put(new float[]{0, 0, 1, 0, 0, 1, 1, 1}).position(0); GLES20.glVertexAttribPointer(aPosition, 2, GLES20.GL_FLOAT, false, 0, vertexBuffer); GLES20.glVertexAttribPointer(aTextureCoordinates, 2, GLES20.GL_FLOAT, false, 0, textureCoordinatesBuffer); GLES20.glEnableVertexAttribArray(aPosition); GLES20.glEnableVertexAttribArray(aTextureCoordinates); Matrix.setIdentityM(textureMatrix, 0); GLES20.glUniformMatrix4fv(uTextureMatrix, 1, false, textureMatrix, 0); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); GLES20.glUniform1i(uTextureSampler, 0); ``` 4.在onFrameAvailable回调中更新第一个SurfaceTexture并将数据复制到第二个SurfaceTexture中。 ```java class MyOnFrameAvailableListener implements SurfaceTexture.OnFrameAvailableListener { @Override public void onFrameAvailable(SurfaceTexture surfaceTexture) { surfaceTexture.updateTexImage(); surfaceTexture.getTransformMatrix(textureMatrix); GLES20.glViewport(0, 0, width, height); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); surface2.attachToGLContext(textureId); surfaceTexture2.updateTexImage(); surface2.detachFromGLContext(); } } ``` 5.最后,您可以使用第二个SurfaceTexture来显示输出图像。 ```java mRenderer = new MyRenderer(surfaceTexture2); mSurfaceView = new GLSurfaceView(this); mSurfaceView.setEGLContextClientVersion(2); mSurfaceView.setRenderer(mRenderer); setContentView(mSurfaceView); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值