Debian Codegraph @

src/Gui/Inventor/SoFCBackgroundGradient.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
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 #include <array>
24 #include <boost/math/constants/constants.hpp>
25 #include <cmath>
26 #include <numbers>
27
28 #include <FCConfig.h>
29
30 #ifdef FC_OS_WIN32
31 # include <windows.h>
32 #endif
33
34 #ifdef FC_OS_MACOSX
35 # include <OpenGL/gl.h>
36 #else
37 # include <GL/gl.h>
38 #endif
39
40 #include "SoFCBackgroundGradient.h"
41
42 static const std::array<GLfloat[2], 32> big_circle = [] {
43 constexpr float pi = std::numbers::pi_v<float>;
44 constexpr float sqrt2 = std::numbers::sqrt2_v<float>;
45 std::array<GLfloat[2], 32> result;
46 int c = 0;
47 for (GLfloat i = 0; i < 2 * pi; i += 2 * pi / 32, c++) {
48 result[c][0] = sqrt2 * cosf(i);
49 result[c][1] = sqrt2 * sinf(i);
50 }
51 return result;
52 }();
53 static const std::array<GLfloat[2], 32> small_oval = [] {
54 constexpr float pi = std::numbers::pi_v<float>;
55 constexpr float sqrt2 = std::numbers::sqrt2_v<float>;
56 static const float sqrt1_2 = std::sqrt(1 / 2.F);
57
58 std::array<GLfloat[2], 32> result;
59 int c = 0;
60 for (GLfloat i = 0; i < 2 * pi; i += 2 * pi / 32, c++) {
61 result[c][0] = 0.3 * sqrt2 * cosf(i);
62 result[c][1] = sqrt1_2 * sinf(i);
63 }
64 return result;
65 }();
66
67 using namespace Gui;
68
69 SO_NODE_SOURCE(SoFCBackgroundGradient)
70
71 void SoFCBackgroundGradient::finish()
72 {
73 atexit_cleanup();
74 }
75
76 /*!
77 Constructor.
78 */
79 SoFCBackgroundGradient::SoFCBackgroundGradient()
80 {
81 SO_NODE_CONSTRUCTOR(SoFCBackgroundGradient);
82 fCol.setValue(0.5f, 0.5f, 0.8f);
83 tCol.setValue(0.7f, 0.7f, 0.9f);
84 mCol.setValue(1.0f, 1.0f, 1.0f);
85 gradient = Gradient::LINEAR;
86 }
87
88 /*!
89 Destructor.
90 */
91 SoFCBackgroundGradient::~SoFCBackgroundGradient() = default;
92
93 // doc from parent
94 void SoFCBackgroundGradient::initClass()
95 {
96 SO_NODE_INIT_CLASS(SoFCBackgroundGradient, SoNode, "Node");
97 }
98
99 void SoFCBackgroundGradient::GLRender(SoGLRenderAction* /*action*/)
100 {
101 glMatrixMode(GL_PROJECTION);
102 glPushMatrix();
103 glLoadIdentity();
104 glOrtho(-1, 1, -1, 1, -1, 1);
105 glMatrixMode(GL_MODELVIEW);
106 glPushMatrix();
107 glLoadIdentity();
108 glPushAttrib(GL_ENABLE_BIT);
109 glDisable(GL_DEPTH_TEST);
110 glDisable(GL_LIGHTING);
111 glDisable(GL_TEXTURE_2D);
112
113 if (gradient == Gradient::LINEAR) {
114 glBegin(GL_TRIANGLE_STRIP);
115 if (mCol[0] < 0) {
116 glColor3f(fCol[0], fCol[1], fCol[2]);
117 glVertex2f(-1, 1);
118 glColor3f(tCol[0], tCol[1], tCol[2]);
119 glVertex2f(-1, -1);
120 glColor3f(fCol[0], fCol[1], fCol[2]);
121 glVertex2f(1, 1);
122 glColor3f(tCol[0], tCol[1], tCol[2]);
123 glVertex2f(1, -1);
124 }
125 else {
126 glColor3f(fCol[0], fCol[1], fCol[2]);
127 glVertex2f(-1, 1);
128 glColor3f(mCol[0], mCol[1], mCol[2]);
129 glVertex2f(-1, 0);
130 glColor3f(fCol[0], fCol[1], fCol[2]);
131 glVertex2f(1, 1);
132 glColor3f(mCol[0], mCol[1], mCol[2]);
133 glVertex2f(1, 0);
134 glEnd();
135 glBegin(GL_TRIANGLE_STRIP);
136 glColor3f(mCol[0], mCol[1], mCol[2]);
137 glVertex2f(-1, 0);
138 glColor3f(tCol[0], tCol[1], tCol[2]);
139 glVertex2f(-1, -1);
140 glColor3f(mCol[0], mCol[1], mCol[2]);
141 glVertex2f(1, 0);
142 glColor3f(tCol[0], tCol[1], tCol[2]);
143 glVertex2f(1, -1);
144 }
145 }
146 else { // radial gradient
147 glBegin(GL_TRIANGLE_FAN);
148 glColor3f(fCol[0], fCol[1], fCol[2]);
149 glVertex2f(0.0f, 0.0f);
150
151 if (mCol[0] < 0) { // simple radial gradient
152 glColor3f(tCol[0], tCol[1], tCol[2]);
153 for (const GLfloat* vertex : big_circle) {
154 glVertex2fv(vertex);
155 }
156 glVertex2fv(big_circle.front());
157 }
158 else {
159 glColor3f(mCol[0], mCol[1], mCol[2]);
160 for (const GLfloat* vertex : small_oval) {
161 glVertex2fv(vertex);
162 }
163 glVertex2fv(small_oval.front());
164 glEnd();
165
166 glBegin(GL_TRIANGLE_STRIP);
167 for (std::size_t i = 0; i < small_oval.size(); i++) {
168 glColor3f(mCol[0], mCol[1], mCol[2]);
169 glVertex2fv(small_oval[i]);
170 glColor3f(tCol[0], tCol[1], tCol[2]);
171 glVertex2fv(big_circle[i]);
172 }
173
174 glColor3f(mCol[0], mCol[1], mCol[2]);
175 glVertex2fv(small_oval.front());
176 glColor3f(tCol[0], tCol[1], tCol[2]);
177 glVertex2fv(big_circle.front());
178 }
179 } // end of radial gradient
180 glEnd();
181
182 glPopAttrib();
183 glPopMatrix(); // restore modelview
184 glMatrixMode(GL_PROJECTION);
185 glPopMatrix();
186 glMatrixMode(GL_MODELVIEW);
187 }
188
189 void SoFCBackgroundGradient::setGradient(SoFCBackgroundGradient::Gradient grad)
190 {
191 gradient = grad;
192 }
193
194 SoFCBackgroundGradient::Gradient SoFCBackgroundGradient::getGradient() const
195 {
196 return gradient;
197 }
198
199 void SoFCBackgroundGradient::setColorGradient(const SbColor& fromColor, const SbColor& toColor)
200 {
201 fCol = fromColor;
202 tCol = toColor;
203 mCol[0] = -1.0f;
204 }
205
206 void SoFCBackgroundGradient::setColorGradient(
207 const SbColor& fromColor,
208 const SbColor& toColor,
209 const SbColor& midColor
210 )
211 {
212 fCol = fromColor;
213 tCol = toColor;
214 mCol = midColor;
215 }