1 /*
  2   Copyright (c) 2008 Seneca College
  3   Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/)
  4 */
  5 
  6 /**	
  7 	@class c3dl.PrimitiveSet represents a set of primitives within a 
  8 	geometric class. It derives from how .DAE files are structured and is
  9 	roughly equal to a 'primitive collation element'.
 10 	
 11 	<p>All primitive sets in a geometric object share the same coordinate 
 12 	system, so when they are rendered, the matrix stack does not need to 
 13 	be updated or queried. Each primitive set can have its own material 
 14 	and texture if it was defined in the .DAE file.</p>
 15 
 16 	<p>In the callback function of an effect, the geometry will need to be 
 17 	queried for all its primitive sets. This set will need to be iterated
 18 	and each must be rendered. Since each set can have its own material
 19 	and texture, the context must be sent commands to have the proper 
 20 	states set.</p>
 21 	
 22 */
 23 c3dl.PrimitiveSet = function()
 24 {
 25 	this.material = null;
 26 	this.texture = null;
 27 
 28 	this.vertices = null;
 29 	this.normals = null;
 30 	this.texCoords = null;
 31 
 32 	this.boundingSphere = null;
 33 	
 34 	this.buffers = {};
 35 	
 36 	/**
 37 		@private
 38 
 39 		@param {Array} vertices
 40 		@param {Array} normals
 41 		@param {Array} texCoords
 42 	*/
 43 	this.init = function(vertices, normals, texCoords)
 44 	{
 45 		this.vertices = vertices;
 46 		this.normals = normals;
 47 		this.texCoords = texCoords;
 48 		this.boundingSphere = new c3dl.BoundingSphere();
 49 
 50 		// give the bounding sphere the vertices, so it can properly
 51 		// adjust its radius to completely enclose the object. 
 52 		this.boundingSphere.init(this.vertices);
 53 		this.boundingSphere.setPosition([0,0,0]);
 54 	}
 55 	
 56 	/**
 57 		@private
 58 		
 59 	*/
 60 	this.setupVBO = function(glCanvas3D)
 61 	{
 62 		this.buffers.vertices = glCanvas3D.createBuffer();
 63 		this.buffers.normals = glCanvas3D.createBuffer();
 64 		this.buffers.texCoords = glCanvas3D.createBuffer();
 65 
 66 		glCanvas3D.bindBuffer(glCanvas3D.ARRAY_BUFFER, this.buffers.vertices);
 67 		glCanvas3D.bufferData(glCanvas3D.ARRAY_BUFFER, new WebGLFloatArray(this.vertices), glCanvas3D.STATIC_DRAW);
 68 		
 69 		glCanvas3D.bindBuffer(glCanvas3D.ARRAY_BUFFER, this.buffers.normals);
 70 		glCanvas3D.bufferData(glCanvas3D.ARRAY_BUFFER, new WebGLFloatArray(this.normals), glCanvas3D.STATIC_DRAW);
 71 
 72 		glCanvas3D.bindBuffer(glCanvas3D.ARRAY_BUFFER, this.buffers.texCoords);
 73 		glCanvas3D.bufferData(glCanvas3D.ARRAY_BUFFER, new WebGLFloatArray(this.texCoords), glCanvas3D.STATIC_DRAW);
 74 	}
 75 	
 76 	this.getVBOVertices = function()
 77 	{
 78 		return this.buffers.vertices;
 79 	}
 80 
 81 	this.getVBONormals = function()
 82 	{
 83 		return this.buffers.normals;
 84 	}
 85 
 86 	this.getVBOTexCoords = function()
 87 	{
 88 		return this.buffers.texCoords;
 89 	}
 90 			
 91 	/**
 92 		@private
 93 		
 94 		Get a semi-deep copy of this object.  The copy will have deep copies
 95 		of the material and texture, but shallow copies of the vertices, normals and
 96 		texCoords.
 97 		
 98 		@returns {c3dl.PrimitiveSet}
 99 	*/
100 	this.getCopy = function()
101 	{
102 		var copy = new c3dl.PrimitiveSet();
103 		
104 		// shallow copy to save memory.
105 		copy.vertices = this.vertices;
106 		copy.normals = this.normals;
107 		copy.texCoords = this.texCoords;
108 		copy.boundingSphere = this.boundingSphere;
109 		
110 		copy.texture = this.texture;
111 		
112 		// get a deep copy of the material since every collada object's primitive set
113 		// can have its own material.		
114 		copy.material = this.material ? this.material.getCopy() : null;
115 
116 		return copy;
117 	}
118 
119 	/**
120 		Get the path of the texture for this primitive set.
121 		
122 		@returns {String} path of the texture.
123 	*/
124 	this.getTexture = function()
125 	{
126 		return this.texture;
127 	}
128 	
129 	/**		
130 		Get the single dimensional array of vertices of this primitive set.
131 		The array of vertices is in the order x,y,z,x,y,z,...
132 
133 		@returns {Array} Vertices are in the order x,y,z,x,y,z,...
134 	*/
135 	this.getVertices = function()
136 	{
137 		return this.vertices;
138 	}
139 
140 	/**
141 		Get the single dimensional array of normals of this primitive set. 
142 		The array of normals is in the order nx, ny, nz, nx, ny, nz,...
143 
144 		@returns {Array} Normals are in the order nx, ny, nz, nx, ny, nz,...
145 	*/	
146 	this.getNormals = function()
147 	{
148 		return this.normals;
149 	}
150 
151 	/**
152 		Get the single dimensional array of texture coordinates of this 
153 		primitive set. The array of texture coords is in the order u, v, u, v,...
154 		
155 		@returns {Array} Textures coordinates are in the order u, v, u, v,...
156 	*/	
157 	this.getTexCoords = function()
158 	{
159 		return this.texCoords;
160 	}
161 
162 	/**
163 		Get the material of this primitive set.
164 
165 		@returns {c3dl.Material} Material of this primitive set.
166 	*/
167 	this.getMaterial = function()
168 	{
169 		return this.material;
170 	}
171 
172 	/**
173 		@private
174 
175 		@returns {c3dl.BoundingSphere}  the updated bounding sphere object.
176 	*/
177 	this.getBoundingSphere = function()
178 	{
179 		return this.boundingSphere;
180 	}
181 	
182 	
183 	/**
184 		@private
185 		Set the material of this primitive set. The material can't be directly
186 		set by the user, but is set by the library when the .DAE file is being
187 		loaded.
188 		
189 		@param {c3dl.Material}
190 	*/
191 	this.setMaterial = function(material)
192 	{
193 		this.material = material;
194 	}
195 
196 	/**
197 		@private
198 		Set the texture of this primitive set. The texture can't be directly
199 		set, but is set when the .DAE file is being loaded.
200 		
201 		@param {String} texture Path of the texture.
202 	*/
203 	this.setTexture = function(texture)
204 	{
205 		this.texture = texture;
206 	}
207 }
208