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
| import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureData;
import com.jogamp.opengl.util.texture.TextureIO;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
public class OpenGL extends GLCanvas implements GLEventListener {
private static final long serialVersionUID = 1L;
Texture boxTexture;
int[] textures = new int[2];
GL2 gl;
public void display(GLAutoDrawable gLDrawable) {
final GL2 gl = gLDrawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity(); // Reset The View
gl.glBindTexture(GL.GL_TEXTURE_2D,1);
//boxTexture.bind(gLDrawable.getGL());
gl.glBegin(GL2.GL_QUADS);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex2f(-0.5f, -0.5f);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex2f(0.5f, -0.5f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex2f(0.5f, 0.50f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex2f(-0.5f, 0.5f);
gl.glEnd();
}
public void init(GLAutoDrawable gLDrawable) {
gl = gLDrawable.getGL().getGL2();
final GL gl = gLDrawable.getGL();
gl.glClearColor(0.0f, 1.0f, 0.0f,0); // Black Background
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
gl.glEnable(GL.GL_TEXTURE_2D);
LoadTexture();
CreateTexture(null);
}
private void LoadTexture() {
URL imageURL= this.getClass().getResource("/Resources/Images/add.png");
try {
TextureData data = TextureIO.newTextureData(gl.getGLProfile(),
imageURL.openStream(),false, "png");
boxTexture = TextureIO.newTexture(data);
} catch (IOException exc) {
exc.printStackTrace();
System.exit(1);
}
}
public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width,
int height) {
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void CreateTexture(Image image)
{
URL imageURL= this.getClass().getResource("/Resources/Images/add.png");
TextureData data=null;
try {
data = TextureIO.newTextureData(gl.getGLProfile(),
imageURL.openStream(),false, "png");
boxTexture = TextureIO.newTexture(data);
} catch (IOException exc) {
exc.printStackTrace();
System.exit(1);
}
gl.glEnable(GL2.GL_TEXTURE_2D);
gl.glGenTextures(1,textures,0);
gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[0]);
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0,GL2.GL_RGBA,data.getWidth(),data.getHeight(),0,GL2.GL_RGBA,
GL2.GL_UNSIGNED_BYTE,data.getBuffer());
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
} |