-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathITexture.java
159 lines (129 loc) · 4.23 KB
/
ITexture.java
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
/*---
iGeo - http://igeo.jp
Copyright (c) 2002-2013 Satoru Sugihara
This file is part of iGeo.
iGeo 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, version 3.
iGeo 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with iGeo. If not, see <http://www.gnu.org/licenses/>.
---*/
package igeo;
import java.awt.image.*;
import java.awt.*;
import igeo.*;
import igeo.gui.*;
/**
Abstract texture class (wrapping ITextureGL2)
*/
public class ITexture{
public ITextureGraphicGL glTexture;
public String filename;
public BufferedImage image;
public boolean needUpdate = false;
/** constructor with image file name */
public ITexture(String filename){
this.filename = filename;
//image = getBufferedImage(IImageLoader.getImage(filename));
}
public ITexture(String filename, int width, int height){
//this.filename = filename;
image = getBufferedImage((BufferedImage)IImageLoader.getImage(filename),width,height);
}
/** constructor with AWT image */
public ITexture(Image img){ image = getBufferedImage(img); }
/** constructor with AWT buffered image */
public ITexture(BufferedImage img){ image = img; }
/** constructor with pixel size */
public ITexture(int width, int height){ image =getBufferedImage(width,height); }
public int width(){
if(glTexture==null) return 0;
if(image!=null) return image.getWidth();
return glTexture.width();
}
public int height(){
if(glTexture==null) return 0;
if(image!=null) return image.getHeight();
return glTexture.height();
}
public BufferedImage getBufferedImage(Image image){
BufferedImage bimg = new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bimg.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimg;
}
public BufferedImage getBufferedImage(Image image, int width, int height){
BufferedImage bimg = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bimg.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return bimg;
}
public BufferedImage getBufferedImage(String imageFile){
BufferedImage bimg = new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bimg.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimg;
}
public BufferedImage getBufferedImage(int width, int height){
return new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
}
public int id(IGraphicsGL g){
if(glTexture!=null){
if(needUpdate){
if(image!=null){
glTexture.init(image,g.getGL());
}
else if(filename!=null){
glTexture.init(filename,g.getGL());
}
needUpdate=false;
}
return glTexture.id();
}
if(image!=null){
//glTexture = new ITextureGL2(image, g.getGL());
glTexture = g.getTextureGraphic(image);
return glTexture.id();
}
if(filename!=null){
//glTexture = new ITextureGL2(filename);
glTexture = g.getTextureGraphic(filename);
return glTexture.id();
}
return 0;
}
public void update(){
needUpdate = true;
}
public void destroy(){
if(glTexture!=null) glTexture.destroy();
}
/*
public Texture getTexture(IGraphicsGL gr){
GL gl = gr.getGL();
BufferedImage bimg = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bimg.createGraphics();
g.setBackground(Color.cyan);
g.clearRect(0,0,100,100);
g.setColor(Color.blue);
g.fill(new Rectangle(10,10,80,80));
g.setColor(Color.yellow);
g.fill(new Rectangle(20,20,10,10));
g.dispose();
try{
return AWTTextureIO.newTexture(gl.getGLProfile(), bimg, true);
}catch(Exception e){
IG.err("error generating texture");
e.printStackTrace();
}
return null;
}
*/
}