FreeWRL / FreeX3D 4.3.0
primitiveStream.cc
1/*
2** License Applicability. Except to the extent portions of this file are
3** made subject to an alternative license as permitted in the SGI Free
4** Software License B, Version 1.1 (the "License"), the contents of this
5** file are subject only to the provisions of the License. You may not use
6** this file except in compliance with the License. You may obtain a copy
7** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9**
10** http://oss.sgi.com/projects/FreeB
11**
12** Note that, as provided in the License, the Software is distributed on an
13** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17**
18** Original Code. The Original Code is: OpenGL Sample Implementation,
19** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21** Copyright in any portions created by third parties is as indicated
22** elsewhere herein. All Rights Reserved.
23**
24** Additional Notice Provisions: The application programming interfaces
25** established by SGI in conjunction with the Original Code are The
26** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29** Window System(R) (Version 1.3), released October 19, 1998. This software
30** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31** published by SGI, but has not been independently verified as being
32** compliant with the OpenGL(R) version 1.2.1 Specification.
33**
34*/
35/*
36*/
37
38#include "gluos.h"
39#include <stdlib.h>
40#include <stdio.h>
41#include <assert.h>
42#include <libnurbs2.h>
43//#include <GL/gl.h>
44
45#include "primitiveStream.h"
46
47Int primStream::num_triangles()
48{
49 Int i;
50 Int ret=0;
51 for(i=0; i<index_lengths; i++)
52 {
53 ret += lengths[i]-2;
54 }
55 return ret;
56}
57
58
59
60/*the begining of inserting a new primitive.
61 *reset counter to be 0.
62 */
63void primStream::begin()
64{
65 counter = 0;
66}
67
68void primStream::insert(Real u, Real v)
69{
70 /*if the space cannot hold u and v,
71 *we have to expand the array
72 */
73 if(index_vertices+1 >= size_vertices) {
74 Real* temp = (Real*) malloc (sizeof(Real) * (2*size_vertices + 2));
75 assert(temp);
76
77 /*copy*/
78 for(Int i=0; i<index_vertices; i++)
79 temp[i] = vertices[i];
80
81 free(vertices);
82 vertices = temp;
83 size_vertices = 2*size_vertices + 2;
84 }
85
86 vertices[index_vertices++] = u;
87 vertices[index_vertices++] = v;
88 counter++;
89}
90
91/*the end of a primitive.
92 *increase index_lengths
93 */
94void primStream::end(Int type)
95{
96 Int i;
97 /*if there is no vertex in this primitive,
98 *nothing needs to be done
99 */
100 if(counter == 0) return ;
101
102 if(index_lengths >= size_lengths){
103 Int* temp = (Int*) malloc(sizeof(Int) * (2*size_lengths + 2));
104 assert(temp);
105 Int* tempTypes = (Int*) malloc(sizeof(Int) * (2*size_lengths + 2));
106 assert(tempTypes);
107
108 /*copy*/
109 for(i=0; i<index_lengths; i++){
110 temp[i] = lengths[i];
111 tempTypes[i] = types[i];
112 }
113
114 free(lengths);
115 free(types);
116 lengths = temp;
117 types = tempTypes;
118 size_lengths = 2*size_lengths + 2;
119 }
120 lengths[index_lengths] = counter;
121 types[index_lengths] = type;
122 index_lengths++;
123}
124
125void primStream::print()
126{
127 Int i,j,k;
128 printf("index_lengths=%i,size_lengths=%i\n", index_lengths, size_lengths);
129 printf("index_vertices=%i,size_vertices=%i\n", index_vertices, size_vertices);
130 k=0;
131 for(i=0; i<index_lengths; i++)
132 {
133 if(types[i] == PRIMITIVE_STREAM_FAN)
134 printf("primitive-FAN:\n");
135 else
136 printf("primitive-STRIP:\n");
137 for(j=0; j<lengths[i]; j++)
138 {
139 printf("(%f,%f) ", vertices[k], vertices[k+1]);
140 k += 2;
141 }
142 printf("\n");
143 }
144}
145
146primStream::primStream(Int sizeLengths, Int sizeVertices)
147{
148 lengths = (Int*)malloc (sizeof(Int) * sizeLengths);
149 assert(lengths);
150 types = (Int*)malloc (sizeof(Int) * sizeLengths);
151 assert(types);
152
153 vertices = (Real*) malloc(sizeof(Real) * sizeVertices);
154 assert(vertices);
155
156 index_lengths = 0;
157 index_vertices = 0;
158 size_lengths = sizeLengths;
159 size_vertices = sizeVertices;
160}
161
162primStream::~primStream()
163{
164 free(lengths);
165 free(types);
166 free(vertices);
167}
168
169void primStream::draw()
170{
171 Int i,j,k;
172 k=0;
173#ifdef HAVE_GL_H
174 for(i=0; i<index_lengths; i++)
175 {
176 switch(types[i]){
177 case PRIMITIVE_STREAM_FAN:
178 glBegin(GL_TRIANGLE_FAN);
179 break;
180 case PRIMITIVE_STREAM_STRIP:
181 glBegin(GL_TRIANGLE_STRIP);
182 break;
183 }
184
185 for(j=0; j<lengths[i]; j++){
186 glVertex2fv(vertices+k);
187 k += 2;
188 }
189 glEnd();
190 }
191#endif
192}
193