示例#1
0
void wgd::Material::bind() {
	if (s_current != this) {
		GLfloat mat[4];
		colour c = specular();
		c.toArray(mat);
		glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
		c = diffuse();
		
		//if diffuse is null, use white (rather than transparent)
		if(get(ix::diffuse)==Null) { c.r=1.0; c.g=1.0; c.b=1.0; c.a=1.0; }
		
		//if (Lighting::enabled() == false) {
		//	glColor4f(c.r,c.g,c.b,c.a);
		//}

		//std::cout << "Colours: " << c.r << " " << c.g << " " << c.b << " " << c.a << "\n";

		c.toArray(mat);
		glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
		c = ambient();
		c.toArray(mat);
		glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
		mat[0] = shininess();
		glMaterialfv(GL_FRONT, GL_SHININESS, mat);
		c = emission();
		c.toArray(mat);
		glMaterialfv(GL_FRONT, GL_EMISSION, mat);
	
	
		s_current = this;

		Texture *tex;

		//if (get(ix::textures) != Null) {
			for (int i=0; i<Texture::maxTextureUnits(); i++) {
				tex = texture(i);
				if (tex == 0) continue;
				tex->bind(i);
			}
		//} else {
		//	if (get(ix::texture) != Null) {
		//		tex = texture();
		//		if(tex!=NULL)
		//			tex->bind();
		//	}
		//}

		if (get(ix::shader) != Null) {
			Shader *shade = shader();
			shade->bind();

			if (Shader::current() == shade) {
				//Set shader variables.
				OID vars = get(ix::variables);
				OID val;
				char sbuf[50];

				if (vars != Null) {
					for (OID::iterator i=vars.begin(); i!=vars.end(); i++) {
						(*i).toString(sbuf, 50);
						val = vars[*i];
						
						if (val.isDouble()) shade->setVariable(sbuf, (float)val);
						if (val.isLongLong()) shade->setVariable(sbuf, (int)val);
						
						//also need arrays
						/*if (val.isObject()) {
							//val is an array of n values
							//either need to send them individually, or
							//create an array and send them all at once
							strcat(sbuf, "[0]");
							
							//get number of values
							int n=0;
							for(n=0; val[n]!=Null; n++);
							
							//floats or ints?
							if (val[0].IsFloat()){
								float *array = new float[n];
								for(int i=0; i<n; i++) array[i] = (float)val[i];
								shade->setVariable(sbuf, n, array);
								delete [] array;
								//cout << "Array " << sbuf << " = ";
								//for(int i=0; i<n; i++) cout << array[i] << ", ";
								//cout <<  " (" << n << " values)\n"; 
								
							}
							if (val.IsInt()){
								int *array = new int[n];
								for(int i=0; i<n; i++) array[i] = (int)val[i];
								shade->setVariable(sbuf, n, array);
								delete [] array;
							}	
						}*/
					}
				}
			}
		}

		OID blnd = blending();
		if (blnd == BLEND_MULTIPLY) {
			glBlendFunc (GL_DST_COLOR, GL_ZERO);
		} else if (blnd == BLEND_ONE) {
			glBlendFunc(GL_ONE, GL_ONE);
		} else if (blnd == BLEND_ADD) {
			glBlendFunc(GL_SRC_ALPHA, GL_ONE);
		} else {
			//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		}
		//} else {
		//	glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
		//}
	}
}