3

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
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
alfred
  • 33
  • 4
  • What is the `Shapes` module? (Obviously it's not the matplot/numpy "shapes") – Kingsley Jul 21 '20 at 23:14
  • The Shape modules is the one that contains the cube generation class that I was talking about. But as I said, I don't think the problem is there because even when I try to draw a simple line it doesn't work. I'm starting to wonder if I installed it incorrectly or something like that – alfred Jul 21 '20 at 23:59
  • Ok, but without `Shapes` it we can't test-run your code. Could that view be from having the camera *inside* a rectangular prism, looking at the far-end, a long long way away. – Kingsley Jul 22 '20 at 00:12
  • Yes that makes sense but, I've also tried to tweak the camera position and nothing happend ;(. This weird X didn't move. I add the lines of code that I'm using to draw the cube in the post. And I am using unit numbers, the vertices axis are between 1 and -1. I'm going to post the entire code on GitHub and update the link on the post – alfred Jul 22 '20 at 00:22
  • What are the vertex coordinates? (What is `nodes`?) – Rabbid76 Jul 22 '20 at 17:41

1 Answers1

2

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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174