2010-02-20 71 views
3

我我的窗口設置是這樣的:德爾福OpenGL繪圖

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

任何繪畫的跡象。任何幫助?

回答

0

由於我們不知道thetax的值是否來自您的代碼,因此您可能在裁剪邊界之外繪圖?

通過OpenGL進行第一次繪製往往是戰鬥的一半。我建議去DelphiGL並找到一個接近你想要的並且從那裏工作的例子。該網站默認爲德語,但有大量的英文翻譯。特別是他們有一套有用​​的默認模板。

0

我不知道,但在一些OpenGL的代碼比較我在很久以前:

  • 我想念定義與glViewport(0,0,寬度,高度)視口; //設置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進行控制,但是如果每次繪製時都可以獲取控制權,那麼您可能沒有問題。

看看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完成後,你也應該CL通過調用wglDeleteContext調用。