Exemplo n.º 1
0
void Renderer::render(const shared_ptr<Scene>& _scene, bool _withNames, bool _fastDraw){
	if(!initDone || _scene->glDirty || (_scene->gl && _scene->gl->dirty)){
		init(_scene);
		_scene->glDirty=false;
		if(_scene->gl) _scene->gl->dirty=false;
	}
	assert(initDone);

	switch(fast){
		case FAST_ALWAYS: fastDraw=true; break;
		case FAST_UNFOCUSED: fastDraw=_fastDraw; break;
		case FAST_NEVER: fastDraw=false; break;
	}

	// make a copy to see if it were true the whole time
	bool wasSetRefNow(setRefNow);

	withNames=_withNames; // used in many methods
	if(withNames) glNamedObjects.clear();

	// acquire shared_ptr to scene
	{ scene=_scene; }

	// smuggle scene and ourselves into GLViewInfo for use with GlRep and field functors
	viewInfo.scene=scene.get();
	viewInfo.renderer=this;

	setClippingPlanes();
	setLighting();
	drawPeriodicCell();

	fieldDispatcher.scene=scene.get(); fieldDispatcher.updateScenePtr();

	for(auto& f: scene->fields){
		fieldDispatcher(f,&viewInfo);
	}

	if(engines){
		for(const auto& e: scene->engines){
			if(!e || e->dead) continue; // !e should not happen, but make sure
			glScopedName name(viewInfo,e,shared_ptr<Node>());
			e->render(viewInfo);
		}
	}

	for(const shared_ptr<GlExtraDrawer>& d: extraDrawers){
		if(!d || d->dead) continue;
		glPushMatrix();
			d->scene=scene.get();
			d->render();
		glPopMatrix();
	}
	
	// if ref positions were set the whole time, unset here, it is done for all nodes
	if(setRefNow && wasSetRefNow){ setRefNow=false; }

	if(withNames) cerr<<"render(withNames==true) done, "<<glNamedObjects.size()<<" objects inserted"<<endl;

	// release the shared_ptr; must be GIL-protected since descruction of Python-constructed object without GIL causes crash
	{ GilLock lock; scene.reset(); }


}
Exemplo n.º 2
0
void OpenGLRenderer::render(const shared_ptr<Scene>& _scene,Body::id_t selection){

	gilLock lockgil;
	if(!initDone) init();
	assert(initDone);
	selId = selection;

	scene=_scene;

	// assign scene inside functors
	boundDispatcher.updateScenePtr();
	geomDispatcher.updateScenePtr();
	physDispatcher.updateScenePtr();
	shapeDispatcher.updateScenePtr();
	// stateDispatcher.updateScenePtr();

	// just to make sure, since it is not initialized by default
	if(!scene->bound) scene->bound=shared_ptr<Aabb>(new Aabb);

	// recompute emissive light colors for highlighted bodies
	Real now=TimingInfo::getNow(/*even if timing is disabled*/true)*1e-9;
	highlightEmission0[0]=highlightEmission0[1]=highlightEmission0[2]=.8*normSquare(now,1);
	highlightEmission1[0]=highlightEmission1[1]=highlightEmission0[2]=.5*normSaw(now,2);

	// clipping
	assert(clipPlaneNormals.size()==(size_t)numClipPlanes);
	for(size_t i=0;i<(size_t)numClipPlanes; i++){
		// someone could have modified those from python and truncate the vectors; fill those here in that case
		if(i==clipPlaneSe3.size()) clipPlaneSe3.push_back(Se3r(Vector3r::Zero(),Quaternionr::Identity()));
		if(i==clipPlaneActive.size()) clipPlaneActive.push_back(false);
		if(i==clipPlaneNormals.size()) clipPlaneNormals.push_back(Vector3r::UnitX());
		// end filling stuff modified from python
		if(clipPlaneActive[i]) clipPlaneNormals[i]=clipPlaneSe3[i].orientation*Vector3r(0,0,1);
		/* glBegin(GL_LINES);glVertex3v(clipPlaneSe3[i].position);glVertex3v(clipPlaneSe3[i].position+clipPlaneNormals[i]);glEnd(); */
	}
	// set displayed Se3 of body (scaling) and isDisplayed (clipping)
	setBodiesDispInfo();

	glClearColor(bgColor[0],bgColor[1],bgColor[2],1.0);

	// set light sources
	glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,1); // important: do lighting calculations on both sides of polygons

	const GLfloat pos[4]	= {(float) light1Pos[0], (float) light1Pos[1], (float) light1Pos[2],1.0};
	const GLfloat ambientColor[4]={0.2,0.2,0.2,1.0};
	const GLfloat specularColor[4]={1,1,1,1.f};
	const GLfloat diffuseLight[4] = { (float) light1Color[0], (float) light1Color[1], (float) light1Color[2], 1.0f };
	glLightfv(GL_LIGHT0, GL_POSITION,pos);
	glLightfv(GL_LIGHT0, GL_SPECULAR, specularColor);
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambientColor);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
	if (light1) glEnable(GL_LIGHT0); else glDisable(GL_LIGHT0);

	const GLfloat pos2[4]	= {(float) light2Pos[0], (float) light2Pos[1], (float) light2Pos[2],1.0};
	const GLfloat ambientColor2[4]={0.0,0.0,0.0,1.0};
	const GLfloat specularColor2[4]={1,1,0.6,1.f};
	const GLfloat diffuseLight2[4] = { (float) light2Color[0], (float) light2Color[1], (float) light2Color[2], 1.0f };
	glLightfv(GL_LIGHT1, GL_POSITION,pos2);
	glLightfv(GL_LIGHT1, GL_SPECULAR, specularColor2);
	glLightfv(GL_LIGHT1, GL_AMBIENT, ambientColor2);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseLight2);
	if (light2) glEnable(GL_LIGHT1); else glDisable(GL_LIGHT1);

	glEnable(GL_LIGHTING);

	glEnable(GL_CULL_FACE);
	// http://www.sjbaker.org/steve/omniv/opengl_lighting.html
	glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
	glEnable(GL_COLOR_MATERIAL);
	//Shared material settings
	resetSpecularEmission();

	drawPeriodicCell();

	if (dof || id) renderDOF_ID();
	if (bound) renderBound();

	if (shape) renderShape();
	if (intrAllWire) renderAllInteractionsWire();
	if (intrGeom) renderIGeom();
	if (intrPhys) renderIPhys();

	FOREACH(const shared_ptr<GlExtraDrawer> d, extraDrawers){
		if(d->dead) continue;
		glPushMatrix();
			d->scene=scene.get();
			d->render();
		glPopMatrix();
	}


}