-
Notifications
You must be signed in to change notification settings - Fork 3
/
STLView.h
185 lines (156 loc) · 4.82 KB
/
STLView.h
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* STLover - A powerful tool for viewing and manipulating 3D STL models
* Copyright (C) 2020 Gerasim Troeglazov <3dEyes@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef STLOVER_VIEW
#define STLOVER_VIEW
#include <View.h>
#include <Bitmap.h>
#include <Message.h>
#include <Point.h>
#include <Rect.h>
#include <OS.h>
#include <Cursor.h>
#include <admesh/stl.h>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
class STLWindow;
class STLView : public BGLView {
public:
STLView(BRect frame, uint32 type);
~STLView();
virtual void AttachedToWindow(void);
virtual void MessageReceived(BMessage *message);
virtual void FrameResized(float w, float h);
virtual void MouseDown(BPoint point);
virtual void MouseUp(BPoint point);
virtual void MouseMoved(BPoint p, uint32 transit, const BMessage *message);
void SetSTL(stl_file *stl);
void Reload(void);
void Reset(bool scale = true, bool rotate = true, bool pan = true);
void ShowAxes(bool show, bool plane, bool compass)
{
showAxes = show;
showAxesPlane = plane;
showAxesCompass = compass;
if (m_buffersInitialized)
Reload();
}
void ShowBoundingBox(bool show) { showBox = show; }
void ShowOXY(bool show)
{
showOXY = show;
if (m_buffersInitialized)
Reload();
}
void SetViewMode(uint32 mode) { viewMode = mode; }
void SetOrthographic(bool ortho) { viewOrtho = ortho; SetupProjection(); };
void Render(void);
void RenderUpdate() { needUpdate = true; }
float XRotate() { return xRotate; }
float YRotate() { return yRotate; }
float ScaleFactor() { return scaleFactor; }
void SetXRotate(float value) { xRotate = value; needUpdate = true; }
void SetYRotate(float value) { yRotate = value; needUpdate = true; }
void SetScaleFactor(float value) { scaleFactor = value; needUpdate = true; }
void SetMeasureMode(bool enable);
void ShowPreview(float *matrix);
void HidePreview() { fShowPreview = false; }
private:
void InitShaders();
GLuint CompileShader(GLenum type, const char* source);
GLuint CreateShaderProgram(const char* vertexSource, const char* fragmentSource);
void InitializeBuffers();
void CleanupBuffers();
void GenerateBoxBuffers();
void GenerateOXYGridBuffers();
void DrawBox(void);
void DrawAxis(void);
void DrawMeasure(void);
void DrawOXY(void);
void DrawAxisLabel(float x, float y, float z,
const char* label, float r, float g, float b);
void DrawSTL() { DrawSTL({128,128,128}); }
void DrawSTL(rgb_color color, float alpha = 1.0);
void Billboard();
void SetupProjection(void);
bool ScreenToPoint3d(BPoint screenPoint, glm::vec3& point3d);
GLuint boxVAO = 0;
GLuint boxVBO = 0;
GLuint axesVAO = 0;
GLuint axesVBO = 0;
GLuint oxyVAO = 0;
GLuint oxyVBO = 0;
GLuint stlVAO = 0;
GLuint stlVertexVBO = 0;
GLuint stlNormalVBO = 0;
size_t stlVertexCount = 0;
struct ColoredVertex {
float x, y, z;
float r, g, b;
};
std::vector<ColoredVertex> boxVertices;
std::vector<ColoredVertex> oxyVertices;
std::vector<float> axisVertices;
bool m_buffersInitialized = false;
GLuint shaderProgram;
GLuint lineShaderProgram;
GLint modelLoc;
GLint viewLoc;
GLint projLoc;
GLint colorLoc;
GLint viewPosLoc;
glm::mat4 modelMatrix;
glm::mat4 viewMatrix;
glm::mat4 projectionMatrix;
bool measureMode;
bool isMeasureSkip = false;
int32 measureMouseState = 0;
glm::vec3 measureStartPoint;
glm::vec3 measureEndPoint;
glm::vec3 lastMousePos3d;
bool lastMousePos3dValid;
bool measureStartPointValid;
bool measureEndPointValid;
BRect boundRect;
BBitmap *appIcon;
BPoint iconPos;
BPoint lastMousePos;
uint32 lastMouseButtons;
bigtime_t lastMouseClickTime;
BCursor *moveCursor;
BCursor *rotateCursor;
BCursor *crossCursor;
BBitmap *rotateCursorBitmap;
STLWindow *stlWindow;
stl_file* stlObject;
float xRotate;
float yRotate;
float xPan;
float yPan;
float scaleFactor;
uint32 viewMode;
bool needUpdate;
bool showBox;
bool showAxes;
bool showAxesPlane;
bool showAxesCompass;
bool showOXY;
float fPreviewMatrix[16];
bool fShowPreview;
bool viewOrtho;
};
#endif // STLOVER_VIEW