Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Vertex arrays... what am i missing?  (Read 1174 times)
0 Members and 1 Guest are viewing this topic.
Offline Jacko

Junior Member





« Posted 2003-04-14 01:22:34 »

OK, ive been tweaking this one way and another for a couple of hours and I cant see anything wrong with it. It should show a square, one half drawn directly, the other half drawn via a vertex array. But its only showing the direct one, and I'm damned if i know why! Any one got any ideas?

Thanks
Jacko

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  
266  
import org.lwjgl.*;
import org.lwjgl.opengl.*;

import java.nio.*;

import org.lwjgl.input.*;
import org.khaos.lib.*;

public final class VertexArray
{
      static {
            try
            {
                  int mode = -1;
                  DisplayMode[] modes = Display.getAvailableDisplayModes();
                  for (int i = 0; i < modes.length; i++)
                  {
                        if (modes[i].width == 640
                              && modes[i].height == 480
                              && modes[i].bpp >= 16)
                        {
                              mode = i;
                              break;
                        }
                  }

                  Display.create(modes[mode], false);
            }
            catch (Exception e)
            {
                  System.exit(1);
            }
      }

      public static final GL gl = new GL(); // The Opengl Context
     public static final GLU glu = new GLU(gl); // The Opengl Utility Context

      static {
            try
            {
                  gl.create(); // Create The Opengl Context
           }
            catch (Exception e)
            {
                  System.exit(1);
            }
      }

      private static boolean finished;

      static float kSpeed = 50.0f;                                                // This is how fast our camera moves

      // Variables for creating the vertex buffer
     private static int vertexBufferStride = 0;
      static MemoryBuffer vertexBuffer;
      static int vertexBufferAddress;
      static int vertexBufferOffset = 0;
      static int maxVertices = 0;
      static DoubleBuffer vertexBufferDouble;

      // Variables for creating the color buffer
     private static int colorBufferStride = 0;
      static MemoryBuffer colorBuffer;
      static int colorBufferAddress;
      static int colorBufferOffset = 0;
      static DoubleBuffer colorBufferDouble;
      static int numberOfVertices = 50;      

      static int frameCounter = 0;

      private VertexArray()
      {
      }

      public static void main(String args[])
      {
            try
            {
                  init(); // Init Opengl

                  while (!finished)
                  {
                        Keyboard.poll(); // Poll The Keyboard
                       AbsMouse.poll(); // Poll The Mouse
                       mainLoop(); // Launch The Main Loop
                       render(); // Render To Screen
                       gl.swapBuffers(); // Swap Opengl Buffers
                 }
            }
            catch (Throwable t)
            {
                  t.printStackTrace();
            }
            finally
            {
                  cleanup();
            }
      }

      private final static void init() throws Exception
      {
            int bytesPerDouble = 8;
            int numberOfItems = 3;

            vertexBufferStride = bytesPerDouble * numberOfItems;

            vertexBuffer = new MemoryBuffer(vertexBufferStride * numberOfVertices);
            vertexBufferAddress=vertexBuffer.getAddress();

            vertexBufferDouble=vertexBuffer.bytes.asDoubleBuffer();

            vertexBufferOffset=0;
            maxVertices=numberOfVertices;

            numberOfItems=4;
            colorBufferStride = bytesPerDouble * numberOfItems;

            colorBuffer = new MemoryBuffer(colorBufferStride * numberOfVertices);
            colorBufferAddress=colorBuffer.getAddress();

            colorBufferDouble=colorBuffer.bytes.asDoubleBuffer();

            colorBufferOffset=0;

            Keyboard.create(); // Create The Keyboard
           Mouse.create(); // Create The Mouse

            gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f);
            gl.matrixMode(GL.PROJECTION);                                          // Select The Projection Matrix
           gl.loadIdentity();                                                      // Reset The Projection Matrix
                                                                                         // The closest distance to the camera before it clips,
                                         // FOV            // Ratio                        // The farthest distance before it stops drawing)
           glu.perspective(45.0f,(float)Display.getWidth()/(float)Display.getHeight(), 0.5f ,150.0f);

            gl.matrixMode(GL.MODELVIEW);                                          // Select The Modelview Matrix
           gl.loadIdentity();                                                      // Reset The Modelview Matrix
           gl.wglSwapIntervalEXT(1);

      }

      private final static void mainLoop()
      {
            processKeyboard(); // Get Keyboard Events
     }

      private final static void render()
      {
            gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);      // Clear The Screen And The Depth Buffer
           gl.loadIdentity();                                                      // Reset The matrix

            gl.translatef(0.0f, 0.0f,-35.0f);

            addColorToBuffer(1,0,0);
            addVertexToBuffer(10,10,0);
            addColorToBuffer(0,1,0);
            addVertexToBuffer(10,-10,0);
            addColorToBuffer(0,0,1);
            addVertexToBuffer(-10,10,0);
            stats();
            drawTriangleVertexArrayInt();
/*
            gl.begin(GL.TRIANGLES);
            gl.color3f(1f,0f,0f);
            gl.vertex3i(10,10,0);
            gl.color3f(0f,1f,0f);
            gl.vertex3i(10,-10,0);
            gl.color3f(0f,0f,1f);
            gl.vertex3i(-10,10,0);
            gl.end();
*/

            gl.begin(GL.TRIANGLES);
            gl.color3f(1f,0f,0f);
            gl.vertex3i(-10,-10,0);
            gl.color3f(0f,1f,0f);
            gl.vertex3i(10,-10,0);
            gl.color3f(0f,0f,1f);
            gl.vertex3i(-10,10,0);
            gl.end();
           
            gl.flush();
      }
      private static void stats()
      {
            if(++frameCounter>100)
            {
                  frameCounter=0;

                  System.out.println("Buffer size: "+vertexBufferDouble.position());
                  System.out.println("Vertex 1: "+vertexBufferDouble.get(0)+", "+vertexBufferDouble.get(1)+", "+vertexBufferDouble.get(2));
                  System.out.println("Vertex 2: "+vertexBufferDouble.get(3)+", "+vertexBufferDouble.get(4)+", "+vertexBufferDouble.get(5));
                  System.out.println("Vertex 3: "+vertexBufferDouble.get(6)+", "+vertexBufferDouble.get(7)+", "+vertexBufferDouble.get(8));

                  System.out.println("Color 1: "+colorBufferDouble.get(0)+", "+colorBufferDouble.get(1)+", "+colorBufferDouble.get(2)+", "+colorBufferDouble.get(3));
                  System.out.println("Color 2: "+colorBufferDouble.get(4)+", "+colorBufferDouble.get(5)+", "+colorBufferDouble.get(6)+", "+colorBufferDouble.get(7));
                  System.out.println("Color 3: "+colorBufferDouble.get(8)+", "+colorBufferDouble.get(9)+", "+colorBufferDouble.get(10)+", "+colorBufferDouble.get(11));
                  System.out.println("");
            }
      }
      public static void drawTriangleVertexArrayInt()
      {
            gl.enableClientState(GL.VERTEX_ARRAY);
            gl.enableClientState(GL.COLOR_ARRAY);

            gl.vertexPointer(3, gl.DOUBLE, 0, vertexBufferAddress);
            gl.colorPointer(4, gl.DOUBLE, 0, colorBufferAddress);

            gl.drawArrays(GL.TRIANGLES, 0, 9*3);
            checkError();

            gl.disableClientState(GL.VERTEX_ARRAY);
            gl.disableClientState(GL.COLOR_ARRAY);

            vertexBufferOffset=0;
            colorBufferOffset=0;

            vertexBufferDouble.rewind();
            colorBufferDouble.rewind();
      }

      private static void checkError()
      {
            int error = gl.getError();

            if(error!=GL.NO_ERROR)
            {
                  System.out.println("error");
                  System.out.println(glu.errorString(error));;
            }

      }


      public static void addVertexToBuffer(double x, double y, double z)
      {
            vertexBufferDouble.put(x);
            vertexBufferDouble.put(y);
            vertexBufferDouble.put(z);
      }
      public static void addColorToBuffer(double red, double green, double blue)
      {
            colorBufferDouble.put(red);
            colorBufferDouble.put(green);
            colorBufferDouble.put(blue);
            colorBufferDouble.put(0.5); //alpha
     }

      /** Process keyboard events
       */


      private final static void processKeyboard()
      {
            if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
                  finished = true;
      }

      /** Cleanup
       */


      private final static void cleanup()
      {
            Keyboard.destroy(); // Destroy The Keyboard
           Mouse.destroy(); // Destroy The Mouse
           gl.destroy(); // Destroy The Opengl Context
           Display.destroy(); // Destroy The Display
     }
}
Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2003-04-14 10:12:16 »

1  
gl.drawArrays(GL.TRIANGLES, 0, 9*3);
should be
1  
gl.drawArrays(GL.TRIANGLES, 0, 3);
coz you've only got 3 vertices.

(Where did the *9 come from?)

Cas Smiley

Offline Jacko

Junior Member





« Reply #2 - Posted 2003-04-14 10:53:02 »

The 9 came from desperation really. I know it should be 3, but will it work? And I've no idea why, there are no gl errors being reported, it just wont draw. And the really annoying thing is that I've used vertex buffers before no problems, but now I've tried to clean up how I use them, I cant get them to work.

The joys of coding ;-)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jacko

Junior Member





« Reply #3 - Posted 2003-04-14 11:57:18 »

And it turns out what I was missing was allocating my ByteBuffer as using native ordering. Doh!
Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2003-04-14 17:26:07 »

Ah yes, I've done that more times than I care to remember. The defining characteristic of this bug is that you always forget the time it happened before and spend all day trying to figure out why it won't draw...

Cas Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (48 views)
2014-04-15 18:08:23

BurntPizza (44 views)
2014-04-15 03:46:01

UprightPath (60 views)
2014-04-14 17:39:50

UprightPath (42 views)
2014-04-14 17:35:47

Porlus (58 views)
2014-04-14 15:48:38

tom_mai78101 (81 views)
2014-04-10 04:04:31

BurntPizza (140 views)
2014-04-08 23:06:04

tom_mai78101 (240 views)
2014-04-05 13:34:39

trollwarrior1 (200 views)
2014-04-04 12:06:45

CJLetsGame (207 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!