-
Notifications
You must be signed in to change notification settings - Fork 0
/
gutils.cpp
129 lines (114 loc) · 3.12 KB
/
gutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "gutils.h"
#include <QCoreApplication>
#include <QDir>
#include <sys/stat.h>
using namespace std;
#define PROJ_NAME "GRaster"
int GUtils::screenWidth = 500;
int GUtils::screenHeight = 500;
float GUtils::worldSize= 2000.f;
GDebugType GUtils::debugType = GDebugType::kNone;
//GDebugType GUtils::debugType = GDebugType::kWorldNormal;
//GDebugType GUtils::debugType = GDebugType::kWorldTangent;
float GUtils::screenAspectRatio()
{
return (float)screenHeight/screenWidth;
}
std::string GUtils::GetProjRootPath()
{
QDir dir(QCoreApplication::applicationDirPath()+"/../../GRaytracer");
if(dir.exists())
{
return dir.absolutePath().toStdString();
}
return "";
}
string GUtils::GetAbsPath(const string &relativePath)
{
return GetProjRootPath()+"/"+relativePath;
}
bool GUtils::IsFileExist(const std::string filepath)
{
struct stat s;
if(stat(filepath.c_str(), &s) == 0)
{
if(s.st_mode & S_IFREG)
{
return true;
}
else if(s.st_mode & S_IFDIR)
{
GLog::LogWarning(filepath, " is directory!");
}
}
return false;
}
GColor GUtils::SampleImage(const TGAImage *img, GMath::vec2 uv, GTextureWrapMode wrapMode, GColor defaultColor)
{
if(img->get_width()<1 || img->get_height()<1)
{
GLog::LogError("img w = ", img->get_width(), " h = ", img->get_height());
return defaultColor;
}
if(uv.x()>1)
{
if(wrapMode == GTextureWrapMode::kTWMClamp)
{
uv.SetX(1);
}
else if(wrapMode == GTextureWrapMode::kTWMRepeat)
{
uv.SetX(uv.x()-(int)uv.x());
}
else
{
GLog::LogError("wrapMode = ", wrapMode);
}
}
if(uv.y()>1)
{
if(wrapMode == GTextureWrapMode::kTWMClamp)
{
uv.SetY(1);
}
else if(wrapMode == GTextureWrapMode::kTWMRepeat)
{
uv.SetY(uv.y()-(int)uv.y());
}
else
{
GLog::LogError("wrapMode = ", wrapMode);
}
}
int w = img->get_width() - 1;
int h = img->get_height() - 1;
float hLerpFactor = w*uv.x();
int rX = w*uv.x();
hLerpFactor = hLerpFactor - rX;
int lX = rX >= w ? rX : rX+1;
float vLerpFactor = h*uv.y();
int bY = h*uv.y();
vLerpFactor = vLerpFactor - bY;
int tY = bY >= h ? bY : bY+1;
GColor hColor1 = GColor::Lerp(img->get(rX,bY), img->get(lX,bY), hLerpFactor);
GColor hColor2 = GColor::Lerp(img->get(rX,tY), img->get(lX,tY), hLerpFactor);
GColor ret = GColor::Lerp(hColor1, hColor2, vLerpFactor);
if(img->get_bytespp()!=TGAImage::Format::RGBA)
{
ret.a = 255;
}
return ret;
}
GColor GUtils::SampleImage(const std::vector<TGAImage> *mipmaps, GMath::vec2 uv, int mipmapLevel, GTextureWrapMode wrapMode, GColor defaultColor)
{
if(mipmaps==nullptr)
{
GLog::LogError("mipmaps == nullptr");
return defaultColor;
}
if((size_t)mipmapLevel>=mipmaps->size())
{
mipmapLevel = mipmaps->size() - 1;
}
return SampleImage(&(mipmaps->at(mipmapLevel)), uv, wrapMode, defaultColor);
}