Skip to content

Commit b47496e

Browse files
committed
add: renderer
1 parent b28884f commit b47496e

File tree

3 files changed

+180
-11
lines changed

3 files changed

+180
-11
lines changed

OpenGL_PlayGround.py

-11
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,6 @@ def draw_points(self):
5151
glFlush()
5252

5353

54-
# def point():
55-
# glClear(GL_COLOR_BUFFER_BIT)
56-
# glColor3f(1.0, 0, 0)
57-
# glPointSize(1)
58-
#
59-
# glBegin(GL_POINTS)
60-
# glVertex2i(100, 300)
61-
# glEnd()
62-
# glFlush()
63-
64-
6554
points = Points()
6655

6756

pointset.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""Geometry type for "point-arrays" w/ colour support"""
2+
from OpenGL.GL import *
3+
from vrml.vrml97 import basenodes
4+
from OpenGLContext.scenegraph import coordinatebounded
5+
from OpenGLContext.arrays import array
6+
from OpenGL.extensions import alternate
7+
from OpenGL.GL.ARB.point_parameters import *
8+
from OpenGL.GL.EXT.point_parameters import *
9+
import logging
10+
log = logging.getLogger( __name__ )
11+
12+
glPointParameterf = alternate(
13+
glPointParameterf, glPointParameterfARB,glPointParameterfEXT
14+
)
15+
glPointParameterfv = alternate(
16+
glPointParameterfv, glPointParameterfvARB, glPointParameterfEXT
17+
)
18+
RESET_ATTENUATION = array( [1,0,0],'f')
19+
20+
class PointSet(
21+
coordinatebounded.CoordinateBounded,
22+
basenodes.PointSet
23+
):
24+
"""VRML97-style Point-Set object
25+
26+
http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-IS-VRML97WithAmendment1/part1/nodesRef.html#PointSet
27+
"""
28+
def render (
29+
self,
30+
visible = 1, # can skip normals and textures if not
31+
lit = 1, # can skip normals if not
32+
textured = 1, # can skip textureCoordinates if not
33+
transparent = 0, # need to sort triangle geometry...
34+
mode = None, # the renderpass object
35+
):
36+
"""Render the point-set, requires coord attribute be present
37+
38+
if color is present (and has the same length as coord), will
39+
render using colors mapped 1:1, otherwise will use the current
40+
color.color
41+
"""
42+
if not self.coord:
43+
# can't render nothing
44+
return 1
45+
points = self.coord.point
46+
if not len(points):
47+
# can't render nothing
48+
return 1
49+
glVertexPointerf(points)
50+
glEnableClientState( GL_VERTEX_ARRAY )
51+
52+
if visible and self.color:
53+
colors = self.color.color
54+
if len(colors) != len(points):
55+
# egads, a content error
56+
if __debug__:
57+
log.warn(
58+
"""PointSet %s has different number of point (%s) and color (%s) values""",
59+
str(self),
60+
len(points),
61+
len(colors),
62+
)
63+
else:
64+
glColorMaterial( GL_FRONT_AND_BACK, GL_DIFFUSE)
65+
glEnable( GL_COLOR_MATERIAL )
66+
glColorPointerf ( colors )
67+
glEnableClientState( GL_COLOR_ARRAY )
68+
glDisable( GL_LIGHTING )
69+
if textured:
70+
# TODO: check for version/extension first!
71+
# point-sprites instead of regular points...
72+
glEnable(GL_POINT_SPRITE);
73+
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE)
74+
glPointSize( self.size )
75+
if glPointParameterf:
76+
glPointParameterf( GL_POINT_SIZE_MIN, self.minSize )
77+
glPointParameterf( GL_POINT_SIZE_MAX, self.maxSize )
78+
glPointParameterfv( GL_POINT_DISTANCE_ATTENUATION, self.attenuation )
79+
glDrawArrays( GL_POINTS, 0, len(points))
80+
glDisableClientState( GL_VERTEX_ARRAY )
81+
glDisable( GL_COLOR_MATERIAL )
82+
if textured:
83+
glDisable( GL_POINT_SPRITE )
84+
if glPointParameterf:
85+
glPointParameterf( GL_POINT_SIZE_MIN, 0.0 )
86+
glPointParameterf( GL_POINT_SIZE_MAX, 1.0 )
87+
glPointParameterfv( GL_POINT_DISTANCE_ATTENUATION, RESET_ATTENUATION )
88+
glPointSize( 1.0 )
89+
glDisableClientState( GL_COLOR_ARRAY )
90+
return 1
91+
def boundingVolume( self, mode=None ):
92+
"""Create a bounding-volume object for this node"""
93+
from OpenGLContext.scenegraph import boundingvolume
94+
return boundingvolume.volumeFromCoordinate(
95+
self.coord,
96+
)

rednerer.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import sys
2+
from OpenGL.GL import *
3+
from OpenGL.GLU import *
4+
from OpenGL.GLUT import *
5+
6+
class Renderer:
7+
points = []
8+
POINT_SIZE = 5
9+
10+
def init(self):
11+
glClearColor(0, 0, 0, 0)
12+
gluOrtho2D(0,1000, 0, 1000)
13+
14+
def test(self):
15+
glClear(GL_COLOR_BUFFER_BIT)
16+
glColor3f(1, 0, 0)
17+
glBegin(GL_TRIANGLES)
18+
19+
glColor3f(1, 0, 0)
20+
glVertex2f(0, 0)
21+
22+
glColor3f(0, 1, 0)
23+
glVertex2f(500, 1000)
24+
25+
glColor3f(0, 0, 1)
26+
glVertex2f(1000, 0)
27+
28+
glEnd()
29+
glFlush()
30+
31+
def set_points(self, point):
32+
assert isinstance(point, Point)
33+
self.points.append(point)
34+
35+
36+
def draw_points(self):
37+
glClear(GL_COLOR_BUFFER_BIT)
38+
glPointSize(self.POINT_SIZE)
39+
glBegin(GL_POINTS)
40+
for point in self.points:
41+
assert isinstance(point , Point)
42+
glColor3f(point.color[0], point.color[1], point.color[2])
43+
glVertex2i(point.x * self.POINT_SIZE, point.y * self.POINT_SIZE)
44+
45+
glEnd()
46+
glFlush()
47+
48+
49+
50+
def draw_scene(self):
51+
self.draw_points()
52+
glutSwapBuffers()
53+
54+
55+
def main(self):
56+
glutInit(sys.argv)
57+
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
58+
glutInitWindowSize(1000, 1000)
59+
glutInitWindowPosition(50, 50)
60+
glutCreateWindow("FUCkCPP")
61+
self.init()
62+
glutDisplayFunc(self.draw_scene)
63+
glutIdleFunc(self.draw_scene)
64+
glutMainLoop()
65+
66+
def test_points(self):
67+
for i in range(1000):
68+
point = Point(i, i ,(1,0,1))
69+
self.set_points(point)
70+
71+
72+
class Point:
73+
def __init__(self, x, y, color):
74+
self.x = x
75+
self.y = y
76+
self.color = color
77+
x = 0
78+
y = 0
79+
color = (1,1,1)
80+
81+
if __name__ == "__main__":
82+
render = Renderer()
83+
render.main()
84+
render.test_points()

0 commit comments

Comments
 (0)