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
|
package test;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
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 javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.media.opengl.glu.GLU;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;
public class MyTextures implements GLEventListener {
// Dimension de la frame
private int width = 1000;
private int height = 1000;
// Textures
private Texture texture1;
private Texture texture2;
// Coordonnées des quads
private static float T = 1f;
private float x1 = -T;
private float y1 = -T * 2 / 3;
private float z1 = 0;
private float x2 = T;
private float y2 = -T * 2 / 3;
private float z2 = 0;
private float x3 = T;
private float y3 = T * 2 / 3;
private float z3 = 0;
private float x4 = -T;
private float y4 = T * 2 / 3;
private float z4 = 0;
public static void main(String[] args) {
new MyTextures();
}
private MyTextures() {
// Création de la frame et du canvas GL
int x = (Toolkit.getDefaultToolkit().getScreenSize().width - width) / 2;
int y = (Toolkit.getDefaultToolkit().getScreenSize().height - height) / 2;
Frame frame = null;
frame = new Frame("MyTextures");
frame.setSize(width, height);
frame.setLocation(x, y);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
GLCanvas canvas = new GLCanvas();
canvas.addGLEventListener(this);
frame.add(canvas);
frame.setVisible(true);
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL.GL_TEXTURE_2D);
try {
// Chargement de la 1ère texture
texture1 = TextureIO.newTexture(new File("./pics/IMGP1680.JPG"),
false);
texture1.bind(gl);
texture1.enable(gl);
texture1.setTexParameteri(gl, GL.GL_TEXTURE_MIN_FILTER,
GL.GL_LINEAR);
texture1.setTexParameteri(gl, GL.GL_TEXTURE_MAG_FILTER,
GL.GL_LINEAR);
// Chargement de la 2ème texture
texture2 = TextureIO.newTexture(new File("./pics/IMGP1680.JPG"),
false);
texture2.bind(gl);
texture2.enable(gl);
texture2.setTexParameteri(gl, GL.GL_TEXTURE_MIN_FILTER,
GL.GL_LINEAR);
texture2.setTexParameteri(gl, GL.GL_TEXTURE_MAG_FILTER,
GL.GL_LINEAR);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
GLU glu = new GLU();
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0, (float) width
/ (float) height, 0, 12);
// Default translation
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glClearColor(0, 0, 0, 0);
gl.glClearDepth(1.0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT
| GL.GL_DEPTH_BUFFER_BIT);
gl.glPushMatrix();
gl.glTranslatef(0, 0, -5);
gl.glPushMatrix();
gl.glTranslatef(1, 1, 0);
// Affichage du 1er quad
texture1.enable(gl);
texture1.bind(gl);
gl.glBegin(GL2.GL_QUADS);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(x1, y1, z1);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(x2, y2, z2);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(x3, y3, z3);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(x4, y4, z4);
gl.glEnd();
gl.glPopMatrix();
gl.glPushMatrix();
gl.glTranslatef(-1, -1, 0);
// Affichage du 2ème quad
texture2.enable(gl);
texture2.bind(gl);
gl.glBegin(GL2.GL_QUADS);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(x1, y1, z1);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(x2, y2, z2);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(x3, y3, z3);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(x4, y4, z4);
gl.glEnd();
gl.glPopMatrix();
gl.glPopMatrix();
}
@Override
public void dispose(GLAutoDrawable drawable) {
// TODO Auto-generated method stub
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
// TODO Auto-generated method stub
}
} |
Partager