新闻中心

EEPW首页>嵌入式系统>设计应用> WinCE OpenGL绘制立方体和纹理贴图

WinCE OpenGL绘制立方体和纹理贴图

作者: 时间:2016-10-08 来源:网络 收藏

{ 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;// 设置OpenGL场景的大小glViewport(rc.left, rc.top, width, height);

// 设置投影矩阵glMatrixMode(GL_PROJECTION);glLoadIdentity();

// 投影变换(透视投影)

float ratio = (float) width / height;glFrustumf(-ratio, ratio, -1, 1, 2, 10);//glOrthox(FixedFromInt(-50),FixedFromInt(50), FixedFromInt(-50), FixedFromInt(50), FixedFromInt(-50), FixedFromInt(50));

// 选择模型观察矩阵glMatrixMode(GL_MODELVIEW);// 重置模型观察矩阵glLoadIdentity();

return TRUE;}

void CreateSurface()

{ glDisable(GL_DITHER);

// 告诉系统对透视进行修正glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);// 黑色背景glClearColor(0, 0, 0, 0);

// 启用阴影平滑glShadeModel(GL_SMOOTH);

// 设置深度缓存glClearDepthf(1.0f);// 启用深度测试glEnable(GL_DEPTH_TEST);// 所作深度测试的类型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);// 重置当前的模型观察矩阵glLoadIdentity();

// 坐标变换glTranslatef(0.0f, 0.0f, -5.0f);

// 设置旋转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);



关键词:

评论


相关推荐

技术专区

关闭