[SOLVED] CSE4020 - Computer Graphics - Lab Assignment 4

30.00 $

Category:

Description

5/5 - (2 votes)

Write down a Python program to draw a transformed triangle in a 2D space.

  1. Set the window title and the window size to (480,480).
  2. Complete the render() function below to draw a triangle in the manner described in C.
  3. You have to use OpenGL transformation functions. Do not use numpy matrix multiplication for composing transformations.
def render():
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()

# draw cooridnates     glBegin(GL_LINES)     glColor3ub(255, 0, 0)     glVertex2fv(np.array([0.,0.]))     glVertex2fv(np.array([1.,0.]))     glColor3ub(0, 255, 0)     glVertex2fv(np.array([0.,0.]))     glVertex2fv(np.array([0.,1.]))     glEnd()

 

glColor3ub(255, 255, 255)

 

    ###########################
    # implement here
    ###########################

drawTriangle()

 

def drawTriangle():
    glBegin(GL_TRIANGLES)
    glVertex2fv(np.array([0.,.5]))     glVertex2fv(np.array([0.,0.]))     glVertex2fv(np.array([.5,0.]))
    glEnd()
  1. If you press or repeat a key, the triangle should be transformed as shown in the Table:

Key Transformation

Q      Translate by -0.1 in  x direction

E      Translate by 0.1 in x direction

A      Rotate by 10 degrees counterclockwise

D      Rotate by 10 degrees clockwise

1      Reset the triangle with identity matrix

  1. 7UDQVIRUPDWLRQVVKRXOGEHDFFXPXODWHGFRPSRVHGZLWKSUHYLRXVRQHXQOHVV\RXSUHVVěĜ
  2. You may need a global variable (like a python list object) to store key inputs.

Write down a Python program to draw rotating point p1=(0.5, 0), p2=(0, 0.5) and vector v1=(0.5, 0), v2=(0, 0.5) in a 2D space.

  1. Set the window title and the window size to (480,480).
  2. Use the following render() and fill “# your implementation” parts to render p1,p2 and v1,v2.
    1. Hint: Render the vector v1, v2 as a line segment starting from the origin (0,0).
    2. Hint2: You need different translation matrix for p1 and p2 to render them correctly.
 

def render(th):
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()
    # draw cooridnate     glBegin(GL_LINES)  

 

    glColor3ub(255, 0, 0)
    glVertex2fv(np.array([0.,0.]))     glVertex2fv(np.array([1.,0.]))
    glColor3ub(0, 255, 0)
    glVertex2fv(np.array([0.,0.]))     glVertex2fv(np.array([0.,1.]))
    glEnd()  
     
glColor3ub(255, 255, 255)  
    # calculate matrix M1, M2 using th  
# your implementation  

 

    # draw point p
    glBegin(GL_POINTS)
    # your implementation
    glEnd()
    # draw vector v
    glBegin(GL_LINES)
# your implementation
    glEnd()
  1. Expected result: Uploaded LabAssignment4-2.mp4
  2. Do not mind the initial angle.
  3. p1,p2 and v1,v2 should be -t rad rotated when t seconds have elapsed since the program was executed.
  4. You need to somehow combine a rotation matrix and a translation matrix to produce the expected result

Extension should be .py)