File: face.hpp

package info (click to toggle)
3ddesktop 0.2.8-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,096 kB
  • ctags: 686
  • sloc: cpp: 5,996; sh: 3,222; makefile: 78
file content (253 lines) | stat: -rw-r--r-- 6,637 bytes parent folder | download
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//  ----------------------------------------------------------
//
//  Copyright (C) 2002 Brad Wasson <bard@systemtoolbox.com>
//
//  This file is part of 3ddesktop.
//
//  3ddesktop 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, or (at your option)
//  any later version.
//
//  3ddesktop 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 3ddesktop; see the file COPYING.   If not, write to
//  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
//

#ifndef _FACE_HPP
#define _FACE_HPP

#include <GL/gl.h>
#include <GL/glu.h>
#include "util.h"
#include "config.hpp"

extern Config *cfg;


class Point {
public:
    vertex_t v;
    Point (float _x = 0, float _y = 0, float _z = 0) {
        v[0] = _x; v[1] = _y; v[2] = _z; v[3] = 1;
    }
    void set (float _x, float _y, float _z) {
        v[0] = _x; v[1] = _y; v[2] = _z; v[3] = 1;
    }
};


class Face {
public:
    Point topleft, topright, bottomleft, bottomright;
    
    Point normal;

    GLuint texture_id;
    float transparency;

    short active;

    uint clist;

    int wireframe;

    Face (int wf = -1) {
        memset (&topleft, 0, sizeof(Point));
        memset (&topright, 0, sizeof(Point));
        memset (&bottomleft, 0, sizeof(Point));
        memset (&bottomright, 0, sizeof(Point));
        memset (&normal, 0, sizeof(Point));
        texture_id = (GLuint)-1;
        transparency = .5;
        active = 0;
        clist = (uint)-1;
        wireframe = wf;
    }

    
    void calculate_normal () {

        // tl ---- tr
        //         |
        //         br

        // calculate normal
        float x1 = topleft.v[0] - topright.v[0];
        float y1 = topleft.v[1] - topright.v[1];
        float z1 = topleft.v[2] - topright.v[2];

        float x2 = bottomright.v[0] - topright.v[0];
        float y2 = bottomright.v[1] - topright.v[1];
        float z2 = bottomright.v[2] - topright.v[2];

        normal.v[0] = (y1 * z2) - (y2 * z1);
	normal.v[1] = (z1 * x2) - (z2 * x1);
	normal.v[2] = (x1 * y2) - (x2 * y1);

        // normalize normal ;) 
        float len = (float)sqrt(normal.v[0] * normal.v[0] + 
                                normal.v[1] * normal.v[1] + 
                                normal.v[2] * normal.v[2]);
        normal.v[0] /= len;
        normal.v[1] /= len;
        normal.v[2] /= len;

    }

    void set_list (uint i) {
        clist = i;
    }

    void set_corners(Point tl, 
                     Point tr, 
                     Point bl, 
                     Point br)
    {
        topleft = tl;
        topright = tr;
        bottomleft = bl;
        bottomright = br;
        
        calculate_normal ();
    }

    void set_active () {
        active = 1;
    }

    void set_transparency (float t) {
        transparency = t;
    }

    void set_texture_id (GLuint tid) {
        texture_id = tid;
    }

    GLuint get_texture_id() {
        return texture_id;
    }

    GLuint *get_texture_id_ptr() {
        return &texture_id;
    }

    void render (void) {
        int use_texture = 1;

        if (texture_id != (GLuint)-1) {
            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D, texture_id);   // 2d texture (x and y size)

            //glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE);
            //glEnable(GL_COLOR_MATERIAL);

            glColor4f(transparency, transparency, transparency, transparency);

            //msgout(DEBUG, "======== t=%f\n", transparency);

            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

        } else {
            // no digit or screenshot so show gray color
            glDisable(GL_TEXTURE_2D);
            use_texture = 0;


            float v = 0.3;

            switch (cfg->options->frame_color) {
            case COLOR_RED:
                glColor4f(v, 0, 0, 1.0); 
                break;
            case COLOR_GREEN:
                glColor4f(0, v, 0, 1.0);
                break;
            case COLOR_BLUE:
                glColor4f(0, 0, v, 1.0);
                break;
            case COLOR_LIGHTBLUE:
                glColor4f(0, v, v, 1.0);
                break;
            case COLOR_GRAY:
                glColor4f(v, v, v, 1.0);
                break;
            case COLOR_WHITE:
                glColor4f(1.0, 1.0, 1.0, 1.0);
                break;
            case COLOR_PURPLE:
                glColor4f(v, 0, v, 1.0);
                break;
            case COLOR_YELLOW:
                glColor4f(v, v, 0, 1.0);
                break;
            default: // gray
                glColor4f(v, v, v, 1.0);
                break;
            }

            if (wireframe == 1 ||
                (wireframe == -1 && cfg->options->use_wireframe))
                glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
            
            glLineWidth (2);
            glDisable (GL_LIGHTING);
        }

        if (clist != (uint)-1) {
            glCallList (clist);
        } else {
            glBegin(GL_QUADS);

            glNormal3fv( normal.v );

            glTexCoord2f (0.0f, 0.0f);
            glVertex3fv(topleft.v);

            glTexCoord2f (1.0f, 0.0f);
            glVertex3fv(topright.v);

            glTexCoord2f (1.0f, 1.0f);
            glVertex3fv(bottomright.v);

            glTexCoord2f (0.0f, 1.0f);
            glVertex3fv(bottomleft.v);

            glEnd();

        }

        glEnable (GL_LIGHTING);


        // restore to fill
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }

    void load_texture_data (int texture_size, unsigned char *data) {

        if (texture_id == (GLuint)-1)
            glGenTextures(1, &texture_id);

        glBindTexture(GL_TEXTURE_2D, texture_id);   
        
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, MIN_FILTER);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, MAX_FILTER);
        
#ifndef GL_COMPRESSED_RGBA
#define GL_COMPRESSED_RGBA GL_RGBA
#endif
        if (cfg->options->glcompression)
            gluBuild2DMipmaps(GL_TEXTURE_2D, GL_COMPRESSED_RGBA, texture_size, texture_size, GL_RGBA, GL_UNSIGNED_BYTE, data);
        else
            gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, texture_size, texture_size, GL_RGBA, GL_UNSIGNED_BYTE, data);
    }

};

#endif // _FACE_HPP