16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-18 00:49:19 +02:00

vbo changes, merge

This commit is contained in:
VB
2017-03-09 20:16:54 +01:00
16 changed files with 376 additions and 126 deletions

View File

@@ -62,6 +62,7 @@ set(SOURCES
"Sound.cpp"
"Spring.cpp"
"shader.cpp"
"frustum.cpp"
)
if (WIN32)

View File

@@ -53,7 +53,7 @@ void TCamera::OnCursorMove(double x, double y)
Yaw += 2 * M_PI;
if (Type == tp_Follow) // jeżeli jazda z pojazdem
{
Fix(Pitch, -M_PI_4, M_PI_4); // ograniczenie kąta spoglądania w dół i w górę
clamp(Pitch, -M_PI_4, M_PI_4); // ograniczenie kąta spoglądania w dół i w górę
// Fix(Yaw,-M_PI,M_PI);
}
}
@@ -169,3 +169,17 @@ void TCamera::Stop()
Type = tp_Follow;
Velocity = vector3(0, 0, 0);
};
// returns true if specified object is within camera frustum, false otherwise
bool
TCamera::IsVisible( TDynamicObject const *Dynamic ) const {
// sphere test is faster than AABB, so we'll use it here
float3 diagonal(
Dynamic->MoverParameters->Dim.L,
Dynamic->MoverParameters->Dim.H,
Dynamic->MoverParameters->Dim.W );
float const radius = static_cast<float>(diagonal.Length()) * 0.5f;
return ( m_frustum.sphere_inside( Dynamic->GetPosition(), radius ) > 0.0f );
}

View File

@@ -11,6 +11,9 @@ http://mozilla.org/MPL/2.0/.
#define CameraH
#include "dumb3d.h"
#include "frustum.h"
#include "dynobj.h"
using namespace Math3D;
//---------------------------------------------------------------------------
@@ -25,6 +28,8 @@ class TCamera
{
private:
vector3 pOffset; // nie używane (zerowe)
cFrustum m_frustum;
public: // McZapkie: potrzebuje do kiwania na boki
double Pitch;
double Yaw; // w środku: 0=do przodu; na zewnątrz: 0=na południe
@@ -51,6 +56,11 @@ class TCamera
void SetCabMatrix(vector3 &p);
void RaLook();
void Stop();
inline
void
SetFrustum() { m_frustum.calculate(); }
bool
IsVisible( TDynamicObject const *Dynamic ) const;
// bool GetMatrix(matrix4x4 &Matrix);
vector3 PtNext, PtPrev;
};

View File

@@ -3703,6 +3703,7 @@ void TDynamicObject::TurnOff()
void TDynamicObject::Render()
{ // rysowanie elementów nieprzezroczystych
// youBy - sprawdzamy, czy jest sens renderowac
/*
double modelrotate;
vector3 tempangle;
// zmienne
@@ -3713,8 +3714,7 @@ void TDynamicObject::Render()
if (ObjSqrDist < 500) // jak jest blisko - do 70m
modelrotate = 0.01; // mały kąt, żeby nie znikało
else
{ // Global::pCameraRotation to kąt bewzględny w świecie (zero - na
// północ)
{ // Global::pCameraRotation to kąt bewzględny w świecie (zero - na północ)
tempangle = (vPosition - Global::pCameraPosition); // wektor od kamery
modelrotate = ABuAcos(tempangle); // określenie kąta
// if (modelrotate>M_PI) modelrotate-=(2*M_PI);
@@ -3730,8 +3730,8 @@ void TDynamicObject::Render()
if (modelrotate < maxrot)
renderme = true;
if (renderme)
*/
if (Global::pCamera->IsVisible(this))
{
TSubModel::iInstance = (size_t)this; //żeby nie robić cudzych animacji
// AnsiString asLoadName="";

View File

@@ -80,6 +80,7 @@ void window_resize_callback(GLFWwindow *window, int w, int h)
{
Global::ScreenWidth = w;
Global::ScreenHeight = h;
Global::fDistanceFactor = std::max( 0.5f, h / 768.0f ); // not sure if this is really something we want to use
glViewport(0, 0, w, h);
}

View File

@@ -118,7 +118,7 @@ int Global::iHiddenEvents = 1; // czy łączyć eventy z torami poprzez nazwę t
int Global::Keys[MaxKeys];
int Global::iWindowWidth = 800;
int Global::iWindowHeight = 600;
float Global::fDistanceFactor = 768.0; // baza do przeliczania odległości dla LoD
float Global::fDistanceFactor = Global::ScreenHeight / 768.0; // baza do przeliczania odległości dla LoD
int Global::iFeedbackMode = 1; // tryb pracy informacji zwrotnej
int Global::iFeedbackPort = 0; // dodatkowy adres dla informacji zwrotnych
bool Global::bFreeFly = false;
@@ -152,7 +152,8 @@ bool Global::bSmoothTraction = false; // wygładzanie drutów starym sposobem
std::string Global::szDefaultExt = Global::szTexturesDDS; // domyślnie od DDS
int Global::iMultisampling = 2; // tryb antyaliasingu: 0=brak,1=2px,2=4px,3=8px,4=16px
bool Global::bGlutFont = false; // czy tekst generowany przez GLUT32.DLL
int Global::iConvertModels = 7; // tworzenie plików binarnych, +2-optymalizacja transformów
//int Global::iConvertModels = 7; // tworzenie plików binarnych, +2-optymalizacja transformów
int Global::iConvertModels{ 0 }; // temporary override, to prevent generation of .e3d not compatible with old exe
int Global::iSlowMotionMask = -1; // maska wyłączanych właściwości dla zwiększenia FPS
int Global::iModifyTGA = 7; // czy korygować pliki TGA dla szybszego wczytywania
// bool Global::bTerrainCompact=true; //czy zapisać teren w pliku
@@ -492,9 +493,7 @@ void Global::ConfigParse(cParser &Parser)
{
Parser.getTokens();
Parser >> token;
Global::bUseVBO = (token == "yes");
Parser >> Global::bUseVBO;
}
else if (token == "feedbackmode")
{
@@ -618,6 +617,11 @@ void Global::ConfigParse(cParser &Parser)
// tworzenie plików binarnych
Parser.getTokens(1, false);
Parser >> Global::iConvertModels;
// temporary override, to prevent generation of .e3d not compatible with old exe
Global::iConvertModels =
( Global::iConvertModels > 128 ?
Global::iConvertModels - 128 :
0 );
}
else if (token == "inactivepause")
{

View File

@@ -74,7 +74,7 @@ void TSubModel::FirstInit()
// Hits=NULL;
// CollisionPts=NULL;
// CollisionPtsCount=0;
Opacity = 1.0; // przy wczytywaniu modeli było dzielone przez 100...
Opacity = 0.0f; // przy wczytywaniu modeli było dzielone przez 100...
bWire = false;
fWireSize = 0;
fNearAttenStart = 40;
@@ -376,13 +376,10 @@ int TSubModel::Load(cParser &parser, TModel3d *Model, int Pos, bool dynamic)
TextureID = GfxRenderer.GetTextureId( texture, szTexturePath );
// TexAlpha=TTexturesManager::GetAlpha(TextureID);
// iFlags|=TexAlpha?0x20:0x10; //0x10-nieprzezroczysta, 0x20-przezroczysta
if (Opacity < 1.0) // przezroczystość z tekstury brana tylko dla Opacity
// 0!
iFlags |= GfxRenderer.Texture(TextureID).has_alpha ?
iFlags |=
( GfxRenderer.Texture(TextureID).has_alpha ?
0x20 :
0x10; // 0x10-nieprzezroczysta, 0x20-przezroczysta
else
iFlags |= 0x10; // normalnie nieprzezroczyste
0x10 ); // 0x10-nieprzezroczysta, 0x20-przezroczysta
// renderowanie w cyklu przezroczystych tylko jeśli:
// 1. Opacity=0 (przejściowo <1, czy tam <100) oraz
// 2. tekstura ma przezroczystość
@@ -391,19 +388,19 @@ int TSubModel::Load(cParser &parser, TModel3d *Model, int Pos, bool dynamic)
else
iFlags |= 0x10;
// visibility range
std::string discard;
parser.getTokens(5, false);
parser >> discard >> fSquareMaxDist >> discard >> fSquareMinDist >> discard;
if (fSquareMaxDist >= 0.0)
{
fSquareMaxDist *= fSquareMaxDist;
if( fSquareMaxDist <= 0.0 ) {
// 15km to więcej, niż się obecnie wyświetla
fSquareMaxDist = 15000.0;
}
else
{
fSquareMaxDist = 15000 * 15000;
} // 15km to więcej, niż się obecnie wyświetla
fSquareMaxDist *= fSquareMaxDist;
fSquareMinDist *= fSquareMinDist;
// transformation matrix
fMatrix = new float4x4();
readMatrix(parser, *fMatrix); // wczytanie transform
if (!fMatrix->IdentityIs())
@@ -688,7 +685,7 @@ void TSubModel::DisplayLists()
glColorMaterial(GL_FRONT, GL_EMISSION);
glDisable(GL_LIGHTING); // Tolaris-030603: bo mu punkty swiecace sie blendowaly
glBegin(GL_POINTS);
glVertex3f( 0.0f, 0.0f, -0.025f ); // shift point towards the viewer, to avoid z-fighting with the light polygons
glVertex3f( 0.0f, 0.0f, -0.05f ); // shift point towards the viewer, to avoid z-fighting with the light polygons
glEnd();
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
@@ -1053,7 +1050,9 @@ void TSubModel::RaAnimation(TAnimType a)
void TSubModel::RenderDL()
{ // główna procedura renderowania przez DL
if (iVisible && (fSquareDist >= fSquareMinDist) && (fSquareDist < fSquareMaxDist))
if( ( iVisible )
&& ( fSquareDist >= (fSquareMinDist / Global::fDistanceFactor) )
&& ( fSquareDist <= (fSquareMaxDist * Global::fDistanceFactor) ) )
{
if (iFlags & 0xC000)
{
@@ -1150,7 +1149,9 @@ void TSubModel::RenderDL()
void TSubModel::RenderAlphaDL()
{ // renderowanie przezroczystych przez DL
if (iVisible && (fSquareDist >= fSquareMinDist) && (fSquareDist < fSquareMaxDist))
if( ( iVisible )
&& ( fSquareDist >= (fSquareMinDist / Global::fDistanceFactor) )
&& ( fSquareDist <= (fSquareMaxDist * Global::fDistanceFactor) ) )
{
if (iFlags & 0xC000)
{
@@ -1230,7 +1231,9 @@ void TSubModel::RenderAlphaDL()
void TSubModel::RenderVBO()
{ // główna procedura renderowania przez VBO
if (iVisible && (fSquareDist >= fSquareMinDist) && (fSquareDist < fSquareMaxDist))
if( ( iVisible )
&& ( fSquareDist >= (fSquareMinDist / Global::fDistanceFactor) )
&& ( fSquareDist <= (fSquareMaxDist * Global::fDistanceFactor) ) )
{
if (iFlags & 0xC000)
{
@@ -1403,7 +1406,9 @@ void TSubModel::RenderVBO()
void TSubModel::RenderAlphaVBO()
{ // renderowanie przezroczystych przez VBO
if (iVisible && (fSquareDist >= fSquareMinDist) && (fSquareDist < fSquareMaxDist))
if( ( iVisible )
&& ( fSquareDist >= (fSquareMinDist / Global::fDistanceFactor) )
&& ( fSquareDist <= (fSquareMaxDist * Global::fDistanceFactor) ) )
{
if (iFlags & 0xC000)
{
@@ -1478,13 +1483,15 @@ void TSubModel::RaArrayFill(CVertNormTex *Vert)
Next->RaArrayFill(Vert);
};
// NOTE: leftover from static distance factor adjustment.
// TODO: get rid of it, once we have the dynamic adjustment code in place
void TSubModel::AdjustDist()
{ // aktualizacja odległości faz LoD, zależna od
// rozdzielczości pionowej oraz multisamplingu
if (fSquareMaxDist > 0.0)
fSquareMaxDist *= Global::fDistanceFactor;
if (fSquareMinDist > 0.0)
fSquareMinDist *= Global::fDistanceFactor;
fSquareMinDist /= Global::fDistanceFactor;
// if (fNearAttenStart>0.0) fNearAttenStart*=Global::fDistanceFactor;
// if (fNearAttenEnd>0.0) fNearAttenEnd*=Global::fDistanceFactor;
if (Child)
@@ -2120,10 +2127,13 @@ void TModel3d::Init()
}
if (iNumVerts)
{
/* // NOTE: we will be applying distance factor dynamically during render,
// so we're leaving the defined ranges intact
if (Global::fDistanceFactor !=
1.0) // trochę zaoszczędzi czasu na modelach z wieloma submocelami
Root->AdjustDist(); // aktualizacja odległości faz LoD, zależnie od
// rozdzielczości pionowej oraz multisamplingu
*/
if (Global::bUseVBO)
{
if (!m_pVNT) // jeśli nie ma jeszcze tablicy (wczytano z pliku

View File

@@ -19,6 +19,7 @@ http://mozilla.org/MPL/2.0/.
//#include "math.h"
#include "Timer.h"
#include "mczapkie/mctools.h"
#include "usefull.h"
TRealSound::TRealSound(std::string const &SoundName, double SoundAttenuation, double X, double Y, double Z, bool Dynamic,
bool freqmod, double rmin)
@@ -177,7 +178,7 @@ void TRealSound::AdjFreq(double Freq, double dt) // McZapkie TODO: dorobic tu ef
// Freq moze byc liczba dodatnia mniejsza od 1 lub wieksza od 1
{
float df, Vlist;
if ((Global::bSoundEnabled) && (AM != 0))
if ((Global::bSoundEnabled) && (AM != 0) && (pSound != nullptr))
{
if (dt > 0)
// efekt Dopplera
@@ -190,9 +191,7 @@ void TRealSound::AdjFreq(double Freq, double dt) // McZapkie TODO: dorobic tu ef
if (Timer::GetSoundTimer())
{
df = fFrequency * df; // TODO - brac czestotliwosc probkowania z wav
pSound->SetFrequency((df < DSBFREQUENCY_MIN ?
DSBFREQUENCY_MIN :
(df > DSBFREQUENCY_MAX ? DSBFREQUENCY_MAX : df)));
pSound->SetFrequency( clamp( df, static_cast<float>(DSBFREQUENCY_MIN), static_cast<float>(DSBFREQUENCY_MAX) ) );
}
}
}

View File

@@ -141,7 +141,9 @@ class TSegment
}
int RaSegCount()
{
return fTsBuffer ? iSegCount : 1;
if (!fTsBuffer || !bCurve)
return 1;
return iSegCount;
};
void RaRenderLoft(CVertNormTex *&Vert, const vector6 *ShapePoints, int iNumShapePoints,
double fTextureLength, int iSkip = 0, int iEnd = 0, double fOffsetX = 0.0);

View File

@@ -275,6 +275,12 @@ opengl_texture::load_DDS() {
--mapcount;
}
*/
if( datasize == 0 ) {
// catch malformed .dds files
WriteLog( "File \"" + name + "\" is malformed and holds no texture data." );
data_state = resource_state::failed;
return;
}
// reserve space and load texture data
data.resize( datasize );
if( offset != 0 ) {

View File

@@ -2473,22 +2473,18 @@ void TTrack::RaRenderVBO(int iPtr)
if ((seg = SwitchExtension->Segments[0]->RaSegCount()) > 0)
{
GfxRenderer.Bind( TextureID1 ); // szyny +
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 24 * i, 24);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 24 * seg);
iPtr += 24 * seg; // pominięcie lewej szyny
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 24 * i, 24);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 24 * seg);
iPtr += 24 * seg; // pominięcie prawej szyny
}
if (TextureID2)
if ((seg = SwitchExtension->Segments[1]->RaSegCount()) > 0)
{
GfxRenderer.Bind( TextureID2 ); // szyny -
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 24 * i, 24);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 24 * seg);
iPtr += 24 * seg; // pominięcie lewej szyny
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 24 * i, 24);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 24 * seg);
}
}
else // dla toru podsypka plus szyny
@@ -2498,18 +2494,15 @@ void TTrack::RaRenderVBO(int iPtr)
if (TextureID2)
{
GfxRenderer.Bind( TextureID2 ); // podsypka
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 8 * i, 8);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 8 * seg);
iPtr += 8 * seg; // pominięcie podsypki
}
if (TextureID1)
{
GfxRenderer.Bind( TextureID1 ); // szyny
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 24 * i, 24);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 24 * seg);
iPtr += 24 * seg; // pominięcie lewej szyny
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 24 * i, 24);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 24 * seg);
}
}
}
@@ -2520,34 +2513,27 @@ void TTrack::RaRenderVBO(int iPtr)
if (TextureID1)
{
GfxRenderer.Bind( TextureID1 ); // nawierzchnia
for (i = 0; i < seg; ++i)
{
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 4);
iPtr += 4;
}
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 4 * seg);
iPtr += 4 * seg;
}
if (TextureID2)
{
GfxRenderer.Bind( TextureID2 ); // pobocze
if (fTexHeight1 >= 0.0)
{ // normalna droga z poboczem
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 6 * i, 6);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 6 * seg);
iPtr += 6 * seg; // pominięcie lewego pobocza
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 6 * i, 6);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 6 * seg);
}
else
{ // z chodnikami o różnych szerokociach
if (fTexWidth != 0.0)
{
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 6 * i, 6);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 6 * seg);
iPtr += 6 * seg; // pominięcie lewego pobocza
}
if (fTexSlope != 0.0)
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 6 * i, 6);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 6 * seg);
}
}
}
@@ -2558,20 +2544,15 @@ void TTrack::RaRenderVBO(int iPtr)
if (TextureID1)
{
GfxRenderer.Bind( TextureID1 ); // nawierzchnia
for (i = 0; i < seg; ++i)
{
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 4);
iPtr += 4;
}
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 4 * seg);
iPtr += 4 * seg;
}
if (TextureID2)
{
GfxRenderer.Bind( TextureID2 ); // pobocze
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 6 * i, 6);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 6 * seg);
iPtr += 6 * seg; // pominięcie lewego pobocza
for (i = 0; i < seg; ++i)
glDrawArrays(GL_TRIANGLE_STRIP, iPtr + 6 * i, 6);
glDrawArrays(GL_TRIANGLE_STRIP, iPtr, 6 * seg);
}
}
break;

View File

@@ -34,8 +34,6 @@ http://mozilla.org/MPL/2.0/.
#include "Console.h"
#include "color.h"
#define TEXTURE_FILTER_CONTROL_EXT 0x8500
#define TEXTURE_LOD_BIAS_EXT 0x8501
//---------------------------------------------------------------------------
TDynamicObject *Controlled = NULL; // pojazd, który prowadzimy
@@ -280,6 +278,7 @@ bool TWorld::Init(GLFWwindow *w)
glEnable(GL_CULL_FACE); // Cull back-facing triangles
glLineWidth(1.0f);
glPointSize(3.0f);
glEnable( GL_POINT_SMOOTH );
#ifdef EU07_USE_OLD_LIGHTING_MODEL
// ----------- LIGHTING SETUP -----------
// Light values and coordinates
@@ -1128,13 +1127,22 @@ bool TWorld::Update()
int updatecount = 1;
if( dt > m_primaryupdaterate ) // normalnie 0.01s
{
/*
// NOTE: experimentally disabled physics update cap
auto const iterations = std::ceil(dt / m_primaryupdaterate);
updatecount = std::min( 20, static_cast<int>( iterations ) );
*/
updatecount = std::ceil( dt / m_primaryupdaterate );
/*
// NOTE: changing dt wrecks things further down the code. re-acquire proper value later or cleanup here
dt = dt / iterations; // Ra: fizykę lepiej by było przeliczać ze stałym krokiem
*/
}
// NOTE: updates are limited to 20, but dt is distributed over potentially many more iterations
// this means at count > 20 simulation and render are going to desync. is that right?
Ground.Update(dt, updatecount); // tu zrobić tylko coklatkową aktualizację przesunięć
// NOTE: experimentally changing this to prevent the desync.
// TODO: test what happens if we hit more than 20 * 0.01 sec slices, i.e. less than 5 fps
Ground.Update(dt / updatecount, updatecount); // tu zrobić tylko coklatkową aktualizację przesunięć
/*
if (DebugModeFlag)
if (Global::bActive) // nie przyspieszać, gdy jedzie w tle :)
@@ -1458,6 +1466,7 @@ bool TWorld::Render()
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity();
Camera.SetMatrix(); // ustawienie macierzy kamery względem początku scenerii
Camera.SetFrustum(); // update camera frustum to match current data
if( !Global::bWireFrame ) {
// bez nieba w trybie rysowania linii

178
frustum.cpp Normal file
View File

@@ -0,0 +1,178 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "frustum.h"
void
cFrustum::calculate() {
float proj[ 16 ];
float modl[ 16 ];
float clip[ 16 ];
glGetFloatv( GL_PROJECTION_MATRIX, proj );
glGetFloatv( GL_MODELVIEW_MATRIX, modl );
// multiply the matrices to retrieve clipping planes
clip[ 0 ] = modl[ 0 ] * proj[ 0 ] + modl[ 1 ] * proj[ 4 ] + modl[ 2 ] * proj[ 8 ] + modl[ 3 ] * proj[ 12 ];
clip[ 1 ] = modl[ 0 ] * proj[ 1 ] + modl[ 1 ] * proj[ 5 ] + modl[ 2 ] * proj[ 9 ] + modl[ 3 ] * proj[ 13 ];
clip[ 2 ] = modl[ 0 ] * proj[ 2 ] + modl[ 1 ] * proj[ 6 ] + modl[ 2 ] * proj[ 10 ] + modl[ 3 ] * proj[ 14 ];
clip[ 3 ] = modl[ 0 ] * proj[ 3 ] + modl[ 1 ] * proj[ 7 ] + modl[ 2 ] * proj[ 11 ] + modl[ 3 ] * proj[ 15 ];
clip[ 4 ] = modl[ 4 ] * proj[ 0 ] + modl[ 5 ] * proj[ 4 ] + modl[ 6 ] * proj[ 8 ] + modl[ 7 ] * proj[ 12 ];
clip[ 5 ] = modl[ 4 ] * proj[ 1 ] + modl[ 5 ] * proj[ 5 ] + modl[ 6 ] * proj[ 9 ] + modl[ 7 ] * proj[ 13 ];
clip[ 6 ] = modl[ 4 ] * proj[ 2 ] + modl[ 5 ] * proj[ 6 ] + modl[ 6 ] * proj[ 10 ] + modl[ 7 ] * proj[ 14 ];
clip[ 7 ] = modl[ 4 ] * proj[ 3 ] + modl[ 5 ] * proj[ 7 ] + modl[ 6 ] * proj[ 11 ] + modl[ 7 ] * proj[ 15 ];
clip[ 8 ] = modl[ 8 ] * proj[ 0 ] + modl[ 9 ] * proj[ 4 ] + modl[ 10 ] * proj[ 8 ] + modl[ 11 ] * proj[ 12 ];
clip[ 9 ] = modl[ 8 ] * proj[ 1 ] + modl[ 9 ] * proj[ 5 ] + modl[ 10 ] * proj[ 9 ] + modl[ 11 ] * proj[ 13 ];
clip[ 10 ] = modl[ 8 ] * proj[ 2 ] + modl[ 9 ] * proj[ 6 ] + modl[ 10 ] * proj[ 10 ] + modl[ 11 ] * proj[ 14 ];
clip[ 11 ] = modl[ 8 ] * proj[ 3 ] + modl[ 9 ] * proj[ 7 ] + modl[ 10 ] * proj[ 11 ] + modl[ 11 ] * proj[ 15 ];
clip[ 12 ] = modl[ 12 ] * proj[ 0 ] + modl[ 13 ] * proj[ 4 ] + modl[ 14 ] * proj[ 8 ] + modl[ 15 ] * proj[ 12 ];
clip[ 13 ] = modl[ 12 ] * proj[ 1 ] + modl[ 13 ] * proj[ 5 ] + modl[ 14 ] * proj[ 9 ] + modl[ 15 ] * proj[ 13 ];
clip[ 14 ] = modl[ 12 ] * proj[ 2 ] + modl[ 13 ] * proj[ 6 ] + modl[ 14 ] * proj[ 10 ] + modl[ 15 ] * proj[ 14 ];
clip[ 15 ] = modl[ 12 ] * proj[ 3 ] + modl[ 13 ] * proj[ 7 ] + modl[ 14 ] * proj[ 11 ] + modl[ 15 ] * proj[ 15 ];
// get the sides of the frustum.
m_frustum[ side_RIGHT ][ plane_A ] = clip[ 3 ] - clip[ 0 ];
m_frustum[ side_RIGHT ][ plane_B ] = clip[ 7 ] - clip[ 4 ];
m_frustum[ side_RIGHT ][ plane_C ] = clip[ 11 ] - clip[ 8 ];
m_frustum[ side_RIGHT ][ plane_D ] = clip[ 15 ] - clip[ 12 ];
normalize_plane( side_RIGHT );
m_frustum[ side_LEFT ][ plane_A ] = clip[ 3 ] + clip[ 0 ];
m_frustum[ side_LEFT ][ plane_B ] = clip[ 7 ] + clip[ 4 ];
m_frustum[ side_LEFT ][ plane_C ] = clip[ 11 ] + clip[ 8 ];
m_frustum[ side_LEFT ][ plane_D ] = clip[ 15 ] + clip[ 12 ];
normalize_plane( side_LEFT );
m_frustum[ side_BOTTOM ][ plane_A ] = clip[ 3 ] + clip[ 1 ];
m_frustum[ side_BOTTOM ][ plane_B ] = clip[ 7 ] + clip[ 5 ];
m_frustum[ side_BOTTOM ][ plane_C ] = clip[ 11 ] + clip[ 9 ];
m_frustum[ side_BOTTOM ][ plane_D ] = clip[ 15 ] + clip[ 13 ];
normalize_plane( side_BOTTOM );
m_frustum[ side_TOP ][ plane_A ] = clip[ 3 ] - clip[ 1 ];
m_frustum[ side_TOP ][ plane_B ] = clip[ 7 ] - clip[ 5 ];
m_frustum[ side_TOP ][ plane_C ] = clip[ 11 ] - clip[ 9 ];
m_frustum[ side_TOP ][ plane_D ] = clip[ 15 ] - clip[ 13 ];
normalize_plane( side_TOP );
m_frustum[ side_BACK ][ plane_A ] = clip[ 3 ] - clip[ 2 ];
m_frustum[ side_BACK ][ plane_B ] = clip[ 7 ] - clip[ 6 ];
m_frustum[ side_BACK ][ plane_C ] = clip[ 11 ] - clip[ 10 ];
m_frustum[ side_BACK ][ plane_D ] = clip[ 15 ] - clip[ 14 ];
normalize_plane( side_BACK );
m_frustum[ side_FRONT ][ plane_A ] = clip[ 3 ] + clip[ 2 ];
m_frustum[ side_FRONT ][ plane_B ] = clip[ 7 ] + clip[ 6 ];
m_frustum[ side_FRONT ][ plane_C ] = clip[ 11 ] + clip[ 10 ];
m_frustum[ side_FRONT ][ plane_D ] = clip[ 15 ] + clip[ 14 ];
normalize_plane( side_FRONT );
}
bool
cFrustum::point_inside( float const X, float const Y, float const Z ) const {
// cycle through the sides of the frustum, checking if the point is behind them
for( int idx = 0; idx < 6; ++idx ) {
if( m_frustum[ idx ][ plane_A ] * X
+ m_frustum[ idx ][ plane_B ] * Y
+ m_frustum[ idx ][ plane_C ] * Z
+ m_frustum[ idx ][ plane_D ] <= 0 )
return false;
}
// the point is in front of each frustum plane, i.e. inside of the frustum
return true;
}
float
cFrustum::sphere_inside( float const X, float const Y, float const Z, float const Radius ) const {
float distance;
// go through all the sides of the frustum. bail out as soon as possible
for( int idx = 0; idx < 6; ++idx ) {
distance =
m_frustum[ idx ][ plane_A ] * X
+ m_frustum[ idx ][ plane_B ] * Y
+ m_frustum[ idx ][ plane_C ] * Z
+ m_frustum[ idx ][ plane_D ];
if( distance <= -Radius )
return 0.0f;
}
return distance + Radius;
}
bool
cFrustum::cube_inside( float const X, float const Y, float const Z, float const Size ) const {
for( int idx = 0; idx < 6; ++idx ) {
if( m_frustum[ idx ][ plane_A ] * ( X - Size )
+ m_frustum[ idx ][ plane_B ] * ( Y - Size )
+ m_frustum[ idx ][ plane_C ] * ( Z - Size )
+ m_frustum[ idx ][ plane_D ] > 0 )
continue;
if( m_frustum[ idx ][ plane_A ] * ( X + Size )
+ m_frustum[ idx ][ plane_B ] * ( Y - Size )
+ m_frustum[ idx ][ plane_C ] * ( Z - Size )
+ m_frustum[ idx ][ plane_D ] > 0 )
continue;
if( m_frustum[ idx ][ plane_A ] * ( X - Size )
+ m_frustum[ idx ][ plane_B ] * ( Y + Size )
+ m_frustum[ idx ][ plane_C ] * ( Z - Size )
+ m_frustum[ idx ][ plane_D ] > 0 )
continue;
if( m_frustum[ idx ][ plane_A ] * ( X + Size )
+ m_frustum[ idx ][ plane_B ] * ( Y + Size )
+ m_frustum[ idx ][ plane_C ] * ( Z - Size )
+ m_frustum[ idx ][ plane_D ] > 0 )
continue;
if( m_frustum[ idx ][ plane_A ] * ( X - Size )
+ m_frustum[ idx ][ plane_B ] * ( Y - Size )
+ m_frustum[ idx ][ plane_C ] * ( Z + Size )
+ m_frustum[ idx ][ plane_D ] > 0 )
continue;
if( m_frustum[ idx ][ plane_A ] * ( X + Size )
+ m_frustum[ idx ][ plane_B ] * ( Y - Size )
+ m_frustum[ idx ][ plane_C ] * ( Z + Size )
+ m_frustum[ idx ][ plane_D ] > 0 )
continue;
if( m_frustum[ idx ][ plane_A ] * ( X - Size )
+ m_frustum[ idx ][ plane_B ] * ( Y + Size )
+ m_frustum[ idx ][ plane_C ] * ( Z + Size )
+ m_frustum[ idx ][ plane_D ] > 0 )
continue;
if( m_frustum[ idx ][ plane_A ] * ( X + Size )
+ m_frustum[ idx ][ plane_B ] * ( Y + Size )
+ m_frustum[ idx ][ plane_C ] * ( Z + Size )
+ m_frustum[ idx ][ plane_D ] > 0 )
continue;
return false;
}
return true;
}
void cFrustum::normalize_plane( cFrustum::side const Side ) {
float magnitude =
std::sqrt(
m_frustum[ Side ][ plane_A ] * m_frustum[ Side ][ plane_A ]
+ m_frustum[ Side ][ plane_B ] * m_frustum[ Side ][ plane_B ]
+ m_frustum[ Side ][ plane_C ] * m_frustum[ Side ][ plane_C ] );
m_frustum[ Side ][ plane_A ] /= magnitude;
m_frustum[ Side ][ plane_B ] /= magnitude;
m_frustum[ Side ][ plane_C ] /= magnitude;
m_frustum[ Side ][ plane_D ] /= magnitude;
}

67
frustum.h Normal file
View File

@@ -0,0 +1,67 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "float3d.h"
#include "dumb3d.h"
// generic frustum class. used to determine if objects are inside current view area
class cFrustum {
public:
// constructors:
// methods:
// update the frustum to match current view orientation
void
calculate();
// returns true if specified point is inside of the frustum
inline
bool
point_inside( float3 const &Point ) const { return point_inside( Point.x, Point.y, Point.z ); }
inline
bool
point_inside( Math3D::vector3 const &Point ) const { return point_inside( static_cast<float>( Point.x ), static_cast<float>( Point.y ), static_cast<float>( Point.z ) ); }
bool
point_inside( float const X, float const Y, float const Z ) const;
// tests if the sphere is in frustum, returns the distance between origin and sphere centre
inline
float
sphere_inside( float3 const &Center, float const Radius ) const { return sphere_inside( Center.x, Center.y, Center.z, Radius ); }
inline
float
sphere_inside( Math3D::vector3 const &Center, float const Radius ) const { return sphere_inside( static_cast<float>( Center.x ), static_cast<float>( Center.y ), static_cast<float>( Center.z ), Radius ); }
float
sphere_inside( float const X, float const Y, float const Z, float const Radius ) const;
// returns true if specified cube is inside of the frustum. Size = half of the length
inline
bool
cube_inside( float3 const &Center, float const Size ) const { return cube_inside( Center.x, Center.y, Center.z, Size ); }
inline
bool
cube_inside( Math3D::vector3 const &Center, float const Size ) const { return cube_inside( static_cast<float>( Center.x ), static_cast<float>( Center.y ), static_cast<float>( Center.z ), Size ); }
bool
cube_inside( float const X, float const Y, float const Z, float const Size ) const;
protected:
// types:
// planes of the frustum
enum side { side_RIGHT = 0, side_LEFT = 1, side_BOTTOM = 2, side_TOP = 3, side_BACK = 4, side_FRONT = 5 };
// parameters of the frustum plane: A, B, C define plane normal, D defines distance from origin
enum plane { plane_A = 0, plane_B = 1, plane_C = 2, plane_D = 3 };
// methods:
void
normalize_plane( cFrustum::side const Side ); // normalizes a plane (A side) from the frustum
// members:
float m_frustum[6][4]; // holds the A B C and D values (normal & distance) for each side of the frustum.
};

View File

@@ -2,6 +2,7 @@
#include "stdafx.h"
#include "skydome.h"
#include "color.h"
#include "usefull.h"
// sky gradient based on "A practical analytic model for daylight"
// by A. J. Preetham Peter Shirley Brian Smits (University of Utah)
@@ -41,21 +42,6 @@ float CSkyDome::m_zenithymatrix[ 3 ][ 4 ] = {
//******************************************************************************//
float clamp( float const Value, float const Min, float const Max ) {
float value = Value;
if( value < Min ) { value = Min; }
if( value > Max ) { value = Max; }
return value;
}
float interpolate( float const First, float const Second, float const Factor ) {
return ( First * ( 1.0f - Factor ) ) + ( Second * Factor );
}
//******************************************************************************//
CSkyDome::CSkyDome (int const Tesselation) :
m_tesselation( Tesselation ) {
@@ -121,8 +107,6 @@ void CSkyDome::Generate() {
}
}
//******************************************************************************//
void CSkyDome::Update( Math3D::vector3 const &Sun ) {
if( true == SetSunPosition( Sun ) ) {
@@ -166,8 +150,6 @@ void CSkyDome::Render() {
::glDisableClientState( GL_VERTEX_ARRAY );
}
//******************************************************************************//
bool CSkyDome::SetSunPosition( Math3D::vector3 const &Direction ) {
auto sundirection = SafeNormalize( float3( Direction.x, Direction.y, Direction.z) );
@@ -205,8 +187,6 @@ void CSkyDome::SetOvercastFactor( float const Overcast ) {
m_overcast = clamp( Overcast, 0.0f, 1.0f );
}
//******************************************************************************//
void CSkyDome::GetPerez( float *Perez, float Distribution[ 5 ][ 2 ], const float Turbidity ) {
Perez[ 0 ] = Distribution[ 0 ][ 0 ] * Turbidity + Distribution[ 0 ][ 1 ];
@@ -227,8 +207,6 @@ float CSkyDome::GetZenith( float Zenithmatrix[ 3 ][ 4 ], const float Theta, cons
}
//******************************************************************************//
float CSkyDome::PerezFunctionO1( float Perezcoeffs[ 5 ], const float Thetasun, const float Zenithval ) {
const float val = ( 1.0f + Perezcoeffs[ 0 ] * std::exp( Perezcoeffs[ 1 ] ) ) *
@@ -244,7 +222,6 @@ float CSkyDome::PerezFunctionO2( float Perezcoeffs[ 5 ], const float Icostheta,
( 1.0f + Perezcoeffs[ 2 ] * std::exp( Perezcoeffs[ 3 ] * Gamma ) + Perezcoeffs[ 4 ] * Cosgamma2 );
}
//******************************************************************************//
void CSkyDome::RebuildColors() {
// get zenith luminance
@@ -320,7 +297,7 @@ void CSkyDome::RebuildColors() {
// override the hue, based on sun height above the horizon. crude way to deal with model shortcomings
// correction begins when the sun is higher than 10 degrees above the horizon, and fully in effect at 10+15 degrees
auto const degreesabovehorizon = 90.0f - m_thetasun * ( 180.0f / M_PI );
float const degreesabovehorizon = 90.0f - m_thetasun * ( 180.0f / M_PI );
auto const sunbasedphase = clamp( (1.0f / 15.0f) * ( degreesabovehorizon - 10.0f ), 0.0f, 1.0f );
// correction is applied in linear manner from the bottom, becomes fully in effect for vertices with y = 0.50
auto const heightbasedphase = clamp( vertex.y * 2.0f, 0.0f, 1.0f );

View File

@@ -9,17 +9,7 @@ http://mozilla.org/MPL/2.0/.
#pragma once
//#define B1(t) (t*t*t)
//#define B2(t) (3*t*t*(1-t))
//#define B3(t) (3*t*(1-t)*(1-t))
//#define B4(t) ((1-t)*(1-t)*(1-t))
// Ra: to jest mocno nieoptymalne: 10+3*4=22 mnożenia, 6 odejmowań, 3*3=9 dodawań
// Ra: po przeliczeniu współczynników mamy: 3*3=9 mnożeń i 3*3=9 dodawań
//#define Interpolate(t,p1,cp1,cp2,p2) (B4(t)*p1+B3(t)*cp1+B2(t)*cp2+B1(t)*p2)
// Ra: "delete NULL" nic nie zrobi, więc "if (a!=NULL)" jest zbędne
//#define SafeFree(a) if (a!=NULL) free(a)
//#define M_PI = 3.141592653589793
#include "stdafx.h"
#define SafeDelete(a) \
{ \
@@ -37,25 +27,26 @@ http://mozilla.org/MPL/2.0/.
#define DegToRad(a) ((M_PI / 180.0) * (a)) //(a) w nawiasie, bo może być dodawaniem
#define RadToDeg(r) ((180.0 / M_PI) * (r))
#define Fix(a, b, c) \
{ \
if (a < b) \
a = b; \
if (a > c) \
a = c; \
}
#define asModelsPath std::string("models\\")
#define asSceneryPath std::string("scenery\\")
//#define asTexturePath AnsiString("textures\\")
//#define asTextureExt AnsiString(".bmp")
#define szSceneryPath "scenery\\"
#define szTexturePath "textures\\"
//#define szDefaultTextureExt ".dds"
//#define DevelopTime //FIXME
//#define EditorMode
#define MAKE_ID4(a,b,c,d) (((std::uint32_t)(d)<<24)|((std::uint32_t)(c)<<16)|((std::uint32_t)(b)<<8)|(std::uint32_t)(a))
template <typename _Type>
_Type clamp( _Type const Value, _Type const Min, _Type const Max ) {
_Type value = Value;
if( value < Min ) { value = Min; }
if( value > Max ) { value = Max; }
return value;
}
template <typename _Type>
_Type interpolate( _Type const First, _Type const Second, float const Factor ) {
return ( First * ( 1.0f - Factor ) ) + ( Second * Factor );
}
//---------------------------------------------------------------------------