It is also possible to layer translucent skyboxes and animate one or more of them. For example, making the sun shimmer a little or rendering lightning in the distance can enhance a skybox. Animating parts of the skybox, or adding atmospheric effects on top of it, can add a little extra realism. One of the downsides of the skybox technique is that it doesn't change over time because the image has been prerendered. # Here is where we would draw the rest of the world in a game ()Īlthough a skybox creates a convincing illusion of distance scenery, there are enhancements that can be used to add some more visual flare to the backdrop. # Reenable lighting and depth test before we redraw the world glEnable(GL_LIGHTING) # Disable lighting and depth test glDisable(GL_LIGHTING) glDepthMask(False) # Rotate around the x and y axes to create a mouse-look camera glRotatef(mouse_y, 1, 0, 0)
Mouse_rel_x, mouse_rel_y = _rel() mouse_x += float(mouse_rel_x) / 5.0 mouse_y += float(mouse_rel_y) / 5.0 # because the skybox covers the entire screen glClear(GL_DEPTH_BUFFER_BIT) # We don't need to clear the color buffer (GL_COLOR_BUFFER_BIT) #Don't display the mouse cursor _visible(False)įor event in (): if event.type = QUIT: return if event.type = KEYDOWN: return # Used to rotate the world mouse_x = 0.0 mouse_y = 0.0 GlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) GlBindTexture(GL_TEXTURE_2D, material.texture)
# Set the wraping mode of all textures in the skybox to GL_CLAMP_TO_EDGE for material in sky_(): # Read the skybox model sky_box = model3d.Model3D() Screen = _mode(SCREEN_SIZE, FULLSCREEN | HWSURFACE | ^ OPENGL | DOUBLEBUF) # Enable light 1 and set position glEnable(GL_LIGHT0) # Enable the GL features we will be using glEnable(GL_LIGHTING) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glEnable(GL_TEXTURE_2D) glShadeModel(GL_SMOOTH) GluPerspective(60.0, float(width)/height. # Import the Model3D class import model3d def resize(width, height): Import pygame from pygame.locals import * Rendering a Skybox (skybox.py) SCREEN_SIZE = (800, 600)įrom OpenGL.GL import * from OpenGL.GLU import * When you run it, you will see a scenic view of mountains, and if you adjust the viewpoint with the mouse you will be able to see the landscape from any direction. Listing 12-6 uses the Model3D class from the previous chapter to load a skybox model and its associated textures. GlEnable(GL_LIGHTING) glDepthMask(True) Seeing Skyboxes in Action The following two lines should follow the call to render the skybox: Once the skybox has been rendered, be sure to reenable lighting and the depth mask, or the other models in the scene may not render correctly. Setting the depth mask to False with glDepthMask(False) tells OpenGL to ignore the depth information in the skybox, so that other models will be rendered on top of it. Normally if the player was inside a cube he would not be able to see anything outside of the cube, which is obviously not want we want. Disable the depth buffer with glDepthMask(False).
With lighting disabled, OpenGL will render the textures with the original brightness levels.Ĥ. We don't need to use OpenGL's lighting features because the textures of the skybox have effectively been prelit. Disable lighting with glDisable(GL_LIGHTING). This is because the skybox represents very distant scenery that can never be reached by the player.ģ. Set the position of the skybox to be the same as the camera (i.e., the player). See the previous chapter for more information on wrapping modes. This is necessary to avoid seams in the skybox, where the cube faces meet. Set the wrap mode of all the textures in the skybox to GL_CLAMP_TO_EDGE. Since a skybox is just a model of a cube, it can be stored as any other model, but there are a few additional steps required prior to rendering:ġ. Rendering the skybox should be the first thing done in a new frame, and negates the need to clear the color buffer (although you will still need to clear the depth buffer). I used Terragen to create the skycube textures in Figure 12-3 that we will be using in the skybox sample code. An excellent choice for rendering skyboxes is Terragen ( which creates remarkably realistic-looking images of virtual landscapes.
If you are artistically inclined, you may be able to draw or paint these textures, but it is probably easier to use 3D modeling software to render six views of a scene for each face of the skybox. Generating the skybox textures may require a little more effort because each texture must seamlessly align with the four other textures it shares an edge with.