[ Pobierz całość w formacie PDF ]
.You use the glViewport()command to choose a smaller drawing region; for example, you can subdividethe window to create a split-screen effect for multiple views in the samewindow.void glViewport(GLint x, GLint y, GLsizeiwidth,GLsizei height);Defines a pixel rectangle in the window into which the final image ismapped.The (x, y) parameter specifies the lower left corner ofthe viewport, and width and height are the size of the viewportrectangle.By default, the initial viewport values are (0, 0, winWidth,winHeight), where winWidth and winHeight are the sizeof the window.The aspect ratio of a viewport should generally equal the aspect ratioof the viewing volume.If the two ratios are different, the projected imagewill be distorted as it's mapped to the viewport, as shown in Figure 3-15.Note that subsequent changes to the size of the window don't explicitlyaffect the viewport.Your application should detect window resize eventsand modify the viewport appropriately.Figure 3-15 : Mapping the Viewing Volume to the Viewport For example, this sequence maps a square image onto a square viewport:gluPerspective(myFovy, 1.0, myNear, myFar); glViewport(0, 0, 400, 400);However, the following sequence projects a nonequilateral rectangular imageonto a square viewport.The image appears compressed along the x-axis,as shown in Figure 3-15.gluPerspective(myFovy, 2.0, myNear, myFar); glViewport (0, 0, 400, 400);To avoid the distortion, this line could be used:glViewport(0, 0, 400, 200);Try ThisTry ThisModify an existing program so that an object is drawn twice, in differentviewports.You might draw the object with different projection and/or viewingtransformations for each viewport.To create two side-by-side viewports,you might issue these commands, along with the appropriate modeling, viewing,and projection transformations:glViewport (0, 0, sizex/2, sizey); . . . glViewport (sizex/2, 0, sizex/2, sizey);The Transformed z CoordinateThe z or depth coordinate is encoded and then stored during theviewport transformation.You can scale z values to lie within adesired range with the glDepthRange() command.(Chapter 10 discussesthe depth buffer and the corresponding uses for the z coordinate.)Unlike x and y window coordinates, z window coordinatesare treated by OpenGL as though they always range from 0.0 to 1.voidglDepthRange(GLclampdnear,GLclampd far);Defines an encoding for z coordinates that's performed duringthe viewport transformation.The near and far values representadjustments to the minimum and maximum values that can be stored in thedepth buffer.By default, they're 0.0 and 1.0, respectively, which workfor most applications.These parameters are clamped to lie within [0,1].Troubleshooting TransformationsIt's pretty easy to get a camera pointed in the right direction, but incomputer graphics, you have to specify position and direction with coordinatesand angles.As we can attest, it's all too easy to achieve the well-knownblack-screen effect.Although any number of things can go wrong, oftenyou get this effect - which results in absolutely nothing being drawn inthe window you open on the screen - from incorrectly aiming the "camera"and taking a picture with the model behind you.A similar problem arisesif you don't choose a field of view that's wide enough to view your objectsbut narrow enough so they appear reasonably large.If you find yourself exerting great programming effort only to createa black window, try these diagnostic steps:Check the obvious possibilities.Make sure your system is plugged in.Make sure you're drawing your objects with a color that's different fromthe color with which you're clearing the screen.Make sure that whateverstates you're using (such as lighting, texturing, alpha blending, logicaloperations, or antialiasing) are correctly turned on or off, as desired. Remember that with the projection commands, the near and far coordinatesmeasure distance from the viewpoint and that (by default) you're lookingdown the negative z axis.Thus, if the near value is 1.0 and thefar 3.0, objects must have z coordinates between -1.0 and -3.0 inorder to be visible.To ensure that you haven't clipped everything outof your scene, temporarily set the near and far clipping planes to someabsurdly inclusive values, such as 0.001 and 1000000.This might negativelyaffect performance for such operations as depth-buffering and fog, butit might uncover inadvertently clipped objects. Determine where the viewpoint is, in which direction you're looking,and where your objects are
[ Pobierz całość w formacie PDF ]