Panagiotis Christopoulos Charitos 15 år sedan
förälder
incheckning
8a22200a15

+ 16 - 13
src/Renderer/BufferObjects/BufferObject.cpp

@@ -37,23 +37,26 @@ void BufferObject::create(GLenum target_, uint sizeInBytes_, const void* dataPtr
 //======================================================================================================================
 // write                                                                                                               =
 //======================================================================================================================
-void BufferObject::write(void* buff, size_t size)
+void BufferObject::write(void* buff)
 {
-	if(usage == GL_STATIC_DRAW)
-	{
-		throw EXCEPTION("Its not recommended to map GL_STATIC_DRAW BOs");
-	}
-
+	RASSERT_THROW_EXCEPTION(usage == GL_STATIC_DRAW);
 	bind();
+	void* mapped = glMapBuffer(target, GL_WRITE_ONLY);
+	memcpy(mapped, buff, sizeInBytes);
+	glUnmapBuffer(target);
+	unbind();
+}
 
-	int bufferSize = 0;
-	glGetBufferParameteriv(target, GL_BUFFER_SIZE, &bufferSize);
-	if(size != uint(bufferSize))
-	{
-		throw EXCEPTION("Data size mismatch");
-	}
 
-	void* mapped = glMapBuffer(target, GL_WRITE_ONLY);
+//======================================================================================================================
+// write                                                                                                               =
+//======================================================================================================================
+void BufferObject::write(void* buff, size_t offset, size_t size)
+{
+	RASSERT_THROW_EXCEPTION(usage == GL_STATIC_DRAW);
+	RASSERT_THROW_EXCEPTION(offset + size > sizeInBytes);
+	bind();
+	void* mapped = glMapBufferRange(target, offset, size, GL_MAP_WRITE_BIT);
 	memcpy(mapped, buff, size);
 	glUnmapBuffer(target);
 	unbind();

+ 8 - 4
src/Renderer/BufferObjects/BufferObject.h

@@ -19,6 +19,8 @@ class BufferObject: public Object
 
 	PROPERTY_R(GLenum, usage, getBufferUsage) ///< GL_STREAM_DRAW or GL_STATIC_DRAW or GL_DYNAMIC_DRAW
 
+	PROPERTY_R(size_t, sizeInBytes, getSizeInBytes)
+
 	public:
 		/// Default constructor @see create
 		BufferObject(GLenum target, uint sizeInBytes, const void* dataPtr, GLenum usage, Object* parent = NULL);
@@ -36,11 +38,13 @@ class BufferObject: public Object
 		/// Throws exception if the given size and the BO size are not equal. It throws an exception if the usage is
 		/// GL_STATIC_DRAW
 		/// @param[in] buff The buffer to copy to BO
-		/// @param[in] size The size in bytes we want to write
-		void write(void* buff, size_t size);
+		void write(void* buff);
 
-	protected:
-		size_t sizeInBytes;
+		/// The same as the other write but it maps only a subset of the data
+		/// @param[in] buff The buffer to copy to BO
+		/// @param[in] offset The offset
+		/// @param[in] size The size in bytes we want to write
+		void write(void* buff, size_t offset, size_t size);
 
 	private:
 		/// Creates a new BO with the given parameters and checks if everything went OK. Throws exception if fails

+ 41 - 32
src/Renderer/Dbg.cpp

@@ -41,44 +41,48 @@ void Dbg::drawLine(const Vec3& from, const Vec3& to, const Vec4& color)
 //======================================================================================================================
 void Dbg::renderGrid()
 {
-	/*Vec4 col0(0.5, 0.5, 0.5, 1.0);
+	Vec4 col0(0.5, 0.5, 0.5, 1.0);
 	Vec4 col1(0.0, 0.0, 1.0, 1.0);
 	Vec4 col2(1.0, 0.0, 0.0, 1.0);
 
 	const float SPACE = 1.0; // space between lines
 	const int NUM = 57;  // lines number. must be odd
 
-	float OPT = ((NUM - 1) * SPACE / 2);
+	const float OPT = ((NUM - 1) * SPACE / 2);
 
-	Vec<Vec3> positions;
-	Vec<Vec4> colors;
+	begin();
 
-	for(int x=0; x<NUM; x++)
+	for(int x = 0; x < NUM; x++)
 	{
-		if(x == NUM / 2) // if the middle line then change color
-			colors.push_back(col1);
-		else if(x == (NUM / 2) + 1) // if the next line after the middle one change back to default col
-			colors.push_back(col0);
+		// if the middle line then change color
+		if(x == NUM / 2)
+		{
+			setColor(col1);
+		}
+		// if the next line after the middle one change back to default col
+		else if(x == (NUM / 2) + 1)
+		{
+			setColor(col0);
+		}
 
 		float opt1 = x * SPACE;
 		// line in z
-		positions.push_back(Vec3(opt1 - OPT, 0.0, -OPT));
-		positions.push_back(Vec3(opt1 - OPT, 0.0, OPT));
+		pushBackVertex(Vec3(opt1 - OPT, 0.0, -OPT));
+		pushBackVertex(Vec3(opt1 - OPT, 0.0, OPT));
 
-		if(x==NUM/2) // if middle line change col so you can highlight the x-axis
-			colors.push_back(col2);
+		// if middle line change col so you can highlight the x-axis
+		if(x == NUM / 2)
+		{
+			setColor(col2);
+		}
 
 		// line in the x
-		positions.push_back(Vec3(-OPT, 0.0, opt1 - OPT));
-		positions.push_back(Vec3(OPT, 0.0, opt1 - OPT));
+		pushBackVertex(Vec3(-OPT, 0.0, opt1 - OPT));
+		pushBackVertex(Vec3(OPT, 0.0, opt1 - OPT));
 	}
 
 	// render
-	setColor(col0);
-	glEnableVertexAttribArray(POSITION_ATTRIBUTE_ID);
-	glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, &positions[0]);
-	glDrawArrays(GL_LINES, 0, positions.size());
-	glDisableVertexAttribArray(POSITION_ATTRIBUTE_ID);*/
+	end();
 }
 
 
@@ -228,6 +232,13 @@ void Dbg::init(const RendererInitializer& initializer)
 	vao = new Vao(this);
 	const int positionAttribLoc = 0;
 	vao->attachArrayBufferVbo(*positionsVbo, positionAttribLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
+
+	//
+	// Rest
+	//
+	pointIndex = 0;
+	ON_GL_FAIL_THROW_EXCEPTION();
+	modelMat.setIdentity();
 }
 
 
@@ -245,16 +256,13 @@ void Dbg::run()
 
 	fbo.bind();
 	sProg->bind();
-	viewProjectionMat = cam.getProjectionMatrix() * cam.getViewMatrix();
 
 	// OGL stuff
 	Renderer::setViewport(0, 0, r.getWidth(), r.getHeight());
 	glEnable(GL_DEPTH_TEST);
 	glDisable(GL_BLEND);
 
-	sProg->bind();
-
-	//R::renderGrid();
+	renderGrid();
 	/// @todo Uncomment
 	/*for(uint i=0; i<app->getScene().nodes.size(); i++)
 	{
@@ -278,22 +286,20 @@ void Dbg::run()
 	}*/
 
 	// Physics
-	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+	/*glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 	setModelMat(Mat4::getIdentity());
 	app->getScene().getPhysics().debugDraw();
-	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);*/
 }
 
 
 //======================================================================================================================
 // setModelMat                                                                                                         =
 //======================================================================================================================
-void Dbg::setModelMat(const Mat4& modelMat)
+void Dbg::setModelMat(const Mat4& modelMat_)
 {
 	RASSERT_THROW_EXCEPTION(pointIndex != 0); // This means that the func called after begin and before end
-
-	Mat4 pmv = viewProjectionMat * modelMat;
-	sProg->findUniVar("modelViewProjectionMat")->setMat4(&pmv);
+	modelMat = modelMat_;
 }
 
 
@@ -313,8 +319,11 @@ void Dbg::end()
 {
 	RASSERT_THROW_EXCEPTION(pointIndex == 0);
 
-	positionsVbo->write(&positions[0], sizeof(Vec3) * pointIndex);
-	colorsVbo->write(&colors[0], sizeof(Vec3) * pointIndex);
+	positionsVbo->write(&positions[0], 0, sizeof(Vec3) * pointIndex);
+	colorsVbo->write(&colors[0], 0, sizeof(Vec3) * pointIndex);
+
+	Mat4 pmv = r.getViewProjectionMat() * modelMat;
+	sProg->findUniVar("modelViewProjectionMat")->setMat4(&pmv);
 
 	vao->bind();
 	glDrawArrays(GL_LINES, 0, pointIndex);

+ 5 - 6
src/Renderer/Dbg.h

@@ -1,6 +1,7 @@
 #ifndef DBG_H
 #define DBG_H
 
+#include <boost/array.hpp>
 #include "RenderingPass.h"
 #include "Fbo.h"
 #include "ShaderProg.h"
@@ -45,7 +46,6 @@ class Dbg: public RenderingPass
 		/// @}
 
 	private:
-		static const uint POSITION_ATTRIBUTE_ID = 0; ///< The glId of the attribute var for position in the dbg shader
 		bool enabled;
 		bool showAxisEnabled;
 		bool showLightsEnabled;
@@ -53,11 +53,10 @@ class Dbg: public RenderingPass
 		bool showCamerasEnabled;
 		Fbo fbo;
 		RsrcPtr<ShaderProg> sProg;
-		Mat4 viewProjectionMat;
-
-		static const uint MAX_POINTS_PER_DRAW = 100;
-		Vec3 positions[MAX_POINTS_PER_DRAW];
-		Vec3 colors[MAX_POINTS_PER_DRAW];
+		static const uint MAX_POINTS_PER_DRAW = 250;
+		boost::array<Vec3, MAX_POINTS_PER_DRAW> positions;
+		boost::array<Vec3, MAX_POINTS_PER_DRAW> colors;
+		Mat4 modelMat;
 		uint pointIndex;
 		Vec3 crntCol;
 		Vbo* positionsVbo;

+ 2 - 1
src/Renderer/Is.cpp

@@ -60,7 +60,8 @@ void Is::calcViewVectors()
 		// end of optimized code
 	}
 
-	viewVectorsVbo->write(viewVectors, sizeof(viewVectors));
+	RASSERT_THROW_EXCEPTION(sizeof(viewVectors) != viewVectorsVbo->getSizeInBytes());
+	viewVectorsVbo->write(viewVectors);
 }
 
 

+ 2 - 7
src/Renderer/MainRenderer.cpp

@@ -29,7 +29,7 @@ void MainRenderer::init(const RendererInitializer& initializer_)
 	initializer.width = app->getWindowWidth() * renderingQuality;
 	initializer.height = app->getWindowHeight() * renderingQuality;
 	Renderer::init(initializer);
-	//dbg.init(initializer);
+	dbg->init(initializer);
 }
 
 
@@ -51,11 +51,6 @@ void MainRenderer::initGl()
 	INFO("OpenGL info: OGL " + reinterpret_cast<const char*>(glGetString(GL_VERSION)) + ", GLSL " +
 	     reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION)));
 
-	if(!glewIsSupported("GL_VERSION_3_1"))
-	{
-		WARNING("OpenGL ver 3.1 not supported. The application may crash (and burn)");
-	}
-
 	// get max texture units
 	//glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxColorAtachments);
 	glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &Texture::textureUnitsNum);
@@ -93,7 +88,7 @@ void MainRenderer::initGl()
 void MainRenderer::render(Camera& cam_)
 {
 	Renderer::render(cam_);
-	//dbg.run();
+	dbg->run();
 
 	//
 	// Render the PPS FAI to the framebuffer

+ 2 - 1
src/Renderer/Smo.cpp

@@ -130,7 +130,8 @@ void Smo::run(const SpotLight& light)
 	};
 
 	// render camera shape to stencil buffer
-	cameraPositionsVbo->write(vertPositions, sizeof(vertPositions));
+	RASSERT_THROW_EXCEPTION(sizeof(vertPositions) != cameraPositionsVbo->getSizeInBytes());
+	cameraPositionsVbo->write(vertPositions);
 	cameraVao->bind();
 	glDrawElements(GL_TRIANGLES, 6 * 3, GL_UNSIGNED_SHORT, 0);
 	cameraVao->unbind();