From 370a073dcb28a7e096a1cce2a1df80a558d6c62c Mon Sep 17 00:00:00 2001 From: Anselme Date: Mon, 29 Jun 2015 16:01:51 +0200 Subject: [PATCH] implemented Texture and added skybox material --- phongmaterial.cpp | 12 ++++++++++++ phongmaterial.h | 9 +++++++-- shader.cpp | 5 +++++ shader.h | 1 + skyboxmaterial.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++ skyboxmaterial.h | 22 +++++++++++++++++++++ sparrowRenderer.pro | 6 ++++-- texture.cpp | 24 +++++++++++++++++++++-- texture.h | 11 ++++++++++- 9 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 skyboxmaterial.cpp create mode 100644 skyboxmaterial.h diff --git a/phongmaterial.cpp b/phongmaterial.cpp index 7a5cf6d..5f724f9 100644 --- a/phongmaterial.cpp +++ b/phongmaterial.cpp @@ -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; +} diff --git a/phongmaterial.h b/phongmaterial.h index 88c401a..d25e977 100644 --- a/phongmaterial.h +++ b/phongmaterial.h @@ -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 diff --git a/shader.cpp b/shader.cpp index 502a43a..988c571 100644 --- a/shader.cpp +++ b/shader.cpp @@ -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)); +} diff --git a/shader.h b/shader.h index 2274150..b97403a 100644 --- a/shader.h +++ b/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 diff --git a/skyboxmaterial.cpp b/skyboxmaterial.cpp new file mode 100644 index 0000000..e35b0d0 --- /dev/null +++ b/skyboxmaterial.cpp @@ -0,0 +1,47 @@ +#include "skyboxmaterial.h" +#include "texture.h" + +void SkyBoxMaterial::bindAttributes() +{ + for(int i=0; ibind(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; + } +} diff --git a/skyboxmaterial.h b/skyboxmaterial.h new file mode 100644 index 0000000..8d7737e --- /dev/null +++ b/skyboxmaterial.h @@ -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 diff --git a/sparrowRenderer.pro b/sparrowRenderer.pro index 77e88bb..2a3ac37 100644 --- a/sparrowRenderer.pro +++ b/sparrowRenderer.pro @@ -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 diff --git a/texture.cpp b/texture.cpp index f0a8877..588f051 100644 --- a/texture.cpp +++ b/texture.cpp @@ -1,12 +1,32 @@ #include "texture.h" +#include "glassert.h" +#include -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)); } diff --git a/texture.h b/texture.h index 7a1d64e..2019be2 100644 --- a/texture.h +++ b/texture.h @@ -1,12 +1,21 @@ #ifndef TEXTURE_H #define TEXTURE_H +#include + +class QImage; +class QString; class Texture { +private: + GLuint texId; + QImage* img; + public: - Texture(); + Texture(QString filename); ~Texture(); + void bind(int slot); }; #endif // TEXTURE_H