فهرست منبع

Add placeholder render_triangles to remove gl dependency from Terrain.cpp

Daniele Bartolini 12 سال پیش
والد
کامیت
c88486ce02
6فایلهای تغییر یافته به همراه54 افزوده شده و 19 حذف شده
  1. 9 19
      src/Terrain.cpp
  2. 2 0
      src/renderers/Renderer.h
  3. 20 0
      src/renderers/gl/GLRenderer.cpp
  4. 2 0
      src/renderers/gl/GLRenderer.h
  5. 20 0
      src/renderers/gles/GLESRenderer.cpp
  6. 1 0
      src/renderers/gles/GLESRenderer.h

+ 9 - 19
src/Terrain.cpp

@@ -27,12 +27,9 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Device.h"
 #include "Device.h"
 #include "Renderer.h"
 #include "Renderer.h"
 #include "MathUtils.h"
 #include "MathUtils.h"
-#include <GL/glew.h>
 #include "Log.h"
 #include "Log.h"
-#include <cstdio>
 #include "Vec2.h"
 #include "Vec2.h"
 #include "Interpolation.h"
 #include "Interpolation.h"
-#include <cmath>
 
 
 namespace crown
 namespace crown
 {
 {
@@ -79,7 +76,7 @@ void Terrain::CreateTerrain(uint32_t xSize, uint32_t zSize, uint32_t tilePerMete
 	mVerticesInSizeX = mTilesInSizeX + 1;
 	mVerticesInSizeX = mTilesInSizeX + 1;
 	mVerticesInSizeZ = mTilesInSizeZ + 1;
 	mVerticesInSizeZ = mTilesInSizeZ + 1;
 
 
-	printf("Vertices in size x/z: %d %d\n", mVerticesInSizeX, mVerticesInSizeZ);
+	Log::D("Vertices in size x/z: %d %d\n", mVerticesInSizeX, mVerticesInSizeZ);
 
 
 	uint32_t heightsCount = mVerticesInSizeX * mVerticesInSizeZ;
 	uint32_t heightsCount = mVerticesInSizeX * mVerticesInSizeZ;
 
 
@@ -283,21 +280,14 @@ uint32_t Terrain::SnapToGrid(const Vec3& vertex)
 
 
 void Terrain::Render()
 void Terrain::Render()
 {
 {
-	//Renderer* renderer = GetDevice()->GetRenderer();
-
-	glBindBuffer(GL_ARRAY_BUFFER, 0);
-	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
-
-	glEnableClientState(GL_VERTEX_ARRAY);
-	glEnableClientState(GL_NORMAL_ARRAY);
-	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-	glVertexPointer(3, GL_FLOAT, 0, mVertices);
-	glNormalPointer(GL_FLOAT, 0, mNormals);
-	glTexCoordPointer(2, GL_FLOAT, 0, mTexCoords);
-	glDrawElements(GL_TRIANGLES, mTilesInSizeX * mTilesInSizeZ * 6, GL_UNSIGNED_SHORT, mIndices);
-	glDisableClientState(GL_VERTEX_ARRAY);
-	glDisableClientState(GL_NORMAL_ARRAY);
-	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+	Renderer* renderer = GetDevice()->GetRenderer();
+
+	renderer->render_triangles(
+				mVertices[0].to_float_ptr(),
+				mNormals[0].to_float_ptr(),
+				mTexCoords[0].to_float_ptr(),
+				mIndices,
+				mTilesInSizeX * mTilesInSizeZ * 6);
 }
 }
 
 
 float Terrain::GaussDist(float x, float y, float sigma)
 float Terrain::GaussDist(float x, float y, float sigma)

+ 2 - 0
src/renderers/Renderer.h

@@ -190,6 +190,8 @@ public:
 
 
 	virtual void render_point_buffer(const VertexBuffer* buffer) = 0;
 	virtual void render_point_buffer(const VertexBuffer* buffer) = 0;
 
 
+	virtual void render_triangles(const float* vertices, const float* normals, const float* uvs, const uint16_t* indices, uint32_t count) = 0;
+
 	// FIXME
 	// FIXME
 	virtual TextureId	load_texture(TextureResource* texture) = 0;
 	virtual TextureId	load_texture(TextureResource* texture) = 0;
 	virtual void		unload_texture(TextureResource* texture) = 0;
 	virtual void		unload_texture(TextureResource* texture) = 0;

+ 20 - 0
src/renderers/gl/GLRenderer.cpp

@@ -744,6 +744,26 @@ void GLRenderer::set_light_attenuation(uint32_t light, float constant, float lin
 	glLightf(GL_LIGHT0 + light, GL_QUADRATIC_ATTENUATION, quadratic);
 	glLightf(GL_LIGHT0 + light, GL_QUADRATIC_ATTENUATION, quadratic);
 }
 }
 
 
+//-----------------------------------------------------------------------------
+void GLRenderer::render_triangles(const float* vertices, const float* normals, const float* uvs, const uint16_t* indices, uint32_t count)
+{
+	glBindBuffer(GL_ARRAY_BUFFER, 0);
+	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+
+	glEnableClientState(GL_VERTEX_ARRAY);
+	glEnableClientState(GL_NORMAL_ARRAY);
+	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+	glVertexPointer(3, GL_FLOAT, 0, vertices);
+	glNormalPointer(GL_FLOAT, 0, normals);
+	glTexCoordPointer(2, GL_FLOAT, 0, uvs);
+
+	glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, indices);
+
+	glDisableClientState(GL_VERTEX_ARRAY);
+	glDisableClientState(GL_NORMAL_ARRAY);
+	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+}
+
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 TextureId GLRenderer::load_texture(TextureResource* texture)
 TextureId GLRenderer::load_texture(TextureResource* texture)
 {
 {

+ 2 - 0
src/renderers/gl/GLRenderer.h

@@ -116,6 +116,8 @@ public:
 	void				render_vertex_index_buffer(const VertexBuffer* vertices, const IndexBuffer* indices);
 	void				render_vertex_index_buffer(const VertexBuffer* vertices, const IndexBuffer* indices);
 	void				render_point_buffer(const VertexBuffer* buffer);
 	void				render_point_buffer(const VertexBuffer* buffer);
 
 
+	void				render_triangles(const float* vertices, const float* normals, const float* uvs, const uint16_t* indices, uint32_t count);
+
 	TextureId			load_texture(TextureResource* texture);
 	TextureId			load_texture(TextureResource* texture);
 	void				unload_texture(TextureResource* texture);
 	void				unload_texture(TextureResource* texture);
 	TextureId			reload_texture(TextureResource* old_texture, TextureResource* new_texture);
 	TextureId			reload_texture(TextureResource* old_texture, TextureResource* new_texture);

+ 20 - 0
src/renderers/gles/GLESRenderer.cpp

@@ -692,6 +692,26 @@ void GLESRenderer::set_light_attenuation(uint32_t light, float constant, float l
 	glLightf(GL_LIGHT0 + light, GL_QUADRATIC_ATTENUATION, quadratic);
 	glLightf(GL_LIGHT0 + light, GL_QUADRATIC_ATTENUATION, quadratic);
 }
 }
 
 
+//-----------------------------------------------------------------------------
+void GLESRenderer::render_triangles(const float* vertices, const float* normals, const float* uvs, const uint16_t* indices, uint32_t count)
+{
+	glBindBuffer(GL_ARRAY_BUFFER, 0);
+	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+
+	glEnableClientState(GL_VERTEX_ARRAY);
+	glEnableClientState(GL_NORMAL_ARRAY);
+	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+	glVertexPointer(3, GL_FLOAT, 0, vertices);
+	glNormalPointer(GL_FLOAT, 0, normals);
+	glTexCoordPointer(2, GL_FLOAT, 0, uvs);
+
+	glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, indices);
+
+	glDisableClientState(GL_VERTEX_ARRAY);
+	glDisableClientState(GL_NORMAL_ARRAY);
+	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+}
+
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 TextureId GLESRenderer::load_texture(TextureResource* texture)
 TextureId GLESRenderer::load_texture(TextureResource* texture)
 {
 {

+ 1 - 0
src/renderers/gles/GLESRenderer.h

@@ -115,6 +115,7 @@ public:
 	void				render_vertex_index_buffer(const VertexBuffer* vertices, const IndexBuffer* indices);
 	void				render_vertex_index_buffer(const VertexBuffer* vertices, const IndexBuffer* indices);
 	void				render_point_buffer(const VertexBuffer* buffer);
 	void				render_point_buffer(const VertexBuffer* buffer);
 
 
+	void				render_triangles(const float* vertices, const float* normals, const float* uvs, const uint16_t* indices, uint32_t count);
 
 
 	TextureId			load_texture(TextureResource* texture);
 	TextureId			load_texture(TextureResource* texture);
 	void				unload_texture(TextureResource* texture);
 	void				unload_texture(TextureResource* texture);