gpt4 book ai didi

c++ - 如何在 C++ 构建器中渲染 openGL 帧?

转载 作者:行者123 更新时间:2023-11-28 03:05:01 27 4
gpt4 key购买 nike

我想在 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. 为 resize、repaint、ontimer 等创建事件并复制主体,以匹配上面的源代码
  5. 编译运行

注意事项

  • 不需要所有的OpenGL 都是表单类的成员
  • 计时器可以有任何间隔
  • OpenGL 也可以只是窗口的一部分而不仅仅是整个窗口
  • 可以与 VCL 组件结合使用(使用按钮面板等并将 OpenGL 调整到外部区域)
  • 如果你不能让它工作,请评论我,但我看不出有什么难做的......
  • 不要忘记包含 gl.h !!!
  • 如果一切正常,那么您应该会在表单中心看到绿色四边形
  • 鼠标滚轮向前/向后移动相机(“缩放”)

当您准备好使用 OpenGL 1.0 时,请查看:

玩得开心...

关于c++ - 如何在 C++ 构建器中渲染 openGL 帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19941859/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com