Frustum culling
This commit is contained in:
		
							parent
							
								
									4d74b5e3e9
								
							
						
					
					
						commit
						3bb8a63ee1
					
				
							
								
								
									
										34
									
								
								src/k3.c
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								src/k3.c
									
									
									
									
									
								
							| @ -12,6 +12,7 @@ | |||||||
| #include<cglm/vec2.h> | #include<cglm/vec2.h> | ||||||
| #include<cglm/frustum.h> | #include<cglm/frustum.h> | ||||||
| #include<cglm/cam.h> | #include<cglm/cam.h> | ||||||
|  | #include<cglm/box.h> | ||||||
| 
 | 
 | ||||||
| #include"ssort.h" | #include"ssort.h" | ||||||
| 
 | 
 | ||||||
| @ -1309,9 +1310,22 @@ static void apply_cpu_skinning(struct k3Mdl *mdl, struct k3AnimationBone *bones) | |||||||
| 	glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, mdl->offV, mdl->verts * sizeof(*boned), boned); | 	glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, mdl->offV, mdl->verts * sizeof(*boned), boned); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static void forward_subpass(mat4 view, int transparent, int lightsStart, int lightsCount, size_t rbleStart, size_t rbleEnd) { | static bool outside_frustum(vec3 *aabb, float *modelmat, vec4 *frustum) { | ||||||
|  | 	vec3 transformedAABB[2]; | ||||||
|  | 	glm_aabb_transform(aabb, modelmat, transformedAABB); | ||||||
|  | 	 | ||||||
|  | 	return !glm_aabb_frustum(transformedAABB, frustum); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | static void forward_subpass(mat4 projection, mat4 view, int transparent, int lightsStart, int lightsCount, size_t rbleStart, size_t rbleEnd) { | ||||||
| 	setup_ff_lights(view, lightsStart, lightsCount); | 	setup_ff_lights(view, lightsStart, lightsCount); | ||||||
| 	 | 	 | ||||||
|  | 	mat4 viewProj; | ||||||
|  | 	glm_mat4_mul(projection, view, viewProj); | ||||||
|  | 	 | ||||||
|  | 	vec4 cameraFrustum[6]; | ||||||
|  | 	glm_frustum_planes(viewProj, cameraFrustum); | ||||||
|  | 	 | ||||||
| 	GLhandleARB lastGLSL = -1; | 	GLhandleARB lastGLSL = -1; | ||||||
| 	GLuint lastVP = 0, lastFP = 0; | 	GLuint lastVP = 0, lastFP = 0; | ||||||
| 	struct k3Mat *lastMaterial = NULL; | 	struct k3Mat *lastMaterial = NULL; | ||||||
| @ -1327,6 +1341,10 @@ static void forward_subpass(mat4 view, int transparent, int lightsStart, int lig | |||||||
| 		GLuint arbvp = renderQueue[rble].arbvp; | 		GLuint arbvp = renderQueue[rble].arbvp; | ||||||
| 		GLuint arbfp = renderQueue[rble].arbfp; | 		GLuint arbfp = renderQueue[rble].arbfp; | ||||||
| 		 | 		 | ||||||
|  | 		if(outside_frustum(mdl->aabb, modelmat, cameraFrustum)) { | ||||||
|  | 			continue; | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
| 		struct k3Mat *mat = &mesh->mat; | 		struct k3Mat *mat = &mesh->mat; | ||||||
| 		 | 		 | ||||||
| 		if(mat->passes[0].additive && lightsStart != 0) { | 		if(mat->passes[0].additive && lightsStart != 0) { | ||||||
| @ -1535,7 +1553,7 @@ void k3PassForward(mat4 projection, mat4 cam) { | |||||||
| 	 | 	 | ||||||
| 	int l = 0, k = LightCount; | 	int l = 0, k = LightCount; | ||||||
| 	while(1) { | 	while(1) { | ||||||
| 		forward_subpass(view, 0, l, k > 4 ? 4 : k, 0, renderQueueSize); | 		forward_subpass(projection, view, 0, l, k > 4 ? 4 : k, 0, renderQueueSize); | ||||||
| 		 | 		 | ||||||
| 		l += 4; | 		l += 4; | ||||||
| 		k -= 4; | 		k -= 4; | ||||||
| @ -1549,7 +1567,7 @@ void k3PassForward(mat4 projection, mat4 cam) { | |||||||
| 	for(size_t rble = 0; rble < renderQueueSize; rble++) { | 	for(size_t rble = 0; rble < renderQueueSize; rble++) { | ||||||
| 		l = 0, k = LightCount; | 		l = 0, k = LightCount; | ||||||
| 		while(1) { | 		while(1) { | ||||||
| 			forward_subpass(view, 1, l, k > 4 ? 4 : k, rble, rble + 1); | 			forward_subpass(projection, view, 1, l, k > 4 ? 4 : k, rble, rble + 1); | ||||||
| 			 | 			 | ||||||
| 			l += 4; | 			l += 4; | ||||||
| 			k -= 4; | 			k -= 4; | ||||||
| @ -1573,6 +1591,12 @@ void k3PassDepthOnly(mat4 projection, mat4 cam, int clear, int cull) { | |||||||
| 	 | 	 | ||||||
| 	setup_arbprog_globals(); | 	setup_arbprog_globals(); | ||||||
| 	 | 	 | ||||||
|  | 	mat4 viewProj; | ||||||
|  | 	glm_mat4_mul(projection, view, viewProj); | ||||||
|  | 	 | ||||||
|  | 	vec4 cameraFrustum[6]; | ||||||
|  | 	glm_frustum_planes(viewProj, cameraFrustum); | ||||||
|  | 	 | ||||||
| 	glEnable(GL_DEPTH_TEST); | 	glEnable(GL_DEPTH_TEST); | ||||||
| 	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); | 	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); | ||||||
| 	glDepthMask(GL_TRUE); | 	glDepthMask(GL_TRUE); | ||||||
| @ -1608,6 +1632,10 @@ void k3PassDepthOnly(mat4 projection, mat4 cam, int clear, int cull) { | |||||||
| 			continue; | 			continue; | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
|  | 		if(outside_frustum(mdl->aabb, modelmat, cameraFrustum)) { | ||||||
|  | 			continue; | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
| 		GLhandleARB glsl = renderQueue[rble].glsl; | 		GLhandleARB glsl = renderQueue[rble].glsl; | ||||||
| 		GLuint arbvp = renderQueue[rble].arbvp; | 		GLuint arbvp = renderQueue[rble].arbvp; | ||||||
| 		 | 		 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Mid
						Mid