FreeCAD

Форк
0
/
SoFCBackgroundGradient.cpp 
183 строки · 6.4 Кб
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 "PreCompiled.h"
24

25
#ifndef _PreComp_
26
#include <array>
27
#include <boost/math/constants/constants.hpp>
28
#ifdef FC_OS_WIN32
29
 #define _USE_MATH_DEFINES
30
#endif
31
#include <cmath>
32
#ifdef FC_OS_MACOSX
33
#include <OpenGL/gl.h>
34
#else
35
#include <GL/gl.h>
36
#endif
37
#endif
38

39
#include "SoFCBackgroundGradient.h"
40

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);
46
    }
47
    return result; }();
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);
53
    }
54
    return result; }();
55

56
using namespace Gui;
57

58
SO_NODE_SOURCE(SoFCBackgroundGradient)
59

60
void SoFCBackgroundGradient::finish()
61
{
62
    atexit_cleanup();
63
}
64

65
/*!
66
  Constructor.
67
*/
68
SoFCBackgroundGradient::SoFCBackgroundGradient()
69
{
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;
75
}
76

77
/*!
78
  Destructor.
79
*/
80
SoFCBackgroundGradient::~SoFCBackgroundGradient() = default;
81

82
// doc from parent
83
void SoFCBackgroundGradient::initClass()
84
{
85
    SO_NODE_INIT_CLASS(SoFCBackgroundGradient,SoNode,"Node");
86
}
87

88
void SoFCBackgroundGradient::GLRender (SoGLRenderAction * /*action*/)
89
{
90
    glMatrixMode(GL_PROJECTION);
91
    glPushMatrix();
92
    glLoadIdentity();
93
    glOrtho(-1,1,-1,1,-1,1);
94
    glMatrixMode(GL_MODELVIEW);
95
    glPushMatrix();
96
    glLoadIdentity();
97
    glPushAttrib(GL_ENABLE_BIT);
98
    glDisable(GL_DEPTH_TEST);
99
    glDisable(GL_LIGHTING);
100
    glDisable(GL_TEXTURE_2D);
101

102
    if (gradient == Gradient::LINEAR) {
103
        glBegin(GL_TRIANGLE_STRIP);
104
        if (mCol[0] < 0) {
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);
109
        }
110
        else {
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);
115
            glEnd();
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);
121
        }
122
    }
123
    else { // radial gradient
124
        glBegin(GL_TRIANGLE_FAN);
125
        glColor3f(fCol[0], fCol[1], fCol[2]); glVertex2f(0.0f, 0.0f);
126

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() );
132
        } else {
133
            glColor3f(mCol[0], mCol[1], mCol[2]);
134
            for (const GLfloat *vertex : small_oval)
135
                glVertex2fv( vertex );
136
            glVertex2fv( small_oval.front() );
137
            glEnd();
138

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] );
143
            }
144

145
            glColor3f(mCol[0], mCol[1], mCol[2]); glVertex2fv( small_oval.front() );
146
            glColor3f(tCol[0], tCol[1], tCol[2]); glVertex2fv( big_circle.front() );
147
        }
148
    } // end of radial gradient
149
    glEnd();
150

151
    glPopAttrib();
152
    glPopMatrix(); // restore modelview
153
    glMatrixMode(GL_PROJECTION);
154
    glPopMatrix();
155
    glMatrixMode(GL_MODELVIEW);
156
}
157

158
void SoFCBackgroundGradient::setGradient(SoFCBackgroundGradient::Gradient grad)
159
{
160
    gradient = grad;
161
}
162

163
SoFCBackgroundGradient::Gradient SoFCBackgroundGradient::getGradient() const
164
{
165
    return gradient;
166
}
167

168
void SoFCBackgroundGradient::setColorGradient(const SbColor& fromColor,
169
                                              const SbColor& toColor)
170
{
171
    fCol = fromColor;
172
    tCol = toColor;
173
    mCol[0] = -1.0f;
174
}
175

176
void SoFCBackgroundGradient::setColorGradient(const SbColor& fromColor,
177
                                              const SbColor& toColor,
178
                                              const SbColor& midColor)
179
{
180
    fCol = fromColor;
181
    tCol = toColor;
182
    mCol = midColor;
183
}
184

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.