2017-06-17 4 views
0

J'essaye de rendre une image avec OpenGL en utilisant Pyglet. Jusqu'à présent, j'ai été capable de configurer le framebuffer et la texture, de le restituer et de l'enregistrer comme une image PNG. Mais je ne peux pas savoir comment utiliser le rendu des polices Pyglets.Pyglet dessine du texte dans la texture

import numpy as np 
import pyglet 
from pyglet.gl import * 
from ctypes import byref, sizeof, POINTER 

width = 800 
height = 600 
cpp = 4 

# Create the framebuffer (rendering target). 
buf = gl.GLuint(0) 
glGenFramebuffers(1, byref(buf)) 
glBindFramebuffer(GL_FRAMEBUFFER, buf) 

# Create the texture (internal pixel data for the framebuffer). 
tex = gl.GLuint(0) 
glGenTextures(1, byref(tex)) 
glBindTexture(GL_TEXTURE_2D, tex) 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, None) 

# Bind the texture to the framebuffer. 
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0) 

# Something may have gone wrong during the process, depending on the 
# capabilities of the GPU. 
res = glCheckFramebufferStatus(GL_FRAMEBUFFER) 
if res != GL_FRAMEBUFFER_COMPLETE: 
    raise RuntimeError('Framebuffer not completed') 

glViewport(0, 0, width, height) 

# DRAW BEGIN 
# ===================== 
glClearColor(0.1, 0.1, 0.1, 1.0) 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glColor3f(1.0, 0.5, 0.02) 
glRectf(-0.75, -0.75, 0.75, 0.75) 

glColor3f(1.0, 1.0, 1.0) 
label = pyglet.text.Label(
    "Hello, World", font_name='Times New Roman', font_size=36, 
    x=0, y=0, anchor_x='center', anchor_y='center') 
label.draw() 
# ===================== 
# DRAW END 

# Read the buffer contents into a numpy array. 
data = np.empty((height, width, cpp), dtype=np.float32) 
glReadPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, data.ctypes.data_as(POINTER(GLfloat))) 

# Save the image. 
import imageio 
data = np.uint8(data * 255) 
imageio.imwrite("foo.png", data) 

Le texte n'apparaît pas sur le framebuffer. Comment puis-je rendre l'étiquette sur le framebuffer?

Répondre

1

Pour le rendu des étiquettes dans Pyglet, d'abord, une projection orthographique devrait être mis en place. Dans l'exemple donné, procédez comme suit:

glMatrixMode(GL_PROJECTION) 
glLoadIdentity() 
glOrtho(0, width, 0, height, -1, 1) 
glMatrixMode(GL_MODELVIEW) 
glLoadIdentity() 
glColor3f(1.0, 1.0, 1.0) 
label = pyglet.text.Label(
    "Hello, World", font_name='Times New Roman', font_size=36, 
    x=width/2, y=height/2, anchor_x='center', anchor_y='center') 
label.draw() 

Ensuite, le libellé est rendu comme prévu. (Note: Il est proposé de compensation de l'étiquette au centre de l'image, à savoir x=width/2, y=height/2,)

foo.png (output framebuffer image)

+0

Merci de qui fonctionne! Il suffit de passer les arguments '0, height' à' glOrtho() 'pour retourner l'image verticalement. :) –

+0

Neat! Ou, je pensais que vous renverseriez l'image après avoir jeté le framebuffer. Quelque chose comme - 'data = np.flipud (data)' après 'glReadPixels()'. – Jay