Search code examples
pythonopenglpyopengl

Consider a circle (𝑥 − 40) 2 + (𝑦 − 40) 2 = 400. Rotate it along X- axis counter clockwise 30 degree and translate it along Z- axis for +20 units


I am able to draw the circle and translate and rotate it with the help of @Rabbid76 from stack overflow.

But need help with these two part of the question:

Q1 - Take orthographic projection of this circle on xy plane.

Q2- Consider 3 axes (100, 0, 0), (0, 100, 0), (0, 0, 100), up vector as Z- axis (of original circle) and position of camera at origin. Look at point is at center of the rotated translated circle. Take perceptive projection of translated circle points on the plane made by these axes. Also write code to get perceptive projection if camera rotates along Z-axis.

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from math import * 

def circle():
    posx, posy = 40,40
    sides = 80    
    radius = 20   
    glBegin(GL_POLYGON)    
    for i in range(100):    
        cosine= radius * cos(i*2*pi/sides) + posx    
        sine  = radius * sin(i*2*pi/sides) + posy    
        glVertex2f(cosine,sine)   
        
    glEnd()      

def iterate():
    glViewport(0, 0, 3000, 3000)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0.0, 500, 0.0, 500, -100, 100)
    
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity()
    glTranslatef(0, 0, 20)
    glRotatef(30, 1, 0, 0)

def showScreen():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    iterate()
    glColor3f(1.0, 0.0, 3.0)
    circle()
    glutSwapBuffers()
       
glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(400, 400)
glutInitWindowPosition(200, 200)
wind = glutCreateWindow("OpenGL Coding Practice")
glutDisplayFunc(showScreen)
glutIdleFunc(showScreen)
glutMainLoop()


Solution

  • Use gluLookAt to define the view matrix and gluPerspective To define the Perspective projection matrix.

    The perspective projection defines a Viewing frustum. The geometry is clipped by the near and far plane of the frustum.

    ensure that the geometry is between the near and far plane. The center of the circle is at (0, 0, 0). Use a camera position of (0, 100, 0), look at the center of the circle. The you vector is (0, 0, 1):

    def circle():
        posx, posy = 0, 0
        sides = 80    
        radius = 20   
        glBegin(GL_POLYGON)    
        for i in range(100):    
            cosine= radius * cos(i*2*pi/sides) + posx    
            sine  = radius * sin(i*2*pi/sides) + posy    
            glVertex3f(cosine,0, sine)   
            
        glEnd()      
    
    angle = 30
    def iterate():
        global angle
    
        glViewport(0, 0, 400, 400)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        #glOrtho(0.0, 500, 0.0, 500, -100, 100)
        gluPerspective(60, 1, 0.1, 500)
        
        glMatrixMode (GL_MODELVIEW)
        glLoadIdentity()
        gluLookAt(0, 100, 0, 0, 0, 0, 0, 0, 1)
        glRotatef(angle, 0, 0, 1)
        angle += 0.1