QuestionQuestion

Purpose:
* Demonstrate ability to backface cull triangles using the method discussed in class.
* Demonstrate ability to write some additional (simple) C code.

Background:
* Use the backface culling techinique developed and discussed in class.
* The goal for this homework is to read a model and a view file and then generate the properly projected and clipped lines for that model -- but only for those faces that are visible from the camera position. The lines are then printed (along with the canvas size) to create a viewable ".sdf" file.
* The Model and View files are as described in previous homework.

Tasks:

1. There are a number of TODO: comments in the .c files that need to be replaced with the appropriate C code. (Do NOT change anything in any .c file except to update the headers and to replace a TODO comment with your code.)

Here's a list:
hmwk_04.c:42:    // TODO: Allocate enough space for the number of vertices the ...
hmwk_04.c:49:    // TODO: Project the vertices of the model into the space you ...
hmwk_04.c:59:    // TODO: Print four 'l' lines, corresponding to the ...
hmwk_04.c:70:    // TODO: Construct a Vertex holding the worldspace position of ...
hmwk_04.c:88:    // TODO: For each face of the model, draw the triangle ...
hmwk_04.c:97:    // TODO: Free all allocated structures. ...
hmwk_04.c:101:    // TODO: Return from main indicating no error. ...

projection.c:93: // TODO: Use projectVertex() to project each of the ...

triangle.c:13:    // TODO: Using a Line structure and calling clipLine(), check ...

visible.c:13:    // TODO: Purpose: Determine if the triangle represent by ...

7. When you have done so, compile and run everything as shown. Notice that there are two input files being given to hmwk_04, the first is the model file (Tests/pyramid_01.svfm) and the second is the view file (Tests/pyramid_01.view).

You should observe the given output.

$ gcc -o hmwk_04 hmwk_04.c face.c line.c model.c projection.c triangle.c vertex.c view.c visible.c -lm
$ ./hmwk_04 Tests/pyramid.svfm Tests/pyramid_setting_01.view
#- Model parameters --------------------
# Vertex count :    5
# Face count   :    6

#- Vertex list -------------------------
# [    0]       0.000000,      0.000000,      0.000000
# [    1]       0.800000,      0.000000,      0.000000
# [    2]       0.000000,      0.800000,      0.000000
# [    3]       0.800000,      0.800000,      0.000000
# [    4]       0.400000,      0.400000,      0.700000

#- Face list ---------------------------
# [    0]      3,    1,    0
# [    1]      2,    3,    0
# [    2]      1,    4,    0
# [    3]      4,    2,    0
# [    4]      1,    3,    4
# [    5]      2,    4,    3
#---------------------------------------
#- View parameters ---------------------
# B├ęzier resolution :    0
# Euler angles      :      0.000000,      0.000000,      0.000000
# World limits      :    -1.000000,    -1.000000,      1.000000,      1.000000
# Camera distance   :      0.000000
# Canvas width      :   500
# Canvas height    :   400
# Screen limits    :      0.100000,      0.100000,      0.900000,      0.900000
# Portal pixels    :   50, 450,   40, 360
#---------------------------------------
#- Projection parameters ---------------
# (fx, fy) : (      1.000000,      1.000000 )
# (gx, gy) : (    50.000000,    40.000000 )
# (sx, sy) : (    200.000000,    160.000000 )
# (ax, ay) : (    250.000000,    200.000000 )
# Camera distance :      0.000000
#---------------------------------------
c 500 400
l    50       40      450       40
l    450       40      450      360
l    450      360       50      360
l    50      360       50       40
l    410.0    200.0    330.0    264.0
l    330.0    264.0    250.0    200.0
l    250.0    200.0    410.0    200.0
l    330.0    264.0    250.0    328.0
l    250.0    328.0    250.0    200.0
l    250.0    200.0    330.0    264.0
l    410.0    200.0    410.0    328.0
l    410.0    328.0    330.0    264.0
l    330.0    264.0    410.0    200.0
l    250.0    328.0    330.0    264.0
l    330.0    264.0    410.0    328.0
l    410.0    328.0    250.0    328.0
$

Your output should match this EXACTLY. 'EXACTLY' means just that. It should be the same character-by-character. When your program is tested, it will be run against other data files and the output compared using diff.

8. There are a number of line files in Tests/ along with a view file for each. Run all of these tests and compare your results to those given in Results/. Your output should match

9. Use diff (fc on Windows) to compare your output to that of the results file. They should match with NO DIFFERENCES. (Remember that line endings can be different among Linux, MacOS, and (shudder) Windows, so do your comparisons ignoring only the line endings. The SPACING within the lines however should be identical.)

10. Save the output of hmwk_04 in a file with the '.sdf' extension and use the Display tool to view it. (See the Display tool posting on Canvas for more info. The SDF files in Results/ are all viewable with the Display tool.)

Your display should look like the images in Screenshots/.

Compare these new screenshots with those that were given for Homework 03. (The '_03' cases are new for this homework so there're no corresponding screenshots in Homework 03 for them.)

Notice that while the general look is the same, you no longer see those triangles whose back sides were facing the camera.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

//----------------------------------------------------------
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include "projection.h"

//----------------------------------------------------------
Projection *allocProjection()
{
Projection *p;

p = (Projection *) calloc( 1, sizeof( Projection ) );
if ( p == NULL ) {
    fprintf( stderr, "allocProjection: Unable to allocate Projection.\n" );
    exit( 1 );
}

return p;
}

//----------------------------------------------------------
Projection *computeProjection( View *v )
{
Projection *p = allocProjection();

p->m_fx = -v->m_worldXMin;
p->m_fy = -v->m_worldYMin;

p->m_gx = v->m_width*v->m_viewportXMin;
p->m_gy = v->m_height*v->m_viewportYMin;

p->m_sx = ( v->m_width*( v->m_viewportXMax - v->m_viewportXMin ) ) /
    ( v->m_worldXMax - v->m_worldXMin );
p->m_sy = ( v->m_height*( v->m_viewportYMax - v->m_viewportYMin ) ) /
    ( v->m_worldYMax - v->m_worldYMin );

p->m_ax = p->m_fx*p->m_sx + p->m_gx;
p->m_ay = p->m_fy*p->m_sy + p->m_gy;

p->m_cameraDistance = v->m_cameraDistance;

return p;
}

//----------------------------------------------------------
void dumpProjection( Projection *p )
{
printf( "#- Projection parameters ---------------\n" );
printf( "# (fx, fy) : ( %13.6f, %13.6f )\n", p->m_fx, p->m_fy );
printf( "# (gx, gy) : ( %13.6f, %13.6f )\n", p->m_gx, p->m_gy );
printf( "# (sx, sy) : ( %13.6f, %13.6f )\n", p->m_sx, p->m_sy );
printf( "# (ax, ay) : ( %13.6f, %13.6f )\n", p->m_ax, p->m_ay );
printf( "# Camera distance : %13.6f\n", p->m_cameraDistance );
printf( "#---------------------------------------\n" );
}

//----------------------------------------------------------
void freeProjection( Projection *p )
{
if ( p != NULL ) {
    free( p );
}
}

//----------------------------------------------------------
void projectVertex( Projection *p, Vertex *v1, Vertex *v2 )
{
double x = v1->x;
double y = v1->y;
double z = v1->z;

if ( p->m_cameraDistance != 0.0 ) {
    if ( z >= p->m_cameraDistance ) {
      fprintf( stderr, "Vertex has z (%13.6f) >= the camera distance (%13.6f)\n",
       z, p->m_cameraDistance );
    } else {
      x = x / ( 1 - z/p->m_cameraDistance );
      y = y / ( 1 - z/p->m_cameraDistance );
    }...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$70.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats