Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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  
  setting up Vertex Array in LWJLG  (Read 3254 times)
0 Members and 1 Guest are viewing this topic.
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 119
Projects: 15


★★★★★


« Posted 2005-10-04 17:07:00 »

i'm trying run a simple test to see how vertex arrays work (just drawing two triangles on screen), but sort of stuck/confused on how lwjgl does the buffer stuff array stuff here my code so far.

1  
2  
FloatBuffer vertices;
IntBuffer buf;


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public void initVariables() {
     
   vertices = BufferUtils.createFloatBuffer(12);
     
   vertices.put(100).put(100).put(300).put(100).put(200).put(250).put(400).put(100).put(600).put(100).put(500).put(250);
   vertices.rewind();
     
   buf = BufferUtils.createIntBuffer(12);
   buf.put(0).put(1).put(2).put(3).put(4).put(5).put(6).put(7).put(8).put(9).put(10).put(11);
   buf.flip();
}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
   
public void mainloop() {
   GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
     
   GL11.glColor3f(1.0f, 1.0f, 1.0f);
     
   /*This is what it should do
   GL11.glBegin(GL11.GL_TRIANGLES);
      GL11.glVertex2f(100, 100);
      GL11.glVertex2f(300, 100);
      GL11.glVertex2f(200, 250);
         
      GL11.glVertex2f(400, 100);
      GL11.glVertex2f(600, 100);
      GL11.glVertex2f(500, 250);
   GL11.glEnd();*/

     
   GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
   GL11.glVertexPointer(2, GL11.GL_FLOAT, buf);
   
   GL11.glDrawElements(GL11.GL_TRIANGLES, buf);
}
}
Offline anarchotron

Junior Devvie




...precious bodily fluids.


« Reply #1 - Posted 2005-10-04 18:06:56 »

I don't do any buffer flip()ing myself, I'm not sure what that is supposed to get you.

However the problem is most likely your use of glDrawElements().  glDrawElements() expects a buffer of indices, not a buffer of verts.  You already specified your verts with glVertexPointer().  The function you want to render with is glDrawArrays().
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 119
Projects: 15


★★★★★


« Reply #2 - Posted 2005-10-04 18:22:55 »

the indices way is probably what i'm after then any idea how to do that?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline anarchotron

Junior Devvie




...precious bodily fluids.


« Reply #3 - Posted 2005-10-04 19:27:31 »

IntBuffer ib = BufferUtils.createIntBuffer(12);
ib.put(0).put(1).put(2).put(3)...

GL11.glDrawElements(GL11.GL_TRIANGLES, ib);
Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2005-10-04 22:02:41 »

You really have to flip your buffer in LWJGL as opposed to JOGL.

Otherwise your stuff Just Don't Work.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline darkprophet

Senior Devvie




Go Go Gadget Arms


« Reply #5 - Posted 2005-10-04 22:18:21 »

1  
GL11.glVertexPointer(2, GL11.GL_FLOAT, buf);


should be:

1  
GL11.glVertexPointer(2, 0, buf);


as the second parameter is the stride, not the type.

DP

Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Offline darkprophet

Senior Devvie




Go Go Gadget Arms


« Reply #6 - Posted 2005-10-04 22:26:23 »

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  
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

/**
 *
 */

public class Game {

   /** Game title */
   public static final String GAME_TITLE = "My Game";

   /** Desired frame time */
   private static final int FRAMERATE = 60;

   /** Exit the game */
   private static boolean finished;

   /** Angle of rotating square */
   private static float angle;
   
   /** the vertex float buffer    */
   private static FloatBuffer vertices;
   
   /** the indices */
   private static IntBuffer indices;

   /**
    * Application init
    *
    * @param args
    *            Commandline args
    */

   public static void main( String[] args ) {
      boolean fullscreen = (args.length == 1 && args[0].equals("-fullscreen"));

      try {
         init(fullscreen);
         run();
      } catch (Exception e) {
         e.printStackTrace(System.err);
         Sys.alert(GAME_TITLE, "An error occured and the game will exit.");
      } finally {
         cleanup();
      }
      System.exit(0);
   }

   /**
    * Initialise the game
    *
    * @throws Exception
    *             if init fails
    */

   private static void init( boolean fullscreen ) throws Exception {
      // Create a fullscreen window with 1:1 orthographic 2D projection
      // (default)
      Display.setTitle(GAME_TITLE);
      Display.setFullscreen(fullscreen);

      // Enable vsync if we can (due to how OpenGL works, it cannot be
      // guarenteed to always work)
      Display.setVSyncEnabled(true);

      // Create default display of 640x480
      Display.create();
     
      vertices = BufferUtils.createFloatBuffer(4 * 2);
      vertices.put(-50).put(-50);
      vertices.put(50).put(-50);
      vertices.put(50).put(50);
      vertices.put(-50).put(50);
      vertices.flip();
     
      indices = BufferUtils.createIntBuffer(6);
      indices.put(0).put(1).put(2).put(0).put(2).put(3);
      indices.flip();
   }

   /**
    * Runs the game (the "main loop")
    */

   private static void run() {

      while (!finished) {
         // Always call Window.update(), all the time - it does some behind
         // the
         // scenes work, and also displays the rendered output
         Display.update();

         // Check for close requests
         if (Display.isCloseRequested()) {
            finished = true;
         }

         // The window is in the foreground, so we should play the game
         else if (Display.isActive()) {
            logic();
            render();
            Display.sync(FRAMERATE);
         }

         // The window is not in the foreground, so we can allow other stuff
         // to run and
         // infrequently update
         else {
            try {
               Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            logic();

            // Only bother rendering if the window is visible or dirty
            if (Display.isVisible() || Display.isDirty()) {
               render();
            }
         }
      }
   }

   /**
    * Do any game-specific cleanup
    */

   private static void cleanup() {
      // Close the window
      Display.destroy();
   }

   /**
    * Do all calculations, handle input, etc.
    */

   private static void logic() {
      // Example input handler: we'll check for the ESC key and finish the
      // game instantly when it's pressed
      if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
         finished = true;
      }

      // Rotate the square
      angle += 2.0f % 360;
   }

   private static void render() {
      // clear the screen
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);

      // center square according to screen size
      GL11.glPushMatrix();
      GL11.glTranslatef(Display.getDisplayMode().getWidth() / 2, Display.getDisplayMode().getHeight() / 2, 0.0f);

      // rotate square according to angle
      GL11.glRotatef(angle, 0, 0, 1.0f);

      // render the square
      GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
      GL11.glVertexPointer(2, 0, vertices);
     
      GL11.glDrawElements(GL11.GL_TRIANGLES, indices);

      GL11.glPopMatrix();
   }
}


Main parts are init and render.

DP

Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 119
Projects: 15


★★★★★


« Reply #7 - Posted 2005-10-04 22:49:24 »

ok thx dp, sorted the problem with the above code too
here the correction for the record

1  
GL11.glVertexPointer(2, GL11.GL_FLOAT, buf);

should have been
1  
GL11.glVertexPointer(2, 0, vertices);


and

1  
vertices.rewind();

was
1  
vertices.flip();

although i've been told that .flip() and rewind() shouldn't really affect the out come!

thx
Offline anarchotron

Junior Devvie




...precious bodily fluids.


« Reply #8 - Posted 2005-10-05 00:37:40 »

Yeah, flip() is only useful if you want to change the limit.

So for instance

FloatBuffer fb = BufferUtils.createFloatBuffer(10);

fb.put(10).put(10);
fp.flip();

you end up with a fb with pos=0 and limit=2 (instead of the 10 from initialization)

If your buffer is already the correct size, you can just use rewind() which is what I usually do:

FloatBuffer fb = BufferUtils.createFloatBuffer(2);

fb.put(10).put(10);
fp.rewind();

So in other words, flip() is used when you have a large scratch Buffer and only want to use a portion of it.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1568 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!