fixed normals calculating (partially)
This commit is contained in:
parent
0acfbcd02c
commit
3aba1f0648
@ -1,7 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
project(glTest)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Og -g -Wall")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -O0 -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/")
|
||||
|
||||
@ -40,7 +40,7 @@ vec3 hsv_to_rgb(float h, float s, float v)
|
||||
|
||||
void main () {
|
||||
vec3 materialColor = hsv_to_rgb(zPos, 1, 1);
|
||||
float diffuse = clamp(dot(norm, -normalize(vec3(1,1,1))), 0.1, 0.6);
|
||||
float diffuse = clamp(dot(norm, -normalize(vec3(1,1,1))), 0.0, 0.6);
|
||||
vec3 color = materialColor*diffuse;
|
||||
|
||||
frag_colour = vec4(color, 1.0);
|
||||
|
||||
@ -11,40 +11,8 @@ layout (location = 1) in vec3 normal;
|
||||
out float zPos;
|
||||
out vec3 norm;
|
||||
|
||||
vec3 hsv_to_rgb(float h, float s, float v)
|
||||
{
|
||||
float c = v * s;
|
||||
h = mod((h * 6.0), 6.0);
|
||||
float x = c * (1.0 - abs(mod(h, 2.0) - 1.0));
|
||||
vec3 color;
|
||||
|
||||
if (0.0 <= h && h < 1.0) {
|
||||
color = vec3(c, x, 0.0);
|
||||
} else if (1.0 <= h && h < 2.0) {
|
||||
color = vec3(x, c, 0.0);
|
||||
} else if (2.0 <= h && h < 3.0) {
|
||||
color = vec3(0.0, c, x);
|
||||
} else if (3.0 <= h && h < 4.0) {
|
||||
color = vec3(0.0, x, c);
|
||||
} else if (4.0 <= h && h < 5.0) {
|
||||
color = vec3(x, 0.0, c);
|
||||
} else if (5.0 <= h && h < 6.0) {
|
||||
color = vec3(c, 0.0, x);
|
||||
} else {
|
||||
color = vec3(0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
color.rgb += v - c;
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
void main () {
|
||||
norm = -normalize(vec3(mProjection*mView*mWorld*vec4(normal, 0)));
|
||||
zPos = position.z;
|
||||
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;
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ Mesh::Mesh(ShaderProgram* shader, std::size_t size) : GLObject(shader),
|
||||
_size(size),
|
||||
_xs(size),
|
||||
_ys(size),
|
||||
_normals(2*size*size) {
|
||||
_normals(2*(size-1)*(size-1)) {
|
||||
_vertexCount = _size*_size;
|
||||
_indexCount = (_size - 1)*(_size - 1)*6;
|
||||
_vertices = std::make_unique<Vertex[]>(_vertexCount);
|
||||
@ -32,8 +32,8 @@ void Mesh::generateVertices() const {
|
||||
}
|
||||
}
|
||||
|
||||
inline size_t Mesh::normalIndex(size_t x, size_t y) const {
|
||||
return 2*(x + y*_size);
|
||||
inline ptrdiff_t Mesh::normalIndex(ptrdiff_t x, ptrdiff_t y) const {
|
||||
return 2*(x + y*(_size - 1));
|
||||
}
|
||||
|
||||
void Mesh::updateVertices() const {
|
||||
@ -46,8 +46,8 @@ void Mesh::updateVertices() const {
|
||||
|
||||
// 1. Calc normals for all triangles
|
||||
_normals.clear();
|
||||
for(std::size_t j = 0; j < _size; ++j) {
|
||||
for(std::size_t i = 0; i < _size; ++i) {
|
||||
for(std::size_t j = 0; j < _size - 1; ++j) {
|
||||
for(std::size_t i = 0; i < _size - 1; ++i) {
|
||||
float x = _xs[i], x2 = _xs[i + 1];
|
||||
float y = _ys[j], y2 = _ys[j + 1];
|
||||
|
||||
@ -65,30 +65,44 @@ void Mesh::updateVertices() const {
|
||||
|
||||
_normals.emplace_back(norm1);
|
||||
_normals.emplace_back(norm2);
|
||||
|
||||
// std::cout << "normal: [" << norm1.x << ", " << norm1.y << ", " << norm1.z << "]" << std::endl;
|
||||
// std::cout << "normal: [" << norm2.x << ", " << norm2.y << ", " << norm2.z << "]" << std::endl;
|
||||
//
|
||||
// if(norm1.z > 0 || norm2.z > 0) {
|
||||
// std::cout << "j = " << j << ", i = " << i << std::endl;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Calc normals for all vertices based on precalculated normals for triangles
|
||||
// Normal of vertex is an average of normals of adjacent triangles
|
||||
size_t normalsCount = 2*(_size - 1)*(_size - 1);
|
||||
for(std::size_t j = 0; j < _size; ++j) {
|
||||
for (std::size_t i = 0; i < _size; ++i) {
|
||||
glm::vec3 normal;
|
||||
|
||||
size_t idx1 = normalIndex(i - 1, j - 1);
|
||||
for(size_t k = idx1; k < idx1 + 3; ++k) {
|
||||
if(k >= 0) {
|
||||
ptrdiff_t idx1 = normalIndex(i - 1, j - 1);
|
||||
for(ptrdiff_t k = idx1; k < idx1 + 3; ++k) {
|
||||
if(k >= 0 && k < normalsCount) {
|
||||
normal += _normals[k];
|
||||
}
|
||||
}
|
||||
|
||||
size_t idx2 = normalIndex(i - 1, j);
|
||||
for(size_t k = idx2; k < idx2 + 3; ++k) {
|
||||
if(k >= 0) {
|
||||
ptrdiff_t idx2 = normalIndex(i - 1, j);
|
||||
for(ptrdiff_t k = idx2; k < idx2 + 3; ++k) {
|
||||
if(k >= 0 && k < normalsCount) {
|
||||
normal += _normals[k];
|
||||
}
|
||||
}
|
||||
|
||||
size_t idx = j*(_size - 1) + i;
|
||||
// if(normal.z == 0) {
|
||||
// std::cout << "j = " << j << ", i = " << i << std::endl;
|
||||
// }
|
||||
//
|
||||
// std::cout << "normal: [" << normal.x << ", " << normal.y << ", " << normal.z << "]" << std::endl;
|
||||
|
||||
size_t idx = j*_size + i;
|
||||
_vertices[idx].nx = normal.x;
|
||||
_vertices[idx].ny = normal.y;
|
||||
_vertices[idx].nz = normal.z;
|
||||
|
||||
@ -25,7 +25,7 @@ public:
|
||||
virtual ~Mesh();
|
||||
|
||||
private:
|
||||
size_t normalIndex(size_t x, size_t y) const;
|
||||
ptrdiff_t normalIndex(ptrdiff_t x, ptrdiff_t y) const;
|
||||
|
||||
protected:
|
||||
void generateVertices() const override final;
|
||||
|
||||
10
src/OGL.cpp
10
src/OGL.cpp
@ -54,11 +54,11 @@ void cursorPositionCallback(GLFWwindow* window, double xpos, double ypos) {
|
||||
float dy = (float)(ypos - lastY);
|
||||
static const float coeff = 0.01f;
|
||||
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) {
|
||||
//Camera::instance()->mouseMoveAroundCenter(coeff*dx, coeff*dy);
|
||||
for(IGLObject* obj: OGL::instance()->_glObjects) {
|
||||
obj->rotate(glm::vec3(coeff*dy, 0.f, coeff*dx));
|
||||
obj->update();
|
||||
}
|
||||
Camera::instance()->mouseMoveAroundCenter(coeff*dx, coeff*dy);
|
||||
// for(IGLObject* obj: OGL::instance()->_glObjects) {
|
||||
// obj->rotate(glm::vec3(coeff*dy, 0.f, coeff*dx));
|
||||
// obj->update();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -21,7 +21,7 @@ int main(int argc, char** argv) {
|
||||
OGL::instance()->setCurShaderProgram(&program);
|
||||
|
||||
// SpectralMesh mesh(&program, 512);
|
||||
WaveMesh mesh(&program, 256);
|
||||
WaveMesh mesh(&program, 128);
|
||||
mesh.create();
|
||||
OGL::instance()->addObject(&mesh);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user