File: Texture.cxx

package info (click to toggle)
glpuzzle 0.2-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,412 kB
  • ctags: 210
  • sloc: cpp: 1,371; makefile: 48
file content (101 lines) | stat: -rw-r--r-- 2,931 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
/*
 *  Copyright (C) 2005  Maarten de Boer <maarten@resorama.com>
 * 
 *  This program 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
 *  of the License, or (at your option) any later version.
 * 
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 * 
 */

#include "Texture.hxx"
#include <cmath>

void Texture::Bind(void)
{
   static Texture *bound = 0;
   if (bound != this)
      glBindTexture(GL_TEXTURE_2D, gl_texture);
}

void Texture::Draw(const Rect & src, const Rect & dst)
{
   float fx1 = float (src.x) / float (rect.w);
   float fy1 = float (src.y) / float (rect.h);
   float fx2 = float (src.x + src.w) / float (rect.w);
   float fy2 = float (src.y + src.h) / float (rect.h);

   Bind();

   glBegin(GL_QUADS);
   glTexCoord2f(fx1, fy1);
   glVertex3f(dst.x, dst.y, 0);
   glTexCoord2f(fx2, fy1);
   glVertex3f(dst.x + dst.w, dst.y, 0);
   glTexCoord2f(fx2, fy2);
   glVertex3f(dst.x + dst.w, dst.y + dst.h, 0);
   glTexCoord2f(fx1, fy2);
   glVertex3f(dst.x, dst.y + dst.h, 0);
   glEnd();
}
void Texture::Draw(const Rect & src, const Rect & dst,float angle)
{
   float fx1 = float (src.x) / float (rect.w);
   float fy1 = float (src.y) / float (rect.h);
   float fx2 = float (src.x + src.w) / float (rect.w);
   float fy2 = float (src.y + src.h) / float (rect.h);

   float fx = dst.x;
   float fy = dst.y;
   float fw = dst.w;
   float fh = dst.h;

   float cx = fx+fw/2.;
   float cy = fy+fh/2.;

   float d,a;

   d = sqrt((fx-cx)*(fx-cx) + (fy-cy)*(fy-cy));
   a = atan2(fy-cy,fx-cx) + angle;
   float dstx1 = cx+(0.5+d*cos(a));
   float dsty1 = cy+(0.5+d*sin(a));

   d = sqrt((fx+fw-cx)*(fx+fw-cx) + (fy-cy)*(fy-cy));
   a = atan2(fy-cy,fx+fw-cx) + angle;
   float dstx2 = cx+(0.5+d*cos(a));
   float dsty2 = cy+(0.5+d*sin(a));

   d = sqrt((fx+fw-cx)*(fx+fw-cx) + (fy+fh-cy)*(fy+fh-cy));
   a = atan2(fy+fh-cy,fx+fw-cx) + angle;
   float dstx3 = cx+(0.5+d*cos(a));
   float dsty3 = cy+(0.5+d*sin(a));

   d = sqrt((fx-cx)*(fx-cx) + (fy+fh-cy)*(fy+fh-cy));
   a = atan2(fy+fh-cy,fx-cx) + angle;
   float dstx4 = cx+(0.5+d*cos(a));
   float dsty4 = cy+(0.5+d*sin(a));

   Bind();

   glBegin(GL_QUADS);
   glTexCoord2f(fx1, fy1);
   glVertex3f(dstx1, dsty1, 0);
   glTexCoord2f(fx2, fy1);
   glVertex3f(dstx2, dsty2, 0);
   glTexCoord2f(fx2, fy2);
   glVertex3f(dstx3, dsty3, 0);
   glTexCoord2f(fx1, fy2);
   glVertex3f(dstx4, dsty4, 0);
   glEnd();
}