Jump to content
  • Advertisement
Sign in to follow this  
dav1dde

Frustum Culluing

This topic is 2126 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I have a problem with Frustum Culling, it seems to cull sometimes on the opposite ends and sometimes it doesn't cull at all.

My Frustum:
[source lang="cpp"]module brala.gfx.frustum;

private {
import gl3n.linalg;
import gl3n.math : abs;

import brala.gfx.aabb : AABB;
}

enum {
OUTSIDE = 0,
INSIDE,
INTERSECT
}

struct Frustum {
vec4 left;
vec4 right;
vec4 bottom;
vec4 top;
vec4 near;
vec4 far;

@safe pure nothrow:

this(mat4 mvp) {
left = vec4(mvp[0][3] + mvp[0][0],
mvp[1][3] + mvp[1][0],
mvp[2][3] + mvp[2][0],
mvp[3][3] + mvp[3][0]).normalized;

right = vec4(mvp[0][3] - mvp[0][0],
mvp[1][3] - mvp[1][0],
mvp[2][3] - mvp[2][0],
mvp[3][3] - mvp[3][0]).normalized;

bottom = vec4(mvp[0][3] + mvp[0][1],
mvp[1][3] + mvp[1][1],
mvp[2][3] + mvp[2][1],
mvp[3][3] + mvp[3][1]).normalized;

top = vec4(mvp[0][3] - mvp[0][1],
mvp[1][3] - mvp[1][1],
mvp[2][3] - mvp[2][1],
mvp[3][3] - mvp[3][1]).normalized;

near = vec4(mvp[0][3] + mvp[0][2],
mvp[1][3] + mvp[1][2],
mvp[2][3] + mvp[2][2],
mvp[3][3] + mvp[3][2]).normalized;

far = vec4(mvp[0][3] - mvp[0][2],
mvp[1][3] - mvp[1][2],
mvp[2][3] - mvp[2][2],
mvp[3][3] - mvp[3][2]).normalized;
}

auto intersects(AABB aabb) {
vec3 hextent = aabb.half_extent;
vec3 center = aabb.center;

int result = INSIDE;
foreach(plane; [left, right, bottom, top, near, far]) {
float d = dot(center, vec3(plane));
float r = dot(hextent, abs(vec3(plane)));

if(d + r < -plane.w) {
// outside
return OUTSIDE;
}
if(d - r < -plane.w) {
result = INTERSECT;
}
}

return result;
}

bool opBinaryRight(string s : "in")(AABB aabb) {
return intersects(aabb) > 0;
}
}[/source]

The "rendering":
[source lang="cpp"] void draw(BraLaEngine engine) {
Frustum frustum = Frustum(engine.proj * engine.view);

foreach(chunkc, chunk; chunks) {
vec3i w_chunkc = vec3i(chunkc.x*width, chunkc.y*height, chunkc.z*depth);

AABB aabb = AABB(vec3(w_chunkc), vec3(w_chunkc.x+width, w_chunkc.y+height, w_chunkc.z+width));
if(aabb in frustum) {
bind(engine, chunk);

engine.model = mat4.translation(chunkc.x*width, chunkc.y*height, chunkc.z*depth);
engine.flush_uniforms();

glDrawArrays(GL_TRIANGLES, 0, cast(uint)chunk.vbo_vcount);
}
}
}[/source]

Some images: http://imgur.com/a/FS4oK

I hope you see what I mean and I hope you have an idea why it doesn't work.

Thanks, dav1d.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!