22
Version 2013-09-30 11:27 L ¨ OSUNG

GED Endterm 2013 Sol

Embed Size (px)

DESCRIPTION

GED

Citation preview

  • Version

    2013-09-30 11:27

    LOSUNG

  • Technische Universitat Munchen Institut fur InformatikLehrstuhl fur Computer Graphik & Visualisierung

    Prof. R. Westermann

    Exam: Game Engine Design

    Surname: First name:

    Course of study: Matriculation number:

    Room: Row: Seat:

    Date: August 2nd, 2013 Signature:

    Notes:

    Working time is 90 minutes.

    This exam consists of two parts: Part I refers to content from the practical course, whereas Part II refers to contentfrom the lecture. In Part I you can gain a maximum of 42 points. In Part II you can gain a maximum of 64 points.

    Please note: To pass the lecture Game Engine Design you need to gain a minimum of 40% of the points in eachpart. If you fail to obtain the required amount of points in either part, the exam will be graded as failed.

    Use a blue or black pen, do not use pencils, whiteouts or similar products.

    No utilities are allowed. Use only the provided sheets of paper.

    Write your name and matriculation number onto all sheets used for your solution before you start working on theexam. We will not grade sheets without those details!

    Write your solution onto the free space below each exercise or on the back of the respective sheet. Ask for additionalsheets if the space should not be sufficient!

    Do not seperate the stapled sheets.

    To exclude an exercise from grading, clearly cross out the entire page.

    Write readable! Unclear parts of your exam will not be rated.

    Do not give multiple or ambigous answers. If this is the case, only your last answer will be evaluated.

    Check if you received all 21 pages and 9 exercises!

    1 2 3 4 5 6 7 8 9

    ..................................Erstkorrektur

    ..................................Zweitkorrektur

  • Part I

    Practical Course

    42 Points, approx. 40 Minutes13 Points needed to pass

  • Name: Matriculation number:

    1.) Mesh Transformation and Rendering (24 Points)In this exercise, a number of meshes should be transformed and rendered with the Direct3D 11 API. Ineach subtask, a part of this procedure should be implemented. All given information is valid throughoutall following subtasks, however all subtasks can be solved independently.

    1.1 Transformations 8 Points

    The object meshes are layed out wrong in object space as the example depicted in the left image infigure 1 below. They should be transformed into the desired object space coordinate system given bythe right image.

    2 -2

    2

    -2

    x

    x

    y

    z

    -1 -1

    1

    1

    3 -3

    2 -2

    2

    -2

    -1 -1

    1

    1

    3 -3

    Wrong Object Space

    x

    x

    y

    z

    2 -2

    2

    -2

    -1

    1

    3 -3

    2

    -2

    1 3 -3 2 -2

    -1

    1

    Object Space

    View along

    +Z

    View along

    -Y

    Figure 1: Transformation

    Additionally, after object space correction for the world transformation each object should be rotatedaround the y axis and translated by a given vector. It also should be transformed into view space usingthe given matrix g view. Fill out the C++ function CorrectAndTransform() that receives the translationcomponents x, y, z and a rotation angle around the y axis in radians rotY. It should return a matrixwhich performs these transformations.

    Page 4 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    The following functions from the Direct3D 11 API are available:

    D3DXMATRIX* D3DXMatrixScaling (D3DXMATRIX *pOut , float sx , float sy , float sz);

    D3DXMATRIX* D3DXMatrixTranslation(D3DXMATRIX *pOut , float x, float y, float z);

    D3DXMATRIX* D3DXMatrixRotationX (D3DXMATRIX *pOut , float Angle);// Angle in radians

    D3DXMATRIX* D3DXMatrixRotationY (D3DXMATRIX *pOut , float Angle);// Angle in radians

    D3DXMATRIX* D3DXMatrixRotationZ (D3DXMATRIX *pOut , float Angle);// Angle in radians

    D3DXMATRIX* D3DXMatrixTranspose (D3DXMATRIX *pOut , const D3DXMATRIX *pM);

    D3DXMATRIX* D3DXMatrixInverse (D3DXMATRIX *pOut , float *pDeterminant ,

    const D3DXMATRIX *pM);

    D3DXMATRIX g_view; // Assume this is already set correctly!

    D3DXMATRIX CorrectAndTransform(float x, float y, float z, float rotY)

    {

    // Your code here:

    D3DXMATRIX matTransLocal;

    D3DXMatrixTranslation (& matTransLocal , 2, -1.5, 1.5);

    D3DXMATRIX matScaleLocal;

    D3DXMatrixScaling (& matScaleLocal , -2, 2, 2);

    // 1 Point per "correction component" (translation , scaling , flip X) = 3P

    D3DXMATRIX matTrans;

    D3DXMatrixTranslation (&matTrans , x, y, z);

    D3DXMATRIX matRot;

    D3DXMatrixRotationY (&matRotY , rotY);

    // 1 Point for correct world transformation (translation + rotation)

    return matTransLocal * matScaleLocal * matRot * matTrans * g_view; // 2P order

    // 2 P for correct usage: 1P no "nullptr" as parameter , 1P correct

    // usage of pointers

    // Alternative solutions: Additional rotation

    D3DXMATRIX matRotLocal;

    D3DXMatrixRotationY (& matRotLocal , M_PI);

    // ...

    return matTransLocal * matScaleLocal * matRotLocal * matRot *

    matTrans * g_view;

    }

    Page 5 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    1.2 std::vector 4 Points

    Given is the following struct that defines an object state and a vector of ObjectState instances:

    struct ObjectState

    {

    // Position

    float x;

    float y;

    float z;

    // Rotation

    float rotY;

    // Output matrix

    D3DXMATRIX transform;

    D3DXMATRIX transformNormals;

    };

    std::vector g_objects;

    Assume that g objects is already filled with several elements.

    Fill out the function body below so that it iterates over the vector, calls CorrectAndTransform() withthe according parameters for each element and stores the result in the member variable transform.

    In addition, transformNormals should be set to the matrix that transforms the objects normals toview space without any assumptions about the performed transformations.

    void TransformAllObjects ()

    {

    // Your code here:

    for (auto it = g_objects.begin (); it != g_objects.end (); ++it) // 1P iteration

    {

    // 1P correct call and assignment

    it ->transform = CorrectAndTransform(it ->x, it ->y, it ->z, it ->rotY);

    D3DXMATRIX inv;

    D3DXMatrixInverse (&inv , nullptr , &it->transform ); // 1P Inverse correct

    // 1P transpose & assignment

    D3DXMatrixTranspose (&it ->transformNormals , &inv);

    // Note: Inverse / Transpose order is arbitrary

    }

    // ... and some possible alternative iterations ...

    for (std::vector :: iterator i = ...) { ... }

    for (auto& obj: g_objects) { obj.transform = ... }

    std:: for_each(g_objects.begin(), g_objects.end(), []( ObjectState& obj)

    { obj.transform = ... });

    }

    Page 6 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    1.3 Vertex Shader 5 Points

    Each of the objects is rendered with the following input layout bound:

    const D3D11_INPUT_ELEMENT_DESC layout [] =

    {

    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT , 0,

    D3D11_APPEND_ALIGNED_ELEMENT , D3D11_INPUT_PER_VERTEX_DATA , 0 },

    { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT , 0,

    D3D11_APPEND_ALIGNED_ELEMENT , D3D11_INPUT_PER_VERTEX_DATA , 0 },

    { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT , 0,

    D3D11_APPEND_ALIGNED_ELEMENT , D3D11_INPUT_PER_VERTEX_DATA , 0 }

    };

    Complete the given input struct VSIn and write a vertex shader that receives vertices of this type asan input. It should transform the vertex positions to clip space and the normals to view space andoutput them to be used in a pixel shader. All other attributes should be passed through without anymodifications.

    cbuffer MyFirstConstantBuffer

    {

    // Contains a matrix that transforms normals from object space to view space

    matrix g_WorldViewNormals;

    // Contains a matrix that transforms positions from object space to clip space

    matrix g_WorldViewProj;

    };

    struct PSIn {

    float4 pos : SV_POSITION; // Clip space position

    float2 uv : TEXCOORD; // Texture coordinate

    float3 normal : NORMAL; // Normal in worldview space

    };

    struct VSIn {

    // Your code here:

    float3 position: POSITION;

    float2 uv: TEXCOORD;

    float3 normal: NORMAL;

    };

    // 1P correct

    // Your code here:

    void VSOfDoom(in VSIn input , out PSIn output) // 1P

    {

    output.pos = mul(float4(input.position.xyz , 1), g_WorldViewProj ); // 1P

    output.uv = input.uv; // 1P

    output.normal = normalize(mul(float4(input.normal.xyz , 0),

    g_WorldViewNormals )); // 1P

    }

    Page 7 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    1.4 Pixel Shader 7 Points

    Fill out the given empty pixel shader such that the texture g diffuse is sampled at the fragmentstexture coordinate using the given sampler state. On the resulting color, calculate simple N dot Llighting and return the result. Also answer the following question:

    Which kind of shading is implemented here? Your Answer: Phong (1P)

    cbuffer MySecondConstantBuffer

    {

    float4 g_LightDirection; // Light direction in view space

    float4 g_LightColor; // Light color

    };

    SamplerState sampler

    {

    Filter = ANISOTROPIC;

    AddressU = Wrap;

    AddressV = Wrap;

    };

    Texture2D g_diffuse;

    float4 PS(in PSIn frag): SV_Target0

    {

    // Your code here:

    // 1P 1P Parameter

    float4 color = g_diffuse.Sample(sampler , frag.uv);

    // 1P Aufruf , 1P Parameter

    float l = dot(normalize(frag.normal), g_LightDirection.xyz);

    // 1P korrekte Rueckgabe , 1P lightColor

    return color * g_LightColor * l;

    }

    Page 8 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    2.) Model Rendering (9 Points)The given vertex buffer and index buffer together define a 2Dmesh with D3D11 PRIMITIVE TOPOLOGY TRIANGLELIST. Thevertex buffer contains in consecutive order the x,y position andthe u,v texture coordinates for each vertex.

    In the grid given in figure 2

    (a) According to the vertex buffer, mark the vertex positionswith a cross.

    (b) According to the index buffer, draw the triangles usinglines.

    (c) In the image to the right, a texture is depicted in tex-ture space. According to the texture coordinates (usingbarycentric interpolation), draw the mapping of this tex-ture onto the rendered triangles into figure 2.

    Texture

    u

    v

    0

    1

    1

    0,25

    0,75

    y

    x 0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    0 1 3 0 3 2 2 3 4 3 5 4 Index Buffer

    Vertex Buffer 10 12 0 0.5 3 12 0 0

    9 7 1 1

    5 7 1 0

    10 2 0 1

    0 11

    12 23

    0 0 2 2

    0 1

    2 3

    4 5

    Figure 2: Vertices 4 points (-1 per incorrect vertex), triangles 3 points (-1 per incorrect triangle), texture2 points

    Page 9 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    3.) Shaders in HLSL (9 Points)Below, four different vertex/pixel shaders are given in HLSL syntax:

    cbuffer MyCBuffer {

    matrix g_WorldViewProj; // World -View -Projection matrix

    float3 g_EyePos; // Position of the camera in world space

    float3 g_LightDir; // Light direction in world space

    };

    struct StructA {

    float3 pos : POSITION;

    float3 nor : NORMAL;

    };

    struct StructB {

    float4 svPos : SV_Position;

    float3 pos : POSITION;

    float3 nor : NORMAL;

    };

    float3 Phong(float3 normal , float3 light , float3 viewer) {...}

    float3 Gray(float3 rgb) { return (float3 )(rgb.r + rgb.g + rgb.b) / 3.0; }

    void Shader1(in StructA i, out StructB o)

    {

    o.svPos = mul(float4(i.pos , 1.0), g_WorldViewProj );

    o.pos = i.pos;

    o.nor = i.nor;

    }

    float4 Shader2(in StructB i) : SV_Target

    {

    return float4(Gray(i.nor), 1.0);

    }

    float4 Shader3(in StructB i) : SV_Target

    {

    i.nor = normalize(i.pos);

    i.nor = Phong(i.nor , g_LightDir , normalize(g_EyePos - i.pos));

    return float4(Gray(i.nor), 1.0);

    }

    void Shader4(in StructA i, out StructB o)

    {

    o.svPos = mul(float4(i.pos , 1.0), g_WorldViewProj );

    o.pos = i.pos;

    o.nor = Phong(i.nor , g_LightDir , normalize(g_EyePos - i.pos));

    }

    In the code above, Phong() is a function which evaluates the Phong lighting model (in a not furtherspecified way) and returns the resulting RGB color (assuming white light and a white object).

    Page 10 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    Three different combinations of these shaders were employed to render a sphere mesh centered at theorigin with per-vertex normals and light coming from the front (with respect to the following images).The global variables g * were set to appropriate values (see comments in the code) which were constantthroughout the experiment.

    Write below each of the images in figure 3 the name (Shader1, Shader2,...) of the vertex shader (VS)and pixel shader (PS) that was used in the rendering of the image.

    VS: Shader4 VS: Shader1 VS: Shader1

    PS: Shader2 PS: Shader2 PS: Shader3

    Figure 3: Rendered images

    3 Points per correct combination

    Page 11 of 21 Game Engine Design Exams SS 2013

  • Part II

    Lecture

    64 Points, approx. 50 Minutes20 Points needed to pass

  • Name: Matriculation number:

    4.) Rasterization (10 Points)

    (a) Name 2 operations which are performed on the incoming vertex coordinates in the rasterizationstage. 2 Points

    e.g. Homogeneous division, viewport transformation

    (b) Does the rasterizer need to know the resolution of the currently selected viewport? 2 Points

    Yes

    (c) Name 3 parameters on which the number of fragments depends which are generated by therasterizer for a triangle. 2 Points

    e.g. size, orientation, viewport resolution, distance

    (d) The triangle in figure 4 subdivides the 2D plane in 7 regions as illustrated. Give the signs ofthe barycentric coordinates with respect to triangle ABC for all points in the 7 regions. It issufficient to specify a value {,+}3 for each region. For instance, (+,+,+) if all points in theregion have positive barycentric coordinates. 4 Points

    Figure 4: -1P for each incorrectly labeled region

    Page 14 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    5.) Texture Mapping (10 Points)

    (a) At which stage in the rasterization-based rendering pipeline is a texture value assigned to a surfacepoint? 2 Points

    Pixel or fragment stage / shader

    (b) In Figure 5, a 2D texture map consisting of white (white circles) and black (black circles) texturevalues is illustrated. For this texture, compute the texture values using bilinear interpolation atthe texture coordinates (0.5, 0.5) and (48 14 18 , 48 14 18). 4 PointsSolution: 0.5 and 616 = 0.375

    Figure 5: Texture

    (c) For the texture in Figure 5, compute the texture value using bilinear interpolation at the texturecoordinate (0.4, 0.4) in the mipmap level l = 1, i.e. the mipmap level succeeding the finestresolution level. 4 Points

    Solution: 0.5

    Page 15 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    6.) Color Blending (8 Points)What color is seen when looking along a ray which first hits an object with color (0.5, 0, 0, 0.5), then anobject with color (1, 1, 1, 0), and then an object with color (0.25, 0, 0, 0.5)? Assume a black background(color (0, 0, 0, 1)). Here, the first three color components specify the Red, Green, and Blue components,respectively, and the fourth component specifies the opacity. Opacity ranges from 0 to 1, where 0indicates full transparency and 1 indicates full opacity. Use -blending to compute the color.

    R = 12 12 + 12 0 1 + 12 12 14 = 14 + 116 = 516 = 0.3125G = B = 0(A = 1), not necessary

    Page 16 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    7.) Shading and Lighting (16 Points)

    (a) Given is a perfectly specular reflecting plane containing the origin (0, 0, 0)T in 3D space. Theplane is described by the set of all points (x, y, z) T such that xy

    z

    21

    0

    = 0A point light source is positioned at (10, 0, 0) T . Compute the direction of the reflected lightat the point in the plane at position (0, 0, 0)T . Note: Answers without a derivation will not begraded. Arithmetic expressions like square roots do not have to be evaluated. 4 Points

    Reflection normal: n = (2,1,0)T

    ||(2,1,0)T || = (25, 1

    5, 0)T .

    Reflection formula: v = i 2n(i n)Light direction: i = (1, 0, 0)T

    Result: v = (35 ,45 , 0)

    T

    (b) Given is the plane y = 0. A point light source is positioned at (1, 10, 1). The plane reflects onlydiffuse. The viewer is positioned at (4, 6, 4) and sees all points on the plane with 0 x 8 and0 z 8. At which point on the plane does the viewer perceive the highest reflection? 4 PointsSolution: (1, 0, 1)

    (c) Describe the visual difference between shading a triangle using a) Gouraud shading and b) Phongshading in the situation where a specular highlight lies entirely in the interior of the triangle.

    2 Points

    Gouraud: highlight can be missed if no vertex is in light cone. Phong: highlight will bevisible in the interior of the triangle.

    Page 17 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    (d) Given is a scene as illustrated in Figure 6. Let p be a point on a specular reflecting materialsurface. Assume L (the vector from p to the light source), N (the normal at p), and V (thevector from p to the viewer) are all in the same plane. Draw into this illustration the angle which is used in the Phong illumination model to compute the specular reflection. 6 Points

    Figure 6: 2P correct R, 4P correct angle

    Page 18 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    8.) Collision Testing (10 Points)Given is a 2D scene consisting of 4 triangles (grey triangles in Figure 7). The circles indicate a corre-sponding bounding circle hierarchy (the hierarchy is depicted on the right of Figure 7). For another setof 3 triangles (black triangles in Figure 7) the bounding circle is shown in bold. The numbers in theFigure indicate the respective spheres.

    (a) How many circle-circle tests and how many triangle-triangle tests have to be performed to testfor collisions between the grey and black triangles in the situation shown in Figure 7? Note: Thetest has to make use of the bounding spheres representation. Write down all circle-circle tests aspairs (i, j), where i and j are the numbers indicating the circles. 6 Points

    Figure 7: Collision Testing

    circle-circle: 7 Tests: (8,1),(8,2),(8,3),(8,4),(8,5),(8,6),(8,7)triangle-triangle: 3 tests - (8,4)

    (b) How many triangle-triangle tests have to be performed when active intervals of the circles indexed3,4,6,7,8 along the horizontal and vertical axis of the 2D domain are used? 4 Points

    (8,6) 3 tests, (8,4) 3 tests - 6 tests

    Page 19 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    9.) Transformations (10 Points)

    (a) Write down a 33 matrix which realizes the following transformation: A 2D perspective projectionin the x/y-plane onto the line y = 0. The center of projection is at (0,6). After the projectionthe homogeneous component should be such that the homogeneous division generates the correctvalues. 4 Points

    1 0 00 0 00 16 1

    In the following exercises you are supposed to specify the coefficients of 4 4 transformation matricesfor the representation of affine mappings in R3. The coefficients should be given as precise as possible,i.e., numbers should be given if possible and identifiers else. The same coefficients should be given thesame identifier. If a matrix can be specified by concatenation of multiple matrices, the concatenationdoes not have to be computed explicitly.

    Example: isotropic scaling

    s 0 0 00 s 0 00 0 s 00 0 0 1

    (b) A rotation so that the vector (1, 0, 0) is aligned with the vector (1, 1, 0), the vector (0, 1, 0) is

    aligned with the vector (1, 1, 0), and the vector (0, 0, 1) is aligned with the vector (0, 0, 1).4 Points

    1 1 0 01 1 0 00 0 1 00 0 0 1

    Columns 1 and 2 need to be normalized ( 1

    2)

    (c) Mirroring at the point (4, 0, 0) 2 Points1 0 0 40 1 0 00 0 1 00 0 0 1

    1 0 0 00 1 0 00 0 1 00 0 0 1

    1 0 0 40 1 0 00 0 1 00 0 0 1

    =1 0 0 80 1 0 00 0 1 00 0 0 1

    Page 20 of 21 Game Engine Design Exams SS 2013

  • Name: Matriculation number:

    Page 21 of 21 Game Engine Design Exams SS 2013

    Transformations8 Pointsstd::vector4 PointsVertex Shader5 PointsPixel Shader7 Points