Просмотр исходного кода

Added scissor support to the new renderer, made entities work with new scissor system

Ivan Safrin 10 лет назад
Родитель
Сommit
eb77d6dfc1

+ 4 - 4
include/polycode/core/PolyEntity.h

@@ -97,9 +97,9 @@ namespace Polycode {
 			virtual void Update(){};			
 			virtual void fixedUpdate(){};
         
-            void transformAndRender(GPUDrawBuffer *drawBuffer);
+            void transformAndRender(GPUDrawBuffer *drawBuffer, Polycode::Rectangle *parentScissorBox);
 
-			void renderChildren(GPUDrawBuffer *buffer);
+			void renderChildren(GPUDrawBuffer *buffer, Polycode::Rectangle *parentScissorBox);
 		
 			
 			/**
@@ -890,14 +890,14 @@ namespace Polycode {
 
             std::vector <EntityProp> entityProps;
         
-            GPUDrawCall drawCall;
-        
             void setContainerScene(Scene *scene);
     
             Scene *getContainerScene();
         
 		protected:
 
+            GPUDrawCall drawCall;
+        
             Scene *containerScene;
         
             AABB aabb;

+ 3 - 0
include/polycode/core/PolyGPUDrawBuffer.h

@@ -39,6 +39,8 @@ namespace Polycode {
         bool backfaceCull;
         bool depthOnly;
         unsigned int blendingMode;
+        Polycode::Rectangle scissorBox;
+        bool enableScissor;
         Color drawColor;
     };
     
@@ -68,6 +70,7 @@ namespace Polycode {
         Color clearColor;
         bool clearDepthBuffer;
         bool clearColorBuffer;
+        Vector2 backingResolutionScale;
         
         Polycode::Rectangle viewport;
         std::vector<GPUDrawCall> drawCalls;

+ 3 - 0
include/polycode/core/PolyOpenGLGraphicsInterface.h

@@ -85,6 +85,9 @@ namespace Polycode {
         void enableBackfaceCulling(bool val);
         void setLineSize(Number lineSize);
         
+        void enableScissor(bool val);
+        void setScissorBox(const Polycode::Rectangle &box);
+        
 	protected:
 		
         GLuint currentShaderID;

+ 3 - 0
include/polycode/core/PolyRenderer.h

@@ -64,6 +64,9 @@ namespace Polycode {
             virtual void enableBackfaceCulling(bool val) = 0;
             virtual void setLineSize(Number lineSize) = 0;
         
+            virtual void enableScissor(bool val) = 0;
+            virtual void setScissorBox(const Polycode::Rectangle &box) = 0;
+        
             virtual void beginDrawCall() = 0;
             virtual void endDrawCall() = 0;
     };

+ 21 - 36
src/core/PolyEntity.cpp

@@ -27,7 +27,7 @@
 using namespace Polycode;
 
 
-int Entity::defaultBlendingMode = Renderer::BLEND_MODE_NONE;
+int Entity::defaultBlendingMode = Renderer::BLEND_MODE_NORMAL;
 
 Rotation::Rotation() {
 	pitch = 0;
@@ -487,7 +487,7 @@ Scene *Entity::getContainerScene() {
     }
 }
 
-void Entity::transformAndRender(GPUDrawBuffer *buffer) {
+void Entity::transformAndRender(GPUDrawBuffer *buffer, Polycode::Rectangle *parentScissorBox) {
 	if(!renderer || !enabled)
 		return;
 
@@ -495,27 +495,26 @@ void Entity::transformAndRender(GPUDrawBuffer *buffer) {
 		rebuildTransformMatrix();
     }
     
+
+    Polycode::Rectangle finalScissorBox = scissorBox;
+    Polycode::Rectangle *scissorBoxForChildren = parentScissorBox;
     
-	
-    /*
-	bool isScissorEnabled;
-	Polycode::Rectangle oldScissorBox;
-	
 	if(enableScissor) {
-		isScissorEnabled = renderer->isScissorEnabled();
-		oldScissorBox = renderer->getScissorBox();
-		renderer->enableScissor(true);
-
-		Rectangle finalScissorBox = scissorBox;
-
+		finalScissorBox = scissorBox;
 		// make sure that our scissor box is constrained to the parent one if it exists
-		if(isScissorEnabled) {
-			finalScissorBox = finalScissorBox.Clipped(renderer->getScissorBox());
+		if(parentScissorBox) {
+			finalScissorBox = finalScissorBox.Clipped(*parentScissorBox);
 		}
+        drawCall.options.enableScissor = true;
+        drawCall.options.scissorBox = finalScissorBox;
+        scissorBoxForChildren = &finalScissorBox;
+    } else if(parentScissorBox){
+        drawCall.options.enableScissor = true;
+        drawCall.options.scissorBox = *parentScissorBox;
+    } else {
+        drawCall.options.enableScissor = false;
+    }
 
-		renderer->setScissorBox(finalScissorBox);
-	}
-     */
     
     drawCall.modelMatrix.identity();
     drawCall.modelMatrix.Translate(-anchorPoint.x * bBox.x * 0.5, -anchorPoint.y * bBox.y * 0.5 * yAdjust, -anchorPoint.z * bBox.z * 0.5);
@@ -545,23 +544,9 @@ void Entity::transformAndRender(GPUDrawBuffer *buffer) {
 	}
     
     if(visible || (!visible && !visibilityAffectsChildren)) {
-        renderChildren(buffer);
+        renderChildren(buffer, scissorBoxForChildren);
     }
-/*
-    renderer->popVertexColor();
-    
-	renderer->popMatrix();
-	
-	
-	if(depthOnly) {
-		renderer->drawToColorBuffer(true);
-	}	
-	
-	if(enableScissor) {
-		renderer->enableScissor(isScissorEnabled);
-		renderer->setScissorBox(oldScissorBox);
-	}
-*/
+
 }
 
 void Entity::setRenderer(Renderer *renderer) {
@@ -572,9 +557,9 @@ void Entity::setRenderer(Renderer *renderer) {
 }
 
 
-void Entity::renderChildren(GPUDrawBuffer *buffer) {
+void Entity::renderChildren(GPUDrawBuffer *buffer, Polycode::Rectangle *parentScissorBox) {
 	for(int i=0;i<children.size();i++) {
-		children[i]->transformAndRender(buffer);
+		children[i]->transformAndRender(buffer, parentScissorBox);
 	}
 }
 

+ 13 - 0
src/core/PolyOpenGLGraphicsInterface.cpp

@@ -190,6 +190,19 @@ void OpenGLGraphicsInterface::disableAttribute(Shader *shader, const ProgramAttr
     glDisableVertexAttribArray(attribLocation);
 }
 
+void OpenGLGraphicsInterface::enableScissor(bool val) {
+    if(val) {
+        glEnable(GL_SCISSOR_TEST);
+    } else {
+        glDisable(GL_SCISSOR_TEST);
+    }
+}
+
+void OpenGLGraphicsInterface::setScissorBox(const Polycode::Rectangle &box) {
+    glScissor(box.x, box.y, box.w, box.h);
+    
+}
+
 GLenum OpenGLGraphicsInterface::getGLDrawMode(int polycodeMode) {
     switch(polycodeMode) {
         case Mesh::POINT_MESH:

+ 16 - 0
src/core/PolyRenderer.cpp

@@ -77,6 +77,20 @@ void RenderThread::processDrawBuffer(GPUDrawBuffer *buffer) {
     for(int i=0; i < buffer->drawCalls.size(); i++) {
         
         
+        if(buffer->drawCalls[i].options.enableScissor) {
+            graphicsInterface->enableScissor(true);
+            Polycode::Rectangle scissorBox = buffer->drawCalls[i].options.scissorBox;
+            
+            scissorBox.x *= buffer->backingResolutionScale.x;
+            scissorBox.w *= buffer->backingResolutionScale.x;
+            scissorBox.h *= buffer->backingResolutionScale.y;
+            scissorBox.y = ((buffer->viewport.h*buffer->backingResolutionScale.y)-(scissorBox.y*buffer->backingResolutionScale.y))-scissorBox.h;
+            
+            graphicsInterface->setScissorBox(scissorBox);
+        } else {
+            graphicsInterface->enableScissor(false);
+        }
+        
         graphicsInterface->enableDepthTest(buffer->drawCalls[i].options.depthTest);
         graphicsInterface->enableDepthWrite(buffer->drawCalls[i].options.depthWrite);
         graphicsInterface->enableBackfaceCulling(buffer->drawCalls[i].options.backfaceCull);
@@ -330,6 +344,8 @@ Cubemap *Renderer::createCubemap(Texture *t0, Texture *t1, Texture *t2, Texture
 }
 
 void Renderer::processDrawBuffer(GPUDrawBuffer *buffer) {
+    buffer->backingResolutionScale.x = backingResolutionScaleX;
+    buffer->backingResolutionScale.y = backingResolutionScaleY;
     renderThread->enqueueJob(RenderThread::JOB_PROCESS_DRAW_BUFFER, buffer);
 }
 

+ 1 - 1
src/core/PolyScene.cpp

@@ -295,7 +295,7 @@ void Scene::Render(Camera *targetCamera, Texture *targetFramebuffer) {
         setEntityVisibility(&rootEntity, targetCamera);
     }
          */
-	rootEntity.transformAndRender(drawBuffer);
+	rootEntity.transformAndRender(drawBuffer, NULL);
 
     
     renderer->processDrawBuffer(drawBuffer);