Professional Documents
Culture Documents
calculated from the view plane along the 𝑧 axis of a coordinate system.
Z buffer Z buffer
Z buffer Algorithm Z buffer Examples
Z buffer Screen
Z buffer Examples Z buffer Examples
Chapter Three What is OpenGL?
hardware.”
Rendering Process with OpenGL it is a 3D graphics and modeling library that is highly portable and very
fast.
Using OpenGL, you can create elegant and beautiful 3D graphics with
Initially, it used algorithms carefully developed and optimized by The OpenGL API itself is not a programming language like C or C++.
Silicon Graphics, Inc. (SGI), an acknowledged world leader in It is more like the C runtime library, which provides some
Over time, OpenGL has evolved as other vendors have contributed OpenGL is intended for use with computer hardware that is designed
their expertise and intellectual property to develop high-performance and optimized for the display and manipulation of 3D graphics.
implementations of their own. However, Software-only implementations of OpenGL are also possible.
1. Setting up the graphics context
The key steps involved in creating initializing the OpenGL context within it.
and displaying graphics Platform-specific libraries like GLFW (OpenGL Framework) or GLUT
OpenGL context.
2. Defining the geometry 3. Sending Data to the GPU
In OpenGL, 3D objects are represented as collections of vertices, The vertex data is sent from the CPU (Central Processing Unit) to the
edges, and faces.
GPU (Graphics Processing Unit) using OpenGL buffer objects.
To render a 3D scene, the application must define the geometry of the
objects using vertex data. These buffer objects efficiently store the vertex data in the GPU's
The vertex data includes the coordinates of the vertices, information memory for fast access during rendering.
about normal (surface directions), texture coordinates, and other
attributes.
4. Compiling and Linking Shaders 5. Rendering Loop
Shaders are small programs written in the OpenGL Shading Language The rendering process in OpenGL occurs within a rendering loop, also
known as the game loop.
(GLSL) that run on the GPU.
This loop continually updates the scene and renders it on the screen.
There are two types of shaders used in OpenGL: The loop typically involves the following steps:
Clearing the Frame Buffer
vertex shaders, which manipulate vertices, and
A)
1.
B) Updating the Scene
2. fragment shaders, which determine the color and depth of fragments C) Setting Up the Camera
The application must compile and link the shader programs before F) Displaying the Frame
The Reference Model is a conceptual framework that defines the A coordinate system is a mathematical framework used to specify the
components and processes involved in creating and displaying graphics precise location of points in space or on a surface.
OpenGL fits into this model as the graphics API responsible for relative to a reference point or reference axes.
rendering 2D and 3D graphics efficiently and interactively. The origin of the 2D Cartesian system is at 𝑥 = 0, 𝑦 = 0.
3.2. Coordinate system Cont’d
For example, a standard VGA screen has 640 pixels from left to
right and 480 pixels from top to bottom.
To specify a point in the middle of the screen, you specify that a
point should be plotted at (320,240)
that is, 320 pixels from the left of the screen and 240 pixels
down from the top of the screen.
In OpenGL, or almost any 3D API, when you create a window to
Pinhole
3D Viewing: The Synthetic Camera 3.4. Output primitives
General synthetic camera: each package has its own Output primitives are the basic geometric shapes that
but they are all (nearly) equivalent, with the can be rendered by a graphics system.
following parameters/degrees of freedom: Common output primitives:
Camera Position and Orientation › Points: A single pixel or dot.
Field of view (angle of view, e.g., wide, narrow/telephoto, › Lines: A straight line segment connecting two vertices
normal...) › Triangles: A three-sided polygon defined by three vertices.
Depth of field/focal distance (near distance, far distance) › Quads: A four-sided polygon defined by four vertices
Tilt of view/ film plane (if not perpendicular to viewing › Other Polygons: Graphics systems may support polygons with
direction, produces oblique projections) more than three or four sides, but they are typically tessellated
Perspective or Orthographic Projection into triangles for rendering.
Output attributes Chapter Four
glBegin(GL_POINTS);
glEnd();
Line Drawing Algorithms Cont’d…
Lines are a very common primitive and will be supported by almost all Given two end-points (𝑥0, 𝑦0) and (𝑥𝑒𝑛𝑑 , 𝑦𝑒𝑛𝑑 ), we can calculate values for
graphics packages. 𝑚 and 𝑏 as follows:
Lines are normally represented by the two end-points of the line, and
y end y 0 −−−−−−−−−−−−−−−−−−−−−−−−−− eq. (2)
m
points 𝑥, 𝑦 along the line must satisfy the following slope-intercept
xend x0
equation:
𝑏 = 𝑦 − 𝑚. Δ𝑥 -------------------------------------------------- eq. (3)
𝑦 = 𝑚𝑥 + 𝑏 ----------------------------------------- eq. (1)
where 𝑚 is the slope or gradient of the line, and 𝑏 is the coordinate at Furthermore, for any given x-interval Δ𝑥, we can calculate the corresponding
Δ𝑦 = 𝑚. Δ𝑥 -------------------------------------------------------------------eq. (4)
The Digital Differential Analyser (DDA) algorithm operates by starting at Figure 1 – ‘Holes’ in a Line Drawn by
one end-point of the line, Incrementing 𝑥 and Computing the
and then using 𝐸𝑞. (4) and (5) to generate successive pixels until the Corresponding y-Coordinate
second end-point is reached.
Therefore, first, we need to assign values for Δ𝑦 and Δ𝑥 This would compute correct line points but, as illustrated by Figure 4.1,
it would leave gaps in the line.
Suppose we simply increment the value of 𝑥 at each iteration (i.e. Δ𝑥 = 1) The reason for this is that the value of Δ𝑦 is greater than one, so the gap
and then compute the corresponding value for y using 𝑒𝑞. (2) 𝑎𝑛𝑑 (4). between subsequent points in the line is greater than 1 pixel.
Cont’d Cont’d
The solution to this problem is to make sure that both Δ𝑥 and Δ𝑦 have Once we have computed values for Δ 𝑥 and Δ 𝑦, the basic DDA algorithm
values less than or equal to one. is:
Start with (𝑥0, 𝑦0)
To ensure this, we must first check the size of the line gradient. The
Find successive pixel positions by adding on ( Δ𝑥 , Δ 𝑦) and rounding
conditions are:
to the nearest integer, i.e.
𝐼𝑓 |𝑚| ≤ 1:
o 𝑥𝑘 + 1 𝑥𝑘 + Δ𝑥
o Δ𝑥 = 1 =
o 𝑦𝑘 + 1 𝑦𝑘 + Δ 𝑦
o Δ𝑦 = 𝑚 =
For each position (𝑥𝑘 , 𝑦𝑘) computed, plot a line point at
𝐼𝑓 |𝑚| > 1:
(𝑟𝑜𝑢𝑛𝑑(𝑥𝑘 ), 𝑟𝑜𝑢𝑛𝑑(𝑦𝑘 )), where the round function will round to the
o Δ 𝑥 = 1/𝑚
nearest integer
o Δ𝑦 = 1
Note that the actual pixel value used will be calculated by rounding to the nearest
integer, but we keep the real-valued location for calculating the next pixel position.
Examples (DDA algorithm) Cont’d…
Using these values of Δ𝑥 and Δy we can now start to plot line points:
Apply the DDA algorithm for drawing a straight-line segment. Start with (𝑥0, 𝑦0) = (𝟏𝟎, 𝟏𝟎) – colour this pixel
Given: 𝑥 ,𝑦 = 10,10 Next, (𝑥1, 𝑦1) = (10 + 1,10 + 0.6) = (11,10.6) – so we colour pixel
𝑥 ,𝑦 = (15,13) (11,11)
Next, (𝑥2, 𝑦2) = (11 + 1,10.6 + 0.6) = (12,11.2) – so we colour pixel
First compute a value for the gradient 𝑚:
(12,11)
y y 0 (13 10) 3
m end 0.6 Next, (𝑥3, 𝑦3) = (12 + 1,11.2 + 0.6) = (13,11.8) – so we colour pixel
xend x0 (15 10) 5
(13,12)
Now, because |𝑚| ≤ 1, we compute Δ𝑥 and Δ𝑦 as follows:
Next, (𝑥4, 𝑦4) = (13 + 1,11.8 + 0.6) = (14,12.4) – so we colour pixel
(14,12)
Δ𝑥 = 1 and Δy = 0.6
Next, (𝑥5, 𝑦5) = (14 + 1,12.4 + 0.6) = (15,13) – so we colour pixel
(15,13)
We have now reached the end-point (𝑥𝑒𝑛𝑑 , 𝑦𝑒𝑛𝑑), so the algorithm
terminates
Cont’d… Bresenham’s Line-Drawing Algorithm
𝑥 ,𝑦 = (15,13)
Bresenham’s line-drawing algorithm provides significant improvements in
These improvements arise from the observation that for any given line,
on the line, we know the next line point must be either pixel A or pixel B.
Cont’d
Therefore we do not need to compute the actual floating-point location of
the ‘true’ line point; we need only make a decision between pixels A and B.