Argh! It makes me crazy. I have difficulties into translating the previous
code from PyOpenGL to Pyglet. I simplified the code and came to this (more
readable here : http://pastebin.com/j7rfaPB9):
import os, sys
from ctypes import (
byref, c_char, c_float, c_char_p, c_int, cast, create_string_buffer,
pointer,
POINTER
)
import numpy as np
import pyglet.gl as gl
from pyglet.window import *
from pyglet import app
width = 640
height = 512
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
gl.glOrtho(0, width, 0, height, -1, 1)
gl.glMatrixMode(gl.GL_MODELVIEW)
fragment = """
uniform sampler2D myTexture;
void main()
{
vec2 uv = gl_TexCoord[0].xy;
vec4 color = texture2D(myTexture, uv);
gl_FragColor = (0.0, 0.0, color.a, 1.0);
}"""
vertex = """
void main()
{
gl_FrontColor = gl_Color;
gl_TexCoord[0].xy = gl_MultiTexCoord0.xy;
gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
}"""
program_handle = gl.glCreateProgram()
def init_shader(source, shader_type):
shader = gl.glCreateShader(shader_type)
c_source = cast(pointer(pointer(create_string_buffer(source))),
POINTER(POINTER(gl.GLchar)))
gl.glShaderSource(shader, 1, c_source, None)
gl.glCompileShader(shader)
gl.glAttachShader(program_handle, shader)
return shader
_vertex_shader = init_shader(vertex, gl.GL_VERTEX_SHADER)
_fragment_shader = init_shader(fragment, gl.GL_FRAGMENT_SHADER)
gl.glLinkProgram(program_handle)
# Indexed Image to be displayed
my_texture = np.random.uniform(0,1,(512, 512)).astype(np.float32) #
generate random noise as test texture
c_my_texture = (c_float * 512 * 512)() # copying under correct ctype format
(likely clumsy)
c_my_texture.value = my_texture[:,:]
my_texture_id = gl.GLuint() # generate 1 component texture (let's say ALPHA)
gl.glGenTextures(1, byref(my_texture_id))
gl.glEnable(gl.GL_TEXTURE_2D)
gl.glBindTexture(gl.GL_TEXTURE_2D, my_texture_id.value)
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_ALPHA, 512, 512, 0,
gl.GL_ALPHA, gl.GL_FLOAT, c_my_texture)
gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER,
gl.GL_NEAREST)
gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
gl.GL_NEAREST)
window = Window(width, height)
@window.event
def on_draw():
gl.glClearColor(1,0,0,1) # fill in red
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
gl.glUseProgram(program_handle)
# set shader variable 'myTexture'
gl.glEnable( gl.GL_TEXTURE_2D )
gl.glActiveTexture( gl.GL_TEXTURE0 )
gl.glBindTexture( gl.GL_TEXTURE_2D, my_texture_id)
gl.glUniform1i(gl.glGetUniformLocation(program_handle, 'myTexture'), 0)
# Drawing texture
gl.glBegin(gl.GL_QUADS)
gl.glTexCoord2f(0, 1), gl.glVertex2i(0, 0)
gl.glTexCoord2f(0, 0), gl.glVertex2i(0, 512)
gl.glTexCoord2f(1, 0), gl.glVertex2i(512, 512)
gl.glTexCoord2f(1, 1), gl.glVertex2i(512, 0)
gl.glEnd()
gl.glUseProgram(0)
window.flip()
app.run()
It should generate a random noised texture with only one component, and the
shader should draw it in levels of blue, with a red background.
It's not what I get (I get a white square where the texture should lie).
And I don't understand why. I have no erros at execution, and it's
difficult to debug (I don't know how to see if the texture is correctly
loaded). I suspected errors with ctypes parameters handling, but I got
errors when I did that wrong.
I'd be grateful if someone could point what is wrong here. Thanks :)
On Thursday, August 15, 2013 9:50:43 AM UTC+2, Fred wrote:
>
> This is definetely the most helpful ressource I got.
>
> I'm trying to adapt it somewhat to pyglet (defining an IndexedTexture
> class to use - along with the shaders - with pyglet's sprites).
>
> Thanks a lot!
>
> Le mardi 13 août 2013 07:40:36 UTC+2, Nicolas Rougier a écrit :
>>
>>
>>
>> Here is an example (using glut but it should be quite simple to adapt to
>> pyglet).
>>
>> The texture to be colorized is "texture" within the shader
>> The lookup color table is "lut".
>>
>> You can also have a look at glumpy where I use this "trick" a lot.
>> Hope that helps.
>>
>>
>> Nicolas
>>
>>
--
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/groups/opt_out.