WinCE OpenGL繪制立方體和紋理貼圖
最近工作之余,開始學(xué)習(xí)OpenGL, 想到WINCE也是支持OpenGL的,只不過是嵌入式的OpenGL ES.于是嘗試寫了一個WINCE下的OpenGL測試程序,實(shí)現(xiàn)了繪制立方體和紋理。效果圖如下:
本文引用地址:http://butianyuan.cn/article/201610/305941.htm需要注意的是,WINCE系統(tǒng)上開發(fā)OpenGL程序需具備以下條件:
1. 處理器的支持,嵌入式處理器需支持3D加速渲染。
2. WINCE內(nèi)核的支持,定制內(nèi)核時需添加OpenGL ES相關(guān)組件。
以下是具體的參考代碼:
view plain /******************************************************************** filename: WinceOpenGLDemo.cpp created: 2011-01-05 author: firehood purpose: 利用OpenGL ES實(shí)現(xiàn)了繪制立方體和紋理效果*********************************************************************/
// WinceOpenGLDemo.cpp : 定義應(yīng)用程序的入口點(diǎn)。
//
#include stdafx.h #include WinceOpenGLDemo.h #include
// OpenGL lib #pragma comment(lib, OpenGlLib\libGLESv1_CM.lib)
#pragma comment(lib, OpenGlLib\libEGL.lib)
// 全局變量:HINSTANCE g_hInst; // 當(dāng)前實(shí)例TCHAR szAppName[] = LOpenGLES; /*The application name and the window caption*/ CImgLoader g_Image;// OpenGL variables EGLDisplay glesDisplay; // EGL display EGLSurface glesSurface; // EGL rendering surface EGLContext glesContext; // EGL rendering context
GLuint texture[6] = {0};
// 立方體定點(diǎn)坐標(biāo)GLshort vertices[] = { -1,-1,1,1,-1,1,1,1,1,-1,1,1,
-1,-1,-1,-1,1,-1,1,1,-1,1,-1,-1,
-1,1,-1,-1,1,1,1,1,1,1,1,-1,
-1,-1,-1,1,-1,-1,1,-1,1,-1,-1,1,
1,-1,-1,1,1,-1,1,1,1,1,-1,1,
-1,-1,-1,-1,-1,1,-1,1,1,-1,1,-1 };
// 各個面紋理坐標(biāo)GLshort texCoords[] = { 0,0,1,0,1,1,0,1,};
// 三角形索引數(shù)據(jù)GLbyte indices1[] = { 0,1,3,2,0,0,0,0,0,0,0,0 };GLbyte indices2[] = { 0,0,0,0,4,5,7,6,0,0,0,0,0,0,0,0 };GLbyte indices3[] = { 0,0,0,0,8,9,11,10,0,0,0,0,0,0,0,0 };GLbyte indices4[] = { 0,0,0,0,12,13,15,14,0,0,0,0,0,0,0,0 };GLbyte indices5[] = { 0,0,0,0,16,17,19,18,0,0,0,0 };GLbyte indices6[] = { 0,0,0,0,20,21,23,22 };
// 此代碼模塊中包含的函數(shù)的前向聲明:ATOM MyRegisterClass(HINSTANCE, LPTSTR);BOOL InitInstance(HINSTANCE, int);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL InitOGLES(HWND hWnd);void CreateSurface();BOOL LoadTexture(LPCTSTR lpFileName,GLuint *id);void Render();void Clean();
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{ MSG msg;
// 執(zhí)行應(yīng)用程序初始化:if (!InitInstance(hInstance, nCmdShow))
{ return FALSE;}
BOOL done = FALSE;// 主消息循環(huán):while(!done)
{ if(PeekMessage(msg,NULL,0,0,PM_REMOVE))
{ if(msg.message==WM_QUIT)
done = TRUE;else { TranslateMessage(msg);DispatchMessage(msg);} else { Render();};}
return (int) msg.wParam;}
// // 函數(shù): MyRegisterClass()
// // 目的: 注冊窗口類。
// // 注釋:// ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{ WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;wc.lpfnWndProc = WndProc;wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.hInstance = hInstance;wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINCEOPENGLDEMO));wc.hCursor = 0;wc.hbrBackground = (HBRUSH) GetStockObject(NULL_BRUSH);wc.lpszMenuName = 0;wc.lpszClassName = szWindowClass;
return RegisterClass(wc);}
// // 函數(shù): InitInstance(HINSTANCE, int)
// // 目的: 保存實(shí)例句柄并創(chuàng)建主窗口// // 注釋:// // 在此函數(shù)中,我們在全局變量中保存實(shí)例句柄并// 創(chuàng)建和顯示主程序窗口。
// BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{ HWND hWnd;
g_hInst = hInstance; // 將實(shí)例句柄存儲在全局變量中
if (!MyRegisterClass(hInstance, szAppName))
{ return FALSE;}
hWnd = CreateWindow(szAppName,WS_VISIBLE,0,::GetSystemMetrics(SM_CXSCREEN),::GetSystemMetrics(SM_CYSCREEN),NULL,hInstance,NULL);
if (!hWnd)
{ return FALSE;}
if(!InitOGLES(hWnd))
{ printf(InitOGLES failedn);return FALSE;} CreateSurface();
ShowWindow(hWnd, SW_SHOW);UpdateWindow(hWnd);
return TRUE;}
// // 函數(shù): WndProc(HWND, UINT, WPARAM, LPARAM)
// // 目的: 處理主窗口的消息。
// // WM_COMMAND - 處理應(yīng)用程序菜單// WM_PAINT - 繪制主窗口// WM_DESTROY - 發(fā)送退出消息并返回// LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
評論