4
$\begingroup$

Is it possible to render a shader generated Texture in Blender Python with a shader ?

That's what I have so far:

    texIDinput = inputTex.gl_load()
    texIDweights = NetData.mWeights.gl_load()


    # 50% alpha, 2 pixel width line
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
    bgl.glLineWidth(2)


    bgl.glBegin(bgl.GL_LINE_STRIP)                  # begin a vertex data list
    bgl.glVertex2i(0, 0)
    bgl.glVertex2i(255, 255)
    bgl.glVertex2i(0, 255)
    bgl.glEnd()

    bgl.glCopyTexImage2D(texIDweights, 0, bgl.GL_RGBA, 0, 0, NetData.size, NetData.size, 0)


    NetData.mWeights.update()

But I dont get Lines on my Texture after running the Script ?

$\endgroup$
4
$\begingroup$

I am not an openGL expert. The documentation links to OpenGL 2.1. I don't think this version supports render to texture.

import bpy
import bgl

viewport_info = bgl.Buffer(bgl.GL_INT, 4)
bgl.glGetIntegerv(bgl.GL_VIEWPORT, viewport_info)

WIDTH  = 400
HEIGHT = 400

img = bpy.data.images.get("texture")
img.gl_load(0, bgl.GL_NEAREST, bgl.GL_NEAREST)
tex = img.bindcode

out = bpy.data.images.new("output", WIDTH, HEIGHT)
buffer = bgl.Buffer(bgl.GL_FLOAT, WIDTH * HEIGHT * 4)

bgl.glDisable(bgl.GL_SCISSOR_TEST)
bgl.glViewport(0, 0, HEIGHT, WIDTH)

bgl.glMatrixMode(bgl.GL_MODELVIEW)
bgl.glPushMatrix()
bgl.glLoadIdentity()

bgl.glMatrixMode(bgl.GL_PROJECTION)
bgl.glPushMatrix()
bgl.glLoadIdentity()
bgl.gluOrtho2D(0, 1, 0, 1)

bgl.glClearDepth(1.0)
bgl.glClearColor(0.0, 0.0, 0.0, 0.0)
bgl.glClear(
    bgl.GL_COLOR_BUFFER_BIT |
    bgl.GL_DEPTH_BUFFER_BIT
)

bgl.glEnable(bgl.GL_TEXTURE_2D)
bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex)

bgl.glBegin(bgl.GL_TRIANGLES)

bgl.glColor3f(1.0, 1.0, 1.0)
bgl.glTexCoord2f(0.0, 0.0)
bgl.glVertex2f(0.0, 0.0)
bgl.glTexCoord2f(1.0, 0.0)
bgl.glVertex2f(1.0, 0.0)
bgl.glTexCoord2f(1.0, 1.0)
bgl.glVertex2f(1.0, 1.0)

bgl.glEnd()

bgl.glCopyTexImage2D(
    bgl.GL_TEXTURE_2D, #target
    0, #level
    bgl.GL_RGBA, #internalformat
    0, #x
    0, #y
    WIDTH,
    HEIGHT,
    0 #border
)

bgl.glBegin(bgl.GL_QUADS)

bgl.glColor3f(1.0, 1.0, 1.0)
bgl.glTexCoord2f(0.0, 0.0)
bgl.glVertex2f(0.0, 0.5)
bgl.glTexCoord2f(1.0, 0.0)
bgl.glVertex2f(0.5, 0.5)
bgl.glTexCoord2f(1.0, 1.0)
bgl.glVertex2f(0.5, 1.0)
bgl.glTexCoord2f(0.0, 1.0)
bgl.glVertex2f(0.0, 1.0)

bgl.glEnd()

bgl.glFinish()
#bgl.glGetTexImage(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, bgl.GL_FLOAT, buffer)
bgl.glReadPixels(0, 0, WIDTH, HEIGHT , bgl.GL_RGBA, bgl.GL_FLOAT, buffer)
out.pixels = buffer[:]
img.gl_free()

#reset
bgl.glEnable(bgl.GL_SCISSOR_TEST)

#necessary(?)
bgl.glViewport(*tuple(viewport_info))

bgl.glMatrixMode(bgl.GL_MODELVIEW)
bgl.glPopMatrix()
bgl.glMatrixMode(bgl.GL_PROJECTION)
bgl.glPopMatrix()

Result

result

$\endgroup$
  • $\begingroup$ is it possible to render to an fbo ? $\endgroup$ – schwenk Apr 19 '15 at 14:15
  • $\begingroup$ glreadPixels is damn slow i thought ? $\endgroup$ – schwenk Apr 19 '15 at 14:15
  • $\begingroup$ expanded the example using bgl.glCopyTexImage2D $\endgroup$ – pink vertex Apr 19 '15 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.