Preliminary Code

OpenGL is intended to be fairly platform-independent, rather than just for Windows. Therefore OpenGL needs a link to Windows, using some special structures and API extensions in Windows to provide this link. In Delphi a good place for the connection is within a form's OnCreate event handler. Create a new project and save it in a new directory under the OpenGL directory created earlier. Name the new directory Chapter.1, name the project First.Dpr, and name the main unit Firstl.Pas. Double-click...

Naming Convention

You may have noticed by now that all the OpenGL command names begin with gl. The graphics library has a well-defined naming convention, which this book mentions from time to time. So far the gl prefix has dominated, which does apply to all OpenGL commands. A glu prefix has also made a cameo appearance. It is the standard for all commands of the utility library. Chapter 1 presented the wgl prefix for the extensions to Microsoft Windows that support OpenGL rendering. Similarly the X Window System...

The Windows Implementation

In Windows, OpenGL is implemented in opengl32.dll for the main graphics commands, and glu32.dll for the utility library. As those file names imply, OpenGL is only available for 32-bit versions of Windows, such as NT and 95 or later. Windows NT, Windows 98, and OEM versions of Windows 95 (pre-installed by the computer manufacturer) include the OpenGL libraries, but a separately purchased copy of Windows 95 may require downloading the two DLL's from the Internet. Alternatively, you may obtain...

Delphi Developers Guide to OpenGL

(Publisher Wordware Publishing, Inc.) Author(s) Jon Jacobs ISBN 1556226578 Publication Date 08 01 99 object FormTransform TFormTransform Left 200 Top 10 0 Width 360 Height 185 Caption 'Transformation Parameters' Font.Charset DEFAULT_CHARSET Font.Color clWindowText Font.Height -11 Font.Name 'MS Sans Serif' Font.Style PixelsPerInch 96 TextHeight 13 object Label1 TLabel Left 76 Top 4 Width 7 Height 13 Caption 'X' object Label2 TLabel Left 120 Top 4 Width 7 Height 13 Caption 'Y' Top 4 Width 7...

Commands and Constants

The command for defining a light is a variation of glLight. Use the f suffix to use glFloat parameters. Many commands, including ones already discussed, have a vector version, indicated by a v suffix. The vector version substitutes a pointer to an array in place of several scalar parameters. The glLight command only takes three parameters in any version, so only the vector version meets the current need. The glLightModel command takes three parameters and also has a vector version suitable for...

The Hard

A more flexible approach uses the glLight command. Make a new directory named OpenGL Chapter.5 Ambient0. In that directory save the program as AmbO.Dpr and the main unit as AmbientO.Pas. Change the bottom of the FormCreate method to this glEnable(GL_LIGHT0) This approach defines a particular light (LIGHT0) instead of just setting up a light model. At least eight lights are available some implementations of OpenGL may allow more. Notice that you must turn on any light you want to use. This...

Order of Events

Now, compile the program and launch it with the Windows Run command. Resize it several ways. Watch the shape of the rectangle as you do the resizing. Careful observation reveals that if the form only shrinks, the shape of the rectangle does not change. If the form enlarges either horizontally or vertically or both, the shape of the rectangle changes appropriately. An expected event is not firing. This calls for further investigation. In the variable declaration section add So that an Event.Log...

Content

This is a hands-on, learn-by-doing book. For greatest benefit from the book place it near the computer, turn on the computer, and launch Delphi. Create a separate directory or folder in a suitable location for placing various OpenGL projects. Naming the directory OPENGL is a good choice. Some people may prefer to develop a thorough background before they write their first line of code. I am not one of those people. While I do like to have a good understanding of the tools I am using and the...

Orthographic Projection

The call to glOrtho defines a clipping volume, which is a region in space in which objects can be seen. Any part of a scene that lies outside of the region is not visible clipped . Of course, though the rendered scenes may be conceptually three-dimensional, they must be projected onto a two-dimensional screen. The available projections are orthographic projection and perspective projection. You get one guess as to which kind glOrtho uses. In orthographic projection, also known as parallel...

Vertex Direction

How does OpenGL know which face of a polygon the programmer intended to be front and which to be back The answer lies in the order of the vertices. Consider the front square of this chapter's cube. glBegin GL_POLYGON near face glColor3f 0.5,0.0,0.0 glVertex3f -0.4,-0.4,-0.4 glVertex3f 0.4,-0.4,-0.4 glVertex3f 0.4, 0.4,-0.4 glVertex3f -0.4, 0.4,-0.4 glEnd Mentally number the vertices from one to four. Vertex one is the lower left corner. Vertex two is the lower right corner. Vertex three is the...

Directional Spotlights

A feature still missing from the spotlight illustrated thus far in the chapter is the ability to aim it. What kind of spotlight is it that you can't spot One thing needed for that purpose is to add some more ell fields in the form class declaration. xl,ylizlixrliyrlizrlianglelicutoff, x1iy1iz1ix2iy2iz2ixr1iyr1izr1, xr2,yr2,zr2iangle1iangle2isign glFloat The xrl, yrl, and zrl fields set up the axis of rotation for the cyan cube and the aiming direction of the spotlight. Similarly the anglel...

State Variables

In the FormPaint method Listing 2-2 , not only does glClearColor set the clear color background , but glColor sets the foreground color for drawing. These calls set state variables which remain the same until similar calls explicitly change them. If the command says to draw in blue, all drawing is done in blue until another call changes the state. OpenGL's state variables are in DLLs, not directly accessible to the programmer. You read and write OpenGL's state variables via function and...

O

Search riy itkmowled e fa j sitemap comtact lis To access the contents, click the chapter and section titles. Publisher Wordware Publishing, Inc. Author s Jon Jacobs ISBN 1556226578 Publication Date 08 01 99 Another new command introduced here is glFlush. For the sake of efficiency, the graphics engine does not necessarily start drawing as soon as it receives its first command. It may continue to accumulate information in a buffer until given a little kick. The call to glFlush forces it to...

Open the Cube

Polygons are two-faced. That is not a character assassination but a geometric statement. When a polygon facing the viewer rotates 180 degrees, it does not disappear it has a back face that the viewer can see. To illustrate, expose the inside of the cube by commenting out the right side as in Listing 3-3. procedure TForm1.FormPaint Sender TObject begin glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT glBegin GL_POLYGON near face glColor3f 0.5,0.0,0.0 glVertex3f...

The Interface Unit

If your version of Delphi does not have an OpenGL interface unit, you can download one from the Internet. Wherever you obtain the interface unit, simply reference it in the uses clause of any unit that invokes OpenGL commands. Be sure to place the .DCU Delphi Compiled Unit file in Delphi's library path, otherwise give a full path to it in the .DPR Delphi PRoject file. For example, in Delphi 3, the interface file is in the LIB directory under the DELPHI 3 directory. To see or modify the library...

Make a Scene

This is enough information to build a scene with multiple copies of the cube. Make the procedure TForm1.FormPaint Sender TObject glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT errorCode glGetError if errorCode lt gt GL_NO_ERROR then raise Exception.Create 'Error in Paint' 13 gluErrorString errorCode end FormPaint Notice the sequence push the ModelView matrix, translate to the desired location, call the routine that builds the object about the origin, and pop the matrix. Do this for each...

Transformation Order Revisited

About the only thing left for this structure is to add scaling as well, but that would not add much to the OpenGL knowledge because the results are easy to predict. There is something else that can contribute significantly to the level of OpenGL understanding. The ability to switch back and forth between rotating before translation and rotating after translation reveals a dramatic difference between the two. Add the following to the case statement The form declaration needs to include the...

Culling

There it is Those polygons have a back side What if the cube was not open The shocking truth is that even if there is no possible way to see the back side of the polygons, OpenGL draws them Fortunately depth testing hidden surface removal keeps them from bleeding though. Better yet, an OpenGL command can tell it not even to draw them. procedure glCullFace mode GLenum stdcall The possible values for mode with obvious meanings are To cull faces means to remove them from the list of things to...