Skip to content

Commit fd24965

Browse files
committed
move remaining GL code out of OpenGLQ-sphere.c
1 parent 93a5938 commit fd24965

2 files changed

Lines changed: 33 additions & 93 deletions

File tree

lib/PDL/Graphics/OpenGLQ-sphere.c

Lines changed: 18 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -22,74 +22,9 @@
2222

2323
#include <stddef.h>
2424
#include <stdlib.h>
25+
#include <stdint.h>
2526
#include <math.h>
2627

27-
#ifdef __APPLE__
28-
#include <OpenGL/gl.h>
29-
#else
30-
#include <GL/gl.h>
31-
#endif
32-
33-
/* Drawing geometry:
34-
* Explanation of the functions has to be separate for the polyhedra and
35-
* the non-polyhedra (objects with a circular cross-section).
36-
* Non-polyhedra:
37-
* - We have implemented the sphere, cylinder, cone, and torus.
38-
* - All shapes are characterized by two parameters: the number of
39-
* subdivisions along two axes used to construct the shape's vertices
40-
* (e.g. stacks and slices for the sphere).
41-
* As different subdivisions are most suitable for different shapes,
42-
* and are thus also named differently, I won't provide general comments
43-
* on them here.
44-
* - Solids are drawn using glDrawArrays and GL_TRIANGLE_STRIP. Each
45-
* strip covers one revolution around one of the two subdivision axes
46-
* of the shape.
47-
*/
48-
49-
50-
/* Draw the geometric shape with filled triangles
51-
*
52-
* Arguments:
53-
* GLfloat *vertices, GLfloat *normals, GLfloat *textcs, GLsizei numVertices
54-
* The vertex coordinate, normal and texture coordinate buffers, and the
55-
* number of entries in those
56-
* GLuint *vertIdxs
57-
* a vertex indices buffer, optional (not passed for the polyhedra with
58-
* triangular faces)
59-
* GLsizei numParts, GLsizei numVertPerPart
60-
* polyhedra: not used for polyhedra with triangular faces
61-
(numEdgePerFace==3), as each vertex+normal pair is drawn only once,
62-
so no vertex indices are used.
63-
Else, the shape was triangulated (DECOMPOSE_TO_TRIANGLE), leading to
64-
reuse of some vertex+normal pairs, and thus the need to draw with
65-
glDrawElements. numParts is always 1 in this case (we can draw the
66-
whole object with one call to glDrawElements as the vertex index
67-
array contains separate triangles), and numVertPerPart indicates
68-
the number of vertex indices in the vertex array.
69-
* non-polyhedra: number of parts (GL_TRIANGLE_STRIPs) to be drawn
70-
separately (numParts calls to glDrawElements) to create the object.
71-
numVertPerPart indicates the number of vertex indices to be
72-
processed at each draw call.
73-
* numParts * numVertPerPart gives the number of entries in the vertex
74-
* array vertIdxs
75-
*/
76-
void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLsizei numVertices,
77-
GLuint *vertIdxs, GLsizei numParts, GLsizei numVertIdxsPerPart)
78-
{
79-
int i;
80-
81-
glEnableClientState(GL_VERTEX_ARRAY);
82-
glEnableClientState(GL_NORMAL_ARRAY);
83-
84-
glVertexPointer(3, GL_FLOAT, 0, vertices);
85-
glNormalPointer(GL_FLOAT, 0, normals);
86-
87-
for (i=0; i<numParts; i++)
88-
glDrawElements(GL_TRIANGLE_STRIP, numVertIdxsPerPart, GL_UNSIGNED_INT, vertIdxs+i*numVertIdxsPerPart);
89-
90-
glDisableClientState(GL_VERTEX_ARRAY);
91-
glDisableClientState(GL_NORMAL_ARRAY);
92-
}
9328

9429
/*
9530
* Compute lookup table of cos and sin values forming a circle
@@ -101,19 +36,19 @@ void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLsizei numVertic
10136
* The last entry is exactly the same as the first
10237
* The sign of n can be flipped to get the reverse loop
10338
*/
104-
static char *fghCircleTable(GLfloat **sint, GLfloat **cost, const int n, const GLboolean halfCircle)
39+
static char *fghCircleTable(float **sint, float **cost, const int n, const char halfCircle)
10540
{
10641
int i;
10742

10843
/* Table size, the sign of n flips the circle direction */
10944
const int size = abs(n);
11045

11146
/* Determine the angle between samples */
112-
const GLfloat angle = (halfCircle?1:2)*(GLfloat)M_PI/(GLfloat)( ( n == 0 ) ? 1 : n );
47+
const float angle = (halfCircle?1:2)*(float)M_PI/(float)( ( n == 0 ) ? 1 : n );
11348

11449
/* Allocate memory for n samples, plus duplicate of first entry at the end */
115-
*sint = malloc(sizeof(GLfloat) * (size+1));
116-
*cost = malloc(sizeof(GLfloat) * (size+1));
50+
*sint = malloc(sizeof(float) * (size+1));
51+
*cost = malloc(sizeof(float) * (size+1));
11752

11853
if (!(*sint) || !(*cost))
11954
{
@@ -128,8 +63,8 @@ static char *fghCircleTable(GLfloat **sint, GLfloat **cost, const int n, const G
12863

12964
for (i=1; i<size; i++)
13065
{
131-
(*sint)[i] = (GLfloat)sin(angle*i);
132-
(*cost)[i] = (GLfloat)cos(angle*i);
66+
(*sint)[i] = (float)sin(angle*i);
67+
(*cost)[i] = (float)cos(angle*i);
13368
}
13469

13570

@@ -147,7 +82,7 @@ static char *fghCircleTable(GLfloat **sint, GLfloat **cost, const int n, const G
14782
return NULL;
14883
}
14984

150-
int calc_nVert(GLint slices, GLint stacks) {
85+
int calc_nVert(int slices, int stacks) {
15186
/* number of unique vertices */
15287
if (slices==0 || stacks<2)
15388
{
@@ -157,15 +92,15 @@ int calc_nVert(GLint slices, GLint stacks) {
15792
return slices*(stacks-1)+2;
15893
}
15994

160-
char *fghGenerateSphere(GLfloat radius, GLint slices, GLint stacks, GLfloat *vertices, GLfloat *normals, int nVert)
95+
char *fghGenerateSphere(float radius, int slices, int stacks, float *vertices, float *normals, int nVert)
16196
{
16297
int i,j;
16398
int idx = 0; /* idx into vertex/normal buffer */
164-
GLfloat x,y,z;
99+
float x,y,z;
165100

166101
/* Pre-computed circle */
167-
GLfloat *sint1,*cost1;
168-
GLfloat *sint2,*cost2;
102+
float *sint1,*cost1;
103+
float *sint2,*cost2;
169104

170105
if (nVert == 0)
171106
{
@@ -174,9 +109,9 @@ char *fghGenerateSphere(GLfloat radius, GLint slices, GLint stacks, GLfloat *ver
174109
}
175110

176111
/* precompute values on unit circle */
177-
char *err = fghCircleTable(&sint1,&cost1,-slices,GL_FALSE);
112+
char *err = fghCircleTable(&sint1,&cost1,-slices,0);
178113
if (err) return err;
179-
err = fghCircleTable(&sint2,&cost2, stacks,GL_TRUE);
114+
err = fghCircleTable(&sint2,&cost2, stacks,1);
180115
if (err) return err;
181116

182117
/* top */
@@ -222,9 +157,9 @@ char *fghGenerateSphere(GLfloat radius, GLint slices, GLint stacks, GLfloat *ver
222157
return NULL;
223158
}
224159

225-
void calc_strip_idx(GLuint *stripIdx, GLint slices, GLint stacks, int nVert) {
160+
void calc_strip_idx(uint32_t *stripIdx, int slices, int stacks, int nVert) {
226161
int i,j,idx;
227-
GLuint offset;
162+
uint32_t offset;
228163
/* top stack */
229164
for (j=0, idx=0; j<slices; j++, idx+=2)
230165
{
@@ -258,10 +193,10 @@ void calc_strip_idx(GLuint *stripIdx, GLint slices, GLint stacks, int nVert) {
258193
stripIdx[idx+1] = offset;
259194
}
260195

261-
int calc_numVertIdxsPerPart(GLint slices) {
196+
int calc_numVertIdxsPerPart(int slices) {
262197
return (slices+1)*2;
263198
}
264199

265-
int calc_nIdx(GLint slices, GLint stacks) {
200+
int calc_nIdx(int slices, int stacks) {
266201
return calc_numVertIdxsPerPart(slices)*stacks;
267202
}

lib/PDL/Graphics/OpenGLQ.pd

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,11 @@ pp_def(
6868
uint [t] stripIdx(nIdx=CALC(calc_nIdx($COMP(slices),$COMP(stacks))));',
6969
OtherPars => 'float radius; int slices; int stacks;',
7070
CHeader => <<'EOF',
71-
void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals,
72-
GLsizei numVertices, GLuint *vertIdxs, GLsizei numParts,
73-
GLsizei numVertIdxsPerPart);
74-
char *fghGenerateSphere(GLfloat radius, GLint slices, GLint stacks, GLfloat *vertices, GLfloat *normals, int nVert);
75-
void calc_strip_idx(GLuint *stripIdx, GLint slices, GLint stacks, int nVert);
76-
int calc_nVert(GLint slices, GLint stacks);
77-
int calc_nIdx(GLint slices, GLint stacks);
78-
int calc_numVertIdxsPerPart(GLint slices);
71+
char *fghGenerateSphere(float radius, int slices, int stacks, float *vertices, float *normals, int nVert);
72+
void calc_strip_idx(uint32_t *stripIdx, int slices, int stacks, int nVert);
73+
int calc_nVert(int slices, int stacks);
74+
int calc_nIdx(int slices, int stacks);
75+
int calc_numVertIdxsPerPart(int slices);
7976
EOF
8077
Code => <<'EOF',
8178
/* Generate vertices and normals */
@@ -86,20 +83,28 @@ if (err) $CROAK("%s", err);
8683
* strip.
8784
*/
8885
calc_strip_idx($P(stripIdx), $COMP(slices), $COMP(stacks), $SIZE(nVert));
86+
glEnableClientState(GL_VERTEX_ARRAY);
87+
glEnableClientState(GL_NORMAL_ARRAY);
88+
glVertexPointer(3, GL_FLOAT, 0, $P(vertices));
89+
glNormalPointer(GL_FLOAT, 0, $P(normals));
90+
int i, numVertIdxsPerPart = calc_numVertIdxsPerPart($COMP(slices));
91+
PDL_ULong *stripIdx = $P(stripIdx);
8992
broadcastloop %{
9093
float oldcoord0 = 0.0, oldcoord1 = 0.0, oldcoord2 = 0.0;
91-
int numVertIdxsPerPart = calc_numVertIdxsPerPart($COMP(slices));
9294
glPushMatrix();
9395
loop(n) %{
9496
glTranslatef(
9597
$coords(tri=>0) - oldcoord0,
9698
$coords(tri=>1) - oldcoord1,
9799
$coords(tri=>2) - oldcoord2
98100
);
99-
fghDrawGeometrySolid($P(vertices),$P(normals),$SIZE(nVert),$P(stripIdx),$COMP(stacks),numVertIdxsPerPart);
101+
for (i=0; i<$COMP(stacks); i++)
102+
glDrawElements(GL_TRIANGLE_STRIP, numVertIdxsPerPart, GL_UNSIGNED_INT, stripIdx+i*numVertIdxsPerPart);
100103
oldcoord0 = $coords(tri=>0), oldcoord1 = $coords(tri=>1), oldcoord2 = $coords(tri=>2);
101104
%}
102105
glPopMatrix();
106+
glDisableClientState(GL_VERTEX_ARRAY);
107+
glDisableClientState(GL_NORMAL_ARRAY);
103108
%}
104109
EOF
105110
@internal

0 commit comments

Comments
 (0)