1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113:
| unit OpenGLEngine;
interface
uses dglOpenGL, Windows;
type TOpenGlEngine = class protected FRC: Cardinal; FDC: Cardinal; FWindowHandle: Hwnd; FWidth,FHeight: Cardinal; FCanRender: boolean; protected procedure SetupOpenGL; public constructor Create(AWindowHandle:Hwnd); destructor Destroy; override; public procedure Resize(AWidth,AHeight: Cardinal); procedure Render; public property CanRender: boolean read FCanRender; property Width: Cardinal read FWidth; property Height: Cardinal read FHeight; end;
implementation
const NearClipping = 1; FarClipping = 1000;
constructor TOpenGlEngine.Create(AWindowHandle:Hwnd); begin inherited Create; FDC := 0; FWidth := 0; FHeight := 0; FCanRender := false; FWindowHandle := AWindowHandle; if not InitOpenGL then halt;
FDC := GetDC(AWindowHandle); FRC := CreateRenderingContext(FDC,[opDoubleBuffered],32,24,0,0,0,0); ActivateRenderingContext(FDC, FRC);
SetupOpenGL;
FCanRender := true;
end;
destructor TOpenGlEngine.Destroy; begin DeactivateRenderingContext; DestroyRenderingContext(FRC); ReleaseDC(FWindowHandle,FDC); inherited; end;
procedure TOpenGlEngine.SetupOpenGL; begin glClearColor(0.3, 0.4, 0.7, 0.0); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); end;
procedure TOpenGlEngine.Resize(AWidth,AHeight: Cardinal); begin FWidth := AWidth; FHeight := AHheight;
glViewport(0, 0, Width, Height); glMatrixMode(GL_PROJECTION); glLoadIdentity; gluPerspective(45.0, Width/Height, NearClipping, FarClipping);
glMatrixMode(GL_MODELVIEW); glLoadIdentity;
Render; end;
procedure TOpenGlEngine.Render; begin glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION); glLoadIdentity; gluPerspective(45.0, Width/Height, NearClipping, FarClipping);
glMatrixMode(GL_MODELVIEW); glLoadIdentity;
glTranslatef( -1.5, 0, -6);
glBegin(GL_TRIANGLES); glColor3f(1,0,0); glVertex3f(-1.0,-1.0, 0.0); glColor3f(0,1,0); glVertex3f( 0.0, 1.0, 0.0); glColor3f(0,0,1); glVertex3f( 1.0,-1.0, 0.0); glEnd();
SwapBuffers(FDC); end;
end. |