Bonjour

Je travaille sur un petit code demo où je cherche à afficher un carré en utilisant les VBO. Ensuite, je veux pouvoir "bouger" ce carré à l'aide de la souris. J'utilise Java6 et jogl 1.1.1. Voici mes codes. Le code MovingSquare.java contient la routine main et tout ce qu'il faut pour afficher et interagir avec la souris:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
 
package demo;
 
 
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.BufferUtil;
import demo.geom.Square;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author GLDavid
 */
public class MovingSquare
        implements GLEventListener, MouseWheelListener, MouseMotionListener,
        MouseListener {
 
    private float zoom = -20;
    private Square square;
    private IntBuffer bufferName;
    private FloatBuffer positionData;
    private FloatBuffer colorData;
    private FloatBuffer falseColorData;
    private IntBuffer starts;
    private IntBuffer counts;
    private final int LIMIT = 3;
    private Point mousePosition;
    private boolean mustCheck;
    private static GLCanvas canvas;
 
    public MovingSquare() {
        this.square = new Square(0, 0, 0);
        this.bufferName = BufferUtil.newIntBuffer(LIMIT);
    }
 
    private void initDataBuffers(final GL gl){
        int i = 0;
        int j = 4;
        this.positionData.put(square.getBufferVertices());
        this.colorData.put(square.getBufferColor());
        this.falseColorData.position(square.sizeofBufferFalseColor());
        starts.put(i);
        counts.put(j);
        i = i + square.getBufferIndicesSize();
        j = square.getBufferIndicesSize();
        this.positionData.rewind();
        this.colorData.rewind();
        this.falseColorData.rewind();
    }
 
    private void pushNodesDataBuffers(final GL gl) {
        initDataBuffers(gl);
 
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
        gl.glBufferData(gl.GL_ARRAY_BUFFER,
                this.square.sizeofBufferVertices() *
                BufferUtil.SIZEOF_FLOAT,
                positionData,
                gl.GL_STREAM_DRAW);
        gl.glVertexPointer(3, gl.GL_FLOAT, 0, 0);
 
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(1));
        gl.glBufferData(gl.GL_ARRAY_BUFFER,
                this.square.sizeofBufferColor() *
                BufferUtil.SIZEOF_FLOAT,
                colorData,
                gl.GL_STATIC_DRAW);
        gl.glColorPointer(4, gl.GL_FLOAT, 0, 0);
 
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(2));
        gl.glBufferData(gl.GL_ARRAY_BUFFER,
                this.square.sizeofBufferFalseColor() *
                BufferUtil.SIZEOF_FLOAT,
                falseColorData,
                gl.GL_STATIC_DRAW);
        gl.glColorPointer(4, gl.GL_FLOAT, 0, 0);
 
        this.starts.rewind();
        this.counts.rewind();
    }
 
    public void init(GLAutoDrawable drawable) {
        final GL gl = drawable.getGL();
 
        drawable.addMouseWheelListener(this);
        drawable.addMouseMotionListener(this);
 
        gl.glShadeModel(gl.GL_SMOOTH);
        gl.glEnable(gl.GL_DEPTH_TEST);
 
        gl.glGenBuffers(LIMIT, bufferName);
 
        int constant = BufferUtil.SIZEOF_FLOAT;
        this.positionData = BufferUtil.newFloatBuffer(square.sizeofBufferVertices() * constant);
        this.colorData = BufferUtil.newFloatBuffer(square.sizeofBufferColor() * constant);
        this.falseColorData = BufferUtil.newFloatBuffer(square.sizeofBufferFalseColor() * constant);
        this.starts = BufferUtil.newIntBuffer(1);
        this.counts = BufferUtil.newIntBuffer(1);
 
        pushNodesDataBuffers(gl);
    }
 
    public void display(GLAutoDrawable drawable) {
        final GL gl = drawable.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glClearColor(1, 1, 1, 0);
 
        gl.glMatrixMode(gl.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, zoom);
 
        gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL.GL_COLOR_ARRAY);
 
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
        gl.glVertexPointer(3, gl.GL_FLOAT, 0, 0);
        if(!mustCheck){
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(1));
            gl.glColorPointer(4, gl.GL_FLOAT, 0, 0);
        }
        else{
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(2));
            gl.glColorPointer(4, gl.GL_FLOAT, 0, 0);
        }
        gl.glMultiDrawArrays(gl.GL_QUADS, starts, counts, 1);
 
        if(mustCheck){
            checkFalseColor(gl);
        }
 
        gl.glFlush();
    }
 
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        final int maxZ = 1000;
        GL gl = drawable.getGL();
        gl.glViewport(0, 0, width, height);
        /*For the camera*/
        gl.glMatrixMode(gl.GL_PROJECTION);
        gl.glLoadIdentity();
        //Change this by glOrtho
        new GLU().gluPerspective(45., (float) width / height, 1., maxZ);
        /*Model & view*/
        gl.glMatrixMode(gl.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
 
    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }
 
    public void mouseWheelMoved(MouseWheelEvent e) {
        if (e.getWheelRotation() > 0) {
            zoom += 0.5;
        } else {
            zoom -= 0.5;
        }
    }
 
    public void mouseDragged(MouseEvent e) {
        this.mousePosition = e.getPoint();
        mustCheck = true;
    }
 
    private boolean onNode(FloatBuffer fb){
        for(int i=0; i<3; i++){
            if(fb.get(i)!=0){
                return false;
            }
        }
        return true;
    }
 
    private void moveNode(final GL gl) {
        final int constant = BufferUtil.SIZEOF_FLOAT;
        final int size = square.sizeofBufferVertices() * constant;
        this.square.changePosition(mousePosition.x, mousePosition.y, gl);
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
        gl.glBufferData(gl.GL_ARRAY_BUFFER,
                size,
                positionData,
                gl.GL_STREAM_DRAW);
        FloatBuffer newPositions = square.getBufferVertices();
        gl.glBufferSubData(gl.GL_ARRAY_BUFFER, 0, size, newPositions);
    }
 
    private void checkFalseColor(final GL gl){
        int[]viewport = new int[4];
        gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
        gl.glReadBuffer(GL.GL_BACK);
        FloatBuffer fb = BufferUtil.newFloatBuffer(3);
        gl.glPixelStorei (gl.GL_UNPACK_ALIGNMENT, 1);
        gl.glReadPixels(mousePosition.x, mousePosition.y, 1, 1, gl.GL_RGB,
                gl.GL_FLOAT, fb);
        if(onNode(fb)){
            moveNode(gl);
        }
        mustCheck = false;
        canvas.display();
    }
 
    public static void main(String[] args) {
        GLCapabilities glc = new GLCapabilities();
        glc.setDoubleBuffered(true);
 
        canvas = new GLCanvas(glc);
        MovingSquare demo = new MovingSquare();
        canvas.addGLEventListener(demo);
        canvas.setAutoSwapBufferMode(true);
 
        final Animator animator = new Animator(canvas);
        animator.start();
 
        JFrame frame = new JFrame("Moving VBO square");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        canvas.setSize(400, 400);
        frame.add(canvas, BorderLayout.CENTER);
 
        frame.pack();
        frame.setVisible(true);
        canvas.requestFocus();
    }
 
    public void mouseMoved(MouseEvent arg0) {
    }
 
    public void mouseClicked(MouseEvent arg0) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
 
    public void mousePressed(MouseEvent arg0) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
 
    public void mouseReleased(MouseEvent arg0) {
        canvas.display();
    }
 
    public void mouseEntered(MouseEvent arg0) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
 
    public void mouseExited(MouseEvent arg0) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
Le code Square.java contient toutes les fonctions nécessaires pour dessiner un carré.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package demo.geom;
 
import com.sun.opengl.util.BufferUtil;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;
 
/**
 *
 * @author GLDavid
 */
public class Square {
 
    private final float side = 0.5f;
    private float x, y, z;
    final private byte[]indices = {0, 1, 2, 3};
    float[]coords;
    private int sizeOfBufferVertices;
    private FloatBuffer nodeVertices;
    private int sizeOfBufferColor;
    private FloatBuffer nodeColor;
    private ByteBuffer nodeIndices;
    private FloatBuffer nodeFalseColor;
    private int sizeOfBufferFalseColor;
 
    public Square(float x, float y, float z){
        setXY(x, y);
        this.z = z;
        setCoordinates();
        setupNodeVerticesBuffer();
        setupNodeColorBuffer();
        setupNodeFalseColorBuffer();
        setupNodeIndicesBuffer();
    }
 
    private void setXY(float x, float y){
        this.x = x;
        this.y = y;
    }
 
    private double[] convertIntoGL(final GL gl, double x, double y) {
        GLU glu = new GLU();
        int viewport[] = new int[4];
        double mvmatrix[] = new double[16];
        double projmatrix[] = new double[16];
        double wcoord[] = new double[4];// wx, wy, wz;// returned xyz coords
        gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
        gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mvmatrix, 0);
        gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projmatrix, 0);
        double realy = viewport[3] - (int) y - 1;
        glu.gluUnProject(x, realy, 0.0, mvmatrix, 0, projmatrix, 0, viewport, 0, wcoord, 0);
        return wcoord;
    }
 
    /**
     * Calculates the translation vector in 2D for two given points
     * @param xA X coordinate of the first point
     * @param yA Y coordinate of the first point
     * @param xB X coordinate of the second point
     * @param yB Y coordinate of the second point
     * @return an array of two float which is the translation vector
     */
    private float[] getTranslationVector(
            float xA,
            float yA,
            float xB,
            float yB) {
        float[] vector = new float[2];
        vector[0] = xB - xA;
        vector[1] = yB - yA;
        return vector;
    }
 
    public void changePosition(float x, float y, final GL gl){
        double[]coord = convertIntoGL(gl, x, y);
        float[]vector = getTranslationVector(this.x, this.y, (float)coord[0], (float)coord[1]);
        setXY(vector[0], vector[1]);
        setCoordinates();
        setupNodeVerticesBuffer();
    }
 
    public FloatBuffer getBufferVertices(){
        return this.nodeVertices;
    }
 
    public int sizeofBufferVertices(){
        return this.sizeOfBufferVertices;
    }
 
    public FloatBuffer getBufferColor(){
        return this.nodeColor;
    }
 
    public FloatBuffer getBufferFalseColor(){
        return this.nodeFalseColor;
    }
 
    public int sizeofBufferColor(){
        return this.sizeOfBufferColor;
    }
 
    public int sizeofBufferFalseColor(){
        return this.sizeOfBufferFalseColor;
    }
 
    public int getBufferIndicesSize(){
        return this.indices.length;
    }
 
    private void setCoordinates(){
        coords = new float[12];
        coords[0] = x-side;
        coords[1] = y+side;
        coords[2] = z;
        coords[3] = x+side;
        coords[4] = y+side;
        coords[5] = z;
        coords[6] = x+side;
        coords[7] = y-side;
        coords[8] = z;
        coords[9] = x-side;
        coords[10] = y-side;
        coords[11] = z;
    }
 
    private void setupNodeVerticesBuffer(){
        int bufferVerticesSize = coords.length;
        this.sizeOfBufferVertices = bufferVerticesSize * BufferUtil.SIZEOF_FLOAT;
        nodeVertices = BufferUtil.newFloatBuffer(bufferVerticesSize);
        nodeVertices.put(coords);
        nodeVertices.rewind();
    }
 
    private void setupNodeColorBuffer(){
        float[]colorComponds = {1, 0, 0, 0};
        int bufferColorSize = colorComponds.length * this.indices.length;
        this.sizeOfBufferColor = bufferColorSize * BufferUtil.SIZEOF_FLOAT;
        float[]allColorComponds = new float[bufferColorSize];
        int j=0;
        for(int i=0; i<allColorComponds.length; i++){
            allColorComponds[i] = colorComponds[j++];
            if(j==colorComponds.length){
                j=0;
            }
        }
        nodeColor = BufferUtil.newFloatBuffer(allColorComponds.
                length);
        nodeColor.put(allColorComponds);
        nodeColor.rewind();
    }
 
    private void setupNodeFalseColorBuffer(){
        float[]colorComponds = {0, 0, 0, 0};
        int bufferColorSize = colorComponds.length * this.indices.length;
        this.sizeOfBufferFalseColor = bufferColorSize * BufferUtil.SIZEOF_FLOAT;
        float[]allColorComponds = new float[bufferColorSize];
        int j=0;
        for(int i=0; i<allColorComponds.length; i++){
            allColorComponds[i] = colorComponds[j++];
            if(j==colorComponds.length){
                j=0;
            }
        }
        nodeFalseColor = BufferUtil.newFloatBuffer(allColorComponds.
                length);
        nodeFalseColor.put(allColorComponds);
        nodeFalseColor.rewind();
    }
 
    private void setupNodeIndicesBuffer(){
        nodeIndices = BufferUtil.newByteBuffer(this.indices.length);
        nodeIndices.put(this.indices);
        nodeIndices.rewind();
    }
 
}
Tout compile correctement et à l'exécution, tout s'affiche bien. Le problème est que lorsque je bouge mon carré avec la souris, celui-ci ne bouge qu'infiniment peu !
Quelqu'un saurait-il me conseiller sur ce problème ?

Merci d'avance.

@++