Professional Documents
Culture Documents
QR 1
QR 1
1) Point P=(x,y) can be expressed as P=xu+yv + O, where u and v are unit vectors in the x and y directions
respectively. Write P in matrix form.
Transformations:
T 4,4
R Z,-45 o
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <iostream>
0.0);
int n = 1;
void display()
glClearColor(0.0,0.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glRotatef(counter/150,0.0,0.0,1.0);
//counter +=(-n*1.0);
n = -n;
counter = (n*100.0);
}*/
glBegin(GL_TRIANGLES);
myFirstTriangle;
glEnd();
/*Second triangle*/
//glLoadIdentity();
//glRotatef(counter/10,0.0,1.0,0.0);
/*glBegin(GL_TRIANGLES);
mySecondTriangle;
glEnd();*/
one*/
glViewport(0,0,w,h);
//glLoadIdentity();
void initOpenGL()
glutInit(&argc, argv);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100,100);
//****glutMainLoop();
initOpenGL();
glutIdleFunc(display);
/* whenever a window is created or changes made to its size, then glut should
know that*/
glutReshapeFunc(reshape);
return 0; }
3) Points P=(x,y,z,w) was transferred (via some transformation matrices) to point P'=(x',y',z',1). Explain
the situation. And derive a formula for x',y' and z'
3) Up direction: this is the direction the camera is rotated at deviated from the up
translates, changes viewing direction,.. etc)? Run an opengl routine that proves concept your
answer
represents the set of stages of the OpenGL rendering pipeline where a sequence of vertices are
processed via a series of Shaders. The subsequent shader stages takes its data from the previous one.
Many of these shader stages are optional, and the last active stage in any rendering operation provides
vertex data to Vertex Post-Processing and beyond.
Translate
Multiplies the current matrix by a matrix that moves (translates) an object by the given x, y, and z values
(or moves the local coordinate system by the same amounts).
Note that using (0.0, 0.0, 0.0) as the argument for glTranslate*() is the identity operation - that is, it has
no effect on an object or its local coordinate system.
Rotate
Multiplies the current matrix by a matrix that rotates an object (or the local coordinate system) in a
counterclockwise direction about the ray from the origin through the point (x, y, z). The angle parameter
specifies the angle of rotation in degrees.
The effect of glRotatef(45.0, 0.0, 0.0, 1.0), which is a rotation of 45 degrees about the z-axis, is shown in
Note that an object that lies farther from the axis of rotation is more dramatically rotated (has a larger
orbit) than an object drawn near the axis. Also, if the angle argument is zero, the glRotate*() command
has no effect.
Scale
Multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axes.
Each x, y, and z coordinate of every point in the object is multiplied by the corresponding argument x, y,
or z. With the local coordinate system approach, the local coordinate axes are stretched, shrunk, or
reflected by the x, y, and z factors, and the associated object is transformed with them.
glScale*() is the only one of the three modeling transformations that changes the apparent size of an
object: Scaling with values greater than 1.0 stretches an object, and using values less than 1.0 shrinks it.
Scaling with a -1.0 value reflects an object across an axis. The identity values for scaling are (1.0, 1.0,
1.0). In general, you should limit your use of glScale*() to those cases where it is necessary. Using
glScale*() decreases the performance of lighting calculations, because the normal vectors have to be
renormalized after transformation
void initGL() {
/* Handler for window-repaint event. Call back when the window first appears and
void display() {
glVertex2f(-0.3f, 0.3f);
glEnd();
glVertex2f(-0.3f, -0.3f);
glVertex2f(-0.3f, 0.3f);
glEnd();
glVertex2f(-0.2f, -0.2f);
glVertex2f(-0.2f, 0.2f);
glEnd();
glVertex2f(-0.3f, -0.2f);
glVertex2f(-0.3f, -0.2f);
glEnd();
glVertex2f(-0.1f, -0.2f);
glVertex2f(-0.1f, 0.2f);
glVertex2f(-0.2f, 0.0f);
glEnd();
/* Handler for window re-size event. Called back when the window first appears and
whenever the window is re-sized with its new width and height */
// Set the aspect ratio of the clipping area to match the viewport
glLoadIdentity();
} else {
glutInitWindowSize(640, 480); // Set the window's initial width & height - non-square
return 0;
6) A point (x,y,z,1) was transferred by CV (i.e. camera and viewing matrices) and the outcome was
(x',y',z',w). What does w represent? How can we bring this point to image space? How can we bring this
point to screen space?
the fourth column represents the translation vector (origin or position) (W) of the space represented by
the transformation matrix.
Computing the coordinates of a point from camera space onto the canvas can be done using perspective
projection (camera space to image space). This process requires a simple division of the point's x- and y-
coordinate by the point's z-coordinate. Before projecting the point onto the canvas, we need to convert
the point from world space to camera space. The resulting projected point is a 2D point defined in image
space (the z-coordinate can be discarded). then convert the 2D point in image space to Normalized
Device Coordinate (NDC) space. In NDC space (image space to NDC space)
7) write and run an opengl routine to explain the use and effect of:
- gluLookAt() command
creates a viewing matrix derived from an eye point, a reference point indicating the center of the scene,
and an up vector
- gluPerspective() command
create a symmetric perspective projection matrix and multiply the matrix by the current matrix
determine which portions of objects are visible within the scene. When two objects cover the same x
and y positions but have different z values, the depth buffer ensures that only the closer object is visible
constructing hierarchical models, in which complicated objects are constructed from simpler ones
8) Explain the difference between using glDraw() versus glDrawArrays(). Write a code that uses
void init(void)
glShadeModel (GL_FLAT);
void display(void)
glEnableClientState (GL_COLOR_ARRAY);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity ();
glTranslatef(0, 0, -20);
};
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
glFlush ();
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glMatrixMode (GL_MODELVIEW);
glutInit(&argc, argv);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
9) Explain the use of opengl command glBindBuffer(). Write and run a code using glBindBuffer().
name of the new buffer object binds the buffer object name to the target.
When a buffer object is bound to a target, the previous binding for that
Buffer object names are unsigned integers. The value zero is reserved, but
Instead, buffer set to zero effectively unbinds any buffer object previously
bound, and restores client memory usage for that buffer object target.
Buffer object names and the corresponding buffer object contents are local
You may use glGenBuffers to generate a set of new buffer object names.
**
*Creates the vertex and normal buffers only. This is typically used for a
* VertexAnimationObject3D's frames.
* @see VertexAnimationObject3D
*/
((FloatBuffer)
mBuffers.get(VERTEX_BUFFER_KEY).buffer).compact().positi
on(0);
((FloatBuffer)
mBuffers.get(NORMAL_BUFFER_KEY).buffer).compact().positi
on(0);
createBuffer(mBuffers.get(VERTEX_BUFFER_KEY),
BufferType.FLOAT_BUFFER, GLES20.GL_ARRAY_BUFFER);
createBuffer(mBuffers.get(NORMAL_BUFFER_KEY),
BufferType.FLOAT_BUFFER, GLES20.GL_ARRAY_BUFFER);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER,
0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
10) Derivation of all model transformation matrices (rotation, translation ,and scaling) in 2D and 3D. You
should understand how geometrical transformations take place. And how to cascade multiple
transformations.
We intend to translate a point in the xy-plane to a new place by adding a vector <h, k>
. It is not difficult to see that between a point (x, y) and its new place (x', y'), we
have x' = x + h and y' = y + k. Let us use a form similar to the homogeneous
coordinates. That is, a point becomes a column vector whose third component is 1.
Thus, point (x,y) becomes the following:
Then, the relationship between (x, y) and (x', y') can be put into a matrix form like the
following:
If a point (x, y) is rotated an angle a about the coordinate origin to become a new point
(x', y'), the relationships can be described as follows:
Thus, rotating a line Ax + By + C = 0 about the origin a degree brings it to a new
equation:
(Acosa - Bsina)x' + (Asina + Bcosa)y' + C = 0
Translations and rotations can be combined into a single equation like the following:
The above means that rotates the point (x,y) an angle a about the coordinate origin and
translates the rotated result in the direction of (h,k). However, if translation (h,k) is
applied first followed by a rotation of angle a (about the coordinate origin), we will
have the following:
Therefore, rotation and translation are not commutative!
This is the new equation of the given conic after the specified transformation. Note
that the new 3-by-3 symmetric matrix that represents the conic in a new position is the
following:
Now you see the power of matrices in describing the concept of transformation.
With this set of equations, letting a be 90 degree rotates (1,0,0) to (0,1,0) and (0,1,0)
to (-1,0,0). Therefore, the x-axis rotates to the y-axis and the y-axis rotates to the
negative direction of the original x-axis. This is the effect of rotating about the z-axis
90 degree.
Based on the same idea, rotating about the x-axis an angle a is the following:
Let us verify the above again with a being 90 degree. This rotates (0,1,0) to (0,0,1)
and (0,0,1) to (0,-1,0). Thus, the y-axis rotates to the z-axis and the z-axis rotates to the
negative direction of the original y-axis.
But, rotating about the y-axis is different! It is because the way of measuring angles.
In a right-handed system, if your right hand holds a coordinate axis with your thumb
pointing in the positive direction, your other four fingers give the positive direction of
angle measuring. More precisely, the positive direction for measuring angles is from
the z-axis to x-axis. However, traditionally the angle measure is from the x-axis to
the z-axis. As a result, rotating an angle a about the y-axis in the sense of a right-
handed system is equivalent to rotating an angle -a measuring from the x-axis to the z-
axis. Therefore, the rotation equations are
Let us verify the above with rotating about the y-axis 90 degree. This rotates (1,0,0) to
(0,0,-1) and (0,0,1) to (1,0,0). Therefore, the x-axis rotates to the negative direction of
the z-axis and the z-axis rotates to the original x-axis.
A rotation matrix and a translation matrix can be combined into a single matrix as
follows, where the r's in the upper-left 3-by-3 matrix form a rotation
and p, q and r form a translation vector. This matrix represents rotations followed by a
translation.
You can apply this transformation to a plane and a quadric surface just as what we did
for lines and conics earlier.
11) Understand camera parameters and how they are reflected in matrices (remember OpenGL
12) Derivation of camera transformation matrix 'C' (which is similar to model transformation) and
derivation of perspective projection matrix 'V'.
13) Understand all spaces that we used (viewing space, frustum space, image space, screen space) also
how to move between them (using different matrices).
View space
The view space is what people usually refer to as the camera of OpenGL (it is sometimes also known as
camera space or eye space). The view space is the result of transforming your world-space coordinates
to coordinates that are in front of the user's view. The view space is thus the space as seen from the
camera's point of view. This is usually accomplished with a combination of translations and rotations to
translate/rotate the scene so that certain items are transformed to the front of the camera. These
combined transformations are generally stored inside a view matrix that transforms world coordinates
to view space. In the next chapter we'll extensively discuss how to create such a view matrix to simulate
a camera.
Clip space
At the end of each vertex shader run, OpenGL expects the coordinates to be within a specific range and
any coordinate that falls outside this range is clipped. Coordinates that are clipped are discarded, so the
remaining coordinates will end up as fragments visible on your screen. This is also where clip space gets
its name from.
Because specifying all the visible coordinates to be within the range -1.0 and 1.0 isn't really intuitive, we
specify our own coordinate set to work in and convert those back to NDC as OpenGL expects them.
To transform vertex coordinates from view to clip-space we define a so called projection matrix that
specifies a range of coordinates e.g. -1000 and 1000 in each dimension. The projection matrix then
converts coordinates within this specified range to normalized device coordinates (-1.0, 1.0) (not
directly, a step called Perspective Division sits in between). All coordinates outside this range will not be
mapped between -1.0 and 1.0 and therefore be clipped. With this range we specified in the projection
matrix, a coordinate of (1250, 500, 750) would not be visible, since the x coordinate is out of range and
thus gets converted to a coordinate higher than 1.0 in NDC and is therefore clipped.
frustum space
The view frustum is typically obtained by taking a frustum—that is a truncation with parallel planes—of
the pyramid of vision, which is the adaptation of (idealized) cone of vision that a camera or eye would
have to the rectangular viewports typically used in computer graphics. Some authors use pyramid of
vision as a synonym for view frustum itself. consider it truncated.
The exact shape of this region varies depending on what kind of camera lens is being simulated, but
typically it is a frustum of a rectangular pyramid (hence the name). The planes that cut the frustum
perpendicular to the viewing direction are called the near plane and the far plane. Objects closer to the
camera than the near plane or beyond the far plane are not drawn. Sometimes, the far plane is placed
infinitely far away from the camera so all objects within the frustum are drawn regardless of their
distance from the camera.
Viewing-frustum culling is the process of removing from the rendering process those objects that lie
completely outside the viewing frustum. Rendering these objects would be a waste of resources since
they are not directly visible. To make culling fast, it is usually done using bounding volumes surrounding
the objects rather than the objects themselves.
Local space
Local space is the coordinate space that is local to your object, i.e. where your object begins in. Imagine
that you've created your cube in a modeling software package (like Blender). The origin of your cube is
probably at (0,0,0) even though your cube may end up at a different location in your final application.
Probably all the models you've created all have (0,0,0) as their initial position. All the vertices of your
model are therefore in local space: they are all local to your object.
The vertices of the container we've been using were specified as coordinates between -0.5 and 0.5 with
0.0 as its origin. These are local coordinates.
World space
If we would import all our objects directly in the application they would probably all be somewhere
positioned inside each other at the world's origin of (0,0,0) which is not what we want. We want to
define a position for each object to position them inside a larger world. The coordinates in world space
are exactly what they sound like: the coordinates of all your vertices relative to a (game) world. This is
the coordinate space where you want your objects transformed to in such a way that they're all
scattered around the place (preferably in a realistic fashion). The coordinates of your object are
transformed from local to world space; this is accomplished with the model matrix.
The model matrix is a transformation matrix that translates, scales and/or rotates your object to place it
in the world at a location/orientation they belong to. Think of it as transforming a house by scaling it
down (it was a bit too large in local space), translating it to a suburbia town and rotating it a bit to the
left on the y-axis so that it neatly fits with the neighboring houses. You could think of the matrix in the
previous chapter to position the container all over the scene as a sort of model matrix as well; we
transformed the local coordinates of the container to some different place in the scene/world.
14) Understand how clipping works, and how clipping in viewing space is a different process than
clipping in the context of computer graphics is a method to selectively enable or disable rendering
operations within a defined range of interest. Mathematically, clipping can be described with the
terminology of constructive geometry. A rendering algorithm draws only pixels at the intersection
between the clip area and the scene model (for example 3D configurator). Lines and areas outside the
visible volume (frustum) are removed.
Clip areas are often specified to improve rendering performance. A well selected clip allows the renderer
to save time and energy by skipping calculations to pixels that the user cannot see. The pixels to draw
are inside the clipart. Pixels that are not drawn are outside the Clip Area. More informally, pixels that are
not to be drawn are called clipped.
In two-dimensional graphics, a clipart area can be defined so that pixels are only drawn within the
boundaries of a window or frame. Clip areas can also be used to selectively control pixel rendering for
aesthetic or artistic purposes. In many implementations, the final clip area is the composite (or
intersection) of one or more application-defined shapes and any hardware limitations of the system.
In a sample application, an image editing program is recommended. A user application can render the
image into a viewport. As the user zooms and scrolls to see a smaller portion of the image, the
application can set a clip boundary so that pixels outside the viewport are not rendered. In addition, GUI
widgets, overlays, and other windows or frames can hide some pixels from the original image. In this
sense, the Clip Area is the interaction of the application-defined “User Clip” and the “Device Clip”, which
is forced by the software and hardware implementation of the system. The application software can use
this clip information to save computing time, energy and storage space and avoid working with pixels
that are not visible.
In three-dimensional graphics, the terminology of clipping can be used to describe many related
features. Typically, “clipping” refers to operations in the plane that work with rectangular shapes, and
“culling” to more general methods of selectively processing scene model elements. This terminology is
not rigid, and the exact use varies from source to source.
Scene model elements include geometric primitives: points or nodes, line segments or edges, polygons
or faces, and more powerful model objects such as curves, splines, faces, and even text. In complicated
scene models, individual elements can be selectively deactivated (truncated), e.g. for reasons of visibility
within the viewport (backface culling), orientation (backside alveation), darkening by other scenes or
model elements (occlusion culling, depth or z clipping). There are sophisticated algorithms to efficiently
detect and perform such clipping. Many optimized clipping methods are based on a specific hardware
acceleration logic provided by a GPU.
The concept of clipping can be extended to a higher dimensionality using methods of abstract algebraic
geometry.
15) What is meant by depth buffers and how could they be used in OpenGL?
In order to use the depth test, the current Framebuffer must have a depth buffer. A depth buffer is an
image that uses a depth image format. The Default Framebuffer may have a depth buffer, and user-
defined framebuffers can attach depth formatted images (either depth/stencil or depth-only) to the
GL_DEPTH_ATTACHMENT attachment point.
If the current framebuffer has no depth buffer, then the depth test behaves as if it is always disabled.
17) Given is a display method which draws three triangles. The triangles are centered on the z-axis and
parallel to the xy-plane. The corresponding vertices of the three triangles have the same x- and y-
coordinates, but different z-values. The first triangle drawn by the display method is red and closest to
the view point. The second triangle is green, and the third triangle blue and furthest from the view
point. The view point is on the z-axis and a perspective projection is used to render the scene. What will
happen if we disabled the depth buffer?
When disabled, the depth comparison and subsequent possible updates to the depth buffer value are
bypassed and the fragment is passed to the next operation.
18) In OpenGL graphical primitives are defined by vertices which are transformed by the OpenGL
pipeline. List the sequence of this pipeline
The OpenGL rendering pipeline is initiated when you perform a rendering operation. Rendering
operations require the presence of a properly-defined vertex array object and a linked Program
Object or Program Pipeline Object which provides the shaders for the programmable pipeline stages.
1. Vertex Processing:
1. Each vertex retrieved from the vertex arrays (as defined by the VAO) is acted upon by
a Vertex Shader. Each vertex in the stream is processed in turn into an output vertex.
2. Vertex Post-Processing, the outputs of the last stage are adjusted or shipped to different
locations.
2. Primitive Assembly
1. Scissor Test
2. Stencil Test
3. Depth Test
4. Blending
5. Logical Operation
6. Write Mask