1
/***************************************************************************
2
* Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
27
#include <boost/math/constants/constants.hpp>
29
#define _USE_MATH_DEFINES
39
#include "SoFCBackgroundGradient.h"
41
static const std::array <GLfloat[2], 32> big_circle = []{
42
static const float pi2 = boost::math::constants::two_pi<float>();
43
std::array <GLfloat[2], 32> result; int c = 0;
44
for (GLfloat i = 0; i < pi2; i += pi2 / 32, c++) {
45
result[c][0] = M_SQRT2*cosf(i); result[c][1] = M_SQRT2*sinf(i);
48
static const std::array <GLfloat[2], 32> small_oval = []{
49
static const float pi2 = boost::math::constants::two_pi<float>();
50
std::array <GLfloat[2], 32> result; int c = 0;
51
for (GLfloat i = 0; i < pi2; i += pi2 / 32, c++) {
52
result[c][0] = 0.3*M_SQRT2*cosf(i); result[c][1] = M_SQRT1_2*sinf(i);
58
SO_NODE_SOURCE(SoFCBackgroundGradient)
60
void SoFCBackgroundGradient::finish()
68
SoFCBackgroundGradient::SoFCBackgroundGradient()
70
SO_NODE_CONSTRUCTOR(SoFCBackgroundGradient);
71
fCol.setValue(0.5f, 0.5f, 0.8f);
72
tCol.setValue(0.7f, 0.7f, 0.9f);
73
mCol.setValue(1.0f, 1.0f, 1.0f);
74
gradient = Gradient::LINEAR;
80
SoFCBackgroundGradient::~SoFCBackgroundGradient() = default;
83
void SoFCBackgroundGradient::initClass()
85
SO_NODE_INIT_CLASS(SoFCBackgroundGradient,SoNode,"Node");
88
void SoFCBackgroundGradient::GLRender (SoGLRenderAction * /*action*/)
90
glMatrixMode(GL_PROJECTION);
93
glOrtho(-1,1,-1,1,-1,1);
94
glMatrixMode(GL_MODELVIEW);
97
glPushAttrib(GL_ENABLE_BIT);
98
glDisable(GL_DEPTH_TEST);
99
glDisable(GL_LIGHTING);
100
glDisable(GL_TEXTURE_2D);
102
if (gradient == Gradient::LINEAR) {
103
glBegin(GL_TRIANGLE_STRIP);
105
glColor3f(fCol[0],fCol[1],fCol[2]); glVertex2f(-1, 1);
106
glColor3f(tCol[0],tCol[1],tCol[2]); glVertex2f(-1,-1);
107
glColor3f(fCol[0],fCol[1],fCol[2]); glVertex2f( 1, 1);
108
glColor3f(tCol[0],tCol[1],tCol[2]); glVertex2f( 1,-1);
111
glColor3f(fCol[0],fCol[1],fCol[2]); glVertex2f(-1, 1);
112
glColor3f(mCol[0],mCol[1],mCol[2]); glVertex2f(-1, 0);
113
glColor3f(fCol[0],fCol[1],fCol[2]); glVertex2f( 1, 1);
114
glColor3f(mCol[0],mCol[1],mCol[2]); glVertex2f( 1, 0);
116
glBegin(GL_TRIANGLE_STRIP);
117
glColor3f(mCol[0],mCol[1],mCol[2]); glVertex2f(-1, 0);
118
glColor3f(tCol[0],tCol[1],tCol[2]); glVertex2f(-1,-1);
119
glColor3f(mCol[0],mCol[1],mCol[2]); glVertex2f( 1, 0);
120
glColor3f(tCol[0],tCol[1],tCol[2]); glVertex2f( 1,-1);
123
else { // radial gradient
124
glBegin(GL_TRIANGLE_FAN);
125
glColor3f(fCol[0], fCol[1], fCol[2]); glVertex2f(0.0f, 0.0f);
127
if (mCol[0] < 0) { // simple radial gradient
128
glColor3f(tCol[0], tCol[1], tCol[2]);
129
for (const GLfloat *vertex : big_circle)
130
glVertex2fv( vertex );
131
glVertex2fv( big_circle.front() );
133
glColor3f(mCol[0], mCol[1], mCol[2]);
134
for (const GLfloat *vertex : small_oval)
135
glVertex2fv( vertex );
136
glVertex2fv( small_oval.front() );
139
glBegin(GL_TRIANGLE_STRIP);
140
for (std::size_t i = 0; i < small_oval.size(); i++) {
141
glColor3f(mCol[0], mCol[1], mCol[2]); glVertex2fv( small_oval[i] );
142
glColor3f(tCol[0], tCol[1], tCol[2]); glVertex2fv( big_circle[i] );
145
glColor3f(mCol[0], mCol[1], mCol[2]); glVertex2fv( small_oval.front() );
146
glColor3f(tCol[0], tCol[1], tCol[2]); glVertex2fv( big_circle.front() );
148
} // end of radial gradient
152
glPopMatrix(); // restore modelview
153
glMatrixMode(GL_PROJECTION);
155
glMatrixMode(GL_MODELVIEW);
158
void SoFCBackgroundGradient::setGradient(SoFCBackgroundGradient::Gradient grad)
163
SoFCBackgroundGradient::Gradient SoFCBackgroundGradient::getGradient() const
168
void SoFCBackgroundGradient::setColorGradient(const SbColor& fromColor,
169
const SbColor& toColor)
176
void SoFCBackgroundGradient::setColorGradient(const SbColor& fromColor,
177
const SbColor& toColor,
178
const SbColor& midColor)