-
Notifications
You must be signed in to change notification settings - Fork 0
/
OGLRenderer.cpp
147 lines (121 loc) · 2.87 KB
/
OGLRenderer.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
#include "OGLRenderer.h"
void drawGrids(void)
{
glPushMatrix();
glLineWidth(10);
glBegin(GL_LINES);
{
glColor3f(1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(20, 0, 0);
glColor3f(0, 1, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 20, 0);
glColor3f(0, 0, 1);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 20);
}
glEnd();
glLineWidth(1);
for (int i = -20; i < 21; i++){
glBegin(GL_LINES);
{
// XZ plane
glColor3f(.5, 0, 0);
glVertex3f(-20, 0, i);
glVertex3f(20, 0, i);
glColor3f(0, 0, .5);
glVertex3f(i, 0, -20);
glVertex3f(i, 0, 20);
}
glEnd();
}
glPopMatrix();
}
OGLRenderer::OGLRenderer()
{
camradius = 1;
camFocus.set(0, 0, -1);
camAngularVel.set(0, 0, 0);
camRot.set(0, 0, 0);
init();
}
OGLRenderer::~OGLRenderer()
{
}
void OGLRenderer::init(void)
{
glClearColor(.1, .1, .1, 1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void OGLRenderer::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
applyView();
//drawGrids();
glScalef(1, 0.5, 1);
glCallList(plotList);
}
void OGLRenderer::setView(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, double(width) / double(height), 0.01, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void OGLRenderer::update(int delta){
camAngularVel.x -= camAngularVel.x * 0.01 * delta;
camAngularVel.y -= camAngularVel.y * 0.01 * delta;
camRot.x += camAngularVel.x;
camRot.y += camAngularVel.y;
}
void OGLRenderer::applyView(){
glTranslatef(0, 0, -camradius);
glRotatef(camRot.x, 1, 0, 0);
glRotatef(camRot.y, 0, 1, 0);
glTranslatef(-camFocus.x, -camFocus.y, -camFocus.z);
}
void OGLRenderer::genPlotDisplayList(PlotData* data){
plotList = glGenLists(1);
glNewList(plotList, GL_COMPILE);
for (int row = 0; row < data->rows; row++){
glBegin(GL_LINE_STRIP);
for (int col = 0; col < data->cols; col++){
Vector3f v = data->points[row * data->cols + col];
float red, blue, green = 0;
green = abs(v.z) / (abs(data->zrange.high) + abs(data->zrange.low));
if (v.z > 0)
red = v.z / abs(data->zrange.high);
else if (v.z < 0)
blue = v.z / abs(data->zrange.low);
glColor4f(red, 1 - green , blue, 1);
glVertex3f(v.x, v.z, v.y);
}
glEnd();
}
for (int col = 0; col < data->cols; col++){
glBegin(GL_LINE_STRIP);
for (int row = 0; row < data->rows; row++){
Vector3f v = data->points[row * data->cols + col];
float red, blue, green = 0;
green = abs(v.z) / (abs(data->zrange.high) + abs(data->zrange.low));
if (v.z > 0)
red = v.z / data->zrange.high;
else if (v.z < 0)
blue = v.z / data->zrange.low;
glColor4f(red, 1 - green, blue, 1);
glVertex3f(v.x, v.z, v.y);
}
glEnd();
}
glEndList();
this->data = data;
}
void OGLRenderer::createVertex(Vector3f v){
}