XGB
XGB

Reputation: 367

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

Write a PYOpenGL code for this operation.

I am able to draw the circle but my code for rotating and translating is not working. The code is executing but not giving the correct result. Help me with the rotation and translation part of the question.

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()
    
  #  glMatrixMode(GL_MODELVIEW)
  #  glLoadIdentity()
  #  glTranslatef(0, 0, -3)
  #  glRotatef(50, 1, 0, 0)
  #  glRotatef(70, 0, 1, 0)
    
    glOrtho(0.0, 500, 0.0, 500, 0.0, 1.0)
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity()

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()

Upvotes: 0

Views: 135

Answers (1)

Rabbid76
Rabbid76

Reputation: 210978

When you transalte the circle along the z axis or rotate the circle it is clipped by the near (= 0) and far plane (= 1) of the Orthographic projection.

Change the distance to the near and far plane (e.g. -100 and 100):

glOrtho(0.0, 500, 0.0, 500, 0.0, 1.0)

glOrtho(0.0, 500, 0.0, 500, -100, 100)

iterate function:

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)

Upvotes: 1

Related Questions