新聞中心

        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);



        關鍵詞:

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 深泽县| 阜宁县| 修水县| 灵川县| 栖霞市| 化隆| 汝南县| 道孚县| 奇台县| 辽阳市| 芜湖市| 淮滨县| 周至县| 盘山县| 河西区| 大同市| 双江| 广南县| 谢通门县| 平泉县| 陇川县| 潍坊市| 逊克县| 宁明县| 南昌市| 靖边县| 临澧县| 军事| 沐川县| 晋州市| 松潘县| 鞍山市| 中方县| 阿勒泰市| 黎川县| 长乐市| 万宁市| 浦北县| 乌鲁木齐县| 松江区| 宁城县|