」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何使用 TForm::Handle 在 C++ Builder 窗體中渲染 OpenGL 幀?

如何使用 TForm::Handle 在 C++ Builder 窗體中渲染 OpenGL 幀?

發佈於2024-11-07
瀏覽:425

How to Render an OpenGL Frame in a C   Builder Form Using TForm::Handle?

在C Builder 中渲染OpenGL 幀

問題

我想在C Builder 中的窗體內渲染OpenGL 幀,但我遵循提供的OpenGL 啟動程式碼時遇到問題。我該如何解決這個問題?

答案

利用 TForm::Handle 作為視窗句柄

利用 TForm::Handle 作為視窗句柄視窗句柄。

範例實作

以下是改編自舊版 C Builder 的範例:

int TForm1::ogl_init()
{
    if (ogl_inicialized)
        return 1;
    hdc = GetDC(Form1->Handle); // Get device context
    PIXELFORMATDESCRIPTOR pfd;
    // Set pixel format for the DC
    ...
    // Create current rendering context
    hrc = wglCreateContext(hdc);
    if (hrc == NULL)
    {
        ShowMessage("Could not initialize OpenGL Rendering context !!!");
        ogl_inicialized = 0;
        return 0;
    }
    if (!wglMakeCurrent(hdc, hrc))
    {
        wglDeleteContext(hrc); // Destroy rendering context
        ogl_inicialized = 0;
        return 0;
    }
    // ...
    ogl_inicialized = 1;
    return 1;
}

附加說明

  • 包含必要的標頭:
  • 建立一個計時器來觸發渲染。
  • 處理表單調整大小、重繪和滑鼠滾輪輸入的事件。
  • 確保 gl.h 包含在專案中。
  • 查閱提供了更高級 OpenGL 技術的連結。
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3