1

Am trying to render cube with tkinter frame opengl. But I don't know where the problem lies the cube didn't show expect 2 lines. Check my code Pls can you help me write the code and do you have any PDF to teach me opengl I can't find much resources online

import tkinter as tk

from OpenGL.GL import *

from pyopengltk import 
OpenGLFrame

cubeVertices = 
((1,1,1),(1,1,-1), 
(1,-1,-1),(1,-1,1),. 
(-1,1,1),(-1,-1,-1), 
(-1,-1,1),(-1,1,-1))
cubeEdges = ((0,1),. 
(0,3),(0,4),(1,2),. 
(1,7),(2,5),(2,3),. 
(3,6),(4,6),(4,7),. 
(5,6),(5,7))
classframe(OpenGLFrame):

  def initgl(self):
        glViewport(0,0,250,250)
        

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(0,self.width,self.height,0,-1,1)

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
  
  def redraw(self):
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()
        glPushMatrix()
        glRotatef(90,0.0,1.0,0.0)
        glBegin(GL_LINES)
        glColor3f(0.0,1.0,0.0)
        for cubeEdge in cubeEdges:
     for cubeVertex in cubeEdge:
               glVertex3fv(cubeVertices[cubeVertex])
        glEnd()
        glPopMatrix()            
root = tk.Tk()
app = frame(root,width=900, height=600)  
  app.pack( 
fill=tk.BOTH,expand=tk.YES)
app.mainloop()

enter image description here

stovfl
  • 14,998
  • 7
  • 24
  • 51

1 Answers1

1

You have to change the projection matrix. Since the cube has a dimension of 2x2x2 and the projection is an orthographic projection in window space, the cube will cover just 4 pixels in the window. Change the view space and increase the distance to the near and far plane. Note, the geometry has to be in between the near and far plane, else the geometry will be clipped. For instance:

glOrtho(-10, 10, -10, 10, -10, 10)

Anyway I recommend to use Perspective projection. The projection matrix defines a 3 dimensional space (clip space) which is projected on the 2 dimensional viewport. At Perspective projection, this space is a frustum (Viewing frustum). The matrix can be set by gluPerspective. For instance:

classframe(OpenGLFrame):

  def initgl(self):
        glViewport(0,0,250,250)
        

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        # glOrtho(-10, 10, -10, 10, -10, 10)
        gluPerspective(90, self.width/self.height, 0.1, 10.0)

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

The value for the near plane and the far plane have to be greater than 0.0 and the far plane has to be greater then the near plane 0 < near < far. In the above example the near plane is 0.1 and the far plane 10. When you draw the geometry, the you have to ensure, that the geometry is in between then near and the far plane (in clip space respectively in the viewing volume), else the geometry is clipped.

Use gluLookAt to define a view matrix with a point of view (0, -3, 0) that has a certain distance to the origin of the world (0, 0, 0):

classframe(OpenGLFrame):
    # [...]

    def redraw(self):
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        
        # define view matrix
        glLoadIdentity()
        gluLookAt(0, -3, 0, 0, 0, 0, 0, 0, 1)
        
        glPushMatrix()
        glRotatef(90,0.0,1.0,0.0)
        glBegin(GL_LINES)
        glColor3f(0.0,1.0,0.0)
        for cubeEdge in cubeEdges:
            for cubeVertex in cubeEdge:
               glVertex3fv(cubeVertices[cubeVertex])
        glEnd()
        glPopMatrix() 

What exactly are eye space coordinates?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174