Last active
December 22, 2015 03:38
-
-
Save TimSC/6411425 to your computer and use it in GitHub Desktop.
Demo to display opengl texture that is read from a file. This code is public domain/CC0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SCREEN_SIZE = (800, 600) | |
import math | |
from OpenGL.GL import * | |
from OpenGL.GLU import * | |
import pygame | |
from pygame.locals import * | |
import numpy as np | |
from scipy import misc | |
### OpenGL Utility Functions | |
def resize(width, height): | |
glViewport(0, 0, width, height) | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
gluPerspective(60.0, float(width)/height, .001, 1000.) | |
glMatrixMode(GL_MODELVIEW) | |
glLoadIdentity() | |
def init(): | |
glEnable(GL_DEPTH_TEST) | |
glDepthFunc(GL_LEQUAL) | |
glClearColor(1.0, 1.0, 1.0, 0.0) | |
# set up texturing | |
glEnable(GL_TEXTURE_2D) | |
glEnable(GL_BLEND) | |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) | |
class GLTexture(object): | |
def __init__(self): | |
self.num = None | |
def __del__(self): | |
if self.num is not None: | |
glDeleteTextures([self.num]) | |
self.num = None | |
def SetFromMatrix(self, img): | |
img = np.array(img) | |
rawData = img.astype('uint8').tostring() | |
self.SetFromString(rawData, img.shape[1], img.shape[0]) | |
def SetFromString(self, img, w, h): | |
self.num = glGenTextures(1) | |
glBindTexture(GL_TEXTURE_2D, self.num) | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, | |
GL_UNSIGNED_BYTE, img) | |
def Draw(self): | |
assert self.num is not None | |
glEnable(GL_TEXTURE_2D) | |
glBindTexture(GL_TEXTURE_2D, self.num) | |
glColor3f(0.98, 0.96, 0.95) | |
glBegin(GL_POLYGON) | |
glTexCoord2f(0.,1.) | |
glVertex(-5.,-5.,0.) | |
glTexCoord2f(0.,0.) | |
glVertex(-5.,5.,0.) | |
glTexCoord2f(1.,0.) | |
glVertex(5.,5.,0.) | |
glTexCoord2f(1.,1.) | |
glVertex(5.,-5.,0.) | |
glEnd() | |
### Main Program | |
def run(): | |
pygame.init() | |
screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE|OPENGL|DOUBLEBUF) | |
resize(*SCREEN_SIZE) | |
init() | |
clock = pygame.time.Clock() | |
glMaterial(GL_FRONT, GL_AMBIENT, (0.1, 0.1, 0.1, 1.0)) | |
glMaterial(GL_FRONT, GL_DIFFUSE, (1.0, 1.0, 1.0, 1.0)) | |
img = misc.imread("CIMG8586.JPG") | |
tex = GLTexture() | |
tex.SetFromMatrix(img) | |
while True: | |
time_passed = clock.tick() | |
time_passed_seconds = time_passed / 1000. | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
return | |
if event.type == KEYUP and event.key == K_ESCAPE: | |
return | |
pressed = pygame.key.get_pressed() | |
# Clear the screen, and z-buffer | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
glMatrixMode(GL_MODELVIEW) | |
glLoadIdentity() | |
gluLookAt(0., 0., 10., # look from camera XYZ | |
0., 0., 0., # look target | |
0., 1., 0.); # up | |
tex.Draw() | |
# Show the screen | |
pygame.display.flip() | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment