Browse files

Light update

  • Loading branch information...
1 parent fa8564e commit a743b50962c59437622d658eff7c661abb22699f @eatfears committed Feb 12, 2012
Showing with 61 additions and 22 deletions.
  1. +1 −2 blocks/Blocks_Definitions.h
  2. +9 −7 blocks/Chunk.cpp
  3. +1 −1 blocks/Chunk.h
  4. +40 −12 blocks/Engine.cpp
  5. +3 −0 blocks/Light.cpp
  6. +3 −0 blocks/World.cpp
  7. +4 −0 blocks/World.h
View
3 blocks/Blocks_Definitions.h
@@ -23,7 +23,7 @@
#define PLAYER_HEIGHT 15.0
#define AIR_ACCEL 0.12
#define JUMP_STR 4.0
-#define WALK_SPEED 1.5
+#define WALK_SPEED 1000
#define SPRINT_KOEF 2.0
@@ -52,7 +52,6 @@ typedef signed short BlockInWorld;
#define GL_EXECUTE 0x1302
#define RENDER_NO_NEED 0
#define RENDER_NEED 1
-#define RENDER_TRY 2
#define HASH_SIZE 0x1000
View
16 blocks/Chunk.cpp
@@ -207,9 +207,9 @@ void Chunk::DrawLoadedBlocks()
void Chunk::Generate()
{
- wWorld.lLandscape.Generate(*this);
+ //wWorld.lLandscape.Generate(*this);
//wWorld.lLandscape.Fill(*this, 0, 0.999, 64);
- //wWorld.lLandscape.Fill(*this, MAT_DIRT, 1, 64);
+ wWorld.lLandscape.Fill(*this, MAT_DIRT, 1, 64);
//ChunkPosition pos = {x, z};
//wWorld.lLandscape.Load(pos);
}
@@ -240,7 +240,7 @@ void Chunk::FillSkyLight(char bright)
}
}
-void Chunk::Render(GLenum mode, char mat)
+void Chunk::Render(GLenum mode, char mat, int *rendered)
{
if(!listgen)
{
@@ -257,9 +257,12 @@ void Chunk::Render(GLenum mode, char mat)
if(NeedToRender[pointertorender] == RENDER_NEED)
{
mode = GL_COMPILE;
- NeedToRender[pointertorender] = RENDER_TRY;
}
+ int prob = 1000/(*rendered + 1);
+ int r = rand()%1000;
+
+ if(r <= prob*2)
if((mode == GL_COMPILE)||(mode == GL_COMPILE_AND_EXECUTE))
{
glNewList(RenderList + pointertorender, mode);
@@ -306,9 +309,8 @@ void Chunk::Render(GLenum mode, char mat)
glEndList();
-
- if(NeedToRender[pointertorender] == RENDER_TRY)
- NeedToRender[pointertorender] = RENDER_NO_NEED;
+ (*rendered) ++;
+ NeedToRender[pointertorender] = RENDER_NO_NEED;
}
if(mode != GL_COMPILE_AND_EXECUTE)
View
2 blocks/Chunk.h
@@ -51,7 +51,7 @@ class Chunk
void Generate();
void FillSkyLight(char bright);
- void Render(GLenum mode, char mat);
+ void Render(GLenum mode, char mat, int *rendered);
void DrawTile(BlockInWorld sXcoord, BlockInWorld sYcoord, BlockInWorld sZcoord, Block* block, char N);
HANDLE mutex;
View
52 blocks/Engine.cpp
@@ -1,6 +1,8 @@
#include "Engine.h"
#include <time.h>
#include <Mmsystem.h>
+#define _USE_MATH_DEFINES
+#include <math.h>
#include "Light.h"
#include "Primes.h"
@@ -108,17 +110,24 @@ void Engine::Display()
static GLuint *tex = wWorld.MaterialLib.texture;
glBindTexture(GL_TEXTURE_2D, tex[TERRAIN]);
+ int render;
-
- GLenum mod = GL_EXECUTE;
if(player.bKeyboard['Z'])
{
player.bKeyboard['Z'] = 0;
wWorld.SoftLight = !wWorld.SoftLight;
+ wWorld.LightRefresh = true;
+ }
+
+ GLenum mod = GL_EXECUTE;
+ if(wWorld.LightRefresh)
+ {
+ wWorld.LightRefresh = false;
mod = GL_COMPILE;
}
+ render = 0;
for (int bin = 0; bin < HASH_SIZE; bin++)
{
auto chunk = wWorld.Chunks[bin].begin();
@@ -134,8 +143,9 @@ void Engine::Display()
_try
{
#endif // _DEBUG
-
- (*chunk)->Render(mod, MAT_NO);
+ if (mod == GL_COMPILE)
+ (*chunk)->NeedToRender[0] = RENDER_NEED;
+ (*chunk)->Render(GL_EXECUTE, MAT_NO, &render);
++chunk;
#ifndef _DEBUG
@@ -154,7 +164,7 @@ void Engine::Display()
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//transparent tiles here
-
+ render = 0;
for (int bin = 0; bin < HASH_SIZE; bin++)
{
auto chunk = wWorld.Chunks[bin].begin();
@@ -164,8 +174,10 @@ void Engine::Display()
_try
{
#endif // _DEBUG
+ if (mod == GL_COMPILE)
+ (*chunk)->NeedToRender[1] = RENDER_NEED;
- (*chunk)->Render(mod, MAT_WATER);
+ (*chunk)->Render(GL_EXECUTE, MAT_WATER, &render);
++chunk;
#ifndef _DEBUG
@@ -430,10 +442,10 @@ void Engine::Loop()
void Engine::GetFrameTime()
{
- static double koef = 0.025;
+ static double koef = 0.0005;
static double max_FPS = 30;
static int sleep_time;
-
+
double currentTime = (double)timeGetTime();
static double frameTime = currentTime; // Âðåìÿ ïîñëåäíåãî êàäðà
@@ -581,7 +593,6 @@ void Engine::DrawSunMoon()
//glDisable(GL_LIGHT2);
//glDisable(GL_LIGHTING);
-
}
void Engine::DrawClouds()
@@ -622,25 +633,42 @@ void Engine::DrawClouds()
void Engine::GetFogColor()
{
static double Dawn = 100.0;
+ static float NightBright = 0.93f;
+ static float DayBright = 0.00f;
if ((TimeOfDay > 600.0 + Dawn)&&(TimeOfDay < 1800.0 - Dawn))
{
for(int i = 0; i < 4; i++)
FogColor[i] = DayFogColor[i];
+ wWorld.SkyBright = DayBright;
}
else if ((TimeOfDay < 600.0 - Dawn)||(TimeOfDay > 1800.0 + Dawn))
{
for(int i = 0; i < 4; i++)
FogColor[i] = NightFogColor[i];
+
+ wWorld.SkyBright = NightBright;
}
else
{
- double ft = (TimeOfDay - (600.0 - Dawn))*3.14 / (2.0 * Dawn);
- double f = (1.0 - cos(ft)) * 0.5;
+ GLfloat ft = (TimeOfDay - (600.0f - Dawn))*M_PI / (2.0 * Dawn);
+ GLfloat f = (1.0f - cos(ft)) * 0.5f;
if(TimeOfDay > 1200.0) f = 1.0 - f;
for(int i = 0; i < 4; i++)
- FogColor[i] = NightFogColor[i]*(1.0 - f) + DayFogColor[i]*f;
+ FogColor[i] = NightFogColor[i]*(1.0f - f) + DayFogColor[i]*f;
+
+ wWorld.SkyBright = NightBright*(1.0f - f) + DayBright * f;
}
+
+ static GLfloat prevBright = 0;
+ GLfloat dif = fabs(wWorld.SkyBright - prevBright);
+ if(dif > 0.005f)
+ {
+ wWorld.LightRefresh = true;
+ prevBright = wWorld.SkyBright;
+ }
}
+
+//0.069f, 0.086f, 0.107f, 0.134f,
View
3 blocks/Light.cpp
@@ -204,6 +204,8 @@ void Light::BlockLight(World& wWorld, Chunk& chunk, int side, BlockInChunk cx, B
}else res = 1.0f;
if ((side == FRONT)||(side == BACK)) res *= 0.85f;
if ((side == RIGHT)||(side == LEFT)) res *= 0.90f;
+
+ res = res - wWorld.SkyBright;
glColor3f(res, res, res);
}
}
@@ -272,6 +274,7 @@ void Light::SoftLight(World& wWorld, BlockInWorld X, BlockInWorld Y, BlockInWorl
}
if ((side == FRONT)||(side == BACK)) res *= 0.85f;
if ((side == RIGHT)||(side == LEFT)) res *= 0.90f;
+ res = res - wWorld.SkyBright;
glColor3f(res, res, res);
}
}
View
3 blocks/World.cpp
@@ -14,6 +14,9 @@ World::World()
mutex = CreateMutex(NULL, false, NULL);
semaphore = CreateSemaphore(NULL, 10, 10, NULL);
SoftLight = true;
+
+ SkyBright = 1.0f;
+ LightRefresh = true;
}
World::~World()
View
4 blocks/World.h
@@ -51,4 +51,8 @@ class World
void UpdateLight(Chunk& chunk);
bool SoftLight;
+
+ GLfloat SkyBright;
+
+ bool LightRefresh;
};

0 comments on commit a743b50

Please sign in to comment.