Starting from:

$25

CENG477-Assignment 2 Solved

CENG477

Introduction to Computer Graphics

Assignment 2 - A Software Rasterizer implementing the Forward Rendering Pipeline

1             Objectives
In this assignment, you are going to implement Modeling Transformation, Viewing Transformation, and Rasterization stages of the Forward Rendering Pipeline. Specifically, given a set of triangles and the position and the color attributes of all of their vertices, your goal is to render the scene as a two-dimensional (2D) image. The parameters of a perspective view camera will be specified similar to the first assignment. You will transform all of the vertices to the viewport and then use line drawing and triangle rasterization algorithms to display the triangles in wireframe or solid modes. You will implement a clipping algorithm of your choice (Cohen-Sutherland or Liang-Barsky) for wireframe mode only. You will also implement the backface culling (for both modes) for correct rendering of the scene. You will write your implementations in C++ language.

Keywords: forward rendering pipeline, modeling transformations, viewing transformations, line drawing, triangle rasterization, interpolation, clipping, backface culling

2             Specifications
1.        Name of the executable will be “rasterizer”.

2.        Your executable will take an input file name from command line. Input file names are not static, they can be anything.

3.        The scene will be composed of instances of meshes. A set of triangles will form a mesh with a sequence of several transformations (translation, rotation, scaling) you will be able to move, rotate, or resize a mesh (i.e., all of the triangles in the mesh). Transformations will be applied to the meshes in the order specified in the input file.

4.        You will not implement any lighting computations in this assignment. The color of each vertex will be provided as input and your goal will be to interpolate color along the edges and the surfaces of the triangles in wireframe and solid modes, respectively.

5.        You will implement two types of projection transformations: orthographic projection and perspective projection.

6.        Use the midpoint algorithm to draw triangle edges and use the barycentric coordinates based algorithm to rasterize triangle faces.

7.        In both wireframe and solid modes, triangles whose backface is facing the viewer will be culled. Backface culling should be enabled/disabled according to the setting in input file. You should calculate normal vectors for backface culling. Default value for backface culling setting is ’disabled’.

8.        You will also implement a clipping algorithm of your choice. For example, you can choose an algorithm among algorithms that you saw on the lectures. You will do clipping for wireframe mode only. For solid mode meshes, you can assume that the mesh is completely visible to the camera.

9.        There can be multiple camera configurations for each given scene.


3             Input File
You will not deal with reading input files, however, it is good for you to know what input file contains for testing purposes. Input files are in XML format and their structure is given below:

<Scene>

<BackgroundColor>R G B</BackgroundColor>

<Culling> enabled or disabled </Culling>

<Cameras>

<Camera id="#" type="perspective or orthographic">

<Position>X Y Z</Position>

<Gaze>X Y Z</Gaze>

<Up>X Y Z</Up>

<ImagePlane>

Left Right Bottom Top Near Far HorRes VerRes

</ImagePlane>

<OutputName> <image_name>.ppm </OutputName>

</Camera>

... more cameras ...

</Cameras>

<Vertices>

<Vertex id="1" position="X Y Z" color="R G B" /> ... more vertices ...

</Vertices>

<Translations>

<Translation id="1" value="tx ty tz" /> ... more translations ...

</Translations>

<Scalings>

<Scaling id="1" value="sx sy sz" /> ... more scalings ...

</Scalings>

<Rotations>

<Rotation id="1" value="angle ux uy uz" /> ... more rotations ...

</Rotations>

<Meshes>

<Mesh id="#" type="wireframe or solid">

<Transformations>

<Transformation> <transformation_type> <transformation_id> </Transformation> ... more transformations ...

</Transformations>

<Faces>

<vertex_id_1_triangle_1> <vertex_id_2_triangle_1> <vertex_id_3_triangle_1>

<vertex_id_1_triangle_2> <vertex_id_2_triangle_2> <vertex_id_3_triangle_2> ... more faces (triangles) ...

</Faces>

</Mesh>

... more meshes ...

</Meshes>

</Scene>

3.1           Input File Sections
3.1.1      Background color:

Specifies the R, G, B values of the background.

3.1.2       Backface Culling Setting:

Specifies whether culling is applied or not. If it is ’disabled’, there will be no culling, just draw all triangles. If it is ’enabled’, culling should be done when triangle’s backface is facing to the viewer.

3.1.3      Cameras:

•    Id specifies the camera id.

•    Type specifies which projection transformation is applied. If it is ’orthographic’, that means you need to apply orthographic projection, If it is ’perspective’, you should apply perspective projection.

•    Position specifies the X, Y, Z coordinates of the camera.

•    Gaze specifies the direction that the camera is looking at.

•    Up specifies the up vector of the camera. The up vector is not necessarily given as orthogonal to the gaze vector. Therefore the camera’s x-axis is found by a cross product of the gaze and up vectors, then the up vector is corrected by a cross product of gaze and x-axis vectors of the camera.

•    ImagePlane specifies the coordinates of the image plane in Left, Right, Bottom, Top parameters; distance of the image plane to the camera in Near and distance of the far plane to the camera in Far parameters, and the resolution of the final image in HorRes and VerRes parameters. All values are floats except HorRes and VerRes, which are integers.

•    OutputName is a string which is the name of the output image.

3.1.4     Vertices:

•    Id specifies the vertex id.

•    Position specifies the position of the vertex in X Y Z.

•    Color specifies the color of each vertex in R G B.

3.1.5     Translations:

•    Id specifies the translation id.

•    Value specifies tx, ty, and tz, which are the translation parameters, i.e., translation amounts along the major axes.

3.1.6      Scalings:

•    Id specifies the scaling id.

•    Value specifies sx, sy, and sz, which are the scaling parameters, i.e., scaling level in the corresponding coordinate axes.

3.1.7      Rotations:

•    Id specifies the rotation id.

•    Value specifies α, ux, uy, and uz, which are the rotation parameters, i.e., the object is rotated α degrees around the rotation axis which pass through points (ux,uy,uz) and (0,0,0). The positive angle of rotation is given as the counter-clockwise rotation along the direction (ux,uy,uz).

3.1.8     Meshes:

•    Id specifies the mesh id.

•    Type specifies the type of the mesh. ’wireframe’ for wireframe, ’solid’ for solid rendering.

•    Each transformation has transformation type and transformation id parameters. The ‘t’ indicates a translation transformation, ‘s’ indicates a scale transformation, and ‘r’ indicates a rotation transformation. The transformation id ranges from [1 ...translation count] for type ‘t’ transformations, and similarly for the other type of transformations.

•    In Faces section you can find the triangles and each line represents a triangle. Each triangle is given as a list of vertex ids in counter clockwise order when faced from the front side. Vertex ids start from 1.

4             Sample input/output
A sample camera and a scene file are provided below:

<Scene>

<BackgroundColor>255 255 255</BackgroundColor>

<Culling>enabled</Culling>    <------- backface culling is enabled <Cameras>

<Camera id="1" type="perspective"> <------- perspective projection

<Position>-10 -40 -26</Position>

<Gaze>1 1 -0.2</Gaze>

<Up>0 1 0</Up>

<ImagePlane>-1 1 -1 1 2 1000 700 700</ImagePlane>

<OutputName>filled_box_1.ppm</OutputName>

</Camera>

<Camera id="2" type="perspective"> <------- perspective projection

<Position>-20 -28 -23</Position>

<Gaze>1 0.5 -0.2</Gaze>

<Up>0 1 0</Up>

<ImagePlane>-1 1 -1 1 2 1000 700 700</ImagePlane>

<OutputName>filled_box_2.ppm</OutputName>

</Camera>

</Cameras>

<Vertices>

<Vertex id="1" position="1.0 1.0 -1.0" color="100 100 100" />

<Vertex id="2" position="-1.0 1.0 -1.0" color="255 0 0" />

<Vertex id="3" position="-1.0 1.0 1.0" color="0 255 0" />

<Vertex id="4" position="1.0 1.0 1.0" color="0 0 255" />

<Vertex id="5" position="1.0 -1.0 -1.0" color="0 0 255" />

<Vertex id="6" position="-1.0 -1.0 -1.0" color="0 255 0" />

<Vertex id="7" position="-1.0 -1.0 1.0" color="255 0 0" />

<Vertex id="8" position="1.0 -1.0 1.0" color="100 100 100" />

</Vertices>

<Translations>

<Translation id="1" value="0.0 10.0 0.0" />

<Translation id="2" value="3.0 -3.0 -6.0" />

</Translations>

<Scalings>

<Scaling id="1" value="5.2 5.2 5.2" />

</Scalings>

<Rotations>

<Rotation id="1" value="45 0.0 1.0 0.0" />

<Rotation id="2" value="60 0.8 0.6 0.0" />

<Rotation id="3" value="20 1.0 0.0 0.0" />

</Rotations>

<Meshes>

<Mesh id="1" type="solid"> <------- mesh is drawn in solid mode <Transformations>

<Transformation>r 1</Transformation>

<Transformation>t 2</Transformation>

<Transformation>s 1</Transformation>

</Transformations>

<Faces>

7 8 4

7     4 3

8     5 1

8 1 4

6 3 2

6 7 3

3 4 1

3 1 2

6 2 5

2 1 5

5 8 6

7 6 8

</Faces>

</Mesh>

</Meshes>

</Scene>

You can see outputs of this example in the next page.

 

                                                  (a) Solid-culling enabled                (b) Solid-culling disabled

 

                                            (c) Wireframe-culling enabled     (d) Wireframe-culling disabled

Figure 1: Same mesh, different modes of rendering

5             Hints & Tips
1.    Start early!

2.    Note that in the wireframe mode only the edges of the triangle will be drawn.

3.    For debugging purposes, consider using simple scenes. Also it may be necessary to debug your code by tracing what happens for a single triangle (always simplify the problem when debugging).

4.    You can reach different inputs in “culling disabled inputs” and “culling enabled inputs” folders and desired images in “culling disabled outputs”, “culling enabled outputs” and “different  projection type” folders. You will see that different camera angles, output of different rendering modes are provided to you to understand 3d scene and rendering modes completely.

More products