This commit is contained in:
Gero Müller 2017-10-29 15:25:04 +10:00
parent 7e7b795fac
commit f2815b796a
6 changed files with 7245 additions and 40 deletions

View File

@ -58,6 +58,10 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action,
Application::instance().KeyCallback(window, key, scancode, action, mods);
}
static void cursor_callback(GLFWwindow* window, double x, double y) {
Application::instance().CursorCallback(window, x, y);
}
static void mouse_button_callback(GLFWwindow* window, int button, int action,
int mods) {
Application::instance().MouseButtonCallback(window, button, action, mods);
@ -120,6 +124,7 @@ void Application::init() {
std::cout << "Failed to load icon.png" << std::endl;
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetKeyCallback(window, key_callback);
glfwSetCharCallback(window, char_callback);
@ -128,6 +133,8 @@ void Application::init() {
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
glfwSwapInterval(1);
glfwGetCursorPos(window, &lastMouseX, &lastMouseY);
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
std::cout << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION)
<< std::endl;
@ -178,7 +185,9 @@ void Application::KeyCallback(GLFWwindow* window, int key, int scancode,
}
}
}
void Application::CursorCallback(GLFWwindow* window, double x, double y) {
}
void Application::MouseButtonCallback(GLFWwindow* window, int button,
int action, int mods) {
ImGui_ImplGlfwGL3_MouseButtonCallback(window, button, action, mods);
@ -441,16 +450,26 @@ void Application::prepareShader() {
glm::mat4 view;
if (fpsMode) {
glm::mat4 fpsTrafoInv = glm::inverse(fpsTrafo);
glm::vec4 movement(0.0);
if (fpsMode) {
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
movement += glm::vec4(0.0, 0.0, 0.1, 0.0);
} else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
movement -= glm::vec4(0.0, 0.0, 0.1, 0.0);
}
}
glm::vec3 absolute = glm::transpose(fpsTrafo) * movement;
double mouseX, mouseY;
glfwGetCursorPos(window, &mouseX, &mouseY);
glm::vec3 up = fpsTrafoInv * glm::vec4(0.0, 1.0, 0.0, 0.0);
glm::vec3 side = fpsTrafoInv * glm::vec4(1.0, 0.0, 0.0, 0.0);
glm::vec3 front = fpsTrafoInv * glm::vec4(0.0, 0.0, 1.0, 0.0);
fpsTrafo = glm::rotate(fpsTrafo, (float)(mouseX - lastMouseX) * 0.01f, up);
fpsTrafo = glm::rotate(fpsTrafo, (float)(mouseY - lastMouseY) * 0.01f, side);
//fpsTrafo = glm::rotate(fpsTrafo, mouseX - lastMouseX, up);
glm::vec3 absolute = fpsTrafoInv * movement;
fpsTrafo = glm::translate(fpsTrafo, absolute);
lastMouseX = mouseX;
lastMouseY = mouseY;
view = fpsTrafo;
} else {
model = glm::rotate(model, (float) glfwGetTime(), vec3(0.f, 1.f, 0.f));

View File

@ -9,7 +9,6 @@
#include <string>
struct GLFWwindow;
class Application {
@ -47,6 +46,7 @@ class Application {
glm::mat4 fpsTrafo;
bool fpsMode;
double lastMouseX, lastMouseY;
void toogleFpsMode();
public:
@ -62,6 +62,7 @@ public:
void KeyCallback(GLFWwindow* window, int key, int scancode, int action,
int mods);
void CursorCallback(GLFWwindow* window, double x, double y);
void MouseButtonCallback(GLFWwindow* window, int button, int action,
int mods);
void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset);

View File

@ -7,14 +7,12 @@
using namespace std;
using namespace glm;
void calculateNormals(const vector<vec3> &vtx, vector<uvec3> &tri,
vector<vec3> &normals) {
void calculateNormals(const vec3v_t &vtx, uvec3v_t &tri, vec3v_t &normals) {
normals.clear();
normals.resize(vtx.size(), vec3(0));
for (size_t i = 0; i < tri.size(); i++) {
int a = tri[i].x, b = tri[i].y, c = tri[i].z;
vec3 faceNormal = normalize(
cross(vtx[b] - vtx[a], vtx[c] - vtx[a]));
vec3 faceNormal = normalize(cross(vtx[b] - vtx[a], vtx[c] - vtx[a]));
normals[a] += faceNormal;
normals[b] += faceNormal;
normals[c] += faceNormal;
@ -24,7 +22,7 @@ void calculateNormals(const vector<vec3> &vtx, vector<uvec3> &tri,
}
void moveToMean(vector<vec3> &vtx) {
void moveToMean(vec3v_t &vtx) {
vec3 mean(0);
for (size_t i = 0; i < vtx.size(); i++) {
mean += vtx[i];
@ -35,8 +33,8 @@ void moveToMean(vector<vec3> &vtx) {
}
}
void smooth(vector<vec3> &vtx, const vector<uvec3> &tri) {
vector<vec3> cogs(vtx.size(), vec3(0.f));
void smooth(vec3v_t &vtx, const uvec3v_t &tri) {
vec3v_t cogs(vtx.size(), vec3(0.f));
vector<int> valence(vtx.size(), 0);
for (size_t iTri = 0; iTri < tri.size(); iTri++) {
@ -70,21 +68,20 @@ void smooth(vector<vec3> &vtx, const vector<uvec3> &tri) {
}
}
void saveAttrib(std::ostream &out, const char *prefix, vector<vec3> &elements) {
void saveAttrib(std::ostream &out, const char *prefix, vec3v_t &elements) {
for (size_t i = 0; i < elements.size(); i++) {
out << prefix << elements[i].x << " " << elements[i].y << " "
<< elements[i].z << "\n";
}
}
void saveAttrib(std::ostream &out, const char *prefix,
vector<vec2> &elements) {
void saveAttrib(std::ostream &out, const char *prefix, vec2v_t &elements) {
for (size_t i = 0; i < elements.size(); i++) {
out << prefix << elements[i].x << " " << elements[i].y << "\n";
}
}
void saveFaces(std::ostream &out, const vector<uvec3> &tris, size_t attribs) {
void saveFaces(std::ostream &out, const uvec3v_t &tris, size_t attribs) {
for (size_t i = 0; i < tris.size(); i++) {
out << "f";
@ -102,8 +99,7 @@ void saveFaces(std::ostream &out, const vector<uvec3> &tris, size_t attribs) {
}
}
void calculateEdges(const vector<vec3> &vtx, vector<uvec3> &tri,
vector<uvec4> &edges) {
void findEdges(const vec3v_t &vtx, uvec3v_t &tri, uvec4v_t &edges) {
edges.clear();
map<pair<uint32_t, uint32_t>, uint32_t> edgeMap;
for (size_t iTri = 0; iTri < tri.size(); iTri++) {
@ -122,9 +118,8 @@ void calculateEdges(const vector<vec3> &vtx, vector<uvec3> &tri,
}
}
void computeTangentBasis(vector<vec3> & vertices, vector<vec2> & uvs,
vector<vec3> & normals, vector<vec3> & tangents,
vector<vec3> & bitangents) {
void computeTangentBasis(vec3v_t & vertices, vec2v_t & uvs, vec3v_t & normals,
vec3v_t & tangents, vec3v_t & bitangents) {
for (size_t i = 0; i < vertices.size(); i += 3) {

View File

@ -6,25 +6,23 @@
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
void calculateNormals(const std::vector<glm::vec3> &vtx,
std::vector<glm::uvec3> &tri, std::vector<glm::vec3> &normals);
typedef std::vector<glm::vec3> vec3v_t;
typedef std::vector<glm::vec2> vec2v_t;
typedef std::vector<glm::uvec3> uvec3v_t;
typedef std::vector<glm::uvec4> uvec4v_t;
void moveToMean(std::vector<glm::vec3> &vtx);
void calculateNormals(const vec3v_t &vtx, uvec3v_t &tri, vec3v_t &normals);
void findEdges(const vec3v_t &vtx, uvec3v_t &tri, uvec4v_t &edges);
void smooth(std::vector<glm::vec3> &vtx, const std::vector<glm::uvec3> &tri);
void moveToMean(vec3v_t &vtx);
void saveAttrib(std::ostream &out, const char *prefix,
std::vector<glm::vec3> &elements);
void smooth(vec3v_t &vtx, const uvec3v_t &tri);
void saveAttrib(std::ostream &out, const char *prefix,
std::vector<glm::vec2> &elements);
void saveAttrib(std::ostream &out, const char *prefix, vec3v_t &elements);
void saveFaces(std::ostream &out, const std::vector<glm::uvec3> &tris,
size_t attribs);
void saveAttrib(std::ostream &out, const char *prefix, vec2v_t &elements);
void calculateEdges(const std::vector<glm::vec3> &vtx,
std::vector<glm::uvec3> &tri, std::vector<glm::uvec4> &edges);
void saveFaces(std::ostream &out, const uvec3v_t &tris, size_t attribs);
void computeTangentBasis(std::vector<glm::vec3> & vertices,
std::vector<glm::vec2> & uvs, std::vector<glm::vec3> & normals,
std::vector<glm::vec3> & tangents, std::vector<glm::vec3> & bitangents);
void computeTangentBasis(vec3v_t & vertices, vec2v_t & uvs, vec3v_t & normals,
vec3v_t & tangents, vec3v_t & bitangents);

7187
vendor/stb_image.h vendored Normal file

File diff suppressed because it is too large Load Diff

5
vendor/stb_impl.cpp vendored Normal file
View File

@ -0,0 +1,5 @@
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"