Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (806)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
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  
  Can't get a simple Vertex Array to work!  (Read 2188 times)
0 Members and 1 Guest are viewing this topic.
Offline sci4me

Junior Devvie


Medals: 1



« Posted 2016-01-17 09:32:23 »

So I have a very simple renderer that uses a display list. I am trying to convert it to use a VBO. The step before a VBO is doing a vertex array (upload every frame). I can not get that to work. I've been trying everything I can think of for like an hour and got nowhere.

Here's the old displaylist code:
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  
public final class RenderReticle<Void> extends Renderer<Void> {
   public static final RenderReticle INSTANCE = new RenderReticle();
   
   private static final float size = 25f;
   private static final float halfSize = size / 2;
   
   private DisplayList displayList;
   private Texture texture;
   
   private RenderReticle() {
     
   }
   
   @Override
   public void init() {
      this.displayList = new DisplayList();
      this.displayList.startDrawing();
     
      glBegin(GL_QUADS);
      glTexCoord2f(0f, 0f); glVertex2f(0.0f, 0.0f);
      glTexCoord2f(0f, 1f); glVertex2f(0.0f, size);
      glTexCoord2f(1f, 1f); glVertex2f(size, size);
      glTexCoord2f(1f, 0f); glVertex2f(size, 0.0f);
      glEnd();

      this.displayList.stopDrawing();
     
      this.texture = Texture.get("reticle");
   }
   
   @Override
   public void render(final Void object) {
      gluSet2D();
     
      glTranslatef(Window.instance().getWidth() / 2 - halfSize, Window.instance().getHeight() / 2 - halfSize, 0);
     
      this.texture.bind();
      this.displayList.call();
     
      gluSet3D();
   }
}


And here's the new vertex array code:
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  
public final class RenderReticle<Void> extends Renderer<Void> {
   public static final RenderReticle INSTANCE = new RenderReticle();
   
   private static final float size = 1f;
   private static final float halfSize = size / 2;
   
   private Texture texture;
   
   private FloatBuffer vBuffer;
   private FloatBuffer tBuffer;

   private RenderReticle() {
     
   }
   
   @Override
   public void init() {
      this.vBuffer = BufferUtils.createFloatBuffer(8);
      this.vBuffer.put(0).put(0);
      this.vBuffer.put(0).put(size);
      this.vBuffer.put(size).put(size);
      this.vBuffer.put(size).put(0);
      this.vBuffer.flip();
     
      this.tBuffer = BufferUtils.createFloatBuffer(8);
      this.tBuffer.put(0).put(0);
      this.tBuffer.put(0).put(1);
      this.tBuffer.put(1).put(1);
      this.tBuffer.put(1).put(0);
      this.tBuffer.flip();
     
      this.texture = Texture.get("reticle");
   }
   
   @Override
   public void render(final Void object) {
      gluSet2D();
     
      glTranslatef(Window.instance().getWidth() / 2 - halfSize, Window.instance().getHeight() / 2 - halfSize, 0);
     
      this.texture.bind();

      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   
      glVertexPointer(4, GL_FLOAT, 2 << 2, this.vBuffer);
      glTexCoordPointer(4, GL_FLOAT, 2 << 2, this.tBuffer);
      glDrawArrays(GL_QUADS, 0, 4);
     
      glDisableClientState(GL_VERTEX_ARRAY);
      glDisableClientState(GL_TEXTURE_COORD_ARRAY);
     
      gluSet3D();
   }
}


"gluSet2D" and "gluSet3D" are functions I wrote. They basically just do glOrtho and the perspective matrix respectively.

So, how come the first one works but the second one doesn't? (It just displays nothing on the screen).
Offline KaiHH

JGO Kernel


Medals: 798



« Reply #1 - Posted 2016-01-17 09:42:20 »

You are doing everything right, except that your vertex has only 2 components, not 4. So use a 2 as the first argument to glVertexPointer and glTexCoordPointer.

EDIT: Btw.: this is not an error, but specifying the stride (third argument to glVertexPointer/glTexCoordPointer) with "2<<2" (which produces the correct result 8 ) is a bit non-intuitive. You could use "4 * 2" or "2 * 4" meaning (2 floats, each taking 4 bytes). That would be a bit better to read/understand.
But since you are not using interleaved vertex arrays, you can just as well leave that third parameter 0.
Offline sci4me

Junior Devvie


Medals: 1



« Reply #2 - Posted 2016-01-17 19:01:46 »

Oh, I thought the first parameter was the number of vertices. Doh.

EDIT: nevermind it works, I'm just dumb.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2016-01-17 22:10:38 »

@KaiHH: I prefer <<2 over *4, as it immediately shows we are bitshifting from ints/floats to bytes, whereas *4 can have any meaning (are we working with an attribute with 4 elements?). IMHO it helps to create this convention of using bit shifts to go from one primitive width to another.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (587 views)
2019-09-04 15:33:17

hadezbladez (5528 views)
2018-11-16 13:46:03

hadezbladez (2410 views)
2018-11-16 13:41:33

hadezbladez (5790 views)
2018-11-16 13:35:35

hadezbladez (1233 views)
2018-11-16 13:32:03

EgonOlsen (4669 views)
2018-06-10 19:43:48

EgonOlsen (5688 views)
2018-06-10 19:43:44

EgonOlsen (3205 views)
2018-06-10 19:43:20

DesertCoockie (4104 views)
2018-05-13 18:23:11

nelsongames (5125 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!