delphiopengl绘图

delphiopengl绘图,delphi,opengl,drawing,primitive,Delphi,Opengl,Drawing,Primitive,我正在这样设置我的窗口: 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_LIN

我正在这样设置我的窗口:

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);
我的绘图例程如下所示:

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

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

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


使用OpenGL绘制您的第一个图形通常是成功的一半。我建议你去寻找一个与你想要的接近的例子,然后从那里开始工作。该网站默认为德语,但有大量的英语翻译。特别是它们有一组有用的默认模板。

我不知道,但与我很久以前编写的一些opengl代码相比:

  • 我错过了使用glViewport(0,0,宽度,高度)定义视口;//设置OpenGL窗口的视口
  • 您确定glortho采用像素坐标(而不是opengl相对坐标)吗
  • θ和θ的范围是多少
我还使用了另一种设置dc的方法:

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

正确设置上下文相当困难

对于一个具有良好性能的可重用控件,您很可能想要一个具有自己DC的控件,但是如果您每次绘制时都可以获得一个,那么您可能就可以了

看看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

获得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进行清理