WinCE OpenGL繪制立方體和紋理貼圖
{ PAINTSTRUCT ps;HDC hdc;
switch (message)
{ case WM_CREATE:break;case WM_PAINT:hdc = BeginPaint(hWnd, ps);
// TODO: 在此添加任意繪圖代碼……
EndPaint(hWnd, ps);break;case WM_DESTROY:{ Clean();PostQuitMessage(0);} break;
default:return DefWindowProc(hWnd, message, wParam, lParam);} return 0;}
BOOL InitOGLES(HWND hWnd)
{ EGLint matchingConfigs;EGLint majorVersion = 0;EGLint minorVersion = 0;
glesDisplay = eglGetDisplay(GetDC(hWnd)); //Ask for an available display if( glesDisplay == EGL_NO_DISPLAY || eglGetError() != EGL_SUCCESS )
return FALSE;
EGLConfig *configs_list;EGLint num_configs;// Display initialization (we don't care about the OGLES version numbers)
if( eglInitialize( glesDisplay, majorVersion, minorVersion) == EGL_FALSE)
{ printf(eglInitialize failed, eglGetError = 0x%04xn,eglGetError());return FALSE;} // find out how many configurations are supported if ( eglGetConfigs( glesDisplay, NULL, 0, num_configs)==EGL_FALSE || eglGetError() != EGL_SUCCESS )
return FALSE;configs_list = (EGLConfig*) malloc(num_configs * sizeof(EGLConfig));if (configs_list == NULL)
return FALSE;// Get Configurations if( eglGetConfigs( glesDisplay, configs_list, num_configs, num_configs)== EGL_FALSE || eglGetError() != EGL_SUCCESS )
return FALSE;// Obtain the first configuration with a depth buffer of 16 bits EGLint attrs[] = { EGL_RED_SIZE, 5,EGL_GREEN_SIZE, 6,EGL_BLUE_SIZE, 5,EGL_DEPTH_SIZE, 16,EGL_NONE };if (!eglChooseConfig(glesDisplay, attrs, configs_list, num_configs, matchingConfigs))
{ return eglGetError();} // If there isn't any configuration enough good if (matchingConfigs 1)
return FALSE;/*eglCreateWindowSurface creates an onscreen EGLSurface and returns a handle to it. Any EGL rendering context created with a compatible EGLConfig can be used to render into this surface.*/ glesSurface = eglCreateWindowSurface(glesDisplay, configs_list[0], hWnd, 0);if(!glesSurface)
return FALSE;
// Let's create our rendering context glesContext=eglCreateContext(glesDisplay, configs_list[0], 0, 0);if(!glesContext)
return FALSE;//Now we will activate the context for rendering eglMakeCurrent(glesDisplay, glesSurface, glesSurface, glesContext);
/*Remember: because we are programming for a mobile device, we cant use any of the OpenGL ES functions that finish in 'f', we must use the fixed point version (they finish in 'x'*/ glClearColorx(0, 0, 0, 0);glShadeModel(GL_SMOOTH);
RECT rc;GetWindowRect(hWnd, rc);UINT width = rc.right - rc.left;UINT height = rc.bottom - rc.top;// 設(shè)置OpenGL場(chǎng)景的大小glViewport(rc.left, rc.top, width, height);
// 設(shè)置投影矩陣glMatrixMode(GL_PROJECTION);glLoadIdentity();
// 投影變換(透視投影)
float ratio = (float) width / height;glFrustumf(-ratio, ratio, -1, 1, 2, 10);//glOrthox(FixedFromInt(-50),F(xiàn)ixedFromInt(50), FixedFromInt(-50), FixedFromInt(50), FixedFromInt(-50), FixedFromInt(50));
// 選擇模型觀察矩陣glMatrixMode(GL_MODELVIEW);// 重置模型觀察矩陣glLoadIdentity();
return TRUE;}
void CreateSurface()
{ glDisable(GL_DITHER);
// 告訴系統(tǒng)對(duì)透視進(jìn)行修正glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);// 黑色背景glClearColor(0, 0, 0, 0);
// 啟用陰影平滑glShadeModel(GL_SMOOTH);
// 設(shè)置深度緩存glClearDepthf(1.0f);// 啟用深度測(cè)試glEnable(GL_DEPTH_TEST);// 所作深度測(cè)試的類型glDepthFunc(GL_LEQUAL);
// 啟用2D紋理glEnable(GL_TEXTURE_2D);// 加載紋理LoadTexture(_T(\NAND2\OpenGlRes\1.png),texture[0]);LoadTexture(_T(\NAND2\OpenGlRes\1.png),texture[1]);LoadTexture(_T(\NAND2\OpenGlRes\1.png),texture[2]);LoadTexture(_T(\NAND2\OpenGlRes\1.png),texture[3]);LoadTexture(_T(\NAND2\OpenGlRes\1.png),texture[4]);LoadTexture(_T(\NAND2\OpenGlRes\1.png),texture[5]);}
void Render()
{ static float rotation = 0;// 清除屏幕和深度緩存glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);// 重置當(dāng)前的模型觀察矩陣glLoadIdentity();
// 坐標(biāo)變換glTranslatef(0.0f, 0.0f, -5.0f);
// 設(shè)置旋轉(zhuǎn)glRotatef(rotation++, 0.0f, 1.0f, 0.0f);glRotatef(rotation++, 1.0f, 0.0f, 0.0f);
glEnableClientState(GL_VERTEX_ARRAY);glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_SHORT, 0, vertices);glTexCoordPointer(2, GL_SHORT, 0, texCoords);
// 繪制立方體并綁定紋理glBindTexture(GL_TEXTURE_2D, texture[0]);glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indices1);glBindTexture(GL_TEXTURE_2D, texture[1]);glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_BYTE, indices2);glBindTexture(GL_TEXTURE_2D, texture[2]);glDrawElements(GL_TRIANGLE_STRIP, 12, GL_UNSIGNED_BYTE, indices3);glBindTexture(GL_TEXTURE_2D, texture[3]);glDrawElements(GL_TRIANGLE_STRIP, 16, GL_UNSIGNED_BYTE, indices4);glBindTexture(GL_TEXTURE_2D, texture[4]);glDrawElements(GL_TRIANGLE_STRIP, 20, GL_UNSIGNED_BYTE, indices5);glBindTexture(GL_TEXTURE_2D, texture[5]);glDrawElements(GL_TRIANGLE_STRIP, 24, GL_UNSIGNED_BYTE, indices6);
評(píng)論