glObj.h 4.03 KB
#ifndef STIM_GLOBJ_H
#define STIM_GLOBJ_H

//#include <GL/glew.h>
#include <GL/glut.h>
#include <stim/math/vector.h>
#include <stim/visualization/obj.h>
#include <stim/gl/error.h>


namespace stim
{
/** This class uses the loading functionality of the obj class in order to
 *  Assist with the visualization the segmented vessels.
 *  Uses
*/
template <typename T>
class glObj : public virtual stim::obj<T>
{
private:
	using stim::obj<T>::L;
	using stim::obj<T>::P;
	using stim::obj<T>::F;
//	using stim::obj<T>::numL();
//	using std::vector<T>::size;
//	using std::vector<T>::at;
	GLuint dList;


	void
	init()
	{
		if(glIsList(dList))
			glDeleteLists(dList, 1);
		dList = glGenLists(1);
		glListBase(dList);

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

	}

	void
	Create(GLenum mode, bool blend = false, float opacity = 1.0)
	{
		
		if(blend)
		{
			glEnable(GL_BLEND);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		}
		int len = (int) stim::obj<T>::numL();
		std::vector< stim::vec<float> > line;
		glNewList(dList, GL_COMPILE);
		glLineWidth(3.5);
		for(int i = 0; i < len; i++){
			line = stim::obj<T>::getL_V(i);
			if(mode == GL_SELECT)
			{
				glLoadName(i);
			}
			if(blend == false)
			{
				glColor3f(0.0, 1.0, 0.05);
			}
			else
			{
				glColor4f(50.0/256.0,205.0/256.0, 50.0/256.0, opacity);		///Dark Green.
			} 
			//glColor3ub(rand()%255, rand()%255, rand()%255);
			glBegin(GL_LINE_STRIP);
			for(int j = 0; j < line.size(); j++){
					glVertex3f(
						line[j][0],
						line[j][1],
						line[j][2]
						);
			}
			glEnd();
		}
		if(blend)
			glDisable(GL_BLEND);
		glEndList();
		CHECK_OPENGL_ERROR
	}

	///Method for drawing selectively opaque things.
	void
	Create(GLenum mode, stim::vec3<float> point)
	{
		float l, opacity;
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		int len = (int) stim::obj<T>::numL();
		std::vector< stim::vec<float> > line;
		glNewList(dList, GL_COMPILE);
		glLineWidth(3.5);
		for(int i = 0; i < len; i++){
			line = stim::obj<T>::getL_V(i);
			if(mode == GL_SELECT)
			{
				glLoadName(i);
			}

			glBegin(GL_LINE_STRIP);
			for(int j = 0; j < line.size(); j++){

				l = sqrt(pow((point[0] - line[j][0]),2) + pow((point[1] - line[j][1]),2) + pow((point[2] - line[j][2]),2));
				if(l < 10)
				{
					opacity = 1.0;
				} else if( l > 40) {
					opacity = 0.0;
				} else {
					opacity = l*(-1.0/30.0) + 4.0/3.0;
				}

				glColor4f(50.0/256.0,205.0/256.0, 50.0/256.0, opacity);		///Dark Green.
				glVertex3f(
					line[j][0],
					line[j][1],
					line[j][2]
					);
			}
			glEnd();
		}
		glDisable(GL_BLEND);
		glEndList();
		CHECK_OPENGL_ERROR
	}

public:
	glObj()
	{

	}

	void
	createFromSelf(GLenum mode = GL_RENDER, bool blend = false, float opacity = 1.0)
	{
	//	glPopMatrix();
		init();
		Create(mode, blend, opacity);
	//	glPushMatrix();
	}

	void
	createFromSelf(stim::vec3<float> point, GLenum mode = GL_RENDER)
	{
	//	glPopMatrix();
		init();
		Create(mode, point);
	//	glPushMatrix();
	}

	void
	createFromFile(std::string filename, GLenum mode = GL_RENDER)
	{
		stim::obj<T>::load(filename);
		glPushMatrix();		//Safety Operation to avoid changing the current matrix.
		init();
		Create(mode);
		glPopMatrix();
		CHECK_OPENGL_ERROR
	}
	

	void
	Render(bool blend = false)
	{
		if(blend)
		{
			glEnable(GL_BLEND);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		}
		glCallList(dList);
		if(blend)
			glDisable(GL_BLEND);
		CHECK_OPENGL_ERROR
	}

	void
	RenderLine(int i)
	{
		std::vector < stim::vec<T> > line;
		int len = (int) stim::obj<T>::numL();
		line = stim::obj<T>::getL_V(i);
		glColor3f(0.5, 1.0, 0.5);
		glLineWidth(3.0);
		glBegin(GL_LINE_STRIP);
		for(int j = 0; j < line.size(); j++){
			glVertex3f(
				line[j][0],
				line[j][1],
				line[j][2]
				);
		}
		glEnd();
	}

	void
	RenderLine(std::vector< stim::vec3<T> > l)
	{
		glColor3f(0.5, 1.0, 0.5);
		glLineWidth(3.0);
		glBegin(GL_LINE_STRIP);
		for(int j = 0; j < l.size(); j++){
			glVertex3f(
				l[j][0],
				l[j][1],
				l[j][2]
				);
		}
		glEnd();
	}

};
}
#endif