Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
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  
  [Solved] View Frustum Culling  (Read 6053 times)
0 Members and 1 Guest are viewing this topic.
Offline marcuiulian13

Senior Devvie


Medals: 5
Exp: 3 years



« Posted 2013-03-29 08:56:58 »

I give up! Hello everyone!

I am struggling to get the frustum culling working for the last day, but i can't find what's wrong with what i do.
Here is my code for updating the frustum (called eveytime camera is moved) and checking if a point (a box) is in frustum.

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  
public void update() {
        FloatBuffer modelMatrixBuffer = BufferUtils.createFloatBuffer(16);
        glGetFloat(GL_MODELVIEW_MATRIX, modelMatrixBuffer);
        modelMatrixBuffer.rewind();

        Matrix4f modelMatrix = new Matrix4f();
        modelMatrix.load(modelMatrixBuffer);

        planes[0] = new Plane(modelMatrix.m30 + modelMatrix.m00, modelMatrix.m31 + modelMatrix.m01, modelMatrix.m32 + modelMatrix.m02, modelMatrix.m33 + modelMatrix.m03);
        planes[1] = new Plane(modelMatrix.m30 - modelMatrix.m00, modelMatrix.m31 - modelMatrix.m01, modelMatrix.m32 - modelMatrix.m02, modelMatrix.m33 - modelMatrix.m03);
        planes[2] = new Plane(modelMatrix.m30 + modelMatrix.m10, modelMatrix.m31 + modelMatrix.m11, modelMatrix.m32 + modelMatrix.m12, modelMatrix.m33 + modelMatrix.m13);
        planes[3] = new Plane(modelMatrix.m30 - modelMatrix.m10, modelMatrix.m31 - modelMatrix.m11, modelMatrix.m32 - modelMatrix.m12, modelMatrix.m33 - modelMatrix.m13);
        planes[4] = new Plane(modelMatrix.m30 + modelMatrix.m20, modelMatrix.m31 + modelMatrix.m21, modelMatrix.m32 + modelMatrix.m22, modelMatrix.m33 + modelMatrix.m23);
        planes[5] = new Plane(modelMatrix.m30 - modelMatrix.m20, modelMatrix.m31 - modelMatrix.m21, modelMatrix.m32 - modelMatrix.m22, modelMatrix.m33 - modelMatrix.m23);
    }

    public static boolean inFrustum(int x, int y, int z) {
        Frustum camFrustum = Camera.getInstance().fow;

        Vector4f vCube[] = new Vector4f[] {
            new Vector4f(x, y, z, 1f),
            new Vector4f(x + 1, y + 1, z + 1, 1f)
        };

        for(int v = 0; v < 2; v++) {
            for(int i = 0; i < 6; i++)
                if(distanceToPlane(vCube[v], camFrustum.planes[i]) < 0)
                    return false;
        }

        return true;
    }

    public static float distanceToPlane(Vector4f vertex, Plane plane) {
        return plane.a * vertex.x + plane.b * vertex.y + plane.c * vertex.z + plane.d;
    }


The Plane class is containing only the a, b, c and d from the plane's equation.

For the moment i just want to see it working and then i will try to optimize it.
Can anyone please help me?

Getting a project done is by far the most hard thing in game development.
Offline Roquen
« Reply #1 - Posted 2013-03-29 13:01:40 »

I stopped reading after I saw you pulling matrix info from OpenGL.  You're right to not think about speed to start with and just make it work first..I'd stick with that notion for the first (conceptual) part as well.

Using the same kind of logical steps I do on the complex number wiki page let me babble about step #1, point inside the near & far plane.  You have a camera at position C with a forward facing vector of F and a test point P.  We can translate the system such that we can think of the camera as being at the origin: P' = (P-C).  Now all we need to know is the parallel projection of P' into F:  If positive it's forward, negative behind and zero at the plane...moreover if F is a unit vector then the result is the distance from the plane.  So we have:  dist = dot(F, P').  Now this is the distance from the plane which contains the camera and our near plane will be some positive distance forward from that.  We'll call that distance 'n'...so if dist-n >= 0 then the point is forward or on the near plane and likewise for the far plane at distance 'f', if f-dist >=0 then we're inside the far plane.
Offline steg90

Senior Devvie


Medals: 1



« Reply #2 - Posted 2013-03-29 14:03:55 »

Just a thought, when doing frustum culling you will need some form of spatial partitioning also otherwise you could be checking 1000's of chunks...Octree comes to mind.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2013-03-29 14:15:52 »

Yeah, that's part of the optimization part.  If you have a region oct-tree and values for the four outer points, then next level values are simply the mid-point values.  Assuming you're doing a tree walk you can classify the planes and ignore all children when outside of all (planes), and likewise you don't need to test with a given plane once a node is inside it.
Offline marcuiulian13

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #4 - Posted 2013-03-29 17:12:39 »

I understand what you say. I will optimize the algorithm after i will manage this to work. First i want to make this work, because i understand how it works, but there is a problem with it. Am i using the right matrix? How can i calculate the W component of the points vector? What am i doing wrong in my code?
The second thig is that i'm 10th grade (in Romania), this is 2nd grade of high school and I didn't learned matrix and planes at mathematics.
I am very towards to learning stuff in advance, just tell me what i should do.

Getting a project done is by far the most hard thing in game development.
Offline steg90

Senior Devvie


Medals: 1



« Reply #5 - Posted 2013-03-29 17:15:14 »

Take look at this:

1  
http://www.crownandcutlass.com/features/technicaldetails/frustum.html


Regards
Offline marcuiulian13

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #6 - Posted 2013-03-29 18:00:11 »

I already checked that tutorial, trust me, i did all the research possible on this subject. Is the normalization of planes a must?

Getting a project done is by far the most hard thing in game development.
Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #7 - Posted 2013-03-29 19:33:28 »

Is the normalization of planes a must?
Of course.

Myomyomyo.
Offline Roquen
« Reply #8 - Posted 2013-03-29 20:57:45 »

If you want to cut straight to the chase, then there's this (as well):  http://www8.cs.umu.se/kurser/5DV051/HT12/lab/plane_extraction.pdf

Note we're now talking about the projection matrix.

Quote
Is the normalization of planes a must?
No, not if you don't need to know the distance.  If you have a normalized plane and plug in some point which returns a distance of 2 and the multiply the plane values by say 3 (and is now not normalized) if you plug the same point into the equation you'd get 6.   So for a given point the sign tells you on which side of the plane the point falls (and is still zero if on the plane)...no normalization required.
Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #9 - Posted 2013-03-29 21:42:56 »

Oh, Roquen's right. I've been using culling spheres though, in which case I need to have exact distance values to compare them against the sphere's radius.

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline marcuiulian13

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #10 - Posted 2013-03-29 23:28:17 »

Allright. Finally i found some source code of a Frustum class and i use that.
If anyone is interested in that, tell me and i will post it.

Next up: implementing quad / octal trees. Cheesy

Getting a project done is by far the most hard thing in game development.
Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #11 - Posted 2013-03-29 23:34:28 »

Yes please, could you post the code? Smiley

Offline marcuiulian13

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #12 - Posted 2013-03-30 07:35:08 »

Here it is. I can't really tell who's the owner, because i found this on google without any description.

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  
import org.lwjgl.BufferUtils;

import java.nio.*;

import static org.lwjgl.opengl.GL11.*;

public class Frustum
{
    public static final int RIGHT   = 0;
    public static final int LEFT    = 1;
    public static final int BOTTOM  = 2;
    public static final int TOP     = 3;
    public static final int BACK    = 4;
    public static final int FRONT   = 5;

    public static final int A = 0;
    public static final int B = 1;
    public static final int C = 2;
    public static final int D = 3;

    float[][] frustum = new float[6][4];

    FloatBuffer modelBuffer;
    FloatBuffer projectionBuffer;

    public Frustum() {
        modelBuffer = BufferUtils.createFloatBuffer(16);
        projectionBuffer = BufferUtils.createFloatBuffer(16);
    }

    public void normalizePlane(float[][] frustum, int side) {
        float magnitude = (float) Math.sqrt(frustum[side][A] * frustum[side][A] + frustum[side][B] * frustum[side][B] + frustum[side][C] * frustum[side][C]);

        frustum[side][A] /= magnitude;
        frustum[side][B] /= magnitude;
        frustum[side][C] /= magnitude;
        frustum[side][D] /= magnitude;
    }

    public void calculateFrustum() {
        float[] projectionMatrix = new float[16];
        float[] modelMatrix = new float[16];
        float[] clipMatrix = new float[16];

        projectionBuffer.rewind();
        glGetFloat(GL_PROJECTION_MATRIX, projectionBuffer);
        projectionBuffer.rewind();
        projectionBuffer.get(projectionMatrix);

        modelBuffer.rewind();
        glGetFloat(GL_MODELVIEW_MATRIX, modelBuffer);
        modelBuffer.rewind();
        modelBuffer.get(modelMatrix);

        clipMatrix[ 0] = modelMatrix[ 0] * projectionMatrix[ 0] + modelMatrix[ 1] * projectionMatrix[ 4] + modelMatrix[ 2] * projectionMatrix[ 8] + modelMatrix[ 3] * projectionMatrix[12];
        clipMatrix[ 1] = modelMatrix[ 0] * projectionMatrix[ 1] + modelMatrix[ 1] * projectionMatrix[ 5] + modelMatrix[ 2] * projectionMatrix[ 9] + modelMatrix[ 3] * projectionMatrix[13];
        clipMatrix[ 2] = modelMatrix[ 0] * projectionMatrix[ 2] + modelMatrix[ 1] * projectionMatrix[ 6] + modelMatrix[ 2] * projectionMatrix[10] + modelMatrix[ 3] * projectionMatrix[14];
        clipMatrix[ 3] = modelMatrix[ 0] * projectionMatrix[ 3] + modelMatrix[ 1] * projectionMatrix[ 7] + modelMatrix[ 2] * projectionMatrix[11] + modelMatrix[ 3] * projectionMatrix[15];

        clipMatrix[ 4] = modelMatrix[ 4] * projectionMatrix[ 0] + modelMatrix[ 5] * projectionMatrix[ 4] + modelMatrix[ 6] * projectionMatrix[ 8] + modelMatrix[ 7] * projectionMatrix[12];
        clipMatrix[ 5] = modelMatrix[ 4] * projectionMatrix[ 1] + modelMatrix[ 5] * projectionMatrix[ 5] + modelMatrix[ 6] * projectionMatrix[ 9] + modelMatrix[ 7] * projectionMatrix[13];
        clipMatrix[ 6] = modelMatrix[ 4] * projectionMatrix[ 2] + modelMatrix[ 5] * projectionMatrix[ 6] + modelMatrix[ 6] * projectionMatrix[10] + modelMatrix[ 7] * projectionMatrix[14];
        clipMatrix[ 7] = modelMatrix[ 4] * projectionMatrix[ 3] + modelMatrix[ 5] * projectionMatrix[ 7] + modelMatrix[ 6] * projectionMatrix[11] + modelMatrix[ 7] * projectionMatrix[15];

        clipMatrix[ 8] = modelMatrix[ 8] * projectionMatrix[ 0] + modelMatrix[ 9] * projectionMatrix[ 4] + modelMatrix[10] * projectionMatrix[ 8] + modelMatrix[11] * projectionMatrix[12];
        clipMatrix[ 9] = modelMatrix[ 8] * projectionMatrix[ 1] + modelMatrix[ 9] * projectionMatrix[ 5] + modelMatrix[10] * projectionMatrix[ 9] + modelMatrix[11] * projectionMatrix[13];
        clipMatrix[10] = modelMatrix[ 8] * projectionMatrix[ 2] + modelMatrix[ 9] * projectionMatrix[ 6] + modelMatrix[10] * projectionMatrix[10] + modelMatrix[11] * projectionMatrix[14];
        clipMatrix[11] = modelMatrix[ 8] * projectionMatrix[ 3] + modelMatrix[ 9] * projectionMatrix[ 7] + modelMatrix[10] * projectionMatrix[11] + modelMatrix[11] * projectionMatrix[15];

        clipMatrix[12] = modelMatrix[12] * projectionMatrix[ 0] + modelMatrix[13] * projectionMatrix[ 4] + modelMatrix[14] * projectionMatrix[ 8] + modelMatrix[15] * projectionMatrix[12];
        clipMatrix[13] = modelMatrix[12] * projectionMatrix[ 1] + modelMatrix[13] * projectionMatrix[ 5] + modelMatrix[14] * projectionMatrix[ 9] + modelMatrix[15] * projectionMatrix[13];
        clipMatrix[14] = modelMatrix[12] * projectionMatrix[ 2] + modelMatrix[13] * projectionMatrix[ 6] + modelMatrix[14] * projectionMatrix[10] + modelMatrix[15] * projectionMatrix[14];
        clipMatrix[15] = modelMatrix[12] * projectionMatrix[ 3] + modelMatrix[13] * projectionMatrix[ 7] + modelMatrix[14] * projectionMatrix[11] + modelMatrix[15] * projectionMatrix[15];

        // This will extract the LEFT side of the frustum
        frustum[LEFT][A] = clipMatrix[ 3] + clipMatrix[ 0];
        frustum[LEFT][B] = clipMatrix[ 7] + clipMatrix[ 4];
        frustum[LEFT][C] = clipMatrix[11] + clipMatrix[ 8];
        frustum[LEFT][D] = clipMatrix[15] + clipMatrix[12];
        normalizePlane(frustum, LEFT);

        // This will extract the RIGHT side of the frustum
        frustum[RIGHT][A] = clipMatrix[ 3] - clipMatrix[ 0];
        frustum[RIGHT][B] = clipMatrix[ 7] - clipMatrix[ 4];
        frustum[RIGHT][C] = clipMatrix[11] - clipMatrix[ 8];
        frustum[RIGHT][D] = clipMatrix[15] - clipMatrix[12];
        normalizePlane(frustum, RIGHT);

        // This will extract the BOTTOM side of the frustum
        frustum[BOTTOM][A] = clipMatrix[ 3] + clipMatrix[ 1];
        frustum[BOTTOM][B] = clipMatrix[ 7] + clipMatrix[ 5];
        frustum[BOTTOM][C] = clipMatrix[11] + clipMatrix[ 9];
        frustum[BOTTOM][D] = clipMatrix[15] + clipMatrix[13];
        normalizePlane(frustum, BOTTOM);

        // This will extract the TOP side of the frustum
        frustum[TOP][A] = clipMatrix[ 3] - clipMatrix[ 1];
        frustum[TOP][B] = clipMatrix[ 7] - clipMatrix[ 5];
        frustum[TOP][C] = clipMatrix[11] - clipMatrix[ 9];
        frustum[TOP][D] = clipMatrix[15] - clipMatrix[13];
        normalizePlane(frustum, TOP);

        // This will extract the FRONT side of the frustum
        frustum[FRONT][A] = clipMatrix[ 3] + clipMatrix[ 2];
        frustum[FRONT][B] = clipMatrix[ 7] + clipMatrix[ 6];
        frustum[FRONT][C] = clipMatrix[11] + clipMatrix[10];
        frustum[FRONT][D] = clipMatrix[15] + clipMatrix[14];
        normalizePlane(frustum, FRONT);

        // This will extract the BACK side of the frustum
        frustum[BACK][A] = clipMatrix[ 3] - clipMatrix[ 2];
        frustum[BACK][B] = clipMatrix[ 7] - clipMatrix[ 6];
        frustum[BACK][C] = clipMatrix[11] - clipMatrix[10];
        frustum[BACK][D] = clipMatrix[15] - clipMatrix[14];
        normalizePlane(frustum, BACK);
    }

    public boolean cubeInFrustum(float x, float y, float z, float size) {
        for(int i = 0; i < 6; i++ ) {
            if(frustum[i][A] * (x - size) + frustum[i][B] * (y - size) + frustum[i][C] * (z - size) + frustum[i][D] > 0)
                continue;
            if(frustum[i][A] * (x + size) + frustum[i][B] * (y - size) + frustum[i][C] * (z - size) + frustum[i][D] > 0)
                continue;
            if(frustum[i][A] * (x - size) + frustum[i][B] * (y + size) + frustum[i][C] * (z - size) + frustum[i][D] > 0)
                continue;
            if(frustum[i][A] * (x + size) + frustum[i][B] * (y + size) + frustum[i][C] * (z - size) + frustum[i][D] > 0)
                continue;
            if(frustum[i][A] * (x - size) + frustum[i][B] * (y - size) + frustum[i][C] * (z + size) + frustum[i][D] > 0)
                continue;
            if(frustum[i][A] * (x + size) + frustum[i][B] * (y - size) + frustum[i][C] * (z + size) + frustum[i][D] > 0)
                continue;
            if(frustum[i][A] * (x - size) + frustum[i][B] * (y + size) + frustum[i][C] * (z + size) + frustum[i][D] > 0)
                continue;
            if(frustum[i][A] * (x + size) + frustum[i][B] * (y + size) + frustum[i][C] * (z + size) + frustum[i][D] > 0)
                continue;

            return false;
        }

        return true;
    }
}

Getting a project done is by far the most hard thing in game development.
Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #13 - Posted 2013-03-30 10:47:16 »

If only I knew what any of that meant Tongue Time to print it out and study up on my math Cheesy

Offline Roquen
« Reply #14 - Posted 2013-03-30 13:39:24 »

Don't bother.  This is reconstructing information which you already have.  If you want to dig into math, then learn how to directly build the planes instead.  But it should be all layed out in the paper I linked and here's an additional reference: http://fgiesen.wordpress.com/2012/08/31/frustum-planes-from-the-projection-matrix/
Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #15 - Posted 2013-03-30 18:36:30 »

So I'm a little confused. I have to create a new instance of the frustum class and then call
1  
calculateFrustum()
correct? Then in my block rendering code, I call
1  
cubeInFrustrum
for every block. But it doesn't work! Basically, this is what I do:

1. When I create a new instance of a chunk, I create a new frustum object.
2. I call a method that calls the method
1  
calculateFrustum()
.
3. In my rebuild method, I have 3 for loops that handle the blocks. I call
1  
cubeInFrustum()
for every block.

After all of that, nothing still shows up. What am I doing wrong here?

Offline marcuiulian13

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #16 - Posted 2013-03-30 20:17:19 »

Make sure you call calculateFrustum everytime your camera moves. I'm pretty sure this is the problem you have.

Getting a project done is by far the most hard thing in game development.
Offline Longor1996
« Reply #17 - Posted 2013-03-30 21:00:20 »

Hi there!

I think the problem is, that you are trying to frustum-cull every single block.
But you have to frustum-cull the chunks to determine if they should be rendered or not.

- Longor1996

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

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #18 - Posted 2013-03-31 13:54:23 »

Now i've made this work i'm struggling to potimize it.

How i use the frustum culling:
1  
2  
3  
4  
if (cameraHasMoved) {
    updateFrustum();
    recalculateVBOOfEveryChunk();
}


I already implemented the octree (one octree per chunk).
The problems are:
- the fps drops from 50-60fps to 5-15fps when i am moving the camera
- memory usage >800mb when moving the camera

Tell me what part of code you need and i will post it.

Getting a project done is by far the most hard thing in game development.
Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #19 - Posted 2013-03-31 20:23:35 »

Quote
1  
recalculateVBOOfEveryChunk();
The whole point of VBOs is that you don't have to recalculate the VBOs each frame. -_-

Myomyomyo.
Offline marcuiulian13

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #20 - Posted 2013-04-01 05:58:11 »

So how can you update the "view" of the chunk if you break a block?

Getting a project done is by far the most hard thing in game development.
Offline Phased
« Reply #21 - Posted 2013-04-01 06:07:21 »

update the chunk when a block is broken
Offline marcuiulian13

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #22 - Posted 2013-04-01 11:25:12 »

But how i use frustum culling then? I f i don't update the chunks when I move the camera, then the blocks won't show.
I found something on LWJGL Wiki, but it is under GL3.1, but maybe it will work in GL2.1.

http://www.lwjgl.org/wiki/index.php?title=The_Quad_updating_a_VBO_with_BufferSubData

Getting a project done is by far the most hard thing in game development.
Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #23 - Posted 2013-04-01 12:32:03 »

You need to check every time the camera is moved whether or not a chunk is in the frustum. If it is, you send the chunk to renderer and render it until it is not in the frustum anymore. Only rebuild chunks when a block is removed or it enters the view frustum. If it leaves the screen, save it to disk or delete it or whatever you want to do. But it's a bad idea to rebuild it every frame. Only rebuild it when you absolutely have to. Otherwise, just have a render method that is constantly running for the chunk.
Also, this:
1  
2  
3  
positionBuffer.put(new float[]{x, y, z + 1f, x + 1f, y, z + 1f, x + 1f, y + 1f, z + 1f, x, y + 1f, z + 1f});
            colorBuffer.put(new float[]{1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0});
            vertexCount += 4;

Should not work. You are creating 3 * 4 vertices per face, not just 4. 3 is the size, 4 is the amount of vertices per face. I don't know how you're render function is actually working without that.

Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #24 - Posted 2013-04-01 12:34:31 »

And another thing, I've never used it, but I don't think streaming your chunks would be beneficial because you have static terrain. It doesn't move every frame. The only time it ever changes is when you place or remove a block. If I'm correct, streaming is more for animated models that use VBOs.

Offline marcuiulian13

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #25 - Posted 2013-04-01 16:20:56 »

You need to check every time the camera is moved whether or not a chunk is in the frustum. If it is, you send the chunk to renderer and render it until it is not in the frustum anymore. Only rebuild chunks when a block is removed or it enters the view frustum. If it leaves the screen, save it to disk or delete it or whatever you want to do. But it's a bad idea to rebuild it every frame. Only rebuild it when you absolutely have to. Otherwise, just have a render method that is constantly running for the chunk.
Also, this:
1  
2  
3  
positionBuffer.put(new float[]{x, y, z + 1f, x + 1f, y, z + 1f, x + 1f, y + 1f, z + 1f, x, y + 1f, z + 1f});
            colorBuffer.put(new float[]{1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0});
            vertexCount += 4;

Should not work. You are creating 3 * 4 vertices per face, not just 4. 3 is the size, 4 is the amount of vertices per face. I don't know how you're render function is actually working without that.

It is correct. I am having 4 verices per face, 3 * 4 float numbers per face.
That int is used in
1  
glDrawArrays(GL_QUADS, 0, chunk.vertexCount);


Also, thank you very much for help, that works perfectly for what i am doing. Cheesy

Getting a project done is by far the most hard thing in game development.
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.

PocketCrafter7 (14 views)
2014-11-28 16:25:35

PocketCrafter7 (9 views)
2014-11-28 16:25:09

PocketCrafter7 (10 views)
2014-11-28 16:24:29

toopeicgaming1999 (76 views)
2014-11-26 15:22:04

toopeicgaming1999 (67 views)
2014-11-26 15:20:36

toopeicgaming1999 (16 views)
2014-11-26 15:20:08

SHC (30 views)
2014-11-25 12:00:59

SHC (28 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!