0

I only just started with OpenGL and PyOpenGL and am using tutorial code from this page https://noobtuts.com/python/opengl-introduction. However I quickly ran into the following problem: while the code successfully draws what is intended the drawing cannot occupy more than the lower left quadrant of my window. E.g. in the below I am setting the size and position of the rectangle such that it occupies the full window, as you can see in the code below I set the rectangle’s width and height to the window’s width and height and the position is 0,0 so I would expect the full window to become blue but this is not happening as you can see below. I am on Mac OS Catalina and running PyOpenGL on Python 3.

I have seen elsewhere that there is something to do with Catalina at this place: https://github.com/redeclipse/base/issues/920 and this place https://github.com/ioquake/ioq3/issues/422#issuecomment-541193050

However this is way too advanced for me to understand.

Would anyone know how that can be solved perhaps?

Thanks ahead for your help

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

window = 0  # glut window number
width, height = 500, 400  # window size

def refresh2d(width, height):
    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0.0, width, 0.0, height, 0.0, 1.0)
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity()

def draw_rect(x, y, width, height):
    glBegin(GL_QUADS)                                  # start drawing a rectangle
    glVertex2f(x, y)                                   # bottom left point
    glVertex2f(x + width, y)                           # bottom right point
    glVertex2f(x + width, y + height)                  # top right point
    glVertex2f(x, y + height)                          # top left point
    glEnd()                                            # done drawing a rectangle

def draw():  # ondraw is called all the time
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)  # clear the screen
    glLoadIdentity()  # reset position
    refresh2d(width, height)  # set mode to 2d

    glColor3f(0.0, 0.0, 1.0)  # set color to blue
    draw_rect(0, 0, 500, 400)  # rect at (0, 0) with width 500, height 400

    glutSwapBuffers()  # important for double buffering


# initialization
glutInit()  # initialize glut
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(width, height)  # set window size
glutInitWindowPosition(0, 0)  # set window position
window = glutCreateWindow("my first attempt")  # create window with title
glutDisplayFunc(draw)  # set draw function callback
glutIdleFunc(draw)  # draw all the time
glutMainLoop()  # start everything

However this isn't working. I am definitely getting a window where the blue rectangle occupies only the lower left quadrant.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Is HiDPI in play? If so, does doubling the width/height arguments to `glViewport()` help? – genpfault Apr 15 '20 at 17:32
  • What size do you get by `glutGet(GLUT_WINDOW_WIDTH)` respectively `glutGet(GLUT_WINDOW_HEIGHT)`? – Rabbid76 Apr 15 '20 at 18:24
  • 1
    @Rabbid76 I get the sizes I gave as input for the window, 500 and 400 respectively – Amateur1 Apr 16 '20 at 12:17
  • 1
    @genpfault you are very probably right, indeed doubling the size of the viewport dimensions fixes it. It would be nice to get my hands on the Retina display DPI scaling factor rather than manually entering a factor of 2. I see there are posts about that elsewhere on stackoverflow e.g. here , with the following reference: - So I will study these and update my question later on. – Amateur1 Apr 16 '20 at 12:20

2 Answers2

0

FWIW, using glfw I'm able to get around this problem:

width = 1280
height = 1024
win = glfw.CreateWindow(width, height, "window title")
fb_width, fb_height = glfw.GetFramebufferSize(win)
glViewport(0, 0, fb_width, fb_height) # <--- this is the key line
0

you can install modified glut http://iihm.imag.fr/blanch/software/glut-macosx/

or you can do

glViewport(0, 0, width*2, height*2)

if you don't care about DPI

eyeholle
  • 13
  • 2