簡體   English   中英

如何在C ++構建器中渲染openGL框架?

[英]How to render an openGL frame in C++ builder?

我想在C ++生成器中的窗體內初始化一個openGL框架。 我嘗試復制此處提供的給定openGL啟動代碼的內容: http : //edn.embarcadero.com/article/10528
我嘗試用TFrame1替換TForm1,然后將其放入表單設計中,但是沒有用。 有任何經驗,如何正確地做到這一點?

簡單,只需將TForm::Handle用作窗口句柄即可...

這是BCB5中我的一些古老示例移植到BDS2006

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <gl/gl.h>
#include <gl/glu.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
int TForm1::ogl_init()
    {
    if (ogl_inicialized) return 1;
    hdc = GetDC(Form1->Handle);             // get device context
    PIXELFORMATDESCRIPTOR pfd;
    ZeroMemory( &pfd, sizeof( pfd ) );      // set the pixel format for the DC
    pfd.nSize = sizeof( pfd );
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 24;
    pfd.iLayerType = PFD_MAIN_PLANE;
    SetPixelFormat(hdc,ChoosePixelFormat(hdc, &pfd),&pfd);
    hrc = wglCreateContext(hdc);            // create current rendering context
    if(hrc == NULL)
            {
            ShowMessage("Could not initialize OpenGL Rendering context !!!");
            ogl_inicialized=0;
            return 0;
            }
    if(wglMakeCurrent(hdc, hrc) == false)
            {
            ShowMessage("Could not make current OpenGL Rendering context !!!");
            wglDeleteContext(hrc);          // destroy rendering context
            ogl_inicialized=0;
            return 0;
            }
    ogl_resize();
    glEnable(GL_DEPTH_TEST);                // Zbuf
    glDisable(GL_CULL_FACE);                // vynechavaj odvratene steny
    glDisable(GL_TEXTURE_2D);               // pouzivaj textury, farbu pouzivaj z textury
    glDisable(GL_BLEND);                    // priehladnost
    glShadeModel(GL_SMOOTH);                // gourard shading
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);   // background color
    ogl_inicialized=1;
    return 1;
    }
//---------------------------------------------------------------------------
void TForm1::ogl_exit()
    {
    if (!ogl_inicialized) return;
    wglMakeCurrent(NULL, NULL);     // release current rendering context
    wglDeleteContext(hrc);          // destroy rendering context
    ogl_inicialized=0;
    }
//---------------------------------------------------------------------------
void TForm1::ogl_draw()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    float x=0.5,y=0.5,z=20.0;
    glBegin(GL_QUADS);

    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(-x,-y,-z);
    glVertex3f(-x,+y,-z);
    glVertex3f(+x,+y,-z);
    glVertex3f(+x,-y,-z);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(-x,-y,+z);
    glVertex3f(-x,+y,+z);
    glVertex3f(+x,+y,+z);
    glVertex3f(+x,-y,+z);

    glEnd();



    glFlush();
    SwapBuffers(hdc);
    }
//---------------------------------------------------------------------------
void TForm1::ogl_resize()
    {
    xs=ClientWidth;
    ys=ClientHeight;
    if (xs<=0) xs = 1;                  // Prevent a divide by zero
    if (ys<=0) ys = 1;
    if (!ogl_inicialized) return;
    glViewport(0,0,xs,ys);              // Set Viewport to window dimensions
    glMatrixMode(GL_PROJECTION);        // operacie s projekcnou maticou
    glLoadIdentity();                   // jednotkova matica projekcie
    gluPerspective(30,float(xs)/float(ys),0.1,100.0); // matica=perspektiva,120 stupnov premieta z viewsize do 0.1
    glMatrixMode(GL_TEXTURE);           // operacie s texturovou maticou
    glLoadIdentity();                   // jednotkova matica textury
    glMatrixMode(GL_MODELVIEW);         // operacie s modelovou maticou
    glLoadIdentity();                   // jednotkova matica modelu (objektu)
    ogl_draw();
    }
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
    {
    ogl_inicialized=0;
    hdc=NULL;
    hrc=NULL;
    ogl_init();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
    {
    ogl_exit();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormResize(TObject *Sender)
    {
    ogl_resize();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
    {
    ogl_draw();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
    {
    ogl_draw();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseWheelDown(TObject *Sender, TShiftState Shift,
      TPoint &MousePos, bool &Handled)
    {
    glMatrixMode(GL_PROJECTION);
    glTranslatef(0,0,+2.0);
    ogl_draw();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseWheelUp(TObject *Sender, TShiftState Shift,
      TPoint &MousePos, bool &Handled)
    {
    glMatrixMode(GL_PROJECTION);
    glTranslatef(0,0,-2.0);
    ogl_draw();
    }
//---------------------------------------------------------------------------
  1. 創建一個空的1-Form項目

  2. 將此添加到表單類標題作為其用戶定義的成員

      int xs,ys; HDC hdc; // device context HGLRC hrc; // rendering context int ogl_inicialized; int ogl_init(); void ogl_exit(); void ogl_draw(); void ogl_resize(); 
  3. 添加計時器〜20-40毫秒

  4. 創建事件並復制正文以進行大小調整,重新繪制,ontimer等...以匹配上述源代碼
  5. 編譯並運行

筆記

  • 不需要所有的OpenGL東西都是form類的成員
  • 計時器可以有任何間隔
  • OpenGL不僅可以作為整個窗口,也可以只是窗口的一部分
  • 可以與VCL組件結合使用(將面板用於按鈕等,並將OpenGL的大小調整到外部區域)
  • 如果您無法使它發揮作用,請給我留言,但是我看不到有任何困難要做...
  • 不要忘記包含gl.h
  • 如果所有工作正常,那么您將在表單中心看到綠色四邊形
  • 鼠標滾輪向前/向后移動相機(“縮放”)

當您准備好使用OpenGL 1.0時,請看一下:

玩得開心 ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM