Blame view

GLWidget.cpp 2.43 KB
ee96a02c   David Mayerich   first commit from...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  #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();
  }