forked from tpaviot/pythonocc-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureItem.cpp
More file actions
109 lines (98 loc) · 3.76 KB
/
TextureItem.cpp
File metadata and controls
109 lines (98 loc) · 3.76 KB
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
/*
##Copyright 2017 Thomas Paviot (tpaviot@gmail.com)
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU Lesser General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC 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 Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TextureItem.h"
#include <OpenGl_GlCore20.hxx>
TextureItem::TextureItem (const TCollection_AsciiString& theImageFilename,
V3d_View* theView,
const Handle(Visual3d_Layer)& theLayer):
myLayer(theLayer), myView(theView), x(0), y(0), TypeOfPosition(0), myTextureId (-1)
{
Handle(Image_AlienPixMap) theImage=new Image_AlienPixMap();
theImage->Load(theImageFilename);
if (!theImage.IsNull()) {
myW = theImage->Width();
myH = theImage->Height();
unsigned char *aData = (unsigned char*)Standard::Allocate ((myW * myH) << 2);
unsigned char *p = aData;
Standard_Integer i, j;
Quantity_Color aColor;
Quantity_Parameter theAlpha;
for ( j = 0; j < myH; j++) {
for ( i = 0; i < myW; i++ ) {
aColor = theImage->PixelColor (i, j, theAlpha);
*p++ = (unsigned char)(255 * aColor.Red());
*p++ = (unsigned char)(255 * aColor.Green());
*p++ = (unsigned char)(255 * aColor.Blue());
*p++ = (unsigned char)(255 * theAlpha);
}
}
Init (aData);
}
if (!myLayer.IsNull ())
myLayer->AddLayerItem (this);
}
/*! Destroys the object and frees all allocated resources.
*/
TextureItem::~TextureItem()
{
if (myTextureId > 0)
glDeleteTextures (1, &myTextureId);
myTextureId = -1;
}
void TextureItem::Init(unsigned char* theData)
{
glGenTextures(1, &myTextureId);
glBindTexture(GL_TEXTURE_2D, myTextureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glTexImage2D(GL_TEXTURE_2D, 0, 4, myW, myH, 0, GL_RGBA, GL_UNSIGNED_BYTE, theData);
Standard_Address aData = theData;
Standard::Free (aData);
}
// render item
void TextureItem::RedrawLayerPrs ()
{
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, myTextureId);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBegin(GL_QUADS);
if (TypeOfPosition==0) {//absolute}
glTexCoord2f (0.0f, 0.0f); glVertex2f (x, y);
glTexCoord2f (1.0f, 0.0f); glVertex2f (x + myW, y);
glTexCoord2f (1.0f, 1.0f); glVertex2f (x + myW, y + myH);
glTexCoord2f (0.0f, 1.0f); glVertex2f (x, y + myH);
}
else if (TypeOfPosition==1) {//relative
int aW=0, aH=0;
myView->Window()->Size (aW, aH);
int x_orig = int(x*aW / 100);
int y_orig = int(y*aH / 100);
glTexCoord2f (0.0f, 0.0f); glVertex2f (x_orig, y_orig);
glTexCoord2f (1.0f, 0.0f); glVertex2f (x_orig + myW, y_orig);
glTexCoord2f (1.0f, 1.0f); glVertex2f (x_orig + myW, y_orig + myH);
glTexCoord2f (0.0f, 1.0f); glVertex2f (x_orig, y_orig + myH);
}
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}