Help Plz

Hi all,

Below is a piece of code im working on.
Im playing with vertex arrays… but im not getting any output on the screen.

This section of code represents a shape… my render will forward calls to draw and init when its display and init are called…

Right now all i get is a black screen.

Guidance would be appreciated… as i want to understand what im doing wrong.


package spacegame;

import java.util.*;
import java.nio.*;
import net.java.games.jogl.*;
import net.java.games.util.*;
import darkmatter.*;



public class Shape implements Model {
    
    private FloatBuffer vertexBuffer;
    private FloatList tmpBuffer = new FloatList();
    private float[] data = {    4.0f, 5.0f, 6.0f, 7.0f,
                                1.0f, 2.0f, 6.0f, 5.0f,
                                0.0f, 1.0f, 5.0f, 4.0f,
                                0.0f, 3.0f, 2.0f, 1.0f,
                                0.0f, 4.0f, 7.0f, 3.0f,
                                2.0f, 3.0f, 7.0f, 6.0f,
                            };
    
    
    public void draw(GLDrawable glDrawable) {
        GL gl = glDrawable.getGL();
        gl.glColor3f(1.0f,0.0f,0.0f);
        gl.glEnableClientState(gl.GL_VERTEX_ARRAY); 
        gl.glVertexPointer(4, gl.GL_FLOAT, 0, vertexBuffer); 
        
        
        gl.glFlush();
    }    
    
    public void init(GLDrawable glDrawable) {
        GL gl = glDrawable.getGL();
        tmpBuffer.setData(data);
        vertexBuffer = BufferUtils.newFloatBuffer(tmpBuffer.size());
        
    }    
    
}

[quote]private float[] data = { 4.0f, 5.0f, 6.0f, 7.0f,
1.0f, 2.0f, 6.0f, 5.0f,
0.0f, 1.0f, 5.0f, 4.0f,
0.0f, 3.0f, 2.0f, 1.0f,
0.0f, 4.0f, 7.0f, 3.0f,
2.0f, 3.0f, 7.0f, 6.0f,
};
[/quote]
Looks to me like you are drawing your shape behind you. If you are using the default view, your camera is setting at (0,0,0) looking down the -z axis.

Ive tried reversing the values making them all negative … same results.

try editing draw to:

public void draw(GLDrawable glDrawable) {
GL gl = glDrawable.getGL();
gl.glTranslatef(0.0f,0.0f,10.0f); //Add this.
gl.glColor3f(1.0f,0.0f,0.0f);
gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
gl.glVertexPointer(4, gl.GL_FLOAT, 0, vertexBuffer);

Which will move the camera back along the z axis.

:frowning:
no joy… man this is fustrating… :slight_smile:
so far everything was going great till i decided to learn how to draw this way :slight_smile:

if anyone had a simple working demo it would be great!

are you sure that your vertex data is 4 and not 3?

gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertexBuffer);

Other than that, I’m at a loss.

public void init(GLDrawable glDrawable) {
  GL gl = glDrawable.getGL();
  tmpBuffer.setData(data);
  vertexBuffer = BufferUtils.newFloatBuffer(tmpBuffer.size());
}

I think your vertexBuffer field is just an empty buffer of the correct length after this bit of init code. You still need to fill it with data. Try adding

vertexBuffer.put( data );

check all of the above still no joy…

i hate to ask to be handle held… but the fact that i get no errors makes this one a hard one to trouble shoot.

would someone have a very basic example of working code that uses vertex arrays to draw a shape…

Im sure i will be able to reference your contribution and learn what ive done wrong… thanks.

JMG

Don’t you need to do a glDrawArrays or somesuch before your flush ?

Yep, you’re telling OpenGL that vertexBuffer is vertex data, but you’re never telling it to go and draw the data.

It’s always the obvious things that bite you. I sure skipped over that while reading the code.

ok… i understand that i missed a method call i have tried serveral variations… all i get now is a blank screen or native exceptions… im creating a single class test case to try and show all code im using and post it up here… its really bugging me that this is not working properly.

Any help that people can provide would be appreaciated. Final working code when i get this solved will be posted here cause i dont want anyone to have to go through this same fustration.

JMG

ok… here is all the logic as one big class… copy and past into a class called Test to run… all i get still is a blank screen… would appreciate any help ppl can provide.



import net.java.games.jogl.*;
import net.java.games.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.nio.*;

public class Test {
    
    private FloatBuffer vertexBuffer;
    private FloatList tmpBuffer = new FloatList();
    private float[] data = {    4.0f, 5.0f, 6.0f, 7.0f,
                                1.0f, 2.0f, 6.0f, 5.0f,
                                0.0f, 1.0f, 5.0f, 4.0f,
                                0.0f, 3.0f, 2.0f, 1.0f,
                                0.0f, 4.0f, 7.0f, 3.0f,
                                2.0f, 3.0f, 7.0f, 6.0f,
                            };
    
    
 
    public Test(){
        JFrame frame = new JFrame("Vertex Array Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800,600);
        GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
        frame.getContentPane().add("Center",canvas);
        Renderer renderer = new Renderer();
        canvas.addGLEventListener(renderer);
        Animator animator = new Animator(canvas);
        frame.setVisible(true);
        animator.start();
    }
    
    
    public void prepareData(GLDrawable glDrawable){
        GL gl = glDrawable.getGL();
        gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
        gl.glColor3f(1.0f,0.0f,0.0f);
        gl.glVertexPointer(4, gl.GL_FLOAT, 0, vertexBuffer); 
        gl.glFlush();
    }
    
    
    
    // MAIN STATEMENT 
    public static void main(String[] args){
       Test test = new Test(); 
    }
  

//  A CONVIENCE CLASS TO STORE FLOAT DATA 
class FloatList{
             ArrayList list = new ArrayList();

        public void add(float f){
            list.add(Float.toString(f));
        }

        public float get(int i){
            return Float.parseFloat((String)this.list.get(i));
        }

        public float[] getData(){
            float[] data = new float[this.list.size()];
            for(int i=0;i<this.list.size();i++){
                data[i] = get(i);
            }
            return data;
        }

        public void setData(float[] data){
            list = new ArrayList();
            for(int i=0;i<data.length;i++){
                this.list.add(Float.toString(data[i]));
            }
        }

        public int size(){
            return this.list.size();
        }
}
  

// GL RENDERER
class Renderer implements GLEventListener {
        
        public void display(net.java.games.jogl.GLDrawable glDrawable) {
            GL gl = glDrawable.getGL();
            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
            gl.glLoadIdentity();
            gl.glTranslatef(0.0f,0.0f,10.0f); 
            gl.glDrawElements(GL.GL_QUADS,data.length,GL.GL_FLOAT,vertexBuffer);
        }
        
        public void displayChanged(net.java.games.jogl.GLDrawable glDrawable, boolean param, boolean param2) {
        }
        
        public void init(net.java.games.jogl.GLDrawable glDrawable) {
            GL gl = glDrawable.getGL();
            gl.glShadeModel(GL.GL_SMOOTH);
            gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
            gl.glClearDepth(GL.GL_DEPTH_TEST);
            gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
            gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
            prepareData(glDrawable);
            gl.glFlush();
        }
        
        public void reshape(GLDrawable glDrawable, int x, int y, int width, int height) {
            GL gl = glDrawable.getGL();
            GLU glu = glDrawable.getGLU();
            if(height == 0){
                height=1;
            }
            gl.glViewport(0, 0, width, height);
            gl.glMatrixMode(GL.GL_PROJECTION);
            gl.glLoadIdentity();
            glu.gluPerspective(45.0f, (float)width/height,0.1f, 100.0f);
            gl.glMatrixMode(GL.GL_MODELVIEW);
            gl.glLoadIdentity();
        }
    
}
}

;D

This line is bogus, and will probably be the problem:

gl.glClearDepth(GL.GL_DEPTH_TEST);

i have removed that line… same result… black screen :frowning:

Hrm… Now I look closer, although that line would mess with the depth test, depth testing isn’t enabled. So it’s a no-op. :frowning:

Apologies, I don’t have a JoGL environment set up here, so can’t test run it. I can only presume the JoGL side is working properly? If you just try a standard glBegin(GL_POINTS) with a few vertices it appears, right?

Should you have a 0 for the stride in the glVertexPointer call? I don’t know what JoGL does behind the scenes, but I’d normally expect that to be 16 (4 bytes * 4 floats).

Also, in your glDrawElements call you are passing in data.length - that should be (data.length / 4), as each element is composed of four floats.

You haven’t included the code to fill the vertexBuffer - I presume that’s working fine.

ok… ive made some corrections to the code… but it still does not work… as before this is all the code… if its missing i didnt write it… so plz comment…

the result of running this is still the same… black screen
no errors.



import net.java.games.jogl.*;
import net.java.games.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.nio.*;

public class Test {
    
    private FloatBuffer vertexBuffer;
    private float[] data = {    4.0f, 5.0f, 6.0f, 7.0f,
                                1.0f, 2.0f, 6.0f, 5.0f,
                                0.0f, 1.0f, 5.0f, 4.0f,
                                0.0f, 3.0f, 2.0f, 1.0f,
                                0.0f, 4.0f, 7.0f, 3.0f,
                                2.0f, 3.0f, 7.0f, 6.0f,
                            };
    
    
 
    public Test(){
        JFrame frame = new JFrame("Vertex Array Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800,600);
        GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
        frame.getContentPane().add("Center",canvas);
        Renderer renderer = new Renderer();
        canvas.addGLEventListener(renderer);
        Animator animator = new Animator(canvas);
        frame.setVisible(true);
        animator.start();
    }
    
    
    public void prepareData(GLDrawable glDrawable){
        GL gl = glDrawable.getGL();
        gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
        gl.glColor3f(1.0f,0.0f,0.0f);
        
        vertexBuffer = BufferUtils.newFloatBuffer(data.length);
        vertexBuffer.put(data);
        
        gl.glVertexPointer(4, gl.GL_FLOAT, 0, vertexBuffer); 
        gl.glFlush();
    }
    
    
    
    // MAIN STATEMENT 
    public static void main(String[] args){
       Test test = new Test(); 
    }
  

  

// GL RENDERER
class Renderer implements GLEventListener {
        
        public void display(net.java.games.jogl.GLDrawable glDrawable) {
            GL gl = glDrawable.getGL();
            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
            gl.glLoadIdentity();
            gl.glTranslatef(0.0f,0.0f,10.0f); 
            gl.glDrawElements(GL.GL_QUADS,data.length,GL.GL_FLOAT,vertexBuffer);
        }
        
        public void displayChanged(net.java.games.jogl.GLDrawable glDrawable, boolean param, boolean param2) {
        }
        
        public void init(net.java.games.jogl.GLDrawable glDrawable) {
            GL gl = glDrawable.getGL();
            gl.glShadeModel(GL.GL_SMOOTH);
            gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
            gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
            gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
            prepareData(glDrawable);
            gl.glFlush();
        }
        
        public void reshape(GLDrawable glDrawable, int x, int y, int width, int height) {
            GL gl = glDrawable.getGL();
            GLU glu = glDrawable.getGLU();
            if(height == 0){
                height=1;
            }
            gl.glViewport(0, 0, width, height);
            gl.glMatrixMode(GL.GL_PROJECTION);
            gl.glLoadIdentity();
            glu.gluPerspective(45.0f, (float)width/height,0.1f, 100.0f);
            gl.glMatrixMode(GL.GL_MODELVIEW);
            gl.glLoadIdentity();
        }
    
}
}

You want to use glDrawArrays rather than glDrawElements. glDrawElements is for indexed arrays, which you are not doing. Other than getting the latest build, your imports for the bytebuffer utilities are old, you should be fine.

gl.glDrawElements(GL.GL_QUADS,data.length,GL.GL_FLOAT,vertexBuffer);

to

gl.glDrawArrays(GL.GL_QUADS,0,data.length);

Here’s a working example of vertex array usage. It draws a single colored quad.

import net.java.games.jogl.*;
import net.java.games.jogl.util.BufferUtils;

import javax.swing.*;
import java.nio.*;
import java.awt.*;

public class VertexArrayRenderer implements GLEventListener {
    private float[] vertices = {
        1.0f, 1.0f, 0.0f,
        -1.0f, 1.0f, 0.0f,
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f
    };

    private float[] colors = {
        1.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 1.0f,
        1.0f, 0.0f, 1.0f
    };

    public VertexArrayRenderer() {
    }

    public void prepareData(GLDrawable glDrawable) {
        GL gl = glDrawable.getGL();
        FloatBuffer vertexBuffer = BufferUtils.newFloatBuffer(vertices.length);
        vertexBuffer.put(vertices);
        gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertexBuffer);

        FloatBuffer colorBuffer = BufferUtils.newFloatBuffer(colors.length);
        colorBuffer.put(colors);
        gl.glEnableClientState(gl.GL_COLOR_ARRAY);
        gl.glColorPointer(3, gl.GL_FLOAT, 0, colorBuffer);
    }

    public void init(net.java.games.jogl.GLDrawable glDrawable) {
        GL gl = glDrawable.getGL();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, 0.0f, -10.0f);

        prepareData(glDrawable);
    }

    public void display(net.java.games.jogl.GLDrawable glDrawable) {
        GL gl = glDrawable.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glDrawArrays(GL.GL_QUADS, 0, vertices.length / 3);
    }

    public void displayChanged(net.java.games.jogl.GLDrawable glDrawable, boolean param, boolean param2) {
    }

    public void reshape(GLDrawable glDrawable, int x, int y, int width, int height) {
        GL gl = glDrawable.getGL();
        GLU glu = glDrawable.getGLU();
        if (height == 0) {
            height = 1;
        }
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, ((float) width) / height, 0.1f, 100.0f);
        gl.glMatrixMode(GL.GL_MODELVIEW);
    }

    public static void main(String[] args) {
        GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());

        VertexArrayRenderer renderer = new VertexArrayRenderer();
        canvas.addGLEventListener(renderer);

        JFrame frame = new JFrame("Vertex Array Test");
        frame.getContentPane().setLayout( new BorderLayout() );
        frame.getContentPane().add(canvas, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setVisible(true);

        Animator animator = new Animator(canvas);
        animator.start();
    }
}

thank you… this is what i was hoping to find… i will analize it :slight_smile:

;D ;D ;D ;D ;D

JMG