import Blender import math from Blender.BGL import * from Blender import Draw # Better decimator # at the moment this will be a simple model viewer, to test my exmesh code # then I will begin work on the decimator-like functionality # I want to be able to later have presets for the decimator stuff # in a text file ..., maybe a python one that can be run... imgbuf = Buffer(GL_BYTE,3) xvalue = 0 yvalue = 0 ### THIS AREA OF CODE NEEDS WORK! colorstart = 200 bpc = 5 # bits per channel mult = 256/(2**bpc) color = colorstart rmask = (2**bpc)-1 gmask = rmask << bpc bmask = gmask << bpc allmask = 2**(3*bpc)#rmask|gmask|bmask #print rmask, gmask, bmask def newcolor(): global color color += 1 if color >= allmask: color = 0 return ( (color & rmask) * mult, ((color & gmask) >> bpc)* mult, ((color & bmask) >> (2 * bpc) )* mult ) def colortoindex(col): return ((col[0] / mult) | ((col[1] / mult) << bpc) | ((col[2] / mult) << (2*bpc))) - colorstart - 1 ## DEBUG: #for i in range(512): # print colortoindex(newcolor()) #for i in range(20): # print newcolor() def midpoint(v1,v2): return ((v1[0]+v2[0])/2.0,(v1[1]+v2[1])/2.0,(v1[2]+v2[2])/2.0) class triangle: def __init__(self, parent): self.parent = parent #redundant #self.divided = 0 self.children = None self.color = newcolor() print self.color if parent: # verts depend on which sub triangle I am... I need to think about this pass else: self.verts = [(0.0,0.0,0.0), (1.0,0.0,0.0), (0.5,1.0,0.0)] #self.verts = [(0.0,0.0,0.0), (1.0,0.0,0.0), (0.0,1.0,0.0)] triangles = [triangle(None)] def win_draw(): glClearColor(0.9, 0.9, 0.9, 0.0) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) glRasterPos2i(1,4) # how do I set the color text again? glColor3ub(255, 255, 255) Draw.Text("Press Q to quit") #glDrawPixels(1, 1, GL_RGB, GL_UNSIGNED_BYTE, imgbuf) #return glColor3ub(255, 31, 12) glBegin(GL_QUADS) glVertex2f(100.5, 100.5) glVertex2f(100.5, 150.5) glVertex2f(150.5, 150.5) glVertex2f(150.5, 100.5) glEnd() glMatrixMode(GL_MODELVIEW) glLoadIdentity() glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0) for tri in triangles: if tri.children: continue glColor3ub(*tri.color) glBegin(GL_TRIANGLES) glVertex3f(*tri.verts[0]) glVertex3f(*tri.verts[1]) glVertex3f(*tri.verts[2]) glEnd() glColor3ub(0,0,0) glBegin(GL_LINE_LOOP) glVertex3f(*tri.verts[0]) glVertex3f(*tri.verts[1]) glVertex3f(*tri.verts[2]) glEnd() #print imgbuf[0:3] def win_event(event_num, event_value): global xvalue global yvalue # act on a window event if (event_num == Draw.QKEY): Draw.Exit() if (event_num == Draw.MOUSEX): xvalue = event_value if (event_num == Draw.MOUSEY): yvalue = event_value if (event_num == Draw.LEFTMOUSE and event_value): # event_value 1 on mouse down glReadPixels( xvalue, yvalue, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, imgbuf) #Blender.Draw.Redraw() triindex = colortoindex(imgbuf) if (imgbuf[0] or imgbuf[1] or imgbuf[2]) and triindex < len(triangles): # subdivide triangles[triindex] parent = triangles[triindex] ov = parent.verts nv = [midpoint(ov[0],ov[1]),midpoint(ov[1],ov[2]),midpoint(ov[2],ov[0])] nt1 = triangle(None) nt1.verts = [ov[0],nv[0],nv[2]] nt2 = triangle(None) nt2.verts = [nv[0], ov[1], nv[1]] nt3 = triangle(None) nt3.verts = [nv[1], ov[2], nv[2]] nt4 = triangle(None) nt4.verts = [nv[0], nv[1], nv[2]] triangles.extend([nt1,nt2,nt3,nt4]) print "subdivided", triindex parent.children = [nt1,nt2,nt3,nt4] Blender.Draw.Redraw() def win_button(button_num): # act on a button push pass Blender.Draw.Register(win_draw, win_event, win_button)