I want to create a cornell box scene. I wrote all the necessary code in Python and installed all the necessary libraries. I try to run the code in Pycharm, but the error appears:
OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling
Code:
from OpenGL.GL import *
from OpenGL.GLUT import *
lamp_pos = (0, 99.9, -350)
light_range = 15
def draw_lamp(x, y, z, side_len):
glBegin(GL_QUADS)
glVertex3f(x - side_len / 2, y, z - side_len / 2)
glVertex3f(x + side_len / 2, y, z - side_len / 2)
glVertex3f(x + side_len / 2, y, z + side_len / 2)
glVertex3f(x - side_len / 2, y, z + side_len / 2)
glEnd()
def show_lamp():
glColor3f(1, 1, 1)
glNormal3f(0, -1, 0)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, [1, 1, 1, 1])
draw_lamp(lamp_pos[0], lamp_pos[2] + 0.01, lamp_pos[2], 40)
glDisable(GL_LIGHT0)
def lighting():
pos0 = [lamp_pos[0] - light_range, lamp_pos[2], lamp_pos[2], 1]
pos1 = [lamp_pos[0] + light_range, lamp_pos[2], lamp_pos[2], 1]
pos2 = [lamp_pos[0], lamp_pos[2], lamp_pos[2] + light_range, 1]
pos3 = [lamp_pos[0], lamp_pos[2], lamp_pos[2] - light_range, 1]
glShadeModel(GL_SMOOTH)
glEnable(GL_LIGHTING)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0, 0, 0, 1])
glEnable(GL_LIGHT0)
glEnable(GL_LIGHT1)
glEnable(GL_LIGHT2)
glEnable(GL_LIGHT3)
glLightfv(GL_LIGHT0, GL_AMBIENT, [0.1, 0.1, 0.1, 0])
glLightfv(GL_LIGHT1, GL_AMBIENT, [0.1, 0.1, 0.1, 0])
glLightfv(GL_LIGHT2, GL_AMBIENT, [0.1, 0.1, 0.1, 0])
glLightfv(GL_LIGHT3, GL_AMBIENT, [0.1, 0.1, 0.1, 0])
glLightfv(GL_LIGHT0, GL_DIFFUSE, [0.4, 0.4, 0.4, 1])
glLightfv(GL_LIGHT1, GL_DIFFUSE, [0.4, 0.4, 0.4, 1])
glLightfv(GL_LIGHT2, GL_DIFFUSE, [0.4, 0.4, 0.4, 1])
glLightfv(GL_LIGHT3, GL_DIFFUSE, [0.4, 0.4, 0.4, 1])
glLightfv(GL_LIGHT0, GL_POSITION, pos0)
glLightfv(GL_LIGHT1, GL_POSITION, pos1)
glLightfv(GL_LIGHT2, GL_POSITION, pos2)
glLightfv(GL_LIGHT3, GL_POSITION, pos3)
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.001)
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.001)
glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.001)
glLightf(GL_LIGHT3, GL_LINEAR_ATTENUATION, 0.001)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
def draw_box():
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
show_lamp()
# Floor
glBegin(GL_QUADS)
glColor3f(112 / 255, 149 / 255, 225 / 255)
glNormal3f(0, 1, 0)
for i in range(0, 300):
for j in range(0, 300):
glVertex3f(-100 + j, -100, -300 - i)
glVertex3f(-100 + j + 1, -100, -300 - i)
glVertex3f(-100 + j + 1, -100, -300 - i - 1)
glVertex3f(-100 + j, -100, -300 - i - 1)
glEnd()
# Left wall
glColor3f(116 / 255, 255 / 255, 225 / 255)
glBegin(GL_QUADS)
glNormal3f(1, 0, 0)
for i in range(0, 300):
for j in range(0, 300):
glVertex3f(-100, -100 + i, -300 - j)
glVertex3f(-100, -100 + i, -300 - j - 1)
glVertex3f(-100, -100 + i + 1, -300 - j - 1)
glVertex3f(-100, -100 + i + 1, -300 - j)
glEnd()
# Right wall
glColor3f(116 / 255, 255 / 255, 225 / 255)
glBegin(GL_QUADS)
glNormal3f(-1, 0, 0)
for i in range(0, 300):
for j in range(0, 300):
glVertex3f(100, -100 + j, -300 - i)
glVertex3f(100, -100 + j + 1, -300 - i)
glVertex3f(100, -100 + j + 1, -300 - i - 1)
glVertex3f(100, -100 + j, -300 - i - 1)
glEnd()
# Back wall
glColor3f(234 / 255, 232 / 255, 116 / 255)
glBegin(GL_QUADS)
glNormal3f(0, 0, 1)
for i in range(0, 300):
for j in range(0, 300):
glVertex3f(-100, -100, -500)
glVertex3f(-100 + j, -100, -500)
glVertex3f(-100 + j, -100 + i, -500)
glVertex3f(-100, -100 + i, -500)
glEnd()
# Ceil
glColor3f(1, 1, 1)
glBegin(GL_QUADS)
glNormal3f(0, -1, 0)
for i in range(0, 300):
for j in range(0, 300):
glVertex3f(-100 + i, 100, -300 - j)
glVertex3f(-100 + i, 100, -300 - j - 1)
glVertex3f(-100 + i + 1, 100, -300 - j - 1)
glVertex3f(-100 + i + 1, 100, -300 - j)
glEnd()
def display():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1.0, 1.0, 1.0)
glClearColor(0, 0.6, 0, 0)
lighting()
draw_box()
# Draw cone
glPushMatrix()
glTranslatef(-50, -80, -320)
glRotatef(270, 1, 0, 0)
glColor3f(193 / 255, 115 / 255, 235 / 255)
glutSolidCone(20, 80, 20, 20)
glPopMatrix()
# Draw cube
glPushMatrix()
glTranslatef(50, -80, -440)
glRotatef(25, 0, 1, 0)
glColor4f(240 / 255, 144 / 255, 84 / 255, 1)
glutSolidCube(50)
glPopMatrix()
glFlush()
def reshape(w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glFrustum(-100, 100, -100, 100, 300, 600)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def main():
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(1050, 1050)
glutInitWindowPosition(100, 150)
glutCreateWindow("Belov Michael")
glutDisplayFunc(display)
glutReshapeFunc(reshape)
glEnable(GL_DEPTH_TEST)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glutMainLoop()
if __name__ == '__main__':
main()```
Result:
I sent the code to a friend of mine who has been doing this for a long time, and it works for him.
You need to download the file from here. PyOpenGL-3.1.6-[Python version]-win_amd64.whl.
Then - pip install PyOpenGL-3.1.6-[Python version]-win_amd64.whl.