GLWidget.cpp 2.43 KB
#include "GLWidget.h"

//constructor 
GLWidget::GLWidget(QWidget *parent)
    : QGLWidget(parent)
{
	//initialize the camera
	SourceList[SelectedSource].S.setPosition(0, 0, 1.6);
	SourceList[SelectedSource].S.LookAt(0, 0, 0);

}

//destructor
GLWidget::~GLWidget()
{
	makeCurrent();

	

	
}

QSize GLWidget::minimumSizeHint() const
{
    return QSize(50, 50);
}

QSize GLWidget::sizeHint() const
{
    return QSize(400, 400);
}

void GLWidget::initializeGL()
{
	GLenum err = glewInit();
	if(GLEW_OK != err)
	{
		printf("Error starting GLEW.");
	}
	fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));

	glClearColor(1.0, 1.0, 1.0, 0.0);
	//glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);
	glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_CULL_FACE);
}

void GLWidget::paintGL()
{
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//set up the camera
	point3D<float> eye = SourceList[RenderSource].S.getPosition();
	vector3D<float> up = SourceList[RenderSource].S.getUp();
	point3D<float> lookat = SourceList[RenderSource].S.getLookAt();
	gluLookAt(eye.x, eye.y, eye.z, lookat.x, lookat.y, lookat.z, up.x, up.y, up.z);

	//clear the screen
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	

	glColor3f(0.0, 1.0, 0.0);

	//if a volume is loaded, render it
	if(VolumeList.size() > 0)
		DrawCube();
	if(RenderSources)
		DrawLights();

	glFlush();
}

void GLWidget::resizeGL(int width, int height)
{
	glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

	float aspect;
	/*if(width > height)
	{
		aspect = (float)height/(float)width;
		gluPerspective(60, aspect, 0.001, 100);
		//glOrtho(-1, 1, -aspect, aspect, -100, 100);
	}
	else
	{*/
		aspect = (float)width/(float)height;
		//glOrtho(-aspect, aspect, -1, 1, -100, 100);
		gluPerspective(60, aspect, 0.01, 100);
	//}
    glMatrixMode(GL_MODELVIEW);
}

void GLWidget::mousePressEvent(QMouseEvent *event)
{
	prevMouse = event->pos();
}

void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
	//find the change in mouse position
	int dx = prevMouse.x() - event->pos().x();
	int dy = prevMouse.y() - event->pos().y();
	prevMouse = event->pos();

	if(event->buttons() == Qt::LeftButton)
	{
		float theta_factor = 0.01;
		SourceList[RenderSource].S.OrbitFocus(dx*theta_factor, -dy*theta_factor);
	}
	else if(event->buttons() == Qt::RightButton)
	{
		float zoom_factor = 0.01;
		SourceList[RenderSource].S.Push(dy*zoom_factor);
	}

	updateGL();
}