Prechádzať zdrojové kódy

We can now render a range of indices

Daniele Bartolini 12 rokov pred
rodič
commit
f6057abdf1

+ 5 - 1
engine/renderers/RenderContext.h

@@ -183,6 +183,8 @@ public:
 	GPUProgramId	program;
 	VertexBufferId	vb;
 	IndexBufferId	ib;
+	uint32_t		start_index;
+	uint32_t		num_indices;
 	Sampler			samplers[STATE_MAX_TEXTURES];
 };
 
@@ -235,9 +237,11 @@ struct RenderContext
 		m_state.vb = vb;
 	}
 
-	void set_index_buffer(IndexBufferId ib)
+	void set_index_buffer(IndexBufferId ib, uint32_t start_index, uint32_t num_indices)
 	{
 		m_state.ib = ib;
+		m_state.start_index = start_index;
+		m_state.num_indices = num_indices;
 	}
 
 	void set_uniform(UniformId id, UniformType::Enum type, const void* value, uint8_t num)

+ 2 - 2
engine/renderers/Renderer.h

@@ -632,10 +632,10 @@ public:
 		m_submit->set_vertex_buffer(id);
 	}
 
-	void set_index_buffer(IndexBufferId id)
+	void set_index_buffer(IndexBufferId id, uint32_t start_index = 0, uint32_t num_indices = UINT32_MAX)
 	{
 		CE_ASSERT(m_index_buffers.has(id), "Index buffer does not exist");
-		m_submit->set_index_buffer(id);
+		m_submit->set_index_buffer(id, start_index, num_indices);
 	}
 
 	void set_uniform(UniformId id, UniformType::Enum type, const void* value, uint8_t num)

+ 5 - 4
engine/renderers/gl/GLRenderer.cpp

@@ -473,16 +473,17 @@ public:
 			if (ib.id != INVALID_ID)
 			{
 				const IndexBuffer& index_buffer = m_index_buffers[ib.index];
-				uint32_t prim_type = (flags & STATE_PRIMITIVE_MASK) >> STATE_PRIMITIVE_SHIFT;
-				GLenum gl_prim_type = PRIMITIVE_TYPE_TABLE[prim_type];
+				const uint32_t prim_type = (flags & STATE_PRIMITIVE_MASK) >> STATE_PRIMITIVE_SHIFT;
+				const GLenum gl_prim_type = PRIMITIVE_TYPE_TABLE[prim_type];
+				const uint32_t num_indices = cur_state.num_indices == UINT32_MAX ? index_buffer.m_index_count : cur_state.num_indices;
+
 				GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer.m_id));
-				GL_CHECK(glDrawElements(gl_prim_type, index_buffer.m_index_count, GL_UNSIGNED_SHORT, 0));
+				GL_CHECK(glDrawElements(gl_prim_type, num_indices, GL_UNSIGNED_SHORT, (void*) (uintptr_t) (cur_state.start_index * sizeof(uint16_t))));
 			}
 			else
 			{
 				GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
 			}
-
 		}
 
 		GL_CHECK(glFinish());