implemented Texture and added skybox material
This commit is contained in:
parent
b6c9377268
commit
370a073dcb
@ -1,9 +1,21 @@
|
||||
#include "phongmaterial.h"
|
||||
#include "texture.h"
|
||||
|
||||
#define TEX_ID 0
|
||||
|
||||
void PhongMaterial::bindAttributes()
|
||||
{
|
||||
shader->bindVec3(shader->getLocation("materialKd"), kd);
|
||||
shader->bindVec3(shader->getLocation("materialKs"), ks);
|
||||
shader->bindFloat(shader->getLocation("materialNs"), ns);
|
||||
if(tex != NULL)
|
||||
{
|
||||
tex->bind(TEX_ID);
|
||||
shader->bindTexture(shader->getLocation("baseTexture"), TEX_ID);
|
||||
}
|
||||
}
|
||||
|
||||
void PhongMaterial::setTexture(Texture* myTexture)
|
||||
{
|
||||
tex = myTexture;
|
||||
}
|
||||
|
@ -4,15 +4,20 @@
|
||||
#include "material.h"
|
||||
#include "glm/vec3.hpp"
|
||||
|
||||
class Texture;
|
||||
|
||||
class PhongMaterial : public Material
|
||||
{
|
||||
glm::vec3 kd;
|
||||
glm::vec3 ks;
|
||||
float ns;
|
||||
Texture* tex;
|
||||
public:
|
||||
PhongMaterial(Shader* myShader) : Material(myShader), kd(0.5f), ks(0.5f), ns(10) {}
|
||||
PhongMaterial(Shader* myShader, glm::vec3 myKd, glm::vec3 myKs, float myNs) : Material(myShader), kd(myKd), ks(myKs), ns(myNs) {}
|
||||
PhongMaterial(Shader* myShader) : Material(myShader), kd(0.5f), ks(0.5f), ns(10), tex(NULL) {}
|
||||
PhongMaterial(Shader* myShader, glm::vec3 myKd, glm::vec3 myKs, float myNs) : Material(myShader), kd(myKd), ks(myKs), ns(myNs), tex(NULL) {}
|
||||
virtual void bindAttributes();
|
||||
|
||||
void setTexture(Texture* myTexture);
|
||||
};
|
||||
|
||||
#endif // PHONGMATERIAL_H
|
||||
|
@ -140,3 +140,8 @@ void Shader::bindVec3(GLuint location, glm::vec3 vec)
|
||||
{
|
||||
glAssert(glUniform3fv(location, 1, glm::value_ptr(vec)));
|
||||
}
|
||||
|
||||
void Shader::bindTexture(GLuint location, GLuint tex_id)
|
||||
{
|
||||
glAssert(glUniform1i(location, tex_id));
|
||||
}
|
||||
|
1
shader.h
1
shader.h
@ -28,6 +28,7 @@ public:
|
||||
void bindFloat(GLuint location, float val);
|
||||
void bindMatrix(GLuint location, glm::mat4 mat);
|
||||
void bindVec3(GLuint location, glm::vec3 vec);
|
||||
void bindTexture(GLuint location, GLuint tex_id);
|
||||
};
|
||||
|
||||
#endif // SHADER_H
|
||||
|
47
skyboxmaterial.cpp
Normal file
47
skyboxmaterial.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include "skyboxmaterial.h"
|
||||
#include "texture.h"
|
||||
|
||||
void SkyBoxMaterial::bindAttributes()
|
||||
{
|
||||
for(int i=0; i<NB_TEXTURES; ++i)
|
||||
{
|
||||
if(skyboxTex[i] != NULL)
|
||||
{
|
||||
skyboxTex[i]->bind(i);
|
||||
shader->bindTexture(shader->getLocation("skybox_" + getDirName((DirEnum)i)), i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SkyBoxMaterial::setTexture(Texture* myTexture, DirEnum direction)
|
||||
{
|
||||
skyboxTex[direction] = myTexture;
|
||||
}
|
||||
|
||||
std::string SkyBoxMaterial::getDirName(DirEnum direction)
|
||||
{
|
||||
switch(direction)
|
||||
{
|
||||
case TOP:
|
||||
return "top";
|
||||
break;
|
||||
case BOTTOM:
|
||||
return "bottom";
|
||||
break;
|
||||
case LEFT:
|
||||
return "left";
|
||||
break;
|
||||
case RIGHT:
|
||||
return "right";
|
||||
break;
|
||||
case FRONT:
|
||||
return "front";
|
||||
break;
|
||||
case BACK:
|
||||
return "back";
|
||||
break;
|
||||
default:
|
||||
return "undefined";
|
||||
break;
|
||||
}
|
||||
}
|
22
skyboxmaterial.h
Normal file
22
skyboxmaterial.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef SKYBOXMATERIAL_H
|
||||
#define SKYBOXMATERIAL_H
|
||||
|
||||
#include "material.h"
|
||||
|
||||
class Texture;
|
||||
|
||||
class SkyBoxMaterial : public Material
|
||||
{
|
||||
public:
|
||||
typedef enum {TOP, BOTTOM, LEFT, RIGHT, FRONT, BACK, NB_TEXTURES} DirEnum;
|
||||
|
||||
SkyBoxMaterial(Shader* myShader) : Material(myShader) {}
|
||||
virtual void bindAttributes();
|
||||
void setTexture(Texture* myTexture, DirEnum direction);
|
||||
std::string getDirName(DirEnum direction);
|
||||
|
||||
private:
|
||||
Texture* skyboxTex[NB_TEXTURES];
|
||||
};
|
||||
|
||||
#endif // SKYBOXMATERIAL_H
|
@ -30,7 +30,8 @@ SOURCES += main.cpp\
|
||||
texture.cpp \
|
||||
phongmaterial.cpp \
|
||||
scenecontroller.cpp \
|
||||
sphere.cpp
|
||||
sphere.cpp \
|
||||
skyboxmaterial.cpp
|
||||
|
||||
HEADERS += mainwindow.h \
|
||||
myglwidget.h \
|
||||
@ -44,7 +45,8 @@ HEADERS += mainwindow.h \
|
||||
texture.h \
|
||||
phongmaterial.h \
|
||||
scenecontroller.h \
|
||||
sphere.h
|
||||
sphere.h \
|
||||
skyboxmaterial.h
|
||||
|
||||
FORMS += mainwindow.ui
|
||||
|
||||
|
24
texture.cpp
24
texture.cpp
@ -1,12 +1,32 @@
|
||||
#include "texture.h"
|
||||
#include "glassert.h"
|
||||
#include <QImage>
|
||||
|
||||
Texture::Texture()
|
||||
Texture::Texture(QString filename)
|
||||
{
|
||||
img = new QImage(filename);
|
||||
|
||||
int bpp = (img->depth() == 32) ? GL_RGBA : GL_RGB;
|
||||
int format = (img->depth() == 32) ? GL_BGRA : GL_BGR;
|
||||
|
||||
glAssert(glGenTextures(1, &texId));
|
||||
glAssert(glBindTexture(GL_TEXTURE_2D, texId));
|
||||
glAssert(glTexImage2D(GL_TEXTURE_2D, 0, bpp, img->width(), img->height(), 0, format, GL_UNSIGNED_BYTE, img->bits()));
|
||||
glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT));
|
||||
glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT));
|
||||
glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
|
||||
glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
|
||||
}
|
||||
|
||||
Texture::~Texture()
|
||||
{
|
||||
|
||||
glAssert(glDeleteTextures(1, &texId));
|
||||
}
|
||||
|
||||
void Texture::bind(int slot)
|
||||
{
|
||||
GLenum texSlot = GL_TEXTURE0+slot;
|
||||
glAssert(glActiveTexture(texSlot));
|
||||
glAssert(glBindTexture(GL_TEXTURE_2D, texId));
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user