Skip to content

Commit 31da50b

Browse files
committed
convert gl_spheres to use degenerate triangles
1 parent ae990a1 commit 31da50b

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

lib/PDL/Graphics/OpenGLQ-sphere.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ void calc_strip_idx(uint32_t *stripIdx, int slices, int stacks, int nVert) {
174174
for (i=0; i<stacks-2; i++, idx+=2)
175175
{
176176
offset = 1+i*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
177+
stripIdx[idx ] = stripIdx[idx-1]; /* degenerate triangle part 1 */
178+
stripIdx[idx+1] = offset+slices; /* degenerate triangle part 2 */
179+
idx+=2;
177180
for (j=0; j<slices; j++, idx+=2)
178181
{
179182
stripIdx[idx ] = offset+j+slices;
@@ -184,6 +187,9 @@ void calc_strip_idx(uint32_t *stripIdx, int slices, int stacks, int nVert) {
184187
}
185188
/* bottom stack */
186189
offset = 1+(stacks-2)*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
190+
stripIdx[idx ] = stripIdx[idx-1]; /* degenerate triangle part 1 */
191+
stripIdx[idx+1] = nVert-1; /* degenerate triangle part 2 */
192+
idx+=2;
187193
for (j=0; j<slices; j++, idx+=2)
188194
{
189195
stripIdx[idx ] = nVert-1; /* zero based index, last element in array (bottom vertex)... */
@@ -193,6 +199,6 @@ void calc_strip_idx(uint32_t *stripIdx, int slices, int stacks, int nVert) {
193199
stripIdx[idx+1] = offset;
194200
}
195201

196-
int calc_numVertIdxsPerPart(int slices) {
197-
return (slices+1)*2;
202+
int calc_numIdxs(int slices, int stacks) {
203+
return (slices+1)*2*stacks + (stacks-1)*2;
198204
}

lib/PDL/Graphics/OpenGLQ.pd

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ pp_addhdr('
5555
int i; \
5656
for (i = 0; i < 3; i++) v[i] *= fac; \
5757
}
58-
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
5958
');
6059

6160
my @internal = (Doc => 'internal', NoPthread => 1);
@@ -66,13 +65,13 @@ pp_def(
6665
Pars => '
6766
[o] vertices(tri=3,nVert=CALC(calc_nVert($COMP(slices),$COMP(stacks))));
6867
[o] normals(tri,nVert);
69-
uint [o] stripIdx(nperpart=CALC(calc_numVertIdxsPerPart($COMP(slices))),nstacks);',
70-
OtherPars => 'float radius; int slices; int stacks => nstacks;',
68+
uint [o] stripIdx(nidx=CALC(calc_numIdxs($COMP(slices),$COMP(stacks))));',
69+
OtherPars => 'float radius; int slices; int stacks;',
7170
CHeader => <<'EOF',
7271
char *fghGenerateSphere(float radius, int slices, int stacks, float *vertices, float *normals, int nVert);
7372
void calc_strip_idx(uint32_t *stripIdx, int slices, int stacks, int nVert);
7473
int calc_nVert(int slices, int stacks);
75-
int calc_numVertIdxsPerPart(int slices);
74+
int calc_numIdxs(int slices, int stacks);
7675
EOF
7776
Code => <<'EOF',
7877
/* Generate vertices and normals */
@@ -90,7 +89,7 @@ pp_def(
9089
'gl_spheres',
9190
GenericTypes => $F,
9291
Pars => 'coords(tri=3,n);',
93-
OtherPars => 'int dim0; int dim1 => nstacks;',
92+
OtherPars => 'int dim0;',
9493
Code => <<'EOF',
9594
float oldcoord0 = 0.0, oldcoord1 = 0.0, oldcoord2 = 0.0;
9695
glPushMatrix();
@@ -100,9 +99,7 @@ loop(n) %{
10099
$coords(tri=>1) - oldcoord1,
101100
$coords(tri=>2) - oldcoord2
102101
);
103-
loop (nstacks) %{
104-
glDrawElements(GL_TRIANGLE_STRIP, $COMP(dim0), GL_UNSIGNED_INT, BUFFER_OFFSET(nstacks * $COMP(dim0) * sizeof(PDL_ULong)));
105-
%}
102+
glDrawElements(GL_TRIANGLE_STRIP, $COMP(dim0), GL_UNSIGNED_INT, 0);
106103
oldcoord0 = $coords(tri=>0), oldcoord1 = $coords(tri=>1), oldcoord2 = $coords(tri=>2);
107104
%}
108105
glPopMatrix();

0 commit comments

Comments
 (0)