Delphi OpenGL 绘图

发布于 2024-08-23 01:56:59 字数 770 浏览 13 评论 0原文

我像这样设置我的窗口:

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, form1.Width, form1.height, 0, 0, 1);
glMatrixMode (GL_MODELVIEW);
glDisable(GL_DEPTH_TEST);

我的绘图例程如下所示:

tempdist:=0.3 / distance(i,0,1,2);
xunit:=1 div 90;
zunit:=1 div 74;
glBegin(GL_LINE_LOOP);
case players[i].isteam of
2:glcolor3f(1.0,0.0,0.0); //Terrorist
3:glcolor3f(0.0,0.0,1.0); //Counter-Terrorist
end;
glvertex2f((thetax / 100)*xunit,(thetaz / 100)*zunit);
glvertex2f((thetax / 100)*xunit+tempdist,(thetaz / 100)*zunit);
glvertex2f((thetax / 100)*xunit+tempdist,(thetaz / 100)*zunit+tempdist);
glvertex2f((thetax / 100)*xunit,(thetaz / 100)*zunit+tempdist);
glEnd();
SwapBuffers(wglGetCurrentDC);

没有任何绘图迹象。有什么帮助吗?

I'm setting up my window like this:

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, form1.Width, form1.height, 0, 0, 1);
glMatrixMode (GL_MODELVIEW);
glDisable(GL_DEPTH_TEST);

And my drawing routine looks like this:

tempdist:=0.3 / distance(i,0,1,2);
xunit:=1 div 90;
zunit:=1 div 74;
glBegin(GL_LINE_LOOP);
case players[i].isteam of
2:glcolor3f(1.0,0.0,0.0); //Terrorist
3:glcolor3f(0.0,0.0,1.0); //Counter-Terrorist
end;
glvertex2f((thetax / 100)*xunit,(thetaz / 100)*zunit);
glvertex2f((thetax / 100)*xunit+tempdist,(thetaz / 100)*zunit);
glvertex2f((thetax / 100)*xunit+tempdist,(thetaz / 100)*zunit+tempdist);
glvertex2f((thetax / 100)*xunit,(thetaz / 100)*zunit+tempdist);
glEnd();
SwapBuffers(wglGetCurrentDC);

No sign of drawing whatsoever. Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆躺在深渊里 2024-08-30 01:56:59

由于我们不知道您的代码中的 thetax 值是多少,您是否有可能在剪切边界之外绘制?

使用 OpenGL 进行第一次绘图通常就成功了一半。我建议去 DelphiGL 并找到一个接近您想要的示例并从那里开始工作。该网站默认为德语,但有大量的英语翻译。特别是他们有一组有用的默认模板。

As we don't know what the value of thetax is from your code is it possible you're drawing outside the clipping boundary?

Getting your first drawing up with OpenGL is quite often half of the battle. I would suggest going to DelphiGL and finding an example that is close to what you want and working from there. The site is in German by default, but there's an extensive English translation. In particular they have a useful set of default templates.

青瓷清茶倾城歌 2024-08-30 01:56:59

我不知道,但是在与我很久以前编写的一些 opengl 代码进行比较时:

  • 我错过了用 glViewport(0, 0, Width, Height); 定义一个视口; // 设置OpenGL窗口的视口,
  • 你确定glortho采用像素坐标(而不是opengl相对坐标?)
  • thetax和thetaz的范围是多少?

我还使用了不同的方式来设置 dc:

 fdc:=getdc(<windowhandle of control we are displaying on  >);
 FRC := wglCreateContext(FDC);
 b:=wglMakeCurrent(FDC, FRC);

并完成

 wglMakeCurrent(0, 0); 
 wglDeleteContext(frc);  

I don't know, but while comparing with some opengl code I made long ago:

  • I miss defining a viewport with glViewport(0, 0, Width, Height); // Set the viewport for the OpenGL window
  • are you sure glortho takes pixel coordinates (instead of opengl relative coordinates?)
  • what range is thetax and thetaz?

I also used a different way of setting up dc:

 fdc:=getdc(<windowhandle of control we are displaying on  >);
 FRC := wglCreateContext(FDC);
 b:=wglMakeCurrent(FDC, FRC);

and to finish

 wglMakeCurrent(0, 0); 
 wglDeleteContext(frc);  
清引 2024-08-30 01:56:59

正确设置上下文非常困难。

对于具有良好性能的可重复使用的控件,您很可能需要一个具有自己的 DC 的控件,但如果您每次绘画时都可以获得一个,则可能没问题。

看一下 http://glscene。 cvs.sourceforge.net/viewvc/glscene/Source/Platform/GLWin32Viewer.pas?view=log 查看 GLScene 如何创建可与 OpenGL 一起使用的控件。

获取自己的 DC 的关键部分是重写 CreateParams 过程并添加:

   with Params do begin
      Style:=Style or WS_CLIPCHILDREN or WS_CLIPSIBLINGS;
      WindowClass.Style:=WindowClass.Style or CS_OWNDC;
   end;

然后您可以在 CreateWnd 中获取要使用的 DC,并在 DestroyWnd 中释放

一旦获得 DC,您需要确保 PixelFormat 支持OpenGL + 具有您想要的详细信息:

const pfd: PIXELFORMATDESCRIPTOR = (
    nSize: sizeof(PIXELFORMATDESCRIPTOR);
    nVersion: 1;                       // version
    dwFlags: PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
    iPixelType: PFD_TYPE_RGBA;
    cColorBits: 24;                    // 24-bit color depth
    cRedBits: 0;
    cRedShift: 0;
    cGreenBits: 0;
    cGreenShift: 0;
    cBlueBits: 0;
    cBlueShift: 0;
    cAlphaBits: 8;                     // alpha bits
    cAlphaShift: 0;
    cAccumBits: 0;                     // accumulation buffer
    cAccumRedBits: 0;
    cAccumGreenBits: 0;
    cAccumBlueBits: 0;
    cAccumAlphaBits: 0;
    cDepthBits: 32;                    // z-buffer
    cStencilBits: 16;                  // stencil buffer
    cAuxBuffers: 0;                    // auxiliary buffer
    iLayerType: PFD_MAIN_PLANE;        // main layer
    bReserved: 0;
    dwLayerMask: 0;
    dwVisibleMask: 0;
    dwDamageMask: 0
);
var
  pf: Integer;
begin
  pf := ChoosePixelFormat(dc, @pfd);
  if not SetPixelFormat(dc, pf, @pfd) then
    Assert(false);//failed, could retry with other settings 
  rc := wglCreateContext(dc);
  if not wglMakeCurrent(dc, rc) then
    Assert(false);// failed
  // we should now have a rc so to test, we'll just clear
  glClearColor(1, 0.5, 0, 1);
  glClear(GL_COLOR_BUFFER_BIT);
  // If we're double-buffered, then swap
  SwapBuffers(dc);
  wglMakeCurrent(0, 0);

使用完 RC 后,您还应该通过调用 wglDeleteContext 进行清理。

Setting up the context correctly is quite hard.

For a re-usable control with decent performance, you'll most likely want a control with it's own DC, but if you can acquire one each time you paint, you might be okay.

Take a look at http://glscene.cvs.sourceforge.net/viewvc/glscene/Source/Platform/GLWin32Viewer.pas?view=log to see how GLScene creates a control that's usable with OpenGL.

The key part in acquiring it's own DC, is overriding the CreateParams procedure + adding:

   with Params do begin
      Style:=Style or WS_CLIPCHILDREN or WS_CLIPSIBLINGS;
      WindowClass.Style:=WindowClass.Style or CS_OWNDC;
   end;

You can then get the DC to use in CreateWnd, and release in DestroyWnd

Once you've got the DC, you'll need to make sure the PixelFormat supports OpenGL + has the details you want:

const pfd: PIXELFORMATDESCRIPTOR = (
    nSize: sizeof(PIXELFORMATDESCRIPTOR);
    nVersion: 1;                       // version
    dwFlags: PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
    iPixelType: PFD_TYPE_RGBA;
    cColorBits: 24;                    // 24-bit color depth
    cRedBits: 0;
    cRedShift: 0;
    cGreenBits: 0;
    cGreenShift: 0;
    cBlueBits: 0;
    cBlueShift: 0;
    cAlphaBits: 8;                     // alpha bits
    cAlphaShift: 0;
    cAccumBits: 0;                     // accumulation buffer
    cAccumRedBits: 0;
    cAccumGreenBits: 0;
    cAccumBlueBits: 0;
    cAccumAlphaBits: 0;
    cDepthBits: 32;                    // z-buffer
    cStencilBits: 16;                  // stencil buffer
    cAuxBuffers: 0;                    // auxiliary buffer
    iLayerType: PFD_MAIN_PLANE;        // main layer
    bReserved: 0;
    dwLayerMask: 0;
    dwVisibleMask: 0;
    dwDamageMask: 0
);
var
  pf: Integer;
begin
  pf := ChoosePixelFormat(dc, @pfd);
  if not SetPixelFormat(dc, pf, @pfd) then
    Assert(false);//failed, could retry with other settings 
  rc := wglCreateContext(dc);
  if not wglMakeCurrent(dc, rc) then
    Assert(false);// failed
  // we should now have a rc so to test, we'll just clear
  glClearColor(1, 0.5, 0, 1);
  glClear(GL_COLOR_BUFFER_BIT);
  // If we're double-buffered, then swap
  SwapBuffers(dc);
  wglMakeCurrent(0, 0);

After you've finished using the RC, you should also clean up by calling wglDeleteContext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文