Basic variant of diffuse lightning, some refactorings
This commit is contained in:
parent
1e31b87a7d
commit
72708b53b5
@ -1,7 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
project(glTest)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -O1 -g -Wall")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Ofast -g -Wall")
|
||||
set(CMAKE_BUILD_TYPE DEBUG)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
#version 330 core
|
||||
|
||||
smooth in vec3 theColor;
|
||||
smooth in vec3 color;
|
||||
out vec4 frag_colour;
|
||||
|
||||
void main () {
|
||||
frag_colour = vec4(theColor, 1.0);
|
||||
frag_colour = vec4(color, 1.0);
|
||||
}
|
||||
@ -3,11 +3,12 @@
|
||||
uniform mat4 mWorld;
|
||||
uniform mat4 mView;
|
||||
uniform mat4 mProjection;
|
||||
uniform vec3 vEye;
|
||||
|
||||
layout (location = 0) in vec3 vp;
|
||||
layout (location = 1) in vec3 normals;
|
||||
layout (location = 0) in vec3 position;
|
||||
layout (location = 1) in vec3 normal;
|
||||
|
||||
smooth out vec3 theColor;
|
||||
smooth out vec3 color;
|
||||
|
||||
vec3 hsv_to_rgb(float h, float s, float v)
|
||||
{
|
||||
@ -38,6 +39,9 @@ vec3 hsv_to_rgb(float h, float s, float v)
|
||||
}
|
||||
|
||||
void main () {
|
||||
gl_Position = mProjection*mView*mWorld*vec4(vp, 1.0);
|
||||
theColor = hsv_to_rgb(vp.z, 1, 1); //vec3(1.0, 1.0, 1.0);
|
||||
gl_Position = mProjection*mView*mWorld*vec4(position, 1.0);
|
||||
|
||||
vec3 materialColor = hsv_to_rgb(position.z, 1, 1);
|
||||
float brightness = clamp(dot(normalize(normal), normalize(vEye)), 0.4, 0.6);
|
||||
color = materialColor*brightness;
|
||||
}
|
||||
|
||||
@ -18,6 +18,8 @@ void Camera::init() {
|
||||
glUniformMatrix4fv(view, 1, GL_FALSE, glm::value_ptr(_view));
|
||||
GLint projection = glGetUniformLocation(*sp, "mProjection");
|
||||
glUniformMatrix4fv(projection, 1, GL_FALSE, glm::value_ptr(_projection));
|
||||
GLint eye = glGetUniformLocation(*sp, "vEye");
|
||||
glUniform3fv(eye, 1, glm::value_ptr(_eye));
|
||||
}
|
||||
|
||||
Camera *Camera::instance() {
|
||||
@ -30,6 +32,8 @@ void Camera::updateView() {
|
||||
ShaderProgram* sp = OGL::instance()->getCurShaderProgram();
|
||||
GLint view = glGetUniformLocation(*sp, "mView");
|
||||
glUniformMatrix4fv(view, 1, GL_FALSE, glm::value_ptr(_view));
|
||||
GLint eye = glGetUniformLocation(*sp, "vEye");
|
||||
glUniform3fv(eye, 1, glm::value_ptr(_eye));
|
||||
}
|
||||
|
||||
void Camera::turnLeft(float angle) {
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
#include "GLObject.h"
|
||||
#include "../utils.h"
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
@ -13,12 +14,8 @@ GLObject::~GLObject() {
|
||||
}
|
||||
|
||||
void GLObject::create() {
|
||||
std::size_t iCount = 0, vCount = 0;
|
||||
std::unique_ptr<Vertex[]> vArray;
|
||||
std::unique_ptr<GLuint[]> iArray;
|
||||
|
||||
std::tie(vArray, vCount) = generateVertices();
|
||||
std::tie(iArray, iCount) = generateIndices();
|
||||
generateVertices();
|
||||
generateIndices();
|
||||
|
||||
GLuint vbo[2];
|
||||
glGenBuffers(2, vbo);
|
||||
@ -26,19 +23,24 @@ void GLObject::create() {
|
||||
_indexVbo = vbo[1];
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _vertexVbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, vCount, vArray.get(), GL_STREAM_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, _vertexCount*sizeof(Vertex), nullptr, GL_STREAM_DRAW);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVbo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, iCount, iArray.get(), GL_STREAM_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _indexCount*sizeof(GLuint), _indices.get(), GL_STATIC_DRAW);
|
||||
|
||||
_trianglesCount = iCount/sizeof(GLuint);
|
||||
_trianglesCount = _indexCount*sizeof(GLuint)/sizeof(GLuint);
|
||||
|
||||
glGenVertexArrays(1, &_vao);
|
||||
glBindVertexArray(_vao);
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _vertexVbo);
|
||||
|
||||
// configuring attributes of vertex
|
||||
// coordinates
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), nullptr);
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// normals
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (GLchar*)(3*sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
}
|
||||
|
||||
void GLObject::rotate(glm::vec3 angles) {
|
||||
@ -70,10 +72,8 @@ void GLObject::draw() {
|
||||
}
|
||||
|
||||
void GLObject::update() {
|
||||
std::size_t vCount = 0;
|
||||
std::unique_ptr<Vertex[]> vArray;
|
||||
std::tie(vArray, vCount) = generateVertices();
|
||||
generateVertices();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _vertexVbo);
|
||||
// glBufferData(GL_ARRAY_BUFFER, _size*_size*sizeof(Vertex), NULL, GL_STREAM_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, vCount, vArray.get(), GL_STREAM_DRAW);
|
||||
//glBufferData(GL_ARRAY_BUFFER, _vertexCount*sizeof(Vertex), _vertices.get(), GL_STATIC_DRAW);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, _vertexCount*sizeof(Vertex), _vertices.get());
|
||||
}
|
||||
|
||||
@ -9,12 +9,6 @@
|
||||
#include <memory>
|
||||
#include <GL/glew.h>
|
||||
|
||||
template <typename T>
|
||||
using ArrayWithSize = std::tuple<std::unique_ptr<T[]>,std::size_t>;
|
||||
|
||||
using VertexArray = ArrayWithSize<Vertex>;
|
||||
using IndexArray = ArrayWithSize<GLuint>;
|
||||
|
||||
class GLObject: public IGLObject {
|
||||
private:
|
||||
GLuint _vertexVbo;
|
||||
@ -24,6 +18,12 @@ private:
|
||||
ShaderProgram* _sp;
|
||||
GLuint _trianglesCount;
|
||||
|
||||
protected:
|
||||
std::unique_ptr<Vertex[]> _vertices;
|
||||
std::unique_ptr<GLuint[]> _indices;
|
||||
std::size_t _vertexCount;
|
||||
std::size_t _indexCount;
|
||||
|
||||
public:
|
||||
GLObject(ShaderProgram *shaderProgram);
|
||||
virtual ~GLObject();
|
||||
@ -36,8 +36,8 @@ public:
|
||||
void update() override final;
|
||||
|
||||
protected:
|
||||
virtual VertexArray generateVertices() const = 0;
|
||||
virtual IndexArray generateIndices() const = 0;
|
||||
virtual void generateVertices() const = 0;
|
||||
virtual void generateIndices() const = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -1,14 +1,19 @@
|
||||
#include "Mesh.h"
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/vec3.hpp>
|
||||
|
||||
Mesh::Mesh(ShaderProgram* shader, std::size_t size) : GLObject(shader), _size(size) {
|
||||
|
||||
_vertexCount = 2*3*(_size - 1)*(_size - 1);
|
||||
_indexCount = _vertexCount;
|
||||
_vertices = std::make_unique<Vertex[]>(_vertexCount);
|
||||
_indices = std::make_unique<GLuint[]>(_indexCount);
|
||||
}
|
||||
|
||||
Mesh::~Mesh() {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
VertexArray Mesh::generateVertices() const {
|
||||
std::size_t u = _size, v = _size;
|
||||
auto retVal = std::make_unique<Vertex[]>(u*v);
|
||||
@ -18,7 +23,8 @@ VertexArray Mesh::generateVertices() const {
|
||||
for (std::size_t j = 0; j < v; j++)
|
||||
{
|
||||
// Vertex vertex;
|
||||
float x = /*(float)i/(float)u*/ logf(i*step + 1)/4.0f - 0.5f;
|
||||
//float x = (float)i/(float)u
|
||||
float x = logf(i*step + 1)/4.0f - 0.5f;
|
||||
float y = (float)j/(float)v - 0.5f;
|
||||
retVal[j*u+i].coords[0] = x*3;
|
||||
retVal[j*u+i].coords[1] = y*3;
|
||||
@ -30,7 +36,77 @@ VertexArray Mesh::generateVertices() const {
|
||||
|
||||
return std::make_tuple(std::move(retVal), u*v*sizeof(Vertex));
|
||||
}
|
||||
*/
|
||||
|
||||
void Mesh::generateVertices() const {
|
||||
const float step = 3.0f/_size;
|
||||
const float expStep = (exp(4) - 1)/(_size - 2);
|
||||
|
||||
for(std::size_t j = 0; j < _size - 1; ++j)
|
||||
for(std::size_t i = 0; i < _size - 1; ++i) {
|
||||
float x = logf(i*expStep + 1)/4.0f - 0.5f; //(float)i/(_size - 0) - 0.5f;
|
||||
float xStep = logf((i + 1)*expStep + 1)/4.0f - 0.5f - x;
|
||||
float y = (float)j/(_size - 2) - 0.5f;
|
||||
//logf(j*expStep + 1)/4.0f - 0.5f;
|
||||
glm::vec3 p1(x*3, y*3, heightMapFunc(i, j, x, y));
|
||||
glm::vec3 p2(p1.x + xStep*3, p1.y, heightMapFunc(i + 1, j, x + xStep, y));
|
||||
glm::vec3 p3(p1.x, p1.y + step, heightMapFunc(i, j + 1, x, y + step));
|
||||
glm::vec3 p4(p1.x + xStep*3, p1.y + step, heightMapFunc(i + 1, j + 1, x + xStep, y + step));
|
||||
glm::vec3 norm1 = glm::cross(p2 - p1, p3 - p1);
|
||||
glm::vec3 norm2 = glm::cross(p3 - p4, p2 - p4);
|
||||
|
||||
std::size_t idx = (j*(_size - 1) + i)*2*3;
|
||||
|
||||
_vertices[idx].x = p1.x;
|
||||
_vertices[idx].y = p1.y;
|
||||
_vertices[idx].z = p1.z;
|
||||
_vertices[idx].nx = norm1.x;
|
||||
_vertices[idx].ny = norm1.y;
|
||||
_vertices[idx].nz = norm1.z;
|
||||
|
||||
idx += 1;
|
||||
_vertices[idx].x = p2.x;
|
||||
_vertices[idx].y = p2.y;
|
||||
_vertices[idx].z = p2.z;
|
||||
_vertices[idx].nx = norm1.x;
|
||||
_vertices[idx].ny = norm1.y;
|
||||
_vertices[idx].nz = norm1.z;
|
||||
|
||||
idx += 1;
|
||||
_vertices[idx].x = p3.x;
|
||||
_vertices[idx].y = p3.y;
|
||||
_vertices[idx].z = p3.z;
|
||||
_vertices[idx].nx = norm1.x;
|
||||
_vertices[idx].ny = norm1.y;
|
||||
_vertices[idx].nz = norm1.z;
|
||||
|
||||
idx += 1;
|
||||
_vertices[idx].x = p3.x;
|
||||
_vertices[idx].y = p3.y;
|
||||
_vertices[idx].z = p3.z;
|
||||
_vertices[idx].nx = norm2.x;
|
||||
_vertices[idx].ny = norm2.y;
|
||||
_vertices[idx].nz = norm2.z;
|
||||
|
||||
idx += 1;
|
||||
_vertices[idx].x = p2.x;
|
||||
_vertices[idx].y = p2.y;
|
||||
_vertices[idx].z = p2.z;
|
||||
_vertices[idx].nx = norm2.x;
|
||||
_vertices[idx].ny = norm2.y;
|
||||
_vertices[idx].nz = norm2.z;
|
||||
|
||||
idx += 1;
|
||||
_vertices[idx].x = p4.x;
|
||||
_vertices[idx].y = p4.y;
|
||||
_vertices[idx].z = p4.z;
|
||||
_vertices[idx].nx = norm2.x;
|
||||
_vertices[idx].ny = norm2.y;
|
||||
_vertices[idx].nz = norm2.z;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
IndexArray Mesh::generateIndices() const {
|
||||
unsigned u = _size, v = _size;
|
||||
auto indices = std::make_unique<GLuint[]>((u-1)*(v-1)*6);
|
||||
@ -49,4 +125,17 @@ IndexArray Mesh::generateIndices() const {
|
||||
}
|
||||
|
||||
return std::make_tuple(std::move(indices), (u-1)*(v-1)*6*sizeof(GLuint));
|
||||
}*/
|
||||
|
||||
void Mesh::generateIndices() const {
|
||||
for(std::size_t i = 0; i < _size - 1; ++i)
|
||||
for(std::size_t j = 0; j < _size - 1; ++j) {
|
||||
std::size_t idx = (i*(_size - 1) + j)*6;
|
||||
_indices[idx] = (GLuint)idx;
|
||||
_indices[idx + 1] = (GLuint)idx + 1;
|
||||
_indices[idx + 2] = (GLuint)idx + 2;
|
||||
_indices[idx + 3] = (GLuint)idx + 3;
|
||||
_indices[idx + 4] = (GLuint)idx + 4;
|
||||
_indices[idx + 5] = (GLuint)idx + 5;
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,8 +19,8 @@ public:
|
||||
virtual ~Mesh();
|
||||
|
||||
protected:
|
||||
VertexArray generateVertices() const override final;
|
||||
IndexArray generateIndices() const override final;
|
||||
void generateVertices() const override final;
|
||||
void generateIndices() const override final;
|
||||
|
||||
protected:
|
||||
virtual float heightMapFunc(std::size_t nx, std::size_t ny, float fx, float fy) const = 0;
|
||||
|
||||
@ -15,7 +15,7 @@ SpectralMesh::~SpectralMesh() {
|
||||
}
|
||||
|
||||
float SpectralMesh::heightMapFunc(std::size_t nx, std::size_t ny, float fx, float fy) const {
|
||||
return *(_data + _size*nx + ny);
|
||||
return *(_data + _size*ny + nx);
|
||||
}
|
||||
|
||||
void SpectralMesh::addLine(float *line, std::size_t depth) {
|
||||
|
||||
@ -2,8 +2,17 @@
|
||||
#define GLTEST_VERTEX_H
|
||||
|
||||
struct Vertex {
|
||||
float coords[3];
|
||||
float normal[3];
|
||||
|
||||
// coordinates
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
|
||||
// normals
|
||||
float nx;
|
||||
float ny;
|
||||
float nz;
|
||||
|
||||
};
|
||||
|
||||
#endif //GLTEST_VERTEX_H
|
||||
|
||||
Loading…
Reference in New Issue
Block a user