Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create an OpenGL context in Python

I am using pygame and pyopengl, and am trying to get glGenTextures(1,img) to return 1. I have checked that 'myPixelArt.bmp' has power-of-2 dimensions. My script starts with:

import pygame, OpenGL, math, numpy
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.WGL import *
from OpenGL.GLU import *
from PIL import Image
from ctypes import *

img = Image.open('myPixelArt.bmp')
glEnable(GL_TEXTURE_2D)

hdc=windll.user32.GetDC(1)
print hdc

hglrc=wglCreateContext(hdc)
wglMakeCurrent(hdc,hglrc)
im=glGenTextures(1,img)
print im

But this prints out 0 for hdc and 0 for im.

What argument should I use for windll.user32.GetDC to create a display context and hopefully get glGenTextures to return 1?

like image 798
Matt Majic Avatar asked Jun 30 '26 10:06

Matt Majic


1 Answers

Try to first create a window!

import pygame, OpenGL, math, numpy
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.WGL import *
from OpenGL.GLU import *
from PIL import Image
from ctypes import *

size=(800,600)
pygame.display.set_mode(size,pygame.OPENGL|pygame.DOUBLEBUF)

img = Image.open('myPixelArt.bmp')
glEnable(GL_TEXTURE_2D)

hdc=windll.user32.GetDC(1)
print hdc

hglrc=wglCreateContext(hdc)
wglMakeCurrent(hdc,hglrc)
im=glGenTextures(1,img)
print im

Becouse Pygame creates an OpenGL-context along with the window.

like image 129
Sasszem Avatar answered Jul 02 '26 23:07

Sasszem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!