forked from precious/dr_program
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics_utils.cpp
More file actions
137 lines (116 loc) · 4.29 KB
/
graphics_utils.cpp
File metadata and controls
137 lines (116 loc) · 4.29 KB
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
#include "graphics_utils.h"
Point Graphics::viewerPosition(0,0,0);
int Graphics::width = 0;
int Graphics::height = 0;
float Graphics::zoomFactor = 1.0;
bool Graphics::isLMousePressed = false;
double Graphics::rotationAngles[2] = {0,0};
void Graphics::initGraphics(int _width, int _height) {
height = _height;
width = _width;
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
cerr << "Video initialization failed: " << SDL_GetError() << endl;
quitGraphics(1);
}
const SDL_VideoInfo* info = NULL;
info = SDL_GetVideoInfo();
if(!info) {
cerr << "getting video info failed: " << SDL_GetError() << endl;
quitGraphics(1);
}
SDL_GL_SetAttribute(SDL_GL_RED_SIZE,5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
int bitsPerPixel = info->vfmt->BitsPerPixel;
int flags = SDL_OPENGL | SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_RESIZABLE;
if(SDL_SetVideoMode(width,height,bitsPerPixel,flags) == 0) {
cerr << "setting video mode failed: " << SDL_GetError() << endl;
quitGraphics(1);
}
glShadeModel(GL_SMOOTH);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glViewport(0, 0, width, height);
glPointSize(1.5);
}
void Graphics::quitGraphics(int code) {
SDL_Quit();
exit(code);
}
void Graphics::draw(Object3D &satelliteObj,Particle* particlesArray = NULL,int particlesNumber = 0)
{
// colors
static GLubyte purple[] = {255,150,255,0};
static GLubyte grey[] = {100,100,100,0};
static GLubyte red[] = {255,0,0,0};
static GLubyte green[] = {0,255,0,0};
static GLubyte blue[] = {0,0,255,0};
glClearColor(255,255,255,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Projections matrix processing
static float ratio = (float)width/(float)height;
static double diameter = satelliteObj.radius*2;
static GLdouble zNear = 0.0;
static GLdouble zFar = zNear + 2*diameter;
static GLdouble left = satelliteObj.center.x - diameter;
static GLdouble right = satelliteObj.center.x + diameter;
static GLdouble bottom = satelliteObj.center.y - diameter;
static GLdouble top = satelliteObj.center.y + diameter;
static bool firstTimeCall = true;
if (firstTimeCall) {
firstTimeCall = false;
viewerPosition.z = 1.5*diameter;
if (ratio < 1.0) { // width < height
bottom /= ratio;
top /= ratio;
} else {
left *= ratio;
right *= ratio;
}
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(left*zoomFactor,right*zoomFactor,bottom*zoomFactor,top*zoomFactor,zNear,zFar);
//glOrtho(left, right, bottom, top, zNear, zFar);
// Modelview matrix processing
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* Move down the z-axis. */
//glTranslatef(viewerPosition.x, viewerPosition.y, viewerPosition.z);
gluLookAt(viewerPosition.x, viewerPosition.y, viewerPosition.z,
satelliteObj.center.x, satelliteObj.center.y, satelliteObj.center.z,
0.0, 1.0, 0.0);
/* Rotation by mouse */
if (rotationAngles[0])
glRotated(rotationAngles[0],0,1,0);
if (rotationAngles[1])
glRotated(rotationAngles[1],1,0,0);
// draw axes:
glBegin(GL_LINES);
glColor4ubv(red); glVertex3d(0,0,0); glVertex3d(1.5*diameter,0,0);
glColor4ubv(green); glVertex3d(0,0,0); glVertex3d(0,1.5*diameter,0);
glColor4ubv(blue); glVertex3d(0,0,0); glVertex3d(0,0,1.5*diameter);
glEnd();
// draw the particles
if (particlesArray != NULL) {
glBegin(GL_POINTS);
for(int i = 0;i < particlesNumber;++i) {
glColor4ubv((particlesArray[i].type == PTYPE_ELECTRON)? blue: red);
glVertex3f(particlesArray[i].x,particlesArray[i].y,particlesArray[i].z);
}
glEnd();
}
// draw the object
glColor4ubv(purple);
vector<PlaneType> *coords = satelliteObj.polygons;
for(vector<PlaneType>::iterator it = coords->begin();it != coords->end();it++) {
glBegin(GL_LINE_LOOP);
for(int i = 0;i < 3;i++)
glVertex3d((*it).set[i].x,(*it).set[i].y,(*it).set[i].z);
glEnd();
}
SDL_GL_SwapBuffers();
}