I want to make a openGL project using python. And the below code is creating the window to display the graphic.
import os
import glfw
from OpenGL.GL import *
class renderwindow():
'''GLFW Renderting window class'''
def __init__(self):
#save current working directory
cwd = os.getcwd()
#initialize glfw
glfw.glfwInit()
#restore cws
os.chdir(cwd)
#version hints
glfw.glfwWindowHint()
glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MAJOR, 3)
glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MINOR, 3)
glfw.glfwWindowHint(glfw.GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
glfw.glfwWindowHint(glfw.GLFW_OPENGL_PROFILE,
glfw.GLFW_OPENGL_CORE_PROFILE)
# make a window
self.width, self.height = 640, 480
self.aspect = self.width / float(self.height)
self.win = glfw.glfwCreateWindow(self.width, self.height,
b'simpleglfw')
# make the context current
glfw.glfwMakeContextCurrent(self.win)
def main(self):
glViewport(0, 0, self.width, self.height)
glEnable(GL_DEPTH_TEST)
glClearColor(0.5, 0.5, 0.5, 1.0)
When I run the code, it report error like this:
Traceback (most recent call last):
File "/snap/pycharm-community/192/plugins/python-ce/helpers/pydev/pydevd.py", line 1438, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "/snap/pycharm-community/192/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/surface/Final-Year-Project/FYP/Main.py", line 6, in <module>
class main():
File "/home/surface/Final-Year-Project/FYP/Main.py", line 29, in main
rw = renderwindow()
File "/home/surface/Final-Year-Project/FYP/Open_GL_project1/RenderWIndow.py", line 16, in __init__
glfw.glfwInit()
AttributeError: module 'glfw' has no attribute 'glfwInit'
Process finished with exit code 1
I search the solution in the internet and some people say problem caused by old version of GLFW . The pycharm mismatch the GLFW. I don't know how to solve it
The name of the method is not glfwInit
, it is init
. This applies to the other methods, too. (window_hint
, create_window
, make_context_current
):
import os
import glfw
from OpenGL.GL import *
class renderwindow():
'''GLFW Renderting window class'''
def __init__(self):
#save current working directory
cwd = os.getcwd()
#initialize glfw
glfw.init()
#restore cws
os.chdir(cwd)
#version hints
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
glfw.window_hint(glfw.OPENGL_PROFILE,
glfw.OPENGL_CORE_PROFILE)
# make a window
self.width, self.height = 640, 480
self.aspect = self.width / float(self.height)
self.win = glfw.create_window(self.width, self.height,
'simpleglfw', None, None)
# make the context current
glfw.make_context_current(self.win)
def main(self):
glViewport(0, 0, self.width, self.height)
glEnable(GL_DEPTH_TEST)
glClearColor(0.5, 0.5, 0.5, 1.0)
Alternatively import glfw.GLFW as glfw
:
import os
#import glfw
import glfw.GLFW as glfw
from OpenGL.GL import *
class renderwindow():
'''GLFW Renderting window class'''
def __init__(self):
#save current working directory
cwd = os.getcwd()
#initialize glfw
glfw.glfwInit()
#restore cws
os.chdir(cwd)
#version hints
glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MAJOR, 3)
glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MINOR, 3)
glfw.glfwWindowHint(glfw.GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
glfw.glfwWindowHint(glfw.GLFW_OPENGL_PROFILE,
glfw.GLFW_OPENGL_CORE_PROFILE)
# make a window
self.width, self.height = 640, 480
self.aspect = self.width / float(self.height)
self.win = glfw.glfwCreateWindow(self.width, self.height,
'simpleglfw', None, None)
# make the context current
glfw.glfwMakeContextCurrent(self.win)
def main(self):
glViewport(0, 0, self.width, self.height)
glEnable(GL_DEPTH_TEST)
glClearColor(0.5, 0.5, 0.5, 1.0)
wnd = renderwindow()