0

The light rotates with the image, but it also illuminates the sphere even though the light is blocked by the cube. How do I stop the light from rotating with the cube, and if it is not possible, how can I stop it from illuminating the sphere when the light is being blocked by the cube?

Full replicable code:

import random
import time
import pygame
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

array = [(0, 0, 0)]
width = 500
height = 500
random_number = 0
turned = 0
angle = 0
switch = False

vertices = [(-1, -1, -1), (1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, 1), (1, -1, 1), (1, 1, 1), (-1, 1, 1)]
faces = [(4, 0, 3, 7), (1, 0, 4, 5), (0, 1, 2, 3), (1, 5, 6, 2), (3, 2, 6, 7), (5, 4, 7, 6)]
colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (1, 0, 1), (0, 1, 1)]


def draw_shapes():
    global turned, switch, random_number, angle
    glDisable(GL_TEXTURE_2D)
    glPushMatrix()

    if turned >= 90:
        random_number = random.randint(0, 2)
        turned = angle = 0

    if random_number == 0:
        glRotatef(angle, 0, 1, 0)
    elif random_number == 1:
        glRotatef(angle, 1, 0, 0)
    else:
        glRotatef(angle, 0, 0, 1)

    turned += 1
    angle += 1
    glColor3fv((.16, .16, .16))

    glBegin(GL_QUADS)
    for i, face in enumerate(faces):
        for surftex, vertex in enumerate(face):
            if surftex == 0:
                glTexCoord2f(1.0, 1.0)
            elif surftex == 1:
                glTexCoord2f(1.0, 0.0)
            elif surftex == 2:
                glTexCoord2f(0.0, 0.0)
            elif surftex == 3:
                glTexCoord2f(0.0, 1.0)
            glVertex3fv(vertices[vertex])
    glEnd()
    glPopMatrix()
    glDisable(GL_TEXTURE_2D)
    glPushMatrix()
    glColor3fv((.16, .16, .16))
    glTranslatef(-3, 0, 0)
    Quadric = gluNewQuadric()
    gluSphere(Quadric, 1, 50, 50)
    glPopMatrix()


def textureBind():
    image = pygame.image.load("Image.png")
    datas = pygame.image.tostring(image, 'RGBA')

    glEnable(GL_TEXTURE_2D)
    texture_id = glGenTextures(1)

    glBindTexture(GL_TEXTURE_2D, texture_id)
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.get_width(), image.get_height(),
                 0, GL_RGBA, GL_UNSIGNED_BYTE, datas)

    return texture_id


def showScreen():
    global width, height
    glClearColor(0, 0, 0, 1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    glEnable(GL_COLOR_MATERIAL)
    glLightfv(GL_LIGHT0, GL_POSITION, (1, -1, -1, 1))  # point light from the left, top, front
    glLightfv(GL_LIGHT0, GL_AMBIENT, (1, 1, 1, 1))
    draw_shapes()

    glutSwapBuffers()


def mouseTracker(mousex, mousey):
    print(f"Mouse pos: {mousex}, {mousey}")


def reshapeWindow(x, y):
    global width, height
    width = x
    height = y
    print(x, y)
    glutReshapeWindow(width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, (width / height), 0.0001, 1000)
    glViewport(0, 0, width, height)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glTranslatef(0, -1, -10)
    glRotatef(3, 1, 0, 0)


glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(500, 500)
wind = glutCreateWindow("OpenGL")
glutDisplayFunc(showScreen)
glutIdleFunc(showScreen)
glutMotionFunc(mouseTracker)
glutPassiveMotionFunc(mouseTracker)
glutReshapeFunc(reshapeWindow)
gluPerspective(45, (width / height), 0.0001, 100)
textureBind()

while True:
    glutMainLoopEvent()
    glutPostRedisplay()
    time.sleep(0.01)
Nicol Bolas
  • 413,367
  • 61
  • 711
  • 904
thirdsandfourths
  • 162
  • 1
  • 12
  • 1
    This code has lots of issues, actually too many to solve in a useful way in a stackoverflow answer. I recommend using a good textbook or tutorial. I also recommend using shaders if you want to learn lighting in 3D graphics. And if you are going to need shadows - as you phrased it "light blocked by the cube" - you definitively want shaders, along with other modern GL features like render-to-texture. – derhass Aug 11 '21 at 16:52

0 Answers0