removed unneeded texture members, added mipmaps

This commit is contained in:
devZoGok
2021-11-20 09:00:41 +02:00
parent a78f6ad32a
commit cbbe6589ea
8 changed files with 54 additions and 46 deletions
+3 -3
View File
@@ -50,15 +50,15 @@ int main(){
mat->addBoolUniform("specularMapEnabled", true);
string im0[] = {TEX_PATH + "bricks.jpg"};
Texture *diffuseTex = new Texture(im0, 1);
Texture *diffuseTex = new Texture(im0, 1, false);
mat->addTexUniform("textures[0]", diffuseTex, true);
string im1[] = {TEX_PATH + "bricksNormal.jpg"};
Texture *normalTex = new Texture(im1, 1, Texture::NORMAL);
Texture *normalTex = new Texture(im1, 1, false);
mat->addTexUniform("textures[1]", normalTex, true);
string im2[] = {TEX_PATH + "bricksSpecular.jpg"};
Texture *specularTex = new Texture(im2, 1, Texture::SPECULAR);
Texture *specularTex = new Texture(im2, 1, false);
mat->addTexUniform("textures[2]", specularTex, true);
//Setting specular parameteres
+1 -1
View File
@@ -44,7 +44,7 @@ int main(){
mat->addBoolUniform("lightingEnabled", false);
mat->addBoolUniform("texturingEnabled", true);
string images[] = {TEX_PATH + "defaultTexture.jpg"};
Texture *texture = new Texture(images, 1);
Texture *texture = new Texture(images, 1, false);
mat->addTexUniform("textures[0]", texture, true);
model->setMaterial(mat);
rootNode->attachChild(model);
+1 -1
View File
@@ -196,7 +196,7 @@ namespace vb01{
void Root::createSkybox(string paths[6]){
skybox = new Box(Vector3(1, 1, 1) * 10);
Material *skyboxMat = new Material(libPath + "skybox");
Texture *texture = new Texture(paths);
Texture *texture = new Texture(paths, 6, true);
skyboxMat->addTexUniform("skybox", texture, true);
skybox->setMaterial(skyboxMat);
}
+2 -2
View File
@@ -46,7 +46,7 @@ int main(){
boxMat->addBoolUniform("texturingEnabled", true);
boxMat->addBoolUniform("lightingEnabled", true);
string im0[] = {TEX_PATH + "bricks.jpg"};
Texture *boxTex = new Texture(im0, 1);
Texture *boxTex = new Texture(im0, 1, false);
boxMat->addTexUniform("textures[0]", boxTex, true);
box->setMaterial(boxMat);
boxNode->attachMesh(box);
@@ -57,7 +57,7 @@ int main(){
jetMat->addBoolUniform("texturingEnabled", true);
jetMat->addBoolUniform("lightingEnabled", true);
string im1[] = {TEX_PATH + "defaultTexture.jpg"};
Texture *jetTex = new Texture(im1, 1);
Texture *jetTex = new Texture(im1, 1, false);
jetMat->addTexUniform("textures[0]", jetTex, true);
jet->setMaterial(jetMat);
rootNode->attachChild(jet);
+1 -1
View File
@@ -44,7 +44,7 @@ int main(){
mat->addBoolUniform("lightingEnabled", false);
mat->addBoolUniform("texturingEnabled", true);
string images[] = {TEX_PATH + "defaultTexture.jpg"};
Texture *texture = new Texture(images, 1);
Texture *texture = new Texture(images, 1, false);
mat->addTexUniform("textures[0]", texture, true);
model->setMaterial(mat);
rootNode->attachChild(model);
+1 -1
View File
@@ -55,7 +55,7 @@ int main(){
textMat->addBoolUniform("texturingEnabled", true);
string frames[]{TEX_PATH + "bricks.jpg", TEX_PATH + "defaultTexture.jpg"};
Texture *texture = new Texture(frames, 2);
Texture *texture = new Texture(frames, 2, false);
textMat->addTexUniform("textures[0]", texture, true);
for(int i = 0; i < numTexts; i++){
+37 -25
View File
@@ -20,6 +20,7 @@ namespace vb01{
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
if(shadowMap){
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
@@ -39,49 +40,53 @@ namespace vb01{
}
}
Texture::Texture(string path[], int numPaths, TextureTypeId textureTypeId, bool flip){
Texture::Texture(string paths[], int numPaths, bool cubemap, int mipmapLevel, bool flip){
this->numFrames = numPaths;
this->cubemap = cubemap;
this->paths = new string[numPaths];
for(int i = 0; i < numPaths; i++)
this->paths[i] = paths[i];
if(cubemap){
texture = new u32;
createCubemap(false, flip);
}
else{
texture = new u32[numPaths];
create2DTexture(flip);
}
}
void Texture::create2DTexture(bool flip){
mixRatio = .1;
this->typeId = textureTypeId;
this->numFrames = numPaths;
texture = new u32[numPaths];
for(int i = 0; i < numPaths; i++){
for(int i = 0; i < numFrames; i++){
bool png = false;
int length = path[i].length();
int length = paths[i].length();
if(path[i].substr(length - 4, string::npos) == ".png")
if(paths[i].substr(length - 4, string::npos) == ".png")
png = true;
glGenTextures(1, &texture[i]);
glBindTexture(GL_TEXTURE_2D, texture[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_set_flip_vertically_on_load(flip);
data = stbi_load(path[i].c_str(), &width, &height, &numChannels, 0);
data = stbi_load(paths[i].c_str(), &width, &height, &numChannels, 0);
glTexImage2D(GL_TEXTURE_2D, 0, png ? GL_RGBA : GL_RGB, width, height, 0, png ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, data);
//glGenerateMipmap(GL_TEXTURE_2D);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
}
}
Texture::Texture(string paths[6], bool flip){
this->type = TextureType::TEXTURE_CUBEMAP;
texture = new u32;
for(int i = 0; i < 6; i++)
this->paths[i] = paths[i];
createCubemap(false, flip);
}
Texture::Texture(int width, bool depth){
this->width = width;
this->type = TextureType::TEXTURE_CUBEMAP;
texture = new u32;
createCubemap(true, false);
@@ -97,23 +102,27 @@ namespace vb01{
for(int i = 0; i < 6; i++){
if(!depth){
data = stbi_load(paths[i].c_str(), &width, &height, &numChannels, 0);
if(data){
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else{
cout<<"Failed to read cubemap texture "<<i<<endl;
cout << "Failed to read cubemap texture " << i << endl;
exit(-1);
}
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
}
else{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT, width, width, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
}
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
@@ -141,10 +150,12 @@ namespace vb01{
}
Texture::~Texture(){
glBindTexture(type == TEXTURE_2D ? GL_TEXTURE_2D : GL_TEXTURE_CUBE_MAP, 0);
glBindTexture(cubemap ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D, 0);
if(numFrames > 0){
for(int i = 0; i < numFrames; i++)
glDeleteTextures(1, &texture[i]);
delete[] texture;
}
else{
@@ -169,12 +180,13 @@ namespace vb01{
void Texture::update(int id){
select(id, frameA);
if(numFrames > 0)
select(id + 1, frameB);
}
void Texture::select(int id, int fr){
glActiveTexture(GL_TEXTURE0 + id);
glBindTexture(type == TEXTURE_2D ? GL_TEXTURE_2D : GL_TEXTURE_CUBE_MAP, texture[fr]);
glBindTexture(cubemap ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D, texture[fr]);
}
}
+8 -12
View File
@@ -12,34 +12,30 @@
namespace vb01{
class Texture : public Animatable{
public:
enum TextureType{TEXTURE_2D, TEXTURE_CUBEMAP};
enum TextureTypeId{DIFFUSE, NORMAL, SPECULAR, PARALLAX, ENVIRONMENT};
~Texture();
Texture(int, int, bool = true);
Texture(std::string[], int, TextureTypeId = DIFFUSE, bool = false);
Texture(std::string[6], bool = false);
Texture(std::string[], int, bool, int = 0, bool = false);
Texture(int, bool = true);
Texture(FT_Face&, char);
void select(int = 0, int = 0);
void update(int = 0);
void animate(float, KeyframeChannel);
inline unsigned int* getTexture(int i = 0){return &(texture[i]);}
inline std::string getPath(){return path;}
inline u32* getTexture(int i = 0){return &(texture[i]);}
inline std::string* getPath(){return paths;}
inline int getNumFrames(){return numFrames;}
inline int getMipmapLevel(){return mipmapLevel;}
inline float getMixRatio(){return mixRatio;}
inline TextureTypeId getTextureTypeId(){return typeId;}
private:
TextureType type = TextureType::TEXTURE_2D;
TextureTypeId typeId = DIFFUSE;
bool cubemap = false;
u32 *texture = nullptr;
s64 lastUpdateTime = 0;
int width, height, numChannels, updateRate = 0, numFrames = 0, frameA = 0, frameB = 0;
int width, height, numChannels, updateRate = 0, numFrames = 0, frameA = 0, frameB = 0, mipmapLevel = 1;
float mixRatio;
u8 *data;
std::string path = "", paths[6];
std::string *paths = nullptr;
void createCubemap(bool, bool);
void create2DTexture(bool);
inline int getNextFrame(int frameId){return (frameId + 1 < numFrames ? frameId + 1 : 0);}
};
}