We don't need a torus function...

This commit is contained in:
Quantum 2018-08-23 21:57:26 -04:00
parent a862302408
commit b54601f9f7
4 changed files with 13 additions and 174 deletions

View file

@ -1,7 +1,4 @@
from libc.math cimport sin, cos, sqrt
from libc.stdlib cimport malloc, free
from libc.string cimport memcpy
cimport cython
include "_cyopengl.pxi"
cdef float PI = 3.1415926535897932324626
@ -12,60 +9,6 @@ cdef extern from "Python.h":
const char* PyBytes_AsString(bytes o)
@cython.cdivision(True)
cpdef torus(float major_radius, float minor_radius, int n_major, int n_minor, tuple material, int shininess=125):
"""
Torus function from the OpenGL red book.
"""
glPushAttrib(GL_CURRENT_BIT)
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, [material[0], material[1], material[2], material[3]])
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, [1, 1, 1, 1])
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess)
assert n_major > 0 and n_minor > 0
assert minor_radius > 0 and major_radius > 0
cdef float major_s, minor_s
cdef float a0, a1, x0, y0, x1, y1, b, c, r, z, m, x, y, z2
cdef int i, j
with nogil:
major_s = TWOPI / n_major
minor_s = TWOPI / n_minor
for i in xrange(n_major):
a0 = i * major_s
a1 = a0 + major_s
x0 = cos(a0)
y0 = sin(a0)
x1 = cos(a1)
y1 = sin(a1)
glBegin(GL_TRIANGLE_STRIP)
for j in xrange(n_minor + 1):
b = j * minor_s
c = cos(b)
r = minor_radius * c + major_radius
z = minor_radius * sin(b)
x = x0 * c
y = y0 * c
z2 = z / minor_radius
m = 1.0 / sqrt(x * x + y * y + z2 * z2)
glNormal3f(x * m, y * z, z2 * m)
glVertex3f(x0 * r, y0 * r, z)
x = x1 * c
y = y1 * c
m = 1.0 / sqrt(x * x + y * y + z2 * z2)
glNormal3f(x * m, y * z, z2 * m)
glVertex3f(x1 * r, y1 * r, z)
glEnd()
glPopAttrib()
cpdef bytes bgr_to_rgb(bytes buffer, int width, int height, bint alpha=0):
cdef int length = len(buffer)
cdef int depth = length / (width * height)

View file

@ -1,16 +1,16 @@
from libc.string cimport strcmp, strlen
from libc.stdlib cimport malloc, free, atof
from libc.stdio cimport fopen, fclose, fgets, FILE
cimport cython
from punyverse.texture import load_texture
include "_cyopengl.pxi"
from uuid import uuid4
import os
import gzip
import bz2
import zipfile
from libc.string cimport strcmp, strlen
cimport cython
include "_cyopengl.pxi"
from punyverse.texture import load_texture
def zip_open(file):
zip = zipfile.ZipFile(file)
return zip.open(zip.namelist()[0])
@ -265,6 +265,7 @@ cdef class WavefrontObject(object):
model_base = None
def load_model(path):
global model_base
if model_base is None:
@ -275,6 +276,7 @@ def load_model(path):
path = path.decode('mbcs' if os.name == 'nt' else 'utf8')
return WavefrontObject(path)
@cython.nonecheck(False)
cdef inline void point(Face f, WavefrontObject m, int tex_id, float sx, float sy, float sz, int n):
cdef float x, y, z
@ -289,6 +291,7 @@ cdef inline void point(Face f, WavefrontObject m, int tex_id, float sx, float sy
x, y, z = m.vertices[f.verts[n]]
glVertex3f(x * sx, y * sy, z * sz)
cpdef int model_list(WavefrontObject model, float sx=1, float sy=1, float sz=1, object rotation=(0, 0, 0)):
for m, text in model.materials.iteritems():
if text.texture:

View file

@ -4,12 +4,11 @@ from random import random, gauss, choice
# noinspection PyUnresolvedReferences
from six.moves import range
from pyglet.gl import *
from pyglet.gl.gl_info import have_extension
TWOPI = pi * 2
__all__ = ['compile', 'ortho', 'frustrum', 'crosshair', 'circle', 'disk', 'sphere', 'colourball', 'torus', 'belt',
'flare', 'normal_sphere', 'glSection', 'glMatrix', 'glRestore', 'progress_bar']
__all__ = ['compile', 'ortho', 'frustrum', 'crosshair', 'circle', 'disk', 'sphere', 'colourball', 'belt',
'flare', 'glSection', 'glMatrix', 'glRestore', 'progress_bar']
class glSection(object):
@ -203,69 +202,6 @@ def colourball(r, lats, longs, colour, fv4=GLfloat * 4):
gluDeleteQuadric(sphere)
def normal_sphere(r, divide, tex, normal, lighting=True, inside=False, fv4=GLfloat * 4):
if (not have_extension('GL_ARB_multitexture') or not
have_extension('GL_ARB_texture_env_combine') or not
have_extension('GL_EXT_texture_env_dot3')):
print('No hardware normal mapping support. No bumping for you.')
return sphere(r, divide, divide, tex, lighting, inside)
from .texture import load_texture
normal = load_texture(normal)
with glRestore(GL_ENABLE_BIT | GL_TEXTURE_BIT):
glEnable(GL_CULL_FACE)
glCullFace(GL_FRONT if inside else GL_BACK)
glActiveTextureARB(GL_TEXTURE0_ARB)
glBindTexture(GL_TEXTURE_2D, normal)
glEnable(GL_TEXTURE_2D)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB)
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGBA_ARB)
glActiveTextureARB(GL_TEXTURE1_ARB)
glBindTexture(GL_TEXTURE_2D, tex)
glEnable(GL_TEXTURE_2D)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB)
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE)
if lighting:
glDisable(GL_BLEND)
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, fv4(1, 1, 1, 0))
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, fv4(1, 1, 1, 0))
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 125)
else:
glDisable(GL_LIGHTING)
glBindTexture(GL_TEXTURE_2D, tex)
twopi_divide = TWOPI / divide
pi_divide = pi / divide
with glSection(GL_TRIANGLE_STRIP):
for j in range(divide + 1):
phi1 = j * twopi_divide
phi2 = (j + 1) * twopi_divide
for i in range(divide + 1):
theta = i * pi_divide
s = phi2 / TWOPI
t = theta / pi
dx, dy, dz = sin(theta) * cos(phi2), sin(theta) * sin(phi2), cos(theta)
glNormal3f(dx, dy, dz)
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, s, 1 - t)
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, s, 1 - t)
glVertex3f(r * dx, r * dy, r * dz)
s = phi1 / TWOPI # x
dx, dy = sin(theta) * cos(phi1), sin(theta) * sin(phi1)
glNormal3f(dx, dy, dz)
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, s, 1 - t)
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, s, 1 - t)
glVertex3f(r * dx, r * dy, r * dz)
def belt(radius, cross, object, count):
for i in range(count):
theta = TWOPI * random()
@ -281,47 +217,6 @@ def belt(radius, cross, object, count):
glCallList(choice(object))
try:
from _glgeom import torus
except ImportError:
def torus(major_radius, minor_radius, n_major, n_minor, material, shininess=125, fv4=GLfloat * 4):
"""
Torus function from the OpenGL red book.
"""
with glRestore(GL_CURRENT_BIT):
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, fv4(*material))
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, fv4(1, 1, 1, 1))
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess)
major_s = TWOPI / n_major
minor_s = TWOPI / n_minor
def n(x, y, z):
m = 1.0 / sqrt(x * x + y * y + z * z)
return x * m, y * m, z * m
for i in range(n_major):
a0 = i * major_s
a1 = a0 + major_s
x0 = cos(a0)
y0 = sin(a0)
x1 = cos(a1)
y1 = sin(a1)
with glSection(GL_TRIANGLE_STRIP):
for j in range(n_minor + 1):
b = j * minor_s
c = cos(b)
r = minor_radius * c + major_radius
z = minor_radius * sin(b)
glNormal3f(*n(x0 * c, y0 * c, z / minor_radius))
glVertex3f(x0 * r, y0 * r, z)
glNormal3f(*n(x1 * c, y1 * c, z / minor_radius))
glVertex3f(x1 * r, y1 * r, z)
def progress_bar(x, y, width, height, filled):
with glRestore(GL_ENABLE_BIT):
glDisable(GL_TEXTURE_2D)

View file

@ -5,6 +5,7 @@ import bz2
import zipfile
import six
# noinspection PyUnresolvedReferences
from six.moves import range
from pyglet.gl import *
@ -136,7 +137,6 @@ class WavefrontObject(object):
else:
type = FACE_QUADS
current_value = -1
texture_len = len(self.textures)
vindices = []
nindices = []
@ -293,8 +293,6 @@ def model_list(model, sx=1, sy=1, sz=1, rotation=(0, 0, 0)):
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, fv4(kx, ky, kz, 1))
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material.shininess)
type = -1
def point(f, vertices, normals, textures, n):
if f.norms:
glNormal3f(*normals[f.norms[n]])