8,305
社区成员
发帖
与我相关
我的任务
分享
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, 100, 0, 100, -1000, 100);
////
// 这里添加需要的图片或文字都可以
////
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
#include <stdio.h>
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#define BLACK_WHITE
HWND hwnd;
LPDIRECT3D9 pD3d = NULL;
D3DCAPS9 ddcaps;
D3DPRESENT_PARAMETERS d3dpp;
LPDIRECT3DDEVICE9 pDevice = NULL;
LPDIRECT3DTEXTURE9 pTexture = NULL;
LPDIRECT3DVERTEXBUFFER9 pVB = NULL;
UINT clientWidth = 0;
UINT clientHeight = 0;
bool bExit = false;
UINT imgWidth = 320;
UINT imgHeight = 240;
// The 2-D vertex format and descriptor
typedef struct {
FLOAT x, y, z; // 2-D coordinates
FLOAT rhw; // rhw
FLOAT u, v; // Texture coordinates
} sVertex;
#define VERTEXFVF (D3DFVF_XYZRHW | D3DFVF_TEX1)
void ReleaseD3D()
{
if( pDevice )
{
pVB->Release();
pTexture->Release();
pDevice->Release();
pD3d->Release();
}
pDevice = NULL;
}
int InitD3D()
{
pD3d=Direct3DCreate9(D3D_SDK_VERSION);
if(FAILED(pD3d->GetDeviceCaps(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
&ddcaps)))
return 0;
//计算显示的实际大小
RECT clientRect;
GetClientRect( hwnd, &clientRect);
clientWidth = clientRect.right - clientRect.left;
clientHeight = clientRect.bottom - clientRect.top;
d3dpp.BackBufferWidth=clientWidth;
d3dpp.BackBufferHeight=clientHeight;
d3dpp.BackBufferFormat=D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount=1;
d3dpp.MultiSampleType=D3DMULTISAMPLE_NONE; //为了清晰关掉反锯齿
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow=hwnd;
d3dpp.Windowed=true;
d3dpp.EnableAutoDepthStencil=true;
d3dpp.AutoDepthStencilFormat=D3DFMT_D24S8;
d3dpp.Flags=0;
d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT;
if(FAILED(pD3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp,&pDevice)))
return 0;
if(FAILED(D3DXCreateTextureFromFile(pDevice,"test.bmp",&pTexture)))
return 0;
D3DSURFACE_DESC faceDesc;
if(FAILED(pTexture->GetLevelDesc(0, &faceDesc)))
return 0;
imgHeight = faceDesc.Height;
imgWidth = faceDesc.Width;
float left = 0.0f;
float right = clientWidth;
float top = 0.0f;
float bottom = clientHeight;
float fWidth=clientWidth/(float)imgWidth;
float fHeight=clientHeight/(float)imgHeight;
sVertex Verts[6] =
{
{ left, top, 0.5f, 1.0f, 0.0f, 0.0f}, // x, y, z, rhw, u, v
{ right, top, 0.5f, 1.0f, fWidth, 0.0f},
{ right, bottom, 0.5f, 1.0f, fWidth, fHeight},
{ left, top, 0.5f, 1.0f, 0.0f, 0.0f},
{ right, bottom, 0.5f, 1.0f, fWidth, fHeight},
{ left, bottom, 0.5f, 1.0f, 0.0f, fHeight},
};
//移半个像素
for (int i=0; i<6; i++)
{
Verts[i].u += (0.5/clientWidth);
Verts[i].v += (0.5/clientHeight);
}
if(FAILED( pDevice->CreateVertexBuffer( sizeof(Verts), D3DUSAGE_WRITEONLY,VERTEXFVF,D3DPOOL_MANAGED,&pVB,NULL)))
return 0;
void* pData = NULL;
pVB->Lock(0,0,&pData,0);
memcpy( pData, Verts, sizeof(Verts));
pVB->Unlock();
//设置点采样
pDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
pDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
pDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
/*pDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
pDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
pDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
*/
pDevice->SetFVF(VERTEXFVF);
pDevice->SetTexture(0,pTexture);
pDevice->SetStreamSource(0,pVB,0,sizeof(sVertex));
return 1;
}
void Render()
{
if( bExit)
return;
if( pDevice )
{
pDevice->BeginScene();
pDevice->DrawPrimitive( D3DPT_TRIANGLELIST,0,2);
pDevice->EndScene();
pDevice->Present(NULL,NULL,hwnd,NULL);
}
}
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch(msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
bExit = true;
}break;
default:
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASS winclass;
MSG msg;
winclass.style=CS_HREDRAW|CS_VREDRAW;
winclass.lpfnWndProc=WindowProc;
winclass.cbClsExtra=NULL;
winclass.cbWndExtra=NULL;
winclass.hInstance=hInstance;
winclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
winclass.hCursor=LoadCursor(NULL,IDC_ARROW);
winclass.hbrBackground=(HBRUSH)GetStockObject(0);
winclass.lpszClassName="Texture test";
winclass.lpszMenuName=NULL;
if(!RegisterClass(&winclass))
return 0;
if(!(hwnd=CreateWindow("Texture test","Texture test",WS_OVERLAPPED | WS_VISIBLE | WS_SYSMENU | WS_CAPTION,
0,0,320+6,240+32,
NULL,NULL,
hInstance,
NULL)))
return 0;
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
if(!InitD3D())
return 0;
while(!bExit)
{
if(PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE))
{
Sleep(0);
if (!TranslateAccelerator(msg.hwnd, NULL, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Render();
}
ReleaseD3D();
UnregisterClassA("Texture test",hInstance);
return(msg.wParam);
}