shadejmg
JGO n00b  Posts: 28
Java Rocks!
|
 |
«
on:
2003-08-04 08:22:21 » |
|
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. 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
| 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()); } } |
|
A thousand monkeys on a thousand computers .. typing away randomly .. eventually one will create a great java game! 
|
|
|
Mojomonkey
JGO Ninja    Posts: 540 Medals: 3
ooh ooh eee eeee
|
 |
«
Reply #1 on:
2003-08-04 08:32:16 » |
|
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, }; 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.
|
Don't send a man to do a monkey's work.
|
|
|
shadejmg
JGO n00b  Posts: 28
Java Rocks!
|
 |
«
Reply #2 on:
2003-08-04 08:33:34 » |
|
Ive tried reversing the values making them all negative .. same results.
|
A thousand monkeys on a thousand computers .. typing away randomly .. eventually one will create a great java game! 
|
|
|
Games published by our own members! Go get 'em!
|
|
Mojomonkey
JGO Ninja    Posts: 540 Medals: 3
ooh ooh eee eeee
|
 |
«
Reply #3 on:
2003-08-04 08:38:47 » |
|
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.
|
Don't send a man to do a monkey's work.
|
|
|
shadejmg
JGO n00b  Posts: 28
Java Rocks!
|
 |
«
Reply #4 on:
2003-08-04 08:42:22 » |
|
 no joy.. man this is fustrating.. so far everything was going great till i decided to learn how to draw this way  if anyone had a simple working demo it would be great!
|
A thousand monkeys on a thousand computers .. typing away randomly .. eventually one will create a great java game! 
|
|
|
Mojomonkey
JGO Ninja    Posts: 540 Medals: 3
ooh ooh eee eeee
|
 |
«
Reply #5 on:
2003-08-04 08:48:11 » |
|
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.
|
Don't send a man to do a monkey's work.
|
|
|
pepijnve
Sr. Member   Posts: 379 Medals: 1
Java games rock!
|
 |
«
Reply #6 on:
2003-08-04 08:57:50 » |
|
1 2 3 4 5
| 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 1
| vertexBuffer.put( data ); |
|
|
|
|
|
shadejmg
JGO n00b  Posts: 28
Java Rocks!
|
 |
«
Reply #7 on:
2003-08-04 09:07:34 » |
|
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
|
A thousand monkeys on a thousand computers .. typing away randomly .. eventually one will create a great java game! 
|
|
|
Rob Nugent
Full Member   Posts: 109
May contain nuts
|
 |
«
Reply #8 on:
2003-08-04 10:16:01 » |
|
Don't you need to do a glDrawArrays or somesuch before your flush ?
|
|
|
|
|
cfmdobbie
JGO Wizard     Posts: 1257
Who, me?
|
 |
«
Reply #9 on:
2003-08-04 12:16:52 » |
|
Yep, you're telling OpenGL that vertexBuffer is vertex data, but you're never telling it to go and draw the data.
|
Hellomynameis Charlie Dobbie.
|
|
|
Games published by our own members! Go get 'em!
|
|
Mojomonkey
JGO Ninja    Posts: 540 Medals: 3
ooh ooh eee eeee
|
 |
«
Reply #10 on:
2003-08-04 13:04:36 » |
|
It's always the obvious things that bite you. I sure skipped over that while reading the code.
|
Don't send a man to do a monkey's work.
|
|
|
shadejmg
JGO n00b  Posts: 28
Java Rocks!
|
 |
«
Reply #11 on:
2003-08-04 14:09:29 » |
|
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
|
A thousand monkeys on a thousand computers .. typing away randomly .. eventually one will create a great java game! 
|
|
|
shadejmg
JGO n00b  Posts: 28
Java Rocks!
|
 |
«
Reply #12 on:
2003-08-04 14:29:18 » |
|
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. 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
| 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(); } public static void main(String[] args){ Test test = new Test(); }
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(); } }
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(); } } } |

|
A thousand monkeys on a thousand computers .. typing away randomly .. eventually one will create a great java game! 
|
|
|
cfmdobbie
JGO Wizard     Posts: 1257
Who, me?
|
 |
«
Reply #13 on:
2003-08-04 14:44:06 » |
|
This line is bogus, and will probably be the problem: 1
| gl.glClearDepth(GL.GL_DEPTH_TEST); |
|
Hellomynameis Charlie Dobbie.
|
|
|
shadejmg
JGO n00b  Posts: 28
Java Rocks!
|
 |
«
Reply #14 on:
2003-08-04 14:45:48 » |
|
i have removed that line.. same result.. black screen 
|
A thousand monkeys on a thousand computers .. typing away randomly .. eventually one will create a great java game! 
|
|
|
cfmdobbie
JGO Wizard     Posts: 1257
Who, me?
|
 |
«
Reply #15 on:
2003-08-04 15:35:03 » |
|
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.  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.
|
Hellomynameis Charlie Dobbie.
|
|
|
shadejmg
JGO n00b  Posts: 28
Java Rocks!
|
 |
«
Reply #16 on:
2003-08-04 15:48:49 » |
|
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. 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
| 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(); } public static void main(String[] args){ Test test = new Test(); }
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(); } } } |
|
A thousand monkeys on a thousand computers .. typing away randomly .. eventually one will create a great java game! 
|
|
|
GKW
Sr. Member   Posts: 453
Revenge is mine!
|
 |
«
Reply #17 on:
2003-08-04 16:14:25 » |
|
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);
|
|
|
|
|
pepijnve
Sr. Member   Posts: 379 Medals: 1
Java games rock!
|
 |
«
Reply #18 on:
2003-08-04 16:24:42 » |
|
Here's a working example of vertex array usage. It draws a single colored quad. 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
| 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(); } } |
|
|
|
|
|
shadejmg
JGO n00b  Posts: 28
Java Rocks!
|
 |
«
Reply #19 on:
2003-08-04 16:59:46 » |
|
|
A thousand monkeys on a thousand computers .. typing away randomly .. eventually one will create a great java game! 
|
|
|
|