简体   繁体   中英

Strange “X” on PyOpenGL + PyGame

I'm trying to create a PyOpenGL "Hello World" project. I made some code that generates a cube to me and displays it on the screen, the problem is it doesn't show a cube, it show a strange flat 2D "X". I am almost sure that the problem is with my PyGame settings because I've tried to display a simple line and it, actually, nothing was displayed.

I don't know if this is a useful information, but my operational system is Linux - Pop!_os .

FULL CODE: https://github.com/caastilho/PyOpenGL-Problem


LAUNCHER.PY

All the PyGame display settings are storage here

import pygame
from pygame import event as events
from pygame import display

import sys, os
from screeninfo import get_monitors
from canvas import setup, draw


# Root class: "Launcher"
class Launcher:

    def __init__(self):
        pygame.init()

        # Create default settings
        self.title = "PyGame Screen"
        self.size = (500, 500)
        self.flags = 0

        setup(self)
        self.__setupSurface()

        # Run screen
        self.__runSurface()


    # Setup environment
    def __setupSurface(self):
        """Setup environment."""

        # Get the main monitor dimensions
        for monitor in get_monitors():
            if monitor.x == monitor.y == 0:
                middle_x = (monitor.width  - self.size[0]) // 2
                middle_y = (monitor.height - self.size[1]) // 2 

        offset = f"{middle_x},{middle_y}"

        # Setup window
        os.environ["SDL_VIDEO_WINDOW_POS"] = offset
        display.set_caption(self.title)
        self.surface = display.set_mode(self.size, self.flags)


    # Run environment    
    def __runSurface(self):
        """Run environment."""

        while True:
            draw(self)
            pygame.time.wait(10)

            # Close condition
            for event in events.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()


# Run launcher
if __name__ == "__main__":
    Launcher()

CANVAS.PY

All the draw functions are storage here

from pygame import display
from pygame.locals import *

from OpenGL.GL  import *
from OpenGL.GLU import *

from Shapes.shape import OpenGL_Cube # Imports the cube class


cube = None
clear_flags = None

# Setup canvas
def setup(app):
    global cube, clear_flags

    app.size = (1280, 720) # Setup window size
    app.title = "PyOpenGl" # Setup window title
    app.flags = DOUBLEBUF|OPENGL # Setup PyGame flags

    # Setup OpenGL environment
    clear_flags = GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT
    gluPerspective(45, (app.size[0]/app.size[1]), 0.1, 50)
    glTranslatef(0, 0, -5)
    glRotatef(0, 0, 0, 0)
    
    # Setup objects
    cube = OpenGL_Cube()


# Run canvas
def draw(app):
    cube.draw()

    display.flip()
    glClear(clear_flags)

OUTPUT


EDIT

Just to clarify, the lines of code that i am using to display the cube edges are this one:

glBegin(GL_LINES)
for node in nodes: # Where node = (x, y, z)
    glVertex3fv(node)
glEnd

The perspective projection and the model view matrix is never set, because you invoke the function setup before self.__setupSurface() .
Note, for any OpenGL instruction a valid and current OpenGL Context is requried, else the instruction has no effect. The OpenGL Context is created when the pygame display surface is generated ( display.set_mode() ). Hence setup has to be invoked after self.__setupSurface() :

class Launcher:
    # [...]

    def __init__(self):
        pygame.init()
  
        # [...]

        self.size = (1280, 720)
        self.title = "Marching Cubes"
        self.flags = DOUBLEBUF|OPENGL

        # create OpenGL window (and make OpenGL context current)
        self.__setupSurface()

        # setup projection and view
        setup(self)
# Setup canvas
def setup(app):
    global cube, clear_flags

    # Setup OpenGL environment
    clear_flags = GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT
    gluPerspective(45, (app.size[0]/app.size[1]), 0.1, 50)
    glTranslatef(0, 10, -5)
    glRotatef(0, 0, 0, 0)
    
    # Setup objects
    cube = OpenGL_Cube()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM