Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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  
  Drawing Blocks....  (Read 990 times)
0 Members and 1 Guest are viewing this topic.
Offline sci4me

Junior Member





« Posted 2013-02-23 10:59:45 »

Ok guys... so at the moment, im basically teaching myself opengl.. by making a voxel engine thingy.. anyway.. i have this block class that "works" but its LAGGY as crap... so.. heres the 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  
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  
import static org.lwjgl.opengl.GL11.*;

public class Block
{
   private static final double size = 1;

   public static void renderBlockAt(int x, int y, int z)
   {
      glTranslatef(x, y, z);
      glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
      glBegin(GL_QUADS);

      glVertex3d(size, size, size);
      glVertex3d(-size, size, size);
      glVertex3d(-size, -size, size);
      glVertex3d(size, -size, size);

      glVertex3d(size, size, -size);
      glVertex3d(-size, size, -size);
      glVertex3d(-size, -size, -size);
      glVertex3d(size, -size, -size);

      glVertex3d(size, size, size);
      glVertex3d(size, -size, size);
      glVertex3d(size, -size, -size);
      glVertex3d(size, size, -size);

      glVertex3d(-size, size, size);
      glVertex3d(-size, -size, size);
      glVertex3d(-size, -size, -size);
      glVertex3d(-size, size, -size);

      glVertex3d(size, size, size);
      glVertex3d(-size, size, size);
      glVertex3d(-size, size, -size);
      glVertex3d(size, size, -size);

      glVertex3d(size, -size, size);
      glVertex3d(-size, -size, size);
      glVertex3d(-size, -size, -size);
      glVertex3d(size, -size, -size);

      glEnd();
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
      glBegin(GL_QUADS);
      glColor3d(1, 0, 0);
      glVertex3d(size, size, -size);
      glVertex3d(-size, size, -size);
      glVertex3d(-size, size, size);
      glVertex3d(size, size, size);
      glColor3d(0, 1, 0);
      glVertex3d(size, -size, size);
      glVertex3d(-size, -size, size);
      glVertex3d(-size, -size, -size);
      glVertex3d(size, -size, -size);
      glColor3d(0, 0, 1);
      glVertex3d(size, size, size);
      glVertex3d(-size, size, size);
      glVertex3d(-size, -size, size);
      glVertex3d(size, -size, size);
      glColor3d(1, 1, 0);
      glVertex3d(size, -size, -size);
      glVertex3d(-size, -size, -size);
      glVertex3d(-size, size, -size);
      glVertex3d(size, size, -size);
      glColor3d(1, 0, 1);
      glVertex3d(-size, size, size);
      glVertex3d(-size, size, -size);
      glVertex3d(-size, -size, -size);
      glVertex3d(-size, -size, size);
      glColor3d(1, 1, 1);
      glVertex3d(size, size, -size);
      glVertex3d(size, size, size);
      glVertex3d(size, -size, size);
      glVertex3d(size, -size, -size);
      glEnd();
      glTranslatef(-x, -y, -z);

   }
}

Now, I'm wondering what the non laggy way to do this would be. If anyone can help me out, please reply! Thanks!
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2013-02-23 11:07:11 »

You have to enlarge your knowledge: Find out about "Vertex Buffer Objects" and then put a whole chunk of blocks into one of those. But only a chunk. You'll see the problems of not having VBO Chunks for blocks later too.

You could even start with minimizing the "glBegin()" and "glEnd()" calls. But then you face the "glTranslate" problem.

And instead of VBO's you could first start with VAO's or VA's: Vertex Array Objects / Vertex Arrays.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline sci4me

Junior Member





« Reply #2 - Posted 2013-02-23 11:08:35 »

ok thanks for fast reply!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #3 - Posted 2013-02-23 11:09:07 »

Mind the edit Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline sci4me

Junior Member





« Reply #4 - Posted 2013-02-23 11:26:45 »

so.. not a vbo per block? ... or.. 1 vbo for all blocks since i just use the static thing?

edit: also, is this how minecraft does it?
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #5 - Posted 2013-02-23 11:55:26 »

NOT a VBO per block. That is the big problem you have and why it slows down: You draw each block seperately.

You have to draw a chunk at once.

As you (probably) know, minecraft has 16x16x128 chunks.
These chunks are further divided into 16x16x16 chunks, which are all in one Display List. Well, a Display list is an old feature and it has been deprecated now, so I guess they have a good reason for that.

You should now have 16x16x16 chunks, where all the block's vertices are stored in one VBO. You should also "cull" (~delete) the faces, which aren't visible, because other blocks are connected to them. You should only render the faces of the blocks, which are connected to the air.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #6 - Posted 2013-02-23 20:35:40 »

You should only render the faces of the blocks, which are connected to the air.

... or some other transparent/semi-transparent block like glass.

Offline sci4me

Junior Member





« Reply #7 - Posted 2013-02-24 01:00:40 »

you know of an example? im trying to do it based off of tutorials but .. eh..
Offline sci4me

Junior Member





« Reply #8 - Posted 2013-02-24 01:38:59 »

edit: was missing a line
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  
package com.sci.bluebear.src;

import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;

public class Chunk
{
   public static final int CHUNK_WIDTH = 16;
   public static final int CHUNK_HEIGHT = 16;
   public static final int CHUNK_LENGTH = 16;
   private Block[][][] blocks;
   private int vboVertexHandle;
   private int vboColorHandle;
   private int startX, startY, startZ;

   public Chunk(int x, int y, int z)
   {
      startX = x;
      startY = y;
      startZ = z;
     
      blocks = new Block[CHUNK_WIDTH][CHUNK_HEIGHT][CHUNK_LENGTH];

      for(int xx = 0; xx < CHUNK_WIDTH; xx++)
      {
         for(int yy = 0; yy < CHUNK_HEIGHT; yy++)
         {
            for(int zz = 0; zz < CHUNK_WIDTH; zz++)
            {
               blocks[xx][yy][zz] = Block.stone;
            }
         }
      }

      vboColorHandle = GL15.glGenBuffers();
      vboVertexHandle = GL15.glGenBuffers();
      this.rebuildMesh(startX, startY, startZ);
   }

   private void rebuildMesh(int startX2, int startY2, int startZ2)
   {
      vboColorHandle = GL15.glGenBuffers();
      vboVertexHandle = GL15.glGenBuffers();
      FloatBuffer vertexPositionData = BufferUtils.createFloatBuffer((CHUNK_WIDTH * CHUNK_LENGTH * CHUNK_HEIGHT) * 6 * 12);
      FloatBuffer vertexColorData = BufferUtils.createFloatBuffer((CHUNK_WIDTH * CHUNK_LENGTH * CHUNK_HEIGHT) * 6 * 12);
      for(int xx = 0; xx < CHUNK_WIDTH; xx++)
      {
         for(int yy = 0; yy < CHUNK_WIDTH; yy++)
         {
            for(int zz = 0; zz < CHUNK_WIDTH; zz++)
            {
               if(blocks[xx][yy][zz] != null)
               {
                  vertexPositionData.put(makeCube((float) startX + xx, (float) startY + yy, (float) startZ + zz));
                  vertexColorData.put(makeCubeVertexCol(getCubeColor(blocks[xx][yy][zz])));
               }
            }
         }
      }
      vertexColorData.flip();
      vertexPositionData.flip();
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboVertexHandle);
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexPositionData, GL15.GL_STATIC_DRAW);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboColorHandle);
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexColorData, GL15.GL_STATIC_DRAW);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
   }

   private float[] makeCubeVertexCol(float[] cubeColorData)
   {
      float[] cubeColors = new float[cubeColorData.length * 4 * 6];
      for(int i = 0; i < cubeColors.length; i++)
      {
         cubeColors[i] = cubeColorData[i % cubeColorData.length];
      }
      return cubeColors;
   }

   private float[] getCubeColor(Block block)
   {
      return new float[]
      { 1, 0.5f, 1 };
   }

   private float[] makeCube(float x, float y, float z)
   {
      int offset = Block.SIZE / 2;
      return new float[]
      { x + offset, y + offset, z, x - offset, y + offset, z, x - offset, y + offset, z - Block.SIZE, x + offset, y + offset, z - Block.SIZE, x + offset, y - offset, z - Block.SIZE, x - offset, y - offset, z - Block.SIZE, x - offset, y - offset, z, x + offset, y - offset, z, x + offset, y + offset, z - Block.SIZE, x - offset, y + offset, z - Block.SIZE, x - offset, y - offset, z - Block.SIZE, x + offset, y - offset, z - Block.SIZE, x + offset, y - offset, z, x - offset, y - offset, z, x - offset, y + offset, z, x + offset, y + offset, z, x - offset, y + offset, z - Block.SIZE, x - offset, y + offset, z, x - offset, y - offset, z, x - offset, y - offset, z - Block.SIZE, x + offset, y + offset, z, x + offset, y + offset, z - Block.SIZE, x + offset, y - offset, z - Block.SIZE, x + offset, y - offset, z };
   }

   private float[] getNormalVector()
   {
      return new float[]
      { 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, };
   }

   public void tick()
   {

   }

   public void render()
   {
      GL11.glPushMatrix();
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboVertexHandle);
      GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboColorHandle);
      GL11.glColorPointer(3, GL11.GL_FLOAT, 0, 0L);
      GL11.glDrawArrays(GL11.GL_QUADS, 0, CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_LENGTH * 24);
      GL11.glPopMatrix();
   }
}

its not rendering anything.. but at least theres no crash..
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #9 - Posted 2013-02-24 11:10:19 »

you need to have
glEnableClientState()
<- Link

Then you have to enable the GL_VERTEX_ARRAY and the GL_COLOR_ARRAY, afaik.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
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.

CopyableCougar4 (15 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (38 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59: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!