Mega Code Archive

 
Categories / Delphi / Graphic
 

OpenGL Intro

Title: OpenGL Intro Question: OpenGL is low level but the fascination is much higher. I will give you a short introduction of this astonishing graphical world. Answer: OpenGL with Delphi #1 ---------------------------------- OpenGL is a low-level graphics library specification originally developed by Silicon Graphics and by low-level we mean near system-level. The system's particular implementation of this specification, often called OpenGL driver, allows you to use a set of geometric primitives (points, lines, polygons, images, and so on) to describe a scene we wish to paint (rendering). The interface consists of about 150 distinct commands with a lot of parameters so you must build up your desired model from a small set of geometric primitives. There are many components or units for Delphi or Lazarus which encapsulates all the complexity that can result in using OpenGL. In other words, all the hassle of dealing with pixel format, matrixmode, viewports, the GL rendering context, etc. is hidden by many free components on the market. The only thing that needs to be done is to handle a few events: OnGLInit(), OnGLPaint() and the classical OnResize(): procedure TGLform.OpenGLControl1Resize(Sender: TObject); begin if (AreaInitialized) and OpenGLControl1.MakeCurrent then glViewport (0, 0, OpenGLControl1.Width, OpenGLControl1.Height); end; Of course this is the simplest one. On top of that, many components are missing two facilities. The first one is to print out the rendered scene, while the second should save it into a bitmap. Visualizing a scene of moderate complexity takes mere milliseconds, which means the library has sufficient performance to support you in the creation of animations, simulations and virtual worlds. The OpenGL driver is generally provided as a library in binary format. It can be linked to your program dynamically. On the Win platform, it will be a DLL (check for opengl32.dll or / and GLU32.dll). const opengl32 = 'OpenGL32.dll'; glu32 = 'GLU32.dll'; initialization Set8087CW($133F); try LoadOpenGL('opengl32.dll'); except end; finalization FreeOpenGL; end. from the GL unit we get: function wglGetProcAddress(ProcName: PChar): Pointer; stdcall; external opengl32; function wglCopyContext(p1: HGLRC; p2: HGLRC; p3: Cardinal): BOOL; stdcall; external opengl32; function wglCreateContext(DC: HDC): HGLRC; stdcall; external opengl32; ........................ Since Delphi is able to use almost any DLL, it is as simple as with any other language to program OpenGL 3D graphics, animations or games. Obviously, by the matrix nature of OpenGL, a good understanding of matrix manipulation and uses of matrices in 3D graphics will help. Also a really solid grasp of algebra, trigonometry, and geometry will certainly be a plus. The more you know, the easier you can produce algorithms to reach your goals. You may be able to solve many problems without much knowledge in these areas, but you will have to work harder (think softer) to get there. This article will help you to get first steps with OpenGL techniques in Delphi. There are two kinds of data that enter OpenGL processing: pixel data and geometry data. These two kinds of data go through two different pipelines like a big state machine until they merge near the end to produce the final step of pixels in the frame buffer. SwapBuffers(Info^.DC); You put it into various states (or modes) that then remain in effect until you change them. The bitplanes are themselves organized into a framebuffer, which holds all the information that the graphics display needs to control the color and intensity of all the pixels on the screen. By the way doubleBuffering means one picture is displayed while the other is being drawn at the background then swapped! Let's illustrate a typical use of OpenGL. First we need a device context (you remember those old days) of the window: Info^.DC:= GetDC(Result); We set a pixel format: Info^.PixelFormat:=ChoosePixelFormat(Info^.DC,@pfd); Then we pass it to a render context: Info^.WGLContext:= wglCreateContext(Info^.DC); TWGLControlInfo = record Window: HWND; DC: HDC; PixelFormat: GLUInt; WGLContext: HGLRC; end; The following describes the major graphics operations which OpenGL performs to render an image on the screen: - Construct shapes from geometric primitives (OpenGL considers points, lines, polygons, images, and bitmaps to be primitives) - Arrange the objects (or models) in three-dimensional space. - Calculate the color of all the objects which must explicitly assigned by the application, determined from specified lighting conditions and textures. - Convert the math. description of objects and their associated color information to pixels on the screen (called rasterization). In addition, after the scene is rasterized but before it's drawn on the screen, you can perform some operations on the pixel data if you want. procedure TExampleForm.IdleFunc(Sender: TObject; var Done: Boolean); begin OpenGLControl1Paint(Self); Done:= false; end; Here's how we draw a white rectangle on a black background: InitializeAWindowPlease(device context and so on); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON); glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0); glEnd(); glFlush(); UpdateTheWindowAndCheckForEvents(timer or onidle()); Some notes about the timing and events. You can choose between onIdle() or a Timer. You can specify a function that's to be executed if no other events are pending or add the time from a timer it takes for your system to clear the screen and to draw a typical frame. Sources: ************************************ http://www.opengl.org/code/ http://opengl.org/resources/faq/technical/ http://www.delphigl.com/ (also german) http://www.delphi3d.net http://wiki.delphigl.com/index.php/Tutorial http://www.glprogramming.com/red/chapter01.html Glut is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL programs. GLUT makes it considerably easier to learn about and explore OpenGL programming. and provides a portable API so you can write a single OpenGL program that works on across all machines or platforms. GLUT provides many of the modeling features, such as quadric surfaces and NURBS curves (Non-uniform rational B-splines) and surfaces. Dot, short for "Delphi OpenGL Toolkit", is a utility library designed to speed up OpenGL development. The Dot can help you manage rendering contexts for your application windows and for offscreen rendering buffers. It also contains a large library of vector and matrix math functions, an assortment of image and 3D model loaders. (((((((((((((((((((((((((((((((((() the schema of each openGL app: use unit opengl32 = 'OpenGL32.dll'; glu32 = 'GLU32.dll'; drawScene() begin //set background color glClearColor() glClear(GL_COLOR_BUFFER_BIT) //color of a polygon primitive glColor3f() //set the polygon glBegin(GL_POLYGON) glVertex2f() glVertex2f() ..... glEnd() //draw the GL commands glFlush() end //main //initialise GLUT begin glutInit() //init window (buffer, RGB color model glutInitDisplayMode() glutInitWindowPosition() glutInitWindowSize() glutCreateWindow() //set callback function to draw scene glutDisplayFunc(drawScene) glutMainLoop() end. one last compile to draw a biorhythmus ---------------------------------------- dayInterval:= 0.1; radian:= ageInDays - periodRange; for i:= 0 to DIM do begin bPts[i].Y:= origy - Round(sin(PI2*(round(radian) mod BODY)/BODY)* yRgePixs); radian:= radian + dayinterval; end; radian:= ageInDays - periodRange; for i:= 0 to DIM do begin ePts[i].Y:= origy - Round(sin(PI2*(round(radian) mod EMOTION)/EMOTION)* yRgePixs); radian:= radian + dayinterval; end; radian:= ageInDays - periodRange; for i:= 0 to DIM do begin sPts[i].Y:= origy - Round(sin(PI2*(round(radian) mod SPIRIT)/SPIRIT)* yRgePixs); radian:= radian + dayinterval; end; end;