IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

OpenGL Discussion :

VBO code de test


Sujet :

OpenGL

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre expérimenté
    Avatar de GLDavid
    Homme Profil pro
    Head of Service Delivery
    Inscrit en
    Janvier 2003
    Messages
    2 898
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Head of Service Delivery
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 898
    Par défaut VBO code de test
    Bonjour

    A l'aide de jogl 1.1.1, je souhaite m'initier aux VBO. Pour le moment, mon objectif est simplement d'afficher un carré à l'aide de cette technique. Voici le code:
    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
     
     
    import com.sun.opengl.util.Animator;
    import com.sun.opengl.util.BufferUtil;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.nio.FloatBuffer;
    import java.nio.IntBuffer;
    import java.nio.ShortBuffer;
    import javax.media.opengl.GL;
    import javax.media.opengl.GLAutoDrawable;
    import javax.media.opengl.GLCanvas;
    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 dbourgais
     */
    public class VBOTestJOGL {
     
        public static void main(String[]args){
            GLCanvas canvas = new GLCanvas();
            VBOTest demo = new VBOTest();
            canvas.addGLEventListener(demo);
     
            final Animator animator = new Animator(canvas);
            animator.start();
     
            JFrame frame = new JFrame("VBO");
            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();
        }
     
    }
     
    class VBOTest implements GLEventListener{
     
        final private float[]coords = {
            -1, 1, 0,
            1, 1, 0,
            1, -1, 0,
            -1, -1, 0
        };
     
        final private short[]indices = {
            0, 1, 2, 3
        };
     
        final private Color color = Color.RED;
     
        /**
             * A rotating square!
             */
    	private int buffer_id = 2;
    	private FloatBuffer vertices;
    	private ShortBuffer b_indices;
        private FloatBuffer b_color;
     
        private IntBuffer squareBuffers;
     
     
        public void init(GLAutoDrawable drawable) {
            final GL gl = drawable.getGL();
            this.vertices = BufferUtil.newFloatBuffer(this.coords.length);
            this.vertices.put(coords);
            this.b_indices = BufferUtil.newShortBuffer(this.indices.length);
            this.b_indices.put(indices);
            float[]comp = new float[4];
            comp = this.color.getColorComponents(comp);
            this.b_color = BufferUtil.newFloatBuffer(4 * comp.length);
            for(int i=0, j=0; i<4 * comp.length; i++){
                this.b_color.put(comp[j++]);
                if(j==4)
                    j=0;
            }
     
            squareBuffers = BufferUtil.newIntBuffer(2);
            gl.glGenBuffers(buffer_id, squareBuffers);
     
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, squareBuffers.get(0));
            gl.glBufferData(gl.GL_ARRAY_BUFFER, BufferUtil.SIZEOF_FLOAT, this.vertices, gl.GL_STATIC_DRAW);
     
            gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, squareBuffers.get(1));
            gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, BufferUtil.SIZEOF_SHORT, this.b_indices, gl.GL_STATIC_DRAW);
        }
     
        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, -5);
     
            if(gl.glIsEnabled(gl.GL_VERTEX_ARRAY)){
                gl.glDisableClientState(gl.GL_VERTEX_ARRAY);
            }
            if(gl.glIsEnabled(gl.GL_COLOR_ARRAY)){
                gl.glDisableClientState(gl.GL_COLOR_ARRAY);
            }
     
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, squareBuffers.get(0));
            gl.glVertexPointer(3, GL.GL_FLOAT, 0, this.vertices);
            gl.glColorPointer(4, GL.GL_FLOAT, 0, this.b_color);
     
            gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
            gl.glEnableClientState(gl.GL_COLOR_ARRAY);
            gl.glDrawElements(gl.GL_QUADS, 4, gl.GL_UNSIGNED_SHORT, 0);
            gl.glDisableClientState(gl.GL_COLOR_ARRAY);
            gl.glDisableClientState(gl.GL_VERTEX_ARRAY);
     
            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) {
        }
     
    }
    Or, à l'exécution, j'ai l'erreur suivante:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    javax.media.opengl.GLException: javax.media.opengl.GLException: array vertex_buffer_object must be disabled to call this method
    La méthode en question étant: gl.glVertexPointer(3, GL.GL_FLOAT, 0, this.vertices);
    Quelqu'un peut-il m'expliquer ce qui se passe et comment y remédier ?

    Merci d'avance.

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code :tagcode: ni le tag :resolu:

    Je ne répond à aucune question technique par MP.

  2. #2
    Membre expérimenté
    Avatar de GLDavid
    Homme Profil pro
    Head of Service Delivery
    Inscrit en
    Janvier 2003
    Messages
    2 898
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Head of Service Delivery
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 898
    Par défaut
    Bonjour

    Suite à quelques lectures sur le net, j'ai arrangé mon code pour que cette erreur n'arrive plus.
    Désormais, voici mon code:
    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
     
     
    import com.sun.opengl.util.Animator;
    import com.sun.opengl.util.BufferUtil;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.MouseWheelEvent;
    import java.awt.event.MouseWheelListener;
    import java.nio.FloatBuffer;
    import java.nio.IntBuffer;
    import java.nio.ShortBuffer;
    import javax.media.opengl.GL;
    import javax.media.opengl.GLAutoDrawable;
    import javax.media.opengl.GLCanvas;
    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 dbourgais
     */
    public class VBOTestJOGL {
     
        public static void main(String[]args){
            GLCanvas canvas = new GLCanvas();
            VBOTest demo = new VBOTest();
            canvas.addGLEventListener(demo);
     
            final Animator animator = new Animator(canvas);
            animator.start();
     
            JFrame frame = new JFrame("VBO");
            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();
        }
     
    }
     
    class VBOTest implements GLEventListener, MouseWheelListener{
     
        final private float[]coords = {
            -1, 1, 0,
            1, 1, 0,
            1, -1, 0,
            -1, -1, 0
        };
     
        final private short[]indices = {
            0, 1, 2, 3
        };
     
        final private Color color = Color.RED;
     
        /**
             * A rotating square!
             */
    	private int buffer_id = 2;
    	private FloatBuffer vertices;
    	private ShortBuffer b_indices;
        private FloatBuffer b_color;
     
        private IntBuffer squareBuffers;
     
        private float zoom = -5;
     
        private boolean vboCompatible(final GL gl){
            String versionStr = gl.glGetString(GL.GL_VERSION);
            versionStr = versionStr.substring(0, 3);
     
            // Check if extension is available.
            boolean extensionOK = gl.isExtensionAvailable("GL_ARB_vertex_buffer_object");
     
            // Check for VBO functions.
            boolean functionsOK =
                    gl.isFunctionAvailable("glGenBuffersARB") &&
                    gl.isFunctionAvailable("glBindBufferARB") &&
                    gl.isFunctionAvailable("glBufferDataARB") &&
                    gl.isFunctionAvailable("glDeleteBuffersARB");
     
            return (extensionOK || functionsOK);
        }
     
        public void init(GLAutoDrawable drawable) {
            final GL gl = drawable.getGL();
     
            this.vertices = BufferUtil.newFloatBuffer(this.coords.length);
            this.vertices.put(coords);
            this.vertices.rewind();
            this.b_indices = BufferUtil.newShortBuffer(this.indices.length);
            this.b_indices.put(indices);
            this.b_indices.rewind();
            float[]comp = new float[4];
            comp = this.color.getColorComponents(comp);
            this.b_color = BufferUtil.newFloatBuffer(4 * comp.length);
            for(int i=0, j=0; i<4 * comp.length; i++){
                this.b_color.put(comp[j++]);
                if(j==4) {
                    j = 0;
                }
            }
            this.b_color.rewind();
     
            squareBuffers = BufferUtil.newIntBuffer(2);
     
            gl.glShadeModel(gl.GL_SMOOTH);
            gl.glEnable(gl.GL_DEPTH_TEST);
     
            drawable.addMouseWheelListener(this);
        }
     
        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.glGenBuffers(buffer_id, squareBuffers);
     
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, squareBuffers.get(0));
            gl.glBufferData(gl.GL_ARRAY_BUFFER, BufferUtil.SIZEOF_FLOAT, this.vertices, gl.GL_STATIC_DRAW);
            gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
     
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, squareBuffers.get(1));
            gl.glBufferData(gl.GL_ARRAY_BUFFER, BufferUtil.SIZEOF_FLOAT, this.b_color, gl.GL_STATIC_DRAW);
            gl.glColorPointer(4, GL.GL_FLOAT, 0, 0);
     
            gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
            gl.glEnableClientState(gl.GL_COLOR_ARRAY);
            gl.glDrawArrays(gl.GL_QUADS, 0, this.indices.length);
            gl.glDisableClientState(gl.GL_COLOR_ARRAY);
            gl.glDisableClientState(gl.GL_VERTEX_ARRAY);
     
            gl.glDeleteBuffers(buffer_id, squareBuffers);
     
            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);
            //new GLU().gluOrtho2D(-10, 10, -10, 10);
            /*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;
            }
            System.out.println(zoom);
        }
     
    }
    Il demeure un problème: mon carré n'apparaît pas !
    J'ai essayé en adaptant mon code pour une utilisation des vertex array et pas de problème. Qu'ai-je manqué ?

    Merci d'avance.

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code :tagcode: ni le tag :resolu:

    Je ne répond à aucune question technique par MP.

  3. #3
    Membre chevronné
    Inscrit en
    Février 2008
    Messages
    413
    Détails du profil
    Informations personnelles :
    Âge : 45

    Informations forums :
    Inscription : Février 2008
    Messages : 413
    Par défaut
    Salut,

    je n'ai pas regardé ton initialisation en détails, mais pour le dessin il me semble que tu dois normalement activer les "client states" AVANT de passer les pointeurs correspondants.

    Un exemple de mon code, qui marche:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    glBindBuffer(GL_ARRAY_BUFFER, m_vboIDs[0] );
     
    		glEnableClientState(GL_VERTEX_ARRAY);
    		glVertexPointer(3, GL_FLOAT, sizeof(BGLVertex), 0);
    		glEnableClientState(GL_NORMAL_ARRAY);
    		glNormalPointer(GL_FLOAT, sizeof(BGLVertex), (char*) NULL + sizeof(GLfloat) * 3);
     
    glDrawArrays(GL_TRIANGLE_STRIP, 0, m_1DArray_Size);
    essaie-voir comme ca!

    Bonne chance!

    EDIT: je viens de voir que tu appelles glDeleteBuffers à la fin de ta boucle de dessin, pourquoi? ca t'oblige á reinitialiser ton VBO avant chaque dessin, alors que normalement tu l'initialises une seule fois, tu dessines autant que tu veux et tu ne le supprimes que quand tu n'en as plus besoin

  4. #4
    Membre expérimenté
    Avatar de GLDavid
    Homme Profil pro
    Head of Service Delivery
    Inscrit en
    Janvier 2003
    Messages
    2 898
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Head of Service Delivery
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 898
    Par défaut
    Hello

    Merci de ta réponse, mais, certainement que j'ai mal compris car cela ne fonctionne toujours pas chez moi.
    Voici mon code (uniquement les fonctions init et display):
    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
     
    public void init(GLAutoDrawable drawable) {
            final GL gl = drawable.getGL();
     
            this.vertices = BufferUtil.newFloatBuffer(this.coords.length);
            this.vertices.put(coords);
            this.vertices.rewind();
            this.b_indices = BufferUtil.newShortBuffer(this.indices.length);
            this.b_indices.put(indices);
            this.b_indices.rewind();
            float[]comp = new float[4];
            comp = this.color.getColorComponents(comp);
            this.b_color = BufferUtil.newFloatBuffer(4 * comp.length);
            for(int i=0, j=0; i<4 * comp.length; i++){
                this.b_color.put(comp[j++]);
                if(j==4) {
                    j = 0;
                }
            }
            this.b_color.rewind();
     
            squareBuffers = BufferUtil.newIntBuffer(2);
            gl.glGenBuffers(buffer_id, squareBuffers);
     
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, squareBuffers.get(0));
            gl.glBufferData(gl.GL_ARRAY_BUFFER, BufferUtil.SIZEOF_FLOAT, this.vertices, gl.GL_STATIC_DRAW);
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, squareBuffers.get(1));
            gl.glBufferData(gl.GL_ARRAY_BUFFER, BufferUtil.SIZEOF_FLOAT, this.b_color, gl.GL_STATIC_DRAW);
     
            gl.glShadeModel(gl.GL_SMOOTH);
            gl.glEnable(gl.GL_DEPTH_TEST);
     
            drawable.addMouseWheelListener(this);
        }
     
        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_COLOR_ARRAY);
            gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
     
     
            gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
            gl.glColorPointer(4, GL.GL_FLOAT, 0, 0);
     
     
            gl.glDrawArrays(gl.GL_QUADS, 0, this.indices.length);
     
            gl.glFlush();
        }
    Qu'ai-je encore oublié ?

    Merci d'avance

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code :tagcode: ni le tag :resolu:

    Je ne répond à aucune question technique par MP.

  5. #5
    Membre expérimenté
    Avatar de GLDavid
    Homme Profil pro
    Head of Service Delivery
    Inscrit en
    Janvier 2003
    Messages
    2 898
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Head of Service Delivery
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 898
    Par défaut
    Bonjour

    Et bien, voilà, j'ai décidé de tout réétudier. Et je suis arrivé à une solution. Je poste ici le code au complet pour qu'il serve à des fins de tutoriel:
    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
     
     
    import com.sun.opengl.util.Animator;
    import com.sun.opengl.util.BufferUtil;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.MouseWheelEvent;
    import java.awt.event.MouseWheelListener;
    import java.nio.ByteBuffer;
    import java.nio.FloatBuffer;
    import java.nio.IntBuffer;
    import java.nio.ShortBuffer;
    import javax.media.opengl.GL;
    import javax.media.opengl.GLAutoDrawable;
    import javax.media.opengl.GLCanvas;
    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 dbourgais
     */
    public class VBOTestJOGL {
     
        public static void main(String[]args){
            GLCanvas canvas = new GLCanvas();
            VBOTest demo = new VBOTest();
            canvas.addGLEventListener(demo);
     
            final Animator animator = new Animator(canvas);
            animator.start();
     
            JFrame frame = new JFrame("VBO");
            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();
        }
     
    }
     
    class VBOTest implements GLEventListener, MouseWheelListener {
     
        private float zoom = -5;
     
        /**Extract from a tuto*/
     
        final int positionSize = 6 * 3 * BufferUtil.SIZEOF_FLOAT;
        final float[]PositionData = {
            -1.0f, -1.0f, 0,
            1.0f, -1.0f, 0,
            1.0f, 1.0f, 0,
            1.0f, 1.0f, 0,
            -1.0f, 1.0f, 0,
            -1.0f, -1.0f, 0
        };
        FloatBuffer positionData;
     
        final int colorSize = 6 * 3 * BufferUtil.SIZEOF_BYTE;
        final byte[]ColorData = {
            (byte)255, (byte)0, (byte)0,
            (byte)255, (byte)255, (byte)0,
            (byte)0, (byte)255, (byte)0,
            (byte)0, (byte)255, (byte)0,
            (byte)0, (byte)0, (byte)255,
            (byte)255, (byte)0, (byte)0
        };
        ByteBuffer colorData;
     
        final int bufferSize = 2;
        int BufferName[];
        IntBuffer bufferName;
     
        final int vertexCount = 6; 
     
        private boolean vboCompatible(final GL gl) {
            String versionStr = gl.glGetString(GL.GL_VERSION);
            versionStr = versionStr.substring(0, 3);
     
            // Check if extension is available.
            boolean extensionOK = gl.isExtensionAvailable("GL_ARB_vertex_buffer_object");
     
            // Check for VBO functions.
            boolean functionsOK =
                    gl.isFunctionAvailable("glGenBuffersARB") &&
                    gl.isFunctionAvailable("glBindBufferARB") &&
                    gl.isFunctionAvailable("glBufferDataARB") &&
                    gl.isFunctionAvailable("glDeleteBuffersARB");
     
            return (extensionOK || functionsOK);
        }
     
        public void init(GLAutoDrawable drawable) {
            final GL gl = drawable.getGL();
            gl.glShadeModel(gl.GL_SMOOTH);
            gl.glEnable(gl.GL_DEPTH_TEST);
     
            bufferName = BufferUtil.newIntBuffer(bufferSize);
            gl.glGenBuffers(bufferSize, bufferName);
     
            colorData = BufferUtil.newByteBuffer(this.colorSize);
            colorData.put(ColorData);
            colorData.rewind();
     
            positionData = BufferUtil.newFloatBuffer(this.positionSize);
            positionData.put(PositionData);
            positionData.rewind();
     
            drawable.addMouseWheelListener(this);
        }
     
        public void display(GLAutoDrawable drawable) {
            final GL gl = drawable.getGL();
            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
            gl.glClearColor(0, 0, 0, 1);
     
            gl.glMatrixMode(gl.GL_MODELVIEW);
            gl.glLoadIdentity();
            gl.glTranslatef(0, 0, zoom);
     
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(1));
            gl.glBufferData(gl.GL_ARRAY_BUFFER, colorSize, colorData, gl.GL_STREAM_DRAW);
            gl.glColorPointer(3, gl.GL_UNSIGNED_BYTE, 0, 0);
     
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
            gl.glBufferData(gl.GL_ARRAY_BUFFER, this.positionSize, positionData, gl.GL_STREAM_DRAW);
            gl.glVertexPointer(3, gl.GL_FLOAT, 0, 0);
     
            gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
            gl.glEnableClientState(gl.GL_COLOR_ARRAY);
     
            gl.glDrawArrays(gl.GL_TRIANGLES, 0, vertexCount);
     
            gl.glDisableClientState(gl.GL_COLOR_ARRAY);
            gl.glDisableClientState(gl.GL_VERTEX_ARRAY);
     
            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);
            //new GLU().gluOrtho2D(-10, 10, -10, 10);
            /*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;
            }
            System.out.println(zoom);
        }
    }
    Bien sûr, on peut toujours optimiser ce code mais en l'état il fonctionne. Les propositions d'optimisations sont donc les bienvenues.
    Voilà

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code :tagcode: ni le tag :resolu:

    Je ne répond à aucune question technique par MP.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [AC-2010] Code VBA Test de Student
    Par StagiaireDK dans le forum VBA Access
    Réponses: 13
    Dernier message: 05/08/2013, 13h08
  2. Premier code de test, problème de console
    Par Frank1010 dans le forum x86 16-bits
    Réponses: 1
    Dernier message: 26/07/2011, 19h41
  3. couverture de code avec test JUnit sur tomcat distant
    Par Hurricae dans le forum Tomcat et TomEE
    Réponses: 1
    Dernier message: 31/08/2010, 23h01
  4. Coded UI Test
    Par sybaris dans le forum Visual Studio
    Réponses: 0
    Dernier message: 24/06/2010, 12h39
  5. Réponses: 8
    Dernier message: 27/03/2006, 17h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo