Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
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  
  Simple Vertex-Array Immediate drawer  (Read 1931 times)
0 Members and 1 Guest are viewing this topic.
Offline Longor1996
« Posted 2013-09-27 22:46:02 »

Hello everyone!

I just made a immediate-mode style Vertex-Array drawer, and thought some of you might find it useful.

Here it is (Only depency is LWJGL):

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  
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;

/**
 This class is distributed under the WTFPL-License!
 Do with it what you want!
 Its free!
**/

public class ImmediateDrawerVA {
   
   public static final ImmediateDrawerVA tessellator = new ImmediateDrawerVA();
   
   FloatBuffer vertexBuffer;
   FloatBuffer colorBuffer;
   FloatBuffer textureBuffer;
   
   int vertexCount = 0;
   int drawMode = -1;
   
   float r = 1;
   float g = 1;
   float b = 1;
   
   float offX = 0;
   float offY = 0;
   float offZ = 0;
   
   float s = 0;
   float t = 0;
   
   boolean useColor = false;
   boolean useUV = false;
   
   boolean isDrawing = false;
   
   
   private ImmediateDrawerVA(){
      this.vertexBuffer = BufferUtils.createFloatBuffer(1024 * 1024);
      this.colorBuffer = BufferUtils.createFloatBuffer(1024 * 1024);
      this.textureBuffer = BufferUtils.createFloatBuffer(1024 * 1024);
      this.reset();
   }
   
   /**
    * Starts drawing.
    *
    * @param mode The OpenGL Geometry Mode to use for this draw.
    * @throws RuntimeException If the ImmediateDrawer was already drawing.
    **/

   public void startDrawing(int mode){
     
      if(this.isDrawing)
         throw new RuntimeException("Tessellator is already drawing!");
     
      this.reset();
      this.drawMode = mode;
      this.isDrawing = true;
   }
   
   /**
    * Sets the UV-Coordinates for the next vertex to draw. (Has to be called in between start/stop!)
    **/

   public void setUV(float u, float v){
      this.s = u;
      this.t = v;
      this.useUV = true;
   }
   
   /**
    * Sets the Color for the next vertex to draw. (Has to be called in between start/stop!)
    **/

   public void setColor(float r,float g,float b){
      this.r = r;
      this.g = g;
      this.b = b;
      this.useColor = true;
   }
   
   /**
    * Sets the Position-Offset for the next vertex to draw. (Has to be called in between start/stop!)
    **/

   public void setVertexOffset(float x, float y, float z){
      this.offX = x;
      this.offY = y;
      this.offZ = z;
   }
   
   /**
    * Draws a vertex with a UV-Coordinate. (Has to be called in between start/stop!)
    *
    * @param x The x-position of the vertex.
    * @param y The y-position of the vertex.
    * @param z The z-position of the vertex.
    *
    * @param u The u-texCoord of the vertex.
    * @param v The v-texCoord of the vertex.
    **/

   public void drawVertWithUV(float x, float y, float z, float u, float v){
      this.setUV(u,v);
      this.drawVert(x,y,z);
   }
   
   /**
    * Draws a vertex. (Has to be called in between start/stop!)
    *
    * @param x The x-position of the vertex.
    * @param y The y-position of the vertex.
    * @param z The z-position of the vertex.
    **/

   public void drawVert(float x, float y, float z){
      this.vertexBuffer.put(x + this.offX);
      this.vertexBuffer.put(y + this.offY);
      this.vertexBuffer.put(z + this.offZ);
     
      this.colorBuffer.put(this.r);
      this.colorBuffer.put(this.g);
      this.colorBuffer.put(this.b);
     
      this.textureBuffer.put(this.s);
      this.textureBuffer.put(this.t);
     
      this.vertexCount++;
   }
   
   /**
    * Stops drawing.
    **/

   public void stopDrawing(){
      if(this.vertexCount == 0){
         this.reset();
         return;
      }
     
      this.vertexBuffer.rewind();
      this.colorBuffer.rewind();
      this.textureBuffer.rewind();
     
      GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
      GL11.glVertexPointer(3, /* stride */3 << 2, this.vertexBuffer);
     
      if(this.useColor){
         GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
         GL11.glColorPointer(3, /* stride */3 << 2, this.colorBuffer);
      }
     
      if(this.useUV){
         GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
         GL11.glTexCoordPointer(2, /* stride */2 << 2, this.textureBuffer);
      }
     
      GL11.glDrawArrays(this.drawMode, 0, this.vertexCount);
     
      GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
     
      if(this.useColor)
         GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
     
      if(this.useUV)
         GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
     
      this.reset();
   }
   
   // Do NOT call this method directly!
  private void reset() {
      this.vertexCount = 0;
      this.drawMode = -1;
      this.offX = 0;
      this.offY = 0;
      this.offZ = 0;
      this.r = 1;
      this.g = 1;
      this.b = 1;
      this.s = 0;
      this.t = 0;
      this.useColor = false;
      this.useUV = false;
      this.isDrawing = false;
     
      this.vertexBuffer.clear();
      this.colorBuffer.clear();
      this.textureBuffer.clear();
   }

   /**
    * Returns true if this tessellator is drawing, false if not.
    **/

   public boolean isDrawing() {
      return this.isDrawing;
   }
   
}


And this is how you use it:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
ImmediateDrawerVA tess = ImmediateDrawerVA.instance;

tess.startDrawing(GL11.GL_QUADS);
 tess.setColor(1, 1, 1);
  tess.drawVertWithUV(0, 0, 0, 0, 0);
  tess.drawVertWithUV(1, 0, 0, 0, 0);
 tess.setColor(0, 0, 0);
  tess.drawVertWithUV(1, 1, 0, 0, 0);
  tess.drawVertWithUV(0, 1, 0, 0, 0);
tess.stopDrawing();


This class should work instantly and without any problems.
Its mostly useful to LWJGL newbies and everyone who is wondering how the heck to use these Vertex-Array's.

If I made some mistake in the rendering code, or you find a bug, please tell me, as I want to use this class in my applications.

- Longor1996

Sorry for my bad English! That's because i am from Germany.
Offline gouessej

« In padded room »



TUER


« Reply #1 - Posted 2013-09-29 21:27:58 »

Why not keeping the same naming style than in OpenGL like in this class (from JOGL, build-in)?

Offline Longor1996
« Reply #2 - Posted 2013-09-29 22:38:23 »

Why not keeping the same naming style than in OpenGL like in this class (from JOGL, build-in)?

Yeah, about that:
I was in an IRC today, and showed some friends the class, and they immdiately told me that this is not a Tessellator, but rather an immediate drawer.
I renamed all the stuff in the class so it fits its name again.

It is an immediate drawer: ImmediateDrawerVA

- Longor1996

Sorry for my bad English! That's because i am from Germany.
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 (55 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (211 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!