From 6a3cd6decd025dec6126a9fa18ca0355ba22713c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mikl=C3=B3s=20T=C3=B3th?= <tothmiklostibor@gmail.com>
Date: Thu, 15 Apr 2021 18:16:11 +0200
Subject: [PATCH] forog

---
 Skeleton.cpp | 43 ++++++++++++++++++++++++++++++-------------
 1 file changed, 30 insertions(+), 13 deletions(-)

diff --git a/Skeleton.cpp b/Skeleton.cpp
index 1e496a6..63829ab 100644
--- a/Skeleton.cpp
+++ b/Skeleton.cpp
@@ -234,8 +234,8 @@ public:
 	bool shadowIntersect(Ray ray) {	// for directional lights
 		for (Intersectable * object : objects) {
             Hit hit = object->intersect(ray);
-            if (hit.t > 0)
-                if(hit.t < length(hit.position - ray.start))
+            if (hit.t < 0)
+                if(hit.t > length(hit.position - ray.start))
                     return true;
         }
 		return false;
@@ -296,10 +296,15 @@ const char *fragmentSource = R"(
 
 class FullScreenTexturedQuad {
 	unsigned int vao = 0;	// vertex array object id and texture id
+    int wW, wH;
 	Texture texture;
 public:
+    void loadImage(const std::vector<vec4>& image) {
+        texture.create(wW, wH, image);
+    }
+    
 	FullScreenTexturedQuad(int windowWidth, int windowHeight, std::vector<vec4>& image)
-		: texture(windowWidth, windowHeight, image) 
+		: wW(windowWidth), wH(windowHeight), texture(windowWidth, windowHeight, image)
 	{
 		glGenVertexArrays(1, &vao);	// create 1 vertex array object
 		glBindVertexArray(vao);		// make it active
@@ -328,22 +333,32 @@ FullScreenTexturedQuad * fullScreenTexturedQuad;
 void onInitialization() {
 	glViewport(0, 0, windowWidth, windowHeight);
 	scene.build();
-
-	std::vector<vec4> image(windowWidth * windowHeight);
-	long timeStart = glutGet(GLUT_ELAPSED_TIME);
-	scene.render(image);
-	long timeEnd = glutGet(GLUT_ELAPSED_TIME);
-	printf("Rendering time: %d milliseconds\n", (timeEnd - timeStart));
-
-	// copy image to GPU as a texture
-	fullScreenTexturedQuad = new FullScreenTexturedQuad(windowWidth, windowHeight, image);
-
+    
+    std::vector<vec4> image(windowWidth * windowHeight);
+    
+    long timeStart = glutGet(GLUT_ELAPSED_TIME);
+    scene.render(image);
+    long timeEnd = glutGet(GLUT_ELAPSED_TIME);
+    printf("Rendering time: %d milliseconds\n", (timeEnd - timeStart));
+    
+    // copy image to GPU as a texture
+    fullScreenTexturedQuad = new FullScreenTexturedQuad(windowWidth, windowHeight, image);
+    
 	// create program for the GPU
 	gpuProgram.create(vertexSource, fragmentSource, "fragmentColor");
 }
 
 // Window has become invalid: Redraw
 void onDisplay() {
+    std::vector<vec4> image(windowWidth * windowHeight);
+    
+    long timeStart = glutGet(GLUT_ELAPSED_TIME);
+    scene.render(image);
+    long timeEnd = glutGet(GLUT_ELAPSED_TIME);
+    printf("Rendering time: %d milliseconds\n", (timeEnd - timeStart));
+    
+    fullScreenTexturedQuad->loadImage(image);
+    
 	fullScreenTexturedQuad->Draw();
 	glutSwapBuffers();									// exchange the two buffers
 }
@@ -367,4 +382,6 @@ void onMouseMotion(int pX, int pY) {
 
 // Idle event indicating that some time elapsed: do animation here
 void onIdle() {
+    scene.Animate(.1f);
+    glutPostRedisplay();
 }
-- 
GitLab