I am trying to draw a square with opengl using tkinter frame but the square didn't render.
Here is my code:
import tkinter as tk
from opengl.gl import *
from pyopengltk import OpenGLFrame
class frame(OpenGLFrame):
def initgl(self):
glViewport(0.0,self.width,self.height)
glClearColor(0.0,1.0,0.0,0.0)
def redraw(self):
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER)
glLoadIdentity()
glBegin(GL_LINES)
glColor3f(1.0,0.0,3.0)
glVertex2f(200,100)
glVertex2f(100,100)
glEnd()
gl_Flush()
if __name__=='__main__':
root = tk.Tk()
app = frame(root,width=500,height=500)
app.pack(fill=tk.BOTH, expand=tk.YES)
app.mainloop()
I got no error expect the green screen without no lines drawn.
This is the image of the codes result it shows no error but the lines didn't show:
The line does not render, because you don't setup an Orthographic projection. If you don't set an orthographic projection, then you have to specify the coordinates in normalized device space. NDC are a unique cube, with the left, bottom, near of (-1, -1, -1) and the right, top, far of (1, 1, 1).
Specify an orthographic GL_PROJECTION
matrix (see glMatrixMode
) by glOrtho
, that maps the window coordinates to NDC:
class frame(OpenGLFrame):
def initgl(self):
glViewport(0, 0, self.width, self.height)
glClearColor(0.0,1.0,0.0,0.0)
# setup projection matrix
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, self.width, self.height, 0, -1, 1)
# setup identity model view matrix
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def redraw(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glBegin(GL_LINES)
glColor3f(1.0,0.0,3.0)
glVertex2f(200,100)
glVertex2f(100,100)
glEnd()
glFlush()