Delphi OpenGL 绘图

3

我正在按照以下方式设置我的窗口:

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

完全没有任何绘图的迹象。需要帮助吗?

3个回答

0

我不知道,但是与我很久以前编写的一些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);  

0

正确设置上下文是相当困难的。

对于具有良好性能的可重用控件,您很可能需要一个具有自己DC的控件,但如果您每次绘制时都可以获取一个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;

然后您可以获取DC以在CreateWnd中使用,并在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进行清理。


0

由于从您的代码中我们不知道thetax的值,所以您是否可能在剪辑边界外绘制?

用OpenGL完成第一次绘图通常是解决问题的一半。我建议您前往DelphiGL,找到与您想要的相似的示例,然后从那里开始工作。该网站默认为德语,但有广泛的英语翻译。特别是他们有非常有用的默认模板。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接