how to you draw a triangle using GLBegin(GL_TRIANGLES) and GLEnd() statements in the blender game engine?
import bgl
bgl.glbegin()
…
…
…
bgl.glend()
Alternatively you can use pyopengl. This gives you many more options as it supports more OpenGL features than bgl does.
import bgl
bgl.glBegin(bgl.GL_QUADS)
i = 20
bgl.glVertex2f(i , i )
bgl.glVertex2f(-i, i)
bgl.glVertex2f(-i ,-i)
bgl.glVertex2f(i ,-i)
bgl.glEnd()
k, doesn’t this need to be in a redraw or reshape statement? how do I get it into the Blender Game area? (or draw anywhere )
@Kupoman: Yes, but I’d like to see if Blender can pull of a rotating cube. Also, pyOpenGL is not bundled with Blender and requires some package fiddling/installations.
http://www.blender.org/documentation/249PythonDoc/BGL-module.html
^^ so, do something similar to this example from 2.49. Like that only for 2.67.
Register it with KX_Scene.post_draw:
http://www.blender.org/documentation/blender_python_api_2_66_6/bge.types.KX_Scene.html#bge.types.KX_Scene.post_draw
Why you need this low-level processing?
I’m doing a proof of concept for a university class - it was called Computer Graphics, but all we did in there was low level OpenGL /GLUT code. I’m trying to get something presentable in Blender so it might improve the overall application/understanding for next semester.
hmm… KX_Scene.post_redraw looks slightly undocumented for my liking. (what does KX stand for, anyways?)
Also, while this thread’s goal is to render to the Blender Game Engine, it might be a better fit in the Python Support area. (feel free to move it )
def draw():
# Here your OpenGL code
scene = logic.getCurrentScene()
scene.post_draw = [draw]
KX_ is the prefix for all Ketsji related parts of BGE. Ketsji is the name the blender game engine. The prefix is used for the core of it. Other parts of the game engine might have other prefixes. E.g.:
SCA_ which belongs to the logic bricks (sensors, controllers, actuators).
Such prefixes were common conventions at the time as the Blender was created. I’m not sure if that still is best practice with C/C++.
hi, I read the code above, but faile to use it, I just want to draw a alpha=0.5 rectangle below the fonts displayed by blf…to make the fonts stand out ,do you know how to do this?(I’ve tried making an object in an overlay scene, but I faile to resize the rectangle to fix window size…maybe I’m too stupid…)
I’m using the script from blf document:
TEXTS=[
[fid,(0.04,0.90),0.8,“Scene A”,(0.772,0,1,1)],
[fid,(0.04,0.05),0.25,“this Scene shows a moon”,(1.0,0.893,0,1)]
]
width = render.getWindowWidth() height = render.getWindowHeight()
# OpenGL setup
bgl.glMatrixMode(bgl.GL_PROJECTION)
bgl.glLoadIdentity()
bgl.gluOrtho2D(0, width, 0, height)
bgl.glMatrixMode(bgl.GL_MODELVIEW)
bgl.glLoadIdentity()
# bgl.glRect(0,0,50,50,30,30) # I’ve tried this but failed with [[[AttributeError: ‘module’ object has no attribute ‘glRect’]]],I’m using 2.65-release
# BLF drawing routine
for x in TEXTS:
font_id = x[0]
blf.position(font_id, (width * x[1][0]), (height * x[1][1]), 1)
blf.size(font_id, int((height*x[2])/8), 72)
bgl.glColor4f(x[4][0],x[4][1],x[4][2],x[4][3])
blf.draw(font_id, x[3])
And I want to draw a rectangle (alpha=0.5) under the fonts to make the fonts stand out ,
I 've tried make an overlay scene with a plane object ,but failed to calculate its position when you start the game from a Non-camera View …
I try to read the bgl API document but failed to understand…
do you have any ideas?
I could use this to do real time rope? tricky though…
hrmmm
spawn empties and then draw a cylinder between them… apply rbj …
Here is the OpenGL Wrapper (bgl) api
http://www.blender.org/documentation/blender_python_api_2_67_release/bgl.html
In Blender only glRectd, glRectf, glRecti, glRects, glRectdv, glRectfv, glRectiv, glRectsv existing.
Change the code to.
# Enable alpha blending
bgl.glEnable(bgl.GL_BLEND)
bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
bgl.glColor4f(1.0, 1.0, 1.0, 0.5) #glColor4f four float values RGBA
bgl.glRecti(0,0,50,50) #glRecti for four integer
I’ve done it!! Much thanks to HG1!!!
here’s my final script,works well in Ubuntu11.04 Blender 2.65 release(maybe highter version…) :(PS:how to display a code block in the thread?)
fontPath = "/home/gnusong/fonts/wqy-microhei.ttf"
fid = blf.load(fontPath)
TEXTS=[
[fid,(0.04,0.90),0.8,"Scene A",(0.772,0,1,1)],
[fid,(0.04,0.05),0.25,"this Scene shows a moon",(1.0,0.893,0,1)]
]
def text():
scene = logic.getCurrentScene()
scene.post_draw = [write]
def write():
width = render.getWindowWidth()
height = render.getWindowHeight()
# OpenGL setup
bgl.glMatrixMode(bgl.GL_PROJECTION)
bgl.glLoadIdentity()
bgl.gluOrtho2D(0, width, 0, height)
bgl.glMatrixMode(bgl.GL_MODELVIEW)
bgl.glLoadIdentity()
#draw a 2D rectangle to make the fonts stand out
bgl.glEnable(bgl.GL_BLEND) # Enable alpha blending
bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
bgl.glColor4f(0.0, 0.0, 0.0, 0.4) #glColor4f four float values RGBA
bgl.glRecti(0,0,480,100) #glRecti for four integer
bgl.glRecti(0,height-200,480,height) # I have two rectangle to draw...
# BLF drawing routine
for x in TEXTS:
font_id = x[0]
blf.position(font_id, (width * x[1][0]), (height * x[1][1]), 1)
blf.size(font_id, int((height*x[2])/8), 72)
blf.enable(font_id,blf.SHADOW)
blf.shadow(font_id, 5, 0.0, 0.0, 0.0, 1.0) # blur_size being 0 means colored Font, 3 or 5 means a colored rim around the font.# Note that you can only use (0, 3, 5).
blf.shadow_offset(font_id,0,0)
blf.blur(font_id,10)
bgl.glColor4f(x[4][0],x[4][1],x[4][2],x[4][3])
blf.draw(font_id, x[3])
The code above takes many people’s scripts and advise , thank you all ! Maybe someone will benefit from this code , Good Luck !!
please use code tags!
PS:how to display a code block in the thread?)
You must press first the “GO ADVANCED” button.