Some fixes to drawing mesh

This commit is contained in:
selim mustafaev 2018-01-26 09:10:06 +03:00
parent 72708b53b5
commit fd6adee1e2
3 changed files with 44 additions and 33 deletions

View File

@ -1,9 +1,10 @@
#include "Mesh.h"
#include <glm/gtc/type_ptr.hpp>
#include <glm/vec3.hpp>
#include <iostream>
Mesh::Mesh(ShaderProgram* shader, std::size_t size) : GLObject(shader), _size(size) {
_vertexCount = 2*3*(_size - 1)*(_size - 1);
_vertexCount = 2*3*(_size + 1)*(_size + 1);
_indexCount = _vertexCount;
_vertices = std::make_unique<Vertex[]>(_vertexCount);
_indices = std::make_unique<GLuint[]>(_indexCount);
@ -39,22 +40,24 @@ VertexArray Mesh::generateVertices() const {
*/
void Mesh::generateVertices() const {
const float step = 3.0f/_size;
const float step = 1.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;
for(std::size_t j = 0; j < _size; ++j)
for(std::size_t i = 0; i <= _size; ++i) {
float x = (float)i/(_size - 0) - 0.5f;
float xStep = step;
float y = (float)j/(_size - 0) - 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 p1(x, y, heightMapFunc(i, j, x, y));
glm::vec3 p2(p1.x + xStep, 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 p4(p1.x + xStep, 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::cout << "p1 = [" << p1.x << "," << p1.y << "]" << std::endl;
std::size_t idx = (j*(_size - 1) + i)*2*3;
_vertices[idx].x = p1.x;

View File

@ -70,6 +70,9 @@ void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
}
}
void glfwErr(int code, const char* description) {
std::cout << "GLFW error - code: " << code << ", description: " << description << std::endl;
}
OGL::OGL() {
@ -102,13 +105,17 @@ void OGL::updateFpsCounter() const {
}
void OGL::init() {
glfwInit();
std::cout << glfwGetVersionString() << std::endl;
if(glfwInit() == GLFW_FALSE) {
throw new std::runtime_error("GLFW initialization error");
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint (GLFW_SAMPLES, 4);
glfwSetErrorCallback(glfwErr);
_window = glfwCreateWindow(1024, 1024, "OpenGL", nullptr, nullptr); // Windowed
glfwMakeContextCurrent(_window);

View File

@ -1,6 +1,6 @@
#include "Shaders/ShaderProgram.h"
#include "Audio/AudioPlayer.h"
#include "GLObjects/SpectralMesh.h"
#include "GLObjects/WaveMesh.h"
#include "OGL.h"
#include "Camera.h"
@ -19,34 +19,35 @@ int main(int argc, char** argv) {
ShaderProgram program("../shaders/vertex.glsl", "../shaders/fragment.glsl");
OGL::instance()->setCurShaderProgram(&program);
SpectralMesh mesh(&program, 512);
//SpectralMesh mesh(&program, 512);
WaveMesh mesh(&program, 4);
mesh.create();
OGL::instance()->addObject(&mesh);
Camera::instance()->init();
AudioPlayer player(musicFile);
float* line = new float[2048];
memset(line, 0, 2048);
player.setStreamListener([&mesh, line](float *data, std::size_t framesCount, std::size_t depth, std::size_t channels) {
memmove(line, line + 1024, 1024);
if(channels > 1) { // if we have more than one channel, use the first
for(std::size_t i = 0, j = 0; i < framesCount; i += channels, ++j) {
line[1024 + j] = data[i];
}
} else {
memcpy(line + 1024, data, 1024);
}
mesh.addLine(line, depth);
mesh.addLine(line + 256, depth);
mesh.addLine(line + 512, depth);
mesh.addLine(line + 768, depth);
});
player.play();
// AudioPlayer player(musicFile);
// float* line = new float[2048];
// memset(line, 0, 2048);
// player.setStreamListener([&mesh, line](float *data, std::size_t framesCount, std::size_t depth, std::size_t channels) {
// memmove(line, line + 1024, 1024);
// if(channels > 1) { // if we have more than one channel, use the first
// for(std::size_t i = 0, j = 0; i < framesCount; i += channels, ++j) {
// line[1024 + j] = data[i];
// }
// } else {
// memcpy(line + 1024, data, 1024);
// }
//
// mesh.addLine(line, depth);
// mesh.addLine(line + 256, depth);
// mesh.addLine(line + 512, depth);
// mesh.addLine(line + 768, depth);
// });
// player.play();
OGL::instance()->run();
delete[] line;
// delete[] line;
} catch (std::exception& ex) {
std::cout << "exception: " << ex.what() << std::endl;
}