added first version of noise texture

This commit is contained in:
Anselme 2015-07-02 19:29:05 +02:00
parent 8d3331dce1
commit d2244fa274
5 changed files with 63 additions and 4 deletions

View File

@ -18,6 +18,11 @@ void Entity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix)
mesh->draw(); mesh->draw();
} }
glm::mat4* Entity::getTransform()
{
return &modelMatrix;
}
Shader* Entity::getShader() Shader* Entity::getShader()
{ {
return mat->getShader(); return mat->getShader();

View File

@ -17,6 +17,7 @@ protected:
public: public:
Entity(Entity* myParent, Mesh* myMesh, Material* myMat) : parent(myParent), mesh(myMesh), mat(myMat) {} Entity(Entity* myParent, Mesh* myMesh, Material* myMat) : parent(myParent), mesh(myMesh), mat(myMat) {}
virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix); virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix);
glm::mat4* getTransform();
Shader* getShader(); Shader* getShader();
Material* getMaterial(); Material* getMaterial();
}; };

View File

@ -8,10 +8,12 @@
#include "skybox.h" #include "skybox.h"
#include "utils.h" #include "utils.h"
#include "texture.h" #include "texture.h"
#include "gridmesh.h"
#include "sparrowrenderer.h" #include "sparrowrenderer.h"
#include "focuscontroller.h" #include "focuscontroller.h"
#include <iostream> #include <iostream>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/ext.hpp>
#include <QKeyEvent> #include <QKeyEvent>
#include <QMouseEvent> #include <QMouseEvent>
@ -46,19 +48,23 @@ Scene* MyGLWidget::buildScene()
SkyBox* skybox = new SkyBox(filenames); SkyBox* skybox = new SkyBox(filenames);
scene->addEntity(skybox); scene->addEntity(skybox);
Mesh* myGrid = new Sphere(2); Mesh* myGrid = new GridMesh(10, 10, true); //new Sphere(2);
myGrid->initGL(); myGrid->initGL();
std::string vertSource = Utils::fileToString("../phong.vert"); std::string vertSource = Utils::fileToString("../phong.vert");
std::string fragSource = Utils::fileToString("../phong.frag"); std::string fragSource = Utils::fileToString("../phong.frag");
Shader* shader = new Shader(vertSource, fragSource); Shader* shader = new Shader(vertSource, fragSource);
PhongMaterial* mat = new PhongMaterial(shader, glm::vec3(1), glm::vec3(1), 20.0f); PhongMaterial* mat = new PhongMaterial(shader, glm::vec3(1), glm::vec3(1), 20.0f);
Texture* tex = new Texture("../data/noise.png"); Texture* tex = new Texture(); // new Texture("../data/noise.png");
mat->setTexture(tex); mat->setTexture(tex);
scene->addEntity(myGrid, mat); Entity* myEntity = new Entity(NULL, myGrid, mat);
glm::mat4* transform = myEntity->getTransform();
*transform = glm::translate(glm::rotate(glm::scale(*transform, glm::vec3(4)), 3.1416f/2, glm::vec3(-1, 0, 0)), glm::vec3(-0.5f, -0.5f, 0));
scene->addEntity(myEntity);
scene->addDirectionnalLight(glm::vec3(6, 4, -4), glm::vec3(0.7f, 0.6f, 0.4f)); // sun scene->addDirectionnalLight(glm::vec3(6, 4, -4), glm::vec3(0.7f, 0.6f, 0.4f)); // sun
scene->addPointLight(glm::vec3(0, 0, 4), glm::vec3(0.7f, 0.6f, 0.4f));
return scene; return scene;
} }

View File

@ -1,6 +1,17 @@
#include "texture.h" #include "texture.h"
#include "glassert.h" #include "glassert.h"
#include "utils.h" #include "utils.h"
#include <glm/gtc/noise.hpp>
Texture::Texture() : type(GL_TEXTURE_2D)
{
glAssert(glGenTextures(1, &texId));
glAssert(glBindTexture(type, texId));
createNoiseTexture(GL_TEXTURE_2D, 512, 512, 10, 1);
setWrap(GL_REPEAT);
setFiltering(GL_LINEAR);
}
Texture::Texture(const std::string filename) : type(GL_TEXTURE_2D) Texture::Texture(const std::string filename) : type(GL_TEXTURE_2D)
{ {
@ -31,6 +42,39 @@ Texture::~Texture()
glAssert(glDeleteTextures(1, &texId)); glAssert(glDeleteTextures(1, &texId));
} }
void Texture::createNoiseTexture(GLenum textureSlot, int width, int height, float frequency, float myScale)
{
GLubyte *data = new GLubyte[ width * height * 4 ];
float xFactor = 1.0f / (width - 1);
float yFactor = 1.0f / (height - 1);
for( int row = 0; row < height; row++ ) {
for( int col = 0 ; col < width; col++ ) {
float x = xFactor * col;
float y = yFactor * row;
float sum = 0.0f;
float freq = frequency;
float scale = myScale;
// Compute the sum for each octave
for( int oct = 0; oct < 4; oct++ ) {
glm::vec2 p(x * freq, y * freq);
float val = glm::perlin(p, glm::vec2(freq)) / scale;
sum += val;
float result = (sum + 1.0f)/ 2.0f;
// Store in texture buffer
data[((row * width + col) * 4) + oct] =
(GLubyte) ( result * 255.0f );
freq *= 2.0f; // Double the frequency
scale *= myScale; // Next power of b
}
}
}
glAssert(glTexImage2D(textureSlot, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data));
}
void Texture::createTexture(std::string filename, GLenum textureSlot) void Texture::createTexture(std::string filename, GLenum textureSlot)
{ {
Utils::Image img(filename); Utils::Image img(filename);

View File

@ -12,10 +12,13 @@ private:
GLuint texId; GLuint texId;
GLenum type; GLenum type;
void createTexture(std::string filename, GLenum type); void createNoiseTexture(GLenum textureSlot, int width, int height, float frequency, float myScale);
void createTexture(std::string filename, GLenum textureSlot);
void setWrap(GLint wrap); void setWrap(GLint wrap);
void setFiltering(GLint filter); void setFiltering(GLint filter);
public: public:
// creates a 2D texture from perlin noise
Texture();
// creates a standard texture from an image // creates a standard texture from an image
Texture(const std::string filename); Texture(const std::string filename);
// creates a cubeMap from 6 images // creates a cubeMap from 6 images