View Setting in OpenGL

1.0 Introduction

Building the solar system using OpenGL, I goto the NASA webpage to load some pretty texture about the sun, moon, and earth. I want to look at the scene from different views: the universe, the earth, and the moon. From the earth, one can see the sun and the moon. From the moon, one can observe the sun and the earth. To set the right views for a better effect, I feel the need to study back some important knowledge of view transformation, model transformation, and the gluLookAt(). Sharing my experience with those problems, I hope that it could be beneficial for graphic beginners to understand and set the view for their own program. 

2.0 Global coordinate system vs. local coordinate system

Figure1: global coordinate system vs. local coordinate system. Before setting the view, it is important to understand the difference between the grand, fixed coordinate system and the local coordinate system. As shown in figure 1, the global coordinate system relates to the whole scene while the local coordinate system relates to specific objects in the whole scene.

Figure1: Global coordinate system vs. local coordinate system.
Source: https://www.sciencedirect.com/science/article/pii/B9780124158429000022[1]

For the global coordinate system, order of transformation is in reverse relationship with the order of coding. As a result, viewing commands appear before modeling commands [2].  Figure 2 shows final results depends on the order of transformation. Also, from figure 2 one can see that if the object is rotated before being translated, the object can be viewed from the side. 

Figure 2: Rotating then transferring is different from transferring than rotating. 
Source: http://what-when-how.com/opengl-programming-guide/viewing-and-modeling-transformations-opengl-programming-part-1/ [2]

2.1 Model transformation

glMatrixMode(GL_MODELVIEW) must be called before model transformation. Also, one should keep in mind that certain operations such as positioning of a light source in world space must be done when the camera is on the GL_MODELVIEW [2]. Understanding the process of model transformation in figure 3 helps learners have a clear insight of what happens behind the coding. For example, glRotatef(45.0, 0.,0.,1.) will rotate the object 45 degrees about the z-axis. For scaling, if a value is set to zero, the objects that along that axit will collapse to zero. As a result, the matrix cannot be inverted. Inverse matrices are required for somelighting operations [2]. Collapsing coordinates might be utilized when shadowing. When the coordinate is collapsed, the projection matrix should be used instead of the modelview matrix[2]. 

glTranslatef(1., 0.,4.)glRotatef(45.,0.0.,1.)glScalef(2.,-.5, 1.)
Figure 3: Model transformation: translation, rotation, and scaling
Source: http://what-when-how.com/opengl-programming-guide/viewing-and-modeling-transformations-opengl-programming-part-1/[2]

2.2 Viewing transformation

glMatrixMode(GL_MODELVIEW) must be called before viewing transformation. By default, the camera is at the origin and looks towards the negative z-axis. Viewing transformation involves translation and rotation; viewing transformation changes the position and orientation of the viewpoint. Viewing transformation needs to be done before modeling transformation so that modeling transformation takes effects first. When changing camera position, one needs to call glLoadIdentity() to erase the effects of any current viewing transformation[3].

Figure 4 shows the effect caused by glTranslatef(0.,0.,-5.). Moving the object -5 units along the z-axis is equivalent to moving the camera +5 units along the z axis. Similarly, rotating the object counterclockwise is equivalent to rotating the camera clockwise. 

Figure 4  shows the effect caused by glTranslatef(0.,0.,-5.).
Source: http://what-when-how.com/opengl-programming-guide/viewing-and-modeling-transformations-opengl-programming-part-2/ [3]

2.3 The function glLookAt()

The glLookAt() function encapsulates a series of rotation and translation commands[3]. There are 9 arguments passed in the glLookAt() function. By understanding what those arguments mean, users can apply those rules when designing views. The first three arguments specify the location of the viewpoint. This viewpoint is always in the center of the image on the screen [3]. The fourth, fifth, and sixth arguments specify the points that the camera is aimed at. Typically, the reference point is somewhere in the middle of the scene. The seventh, eighth, and ninth arguments indicate the up direction. When the scene is built at origin, reference points are probably at the origin and up direction is the positive y axis. Up direction should be perpendicular to the plane’s wings if one is designing a flight [3]. 

By default, the camera is at origin and views towards the negative z axis. This is equivalent with the setting of glLookAt(0.,0.,0. ,0.,0.,-100.,0.,1.,0.).  Figure 5 shows the effect of setting glLookAt() function. Reference point is at (2.,4.,-3.) and the up vector is (2.,2.,-1.). Thus, the camera is looking at the model, the viewpoint is rotated 45 degrees. 

Default lookat positionsetup
Figure 5: Default glLookAt() vs. glLookAt(4.,2.,1., 2., 4.,-3.,2.,2.,-1.)
Source: http://what-when-how.com/opengl-programming-guide/viewing-and-modeling-transformations-opengl-programming-part-2/.[3]

3.0 Applying the concept in my project of designing the solar system: 

After understanding the basic concept, I feel more confident to implement the view from the earth and the view from the moon. Figure 6 shows the result of my design. 

View from earthView from moonView from sun
Figure 6: setting different views for sun-earth-moon system.

4.0 Conclusion

In order to set the proper view for the graphic scenes, visualizing the transformation step by step and writing the codes in reverse order takes some time and effort. Also, understanding the concept of viewing transformation, model transformation, and glLookAt() function are essential for learners to successfully design their own graphic programs. As learners succeed in their graphic design, it is surely a great reward. 

Reference

[1] Rick Parent. (2012). Local coordinate system. Science Direct. [Online]. Available: https://www.sciencedirect.com/science/article/pii/B9780124158429000022. Accessed Nov 6th, 2021. 

[2] what-when-how In Depth Tutorials and information. Viewing and Modeling Transformations (OpenGL Programming) Part 1.  [Online]. Available: http://what-when-how.com/opengl-programming-guide/viewing-and-modeling-transformations-opengl-programming-part-1/. Accessed Nov 6th, 2021. 

[3] what-when-how In Depth Tutorials and information. Viewing and Modeling Transformations (OpenGL Programming) Part 2.  [Online]. Available: http://what-when-how.com/opengl-programming-guide/viewing-and-modeling-transformations-opengl-programming-part-2/. Accessed Nov 6th, 2021. 

Print Friendly, PDF & Email

Leave a comment

Your email address will not be published.