-
Notifications
You must be signed in to change notification settings - Fork 0
/
CGLArea.cpp
160 lines (124 loc) · 3.13 KB
/
CGLArea.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "CGLArea.h"
#include <math.h>
#include <GLUT/glut.h>
CGLArea::CGLArea(QWidget* parent) : QGLWidget(parent)
{
maSphere = new CSphere(100);
}
CGLArea::~CGLArea()
{
}
/**
* @brief CGLArea::minimumSizeHint
* @return taille minimum de la fenetre openGL
*/
QSize CGLArea::minimumSizeHint() const
{
return QSize(50,50);
}
/**
* @brief CGLArea::sizeHint
* @return la taille par default
*/
QSize CGLArea::sizeHint() const
{
return QSize(400,400);
}
/**
* @brief CGLArea::initializeGL
*
* Initialise la fenetre openGL
*/
void CGLArea::initializeGL()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDisable(GL_COLOR_MATERIAL);
glViewport(0, 0, 400, 400);
initializeCamera();
}
void CGLArea::initializeCamera()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float m_fRadius = 5.0;
float m_fAzimuth = 0.0;
float m_fElevation = 0.0;
glOrtho(-2.0, +2.0, -2.0, +2.0, 0.0, 15.0);
gluLookAt(m_fRadius*cos(m_fAzimuth)*cos(m_fElevation),
m_fRadius*sin(m_fAzimuth)*cos(m_fElevation),
m_fRadius*sin(m_fElevation),
0,0,0,
0,0,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/**
* @brief CGLArea::paintGL
*
* Fonction de dessin sur la fenetre openGL
*/
void CGLArea::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// paintRef();
GLfloat ambientLight[] = {0.2f, 0.2f, 0.2f, 1.0f};
GLfloat lightColor[] = {0.6f, 0.6f, 0.6f, 1.0f};
GLfloat lightPos[] ={10,0,-15,1};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
maSphere->vSetMaterialColor();
CVector3 oPos;
maSphere->vGetPosition(&oPos);
glLoadIdentity();
glTranslatef(oPos.fGetX(), oPos.fGetY(), oPos.fGetZ());
// For each face...
for(int j=0; j<maSphere->iGetNbFaces(); j++)
{
glBegin(GL_POLYGON);
// For each vertex of the face
for(int k=0;k<maSphere->iGetNbVertices(j);k++)
{
CVector3 oVertex;
CVector3 oNormal;
maSphere->vGetVertex(j,k,&oVertex);
maSphere->vGetNormale(j,k,&oNormal);
glNormal3f(oVertex.fGetX(), oVertex.fGetY(), oVertex.fGetZ());
glVertex3f(oVertex.fGetX(), oVertex.fGetY(), oVertex.fGetZ());
}
glEnd();
}
}
/**
* @brief CGLArea::resizeGL
* @param width
* @param height
*
* Evenement lors du resize de la fenetre openGL
*/
void CGLArea::resizeGL(int _width, int _height)
{
int side = qMin(_width, _height);
glViewport((_width - side) / 2, (_height - side) / 2, side, side);
initializeCamera();
}
void CGLArea::paintRef()
{
glBegin(GL_LINES);
glColor3f(1, 0, 0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glColor3f(0, 1, 0);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glColor3f(0, 0, 1);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
}