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); 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, static void mouse_button_callback(GLFWwindow* window, int button, int action,
int mods) { int mods) {
Application::instance().MouseButtonCallback(window, button, action, mods); Application::instance().MouseButtonCallback(window, button, action, mods);
@ -120,6 +124,7 @@ void Application::init() {
std::cout << "Failed to load icon.png" << std::endl; std::cout << "Failed to load icon.png" << std::endl;
glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_callback);
glfwSetScrollCallback(window, scroll_callback); glfwSetScrollCallback(window, scroll_callback);
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
glfwSetCharCallback(window, char_callback); glfwSetCharCallback(window, char_callback);
@ -128,6 +133,8 @@ void Application::init() {
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
glfwSwapInterval(1); glfwSwapInterval(1);
glfwGetCursorPos(window, &lastMouseX, &lastMouseY);
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl; std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
std::cout << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) std::cout << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION)
<< std::endl; << 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, void Application::MouseButtonCallback(GLFWwindow* window, int button,
int action, int mods) { int action, int mods) {
ImGui_ImplGlfwGL3_MouseButtonCallback(window, button, action, mods); ImGui_ImplGlfwGL3_MouseButtonCallback(window, button, action, mods);
@ -258,7 +267,7 @@ bool Application::bindVertexAttrib(GLuint id, GLint loc, GLuint size) {
size,// size size,// size
GL_FLOAT,// type GL_FLOAT,// type
GL_FALSE,// normalized? GL_FALSE,// normalized?
0,// stride 0, // stride
(void* ) 0// array buffer offset (void* ) 0// array buffer offset
)) ))
return true; return true;
@ -441,16 +450,26 @@ void Application::prepareShader() {
glm::mat4 view; glm::mat4 view;
if (fpsMode) { if (fpsMode) {
glm::mat4 fpsTrafoInv = glm::inverse(fpsTrafo);
glm::vec4 movement(0.0); glm::vec4 movement(0.0);
if (fpsMode) { if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { movement += glm::vec4(0.0, 0.0, 0.1, 0.0);
movement += glm::vec4(0.0, 0.0, 0.1, 0.0); } else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
} else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { movement -= glm::vec4(0.0, 0.0, 0.1, 0.0);
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); fpsTrafo = glm::translate(fpsTrafo, absolute);
lastMouseX = mouseX;
lastMouseY = mouseY;
view = fpsTrafo; view = fpsTrafo;
} else { } else {
model = glm::rotate(model, (float) glfwGetTime(), vec3(0.f, 1.f, 0.f)); model = glm::rotate(model, (float) glfwGetTime(), vec3(0.f, 1.f, 0.f));

View File

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

View File

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

View File

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