Debian Codegraph @

src/Gui/View3DInventorRiftViewer.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2014 Jürgen Riegel <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #if BUILD_VR
25
26 # include <App/Application.h>
27 # include <Base/Console.h>
28
29 # include "View3DInventorRiftViewer.h"
30
31
32 using namespace Gui;
33
34 View3DInventorRiftViewer::View3DInventorRiftViewer()
35 : CoinRiftWidget()
36 {
37 workplace = new SoGroup();
38
39 // translation = new SoTranslation ;
40 // translation->translation.setValue(0,-1,0);
41 // workplace->addChild(translation);
42
43 rotation1 = new SoRotationXYZ;
44 rotation1->axis.setValue(SoRotationXYZ::X);
45 rotation1->angle.setValue(-std::numbers::pi / 2);
46 workplace->addChild(rotation1);
47
48 rotation2 = new SoRotationXYZ;
49 rotation2->axis.setValue(SoRotationXYZ::Z);
50 rotation2->angle.setValue(0);
51 workplace->addChild(rotation2);
52
53
54 scale = new SoScale;
55 scale->scaleFactor.setValue(0.001f, 0.001f, 0.001f); // scale from mm to m as needed by the Rift
56 workplace->addChild(scale);
57
58 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
59 "User parameter:BaseApp/Preferences/Oculus"
60 );
61
62 this->setGeometry(
63 hGrp->GetInt("RenderWindowPosX", 100),
64 hGrp->GetInt("RenderWindowPosY", 100),
65 hGrp->GetInt("RenderWindowSizeW", 1920),
66 hGrp->GetInt("RenderWindowSizeH", 1080)
67 );
68
69
70 setBackgroundColor(SbColor(51, 51, 101));
71 basePosition = SbVec3f(0.0f, 0.5f, 0.8f);
72 }
73
74 // void saveWinPosition(void)
75 //{
76 //
77 //
78 //
79 // }
80
81 View3DInventorRiftViewer::~View3DInventorRiftViewer()
82 {
83 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
84 "User parameter:BaseApp/Preferences/Oculus"
85 );
86
87 // remember last position on close
88 hGrp->SetInt("RenderWindowPosX", pos().x());
89 hGrp->SetInt("RenderWindowPosY", pos().y());
90 hGrp->SetInt("RenderWindowSizeW", size().width());
91 hGrp->SetInt("RenderWindowSizeH", size().height());
92
93 Base::Console()
94 .log("pos: %d %d size: %d %d \n", pos().x(), pos().y(), size().width(), size().height());
95 }
96
97 void View3DInventorRiftViewer::setSceneGraph(SoNode* sceneGraph)
98 {
99
100 workplace->addChild(sceneGraph);
101
102 CoinRiftWidget::setSceneGraph(workplace);
103 }
104
105
106 void View3DInventorRiftViewer::keyPressEvent(QKeyEvent* event)
107 {
108 static const float increment = 0.02; // move two centimeter per key
109 static const float rotIncrement = std::numbers::pi / 4; // move two 90° per key
110
111
112 if (event->key() == Qt::Key_Plus) {
113 scale->scaleFactor.setValue(scale->scaleFactor.getValue() * 2.0f);
114 }
115 else if (event->key() == Qt::Key_Minus) {
116 scale->scaleFactor.setValue(scale->scaleFactor.getValue() * 0.2f);
117 }
118 else if (event->key() == Qt::Key_S) {
119 basePosition += SbVec3f(0, 0, increment);
120 }
121 else if (event->key() == Qt::Key_W) {
122 basePosition += SbVec3f(0, 0, -increment);
123 }
124 else if (event->key() == Qt::Key_Up) {
125 basePosition += SbVec3f(0, -increment, 0);
126 }
127 else if (event->key() == Qt::Key_Down) {
128 basePosition += SbVec3f(0, increment, 0);
129 }
130 else if (event->key() == Qt::Key_Left) {
131 rotation2->angle.setValue(rotation2->angle.getValue() + rotIncrement);
132 }
133 else if (event->key() == Qt::Key_Right) {
134 rotation2->angle.setValue(rotation2->angle.getValue() - rotIncrement);
135 }
136 else if (event->key() == Qt::Key_A) {
137 basePosition += SbVec3f(-increment, 0, 0);
138 }
139 else if (event->key() == Qt::Key_D) {
140 basePosition += SbVec3f(increment, 0, 0);
141 }
142 else {
143
144 CoinRiftWidget::keyPressEvent(event);
145 }
146 }
147
148
149 // static test code ================================================================================
150 static View3DInventorRiftViewer* window = 0;
151
152 void oculusSetTestScene(View3DInventorRiftViewer* window)
153 {
154 assert(window);
155 // An example scene.
156 static const char* inlineSceneGraph[] = {
157 "#Inventor V2.1 ascii\n",
158 "\n",
159 "Separator {\n",
160 " Rotation { rotation 1 0 0 0.3 }\n",
161 " Cone { }\n",
162 " BaseColor { rgb 1 0 0 }\n",
163 " Scale { scaleFactor .7 .7 .7 }\n",
164 " Cube { }\n",
165 "\n",
166 " DrawStyle { style LINES }\n",
167 " ShapeHints { vertexOrdering COUNTERCLOCKWISE }\n",
168 " Coordinate3 {\n",
169 " point [\n",
170 " -2 -2 1.1, -2 -1 1.1, -2 1 1.1, -2 2 1.1,\n",
171 " -1 -2 1.1, -1 -1 1.1, -1 1 1.1, -1 2 1.1\n",
172 " 1 -2 1.1, 1 -1 1.1, 1 1 1.1, 1 2 1.1\n",
173 " 2 -2 1.1, 2 -1 1.1, 2 1 1.1, 2 2 1.1\n",
174 " ]\n",
175 " }\n",
176 "\n",
177 " Complexity { value 0.7 }\n",
178 " NurbsSurface {\n",
179 " numUControlPoints 4\n",
180 " numVControlPoints 4\n",
181 " uKnotVector [ 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0 ]\n",
182 " vKnotVector [ 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0 ]\n",
183 " }\n",
184 "}\n",
185 NULL
186 };
187
188 SoInput in;
189 in.setStringArray(inlineSceneGraph);
190
191 window->setSceneGraph(SoDB::readAll(&in));
192 }
193
194
195 void oculusStop()
196 {
197 // SoDB::finish();
198 if (window) {
199 delete window;
200 window = 0;
201 ovr_Shutdown();
202 }
203 }
204
205 bool oculusUp(void)
206 {
207 return window != 0;
208 }
209
210 View3DInventorRiftViewer* oculusStart(void)
211 {
212 // SoDB::init();
213
214 // QApplication app(argc, argv);
215 // qAddPostRoutine(cleanup);
216
217 // Moved here because of
218 // https://developer.oculusvr.com/forums/viewtopic.php?f=17&t=7915&p=108503#p108503 Init libovr.
219 if (!ovr_Initialize()) {
220 qDebug() << "Could not initialize Oculus SDK.";
221 return 0;
222 }
223 if (window) {
224 return window;
225 }
226
227 window = new View3DInventorRiftViewer;
228 window->show();
229
230
231 return window;
232 // return app.exec();
233 }
234
235
236 #endif // BUILD_VR