commit 847b3cdee883d86c0e7e502561efa599b9d691fa Author: Anakin Date: Tue Sep 6 15:15:29 2016 +0200 managed old tutorial implementation with classes. Bugs: nothing is displayed diff --git a/MshViewer/Header/Camera.h b/MshViewer/Header/Camera.h new file mode 100644 index 0000000..0509152 --- /dev/null +++ b/MshViewer/Header/Camera.h @@ -0,0 +1,44 @@ +#pragma once +#include +#include +#include + +class Camera +{ +public: + Camera(int width, int height); + ~Camera(); + +private: + float fFOV; + float fMinView; + float fMaxView; + + int iWidth; + int iHeight; + + double dTranslationX; + double dTranslationY; + double dTranslationZ; + + + glm::mat4 m4x4Projection; + glm::mat4 m4x4View; + +private: + void updateMatrices(); + +public: + glm::mat4 getMatrix(); + + void setFOV(float fov); + void setMinView(float distance); + void setMaxView(float distance); + void setSize(int width, int height); + + void add2x(double value); + void add2y(double value); + void add2z(double value); + + +}; \ No newline at end of file diff --git a/MshViewer/Header/Object.h b/MshViewer/Header/Object.h new file mode 100644 index 0000000..0c87730 --- /dev/null +++ b/MshViewer/Header/Object.h @@ -0,0 +1,47 @@ +#pragma once +#include +#include +#include +#include + +class Object +{ +public: + Object(const char* path); + ~Object(); + +private: + GLuint gluiVertexArrayID; + GLuint gluiVertexBufferID; + GLuint gluiUVBufferID; + + GLuint gluiShaderPrgmID; + GLuint gluiTextureID; + + std::vector vfVertices; + std::vector vfUV; + + float fRotationX; + float fRotationY; + float fRotationZ; + + glm::mat4 m4x4Model; + +private: + void processTexture(); + void calcMatrix(); + void loadMesh2OGL(); + + +public: + glm::mat4 getMatrix(); + GLuint getShader() const; + GLuint getTextureID() const; + GLuint getVertexBufferID() const; + GLuint getUVBufferID() const; + int getVertexNumber() const; + + void add2x(float value); + void add2y(float value); + void add2z(float value); +}; diff --git a/MshViewer/Header/OpenGLController.h b/MshViewer/Header/OpenGLController.h new file mode 100644 index 0000000..dd85c21 --- /dev/null +++ b/MshViewer/Header/OpenGLController.h @@ -0,0 +1,70 @@ +#pragma once +#include +#include +#include +#include "Camera.h" +#include "Object.h" + +class OpenGLController +{ +public: + OpenGLController(); + OpenGLController(int oglMajor, int oglMinor); + ~OpenGLController(); + +private: + int iOglMajorVersion; + int iOglMinorVersion; + int iAntiAliasingLevel; + std::string sWindowName; + + GLFWwindow* pWindow; + + int iWidth; + int iHeight; + + + GLuint gluiMatrixID; + GLuint gluiSamplerID; + + glm::mat4 m4x4Model; + glm::mat4 m4x4MVP; + + Camera camera; + Object* object; + + struct { + double posX; + double posY; + bool leftHold; + bool middleHold; + bool rightHold; + double speed; + } stcMouse; + + +private: + void initDefault(); + void processInit(); + void startGLFW(); + void startGLEW(); + void createWindow(); + void setCallbackFunctions(); + +public: + glm::mat4 getMVPMatrix(); + GLFWwindow* getWindow() const; + + void resize(int width, int height); + + void addRotX(float value); + void addRotY(float value); + + void addTransX(double value); + void addTransY(double value); + void addTransZ(double value); + + void updateScene(); + +}; + diff --git a/MshViewer/Header/Texture.h b/MshViewer/Header/Texture.h new file mode 100644 index 0000000..ad193d1 --- /dev/null +++ b/MshViewer/Header/Texture.h @@ -0,0 +1,31 @@ +#pragma once +#include + + +class TextureTGA +{ +public: + TextureTGA(const char* filePath); + ~TextureTGA(); + +private: + std::vector vui8Pixels; + bool bCompressed; + std::uint32_t ui32IDLength; + bool bColorTabel; + std::uint32_t ui32PicType; + std::uint32_t ui32PaletteBegin; + std::uint32_t ui32PaletteLength; + std::uint32_t ui32PaletteBpP; + std::uint32_t ui32Width; + std::uint32_t ui32Height; + std::uint32_t ui32Size; + std::uint32_t ui32BpP; + std::uint32_t ui32Attribut; + +public: + std::vector getData() const; + bool hasAlpha() const; + std::uint32_t getWidth() const; + std::uint32_t getHeight() const; +}; diff --git a/MshViewer/Header/callback.h b/MshViewer/Header/callback.h new file mode 100644 index 0000000..52de8c9 --- /dev/null +++ b/MshViewer/Header/callback.h @@ -0,0 +1,11 @@ +#pragma once + +extern void windowResize(GLFWwindow * window, int width, int height); + +extern void mouseButton(GLFWwindow *window, int button, int action, int mod); + +extern void mouseMove(GLFWwindow *window, double xpos, double ypos); + +extern void mouseWheel(GLFWwindow *window, double xoffset, double yoffset); + +extern void keyPress(GLFWwindow *window, int key, int scancode, int action, int mods); \ No newline at end of file diff --git a/MshViewer/Header/import.h b/MshViewer/Header/import.h new file mode 100644 index 0000000..ae99526 --- /dev/null +++ b/MshViewer/Header/import.h @@ -0,0 +1,7 @@ +#pragma once +#include +#include + +extern std::vector loadData(); + +extern std::vector loadUV(); diff --git a/MshViewer/Header/shader.hpp b/MshViewer/Header/shader.hpp new file mode 100644 index 0000000..3bcd95f --- /dev/null +++ b/MshViewer/Header/shader.hpp @@ -0,0 +1,6 @@ +#ifndef SHADER_HPP +#define SHADER_HPP + +GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path); + +#endif diff --git a/MshViewer/Shader/FragmentColorShader.mv2shdr b/MshViewer/Shader/FragmentColorShader.mv2shdr new file mode 100644 index 0000000..5f13d27 --- /dev/null +++ b/MshViewer/Shader/FragmentColorShader.mv2shdr @@ -0,0 +1,12 @@ +#version 450 core + +// Input +in vec3 fragmentColor; + +// Ouput data +out vec3 color; + +void main() +{ + color = fragmentColor; +} \ No newline at end of file diff --git a/MshViewer/Shader/FragmentTextureShader.mv2shdr b/MshViewer/Shader/FragmentTextureShader.mv2shdr new file mode 100644 index 0000000..2ed0112 --- /dev/null +++ b/MshViewer/Shader/FragmentTextureShader.mv2shdr @@ -0,0 +1,14 @@ +#version 450 core + +// Input +in vec2 UV; + +// Ouput data +out vec3 color; + +uniform sampler2D textureSampler; + +void main() +{ + color = texture(textureSampler, UV).rgb; +} \ No newline at end of file diff --git a/MshViewer/Shader/VertexColorShader.mv2shdr b/MshViewer/Shader/VertexColorShader.mv2shdr new file mode 100644 index 0000000..e07d841 --- /dev/null +++ b/MshViewer/Shader/VertexColorShader.mv2shdr @@ -0,0 +1,20 @@ +#version 450 core + +// Input vertex data, different for all executions of this shader. +layout(location = 0) in vec3 vertexPosition_modelspace; +layout(location = 1) in vec3 vertexColor; + +// Output +out vec3 fragmentColor; + +// Values that stay constant for the whole mesh. +uniform mat4 MVP; + +void main(){ + + // Output position of the vertex, in clip space : MVP * position + gl_Position = MVP * vec4(vertexPosition_modelspace,1); + + fragmentColor = vertexColor; + +} diff --git a/MshViewer/Shader/VertexTextureShader.mv2shdr b/MshViewer/Shader/VertexTextureShader.mv2shdr new file mode 100644 index 0000000..5776ae8 --- /dev/null +++ b/MshViewer/Shader/VertexTextureShader.mv2shdr @@ -0,0 +1,20 @@ +#version 450 core + +// Input vertex data, different for all executions of this shader. +layout(location = 0) in vec3 vertexPosition_modelspace; +layout(location = 1) in vec2 vertexUV; + +// Output +out vec2 UV; + +// Values that stay constant for the whole mesh. +uniform mat4 MVP; + +void main(){ + + // Output position of the vertex, in clip space : MVP * position + gl_Position = MVP * vec4(vertexPosition_modelspace,1); + + UV = vertexUV; + +} diff --git a/MshViewer/Source/Camera.cpp b/MshViewer/Source/Camera.cpp new file mode 100644 index 0000000..821a314 --- /dev/null +++ b/MshViewer/Source/Camera.cpp @@ -0,0 +1,87 @@ +#include "..\header\Camera.h" +#include + + +///////////////////////////////////////////////////////////////////////// +// constructor/destructor + +Camera::Camera(int width, int height): + fFOV(45), + fMinView(0.1f), + fMaxView(100.0f), + iWidth(width), + iHeight(height), + dTranslationX(0), + dTranslationY(0), + dTranslationZ(5) +{ +} + + +Camera::~Camera() +{ +} + + +///////////////////////////////////////////////////////////////////////// +// private functions + +void Camera::updateMatrices() +{ + m4x4Projection = glm::perspective(fFOV, float(iWidth) / float(iHeight), fMinView, fMaxView); + m4x4View = glm::lookAt( + glm::vec3(dTranslationX, dTranslationY, dTranslationZ), + glm::vec3(dTranslationX, dTranslationY, dTranslationZ - 1), + glm::vec3(0, 1, 0) + ); +} + + +///////////////////////////////////////////////////////////////////////// +// public getter + +glm::mat4 Camera::getMatrix() +{ + updateMatrices(); + return m4x4Projection * m4x4View; +} + + +///////////////////////////////////////////////////////////////////////// +// public setter + +void Camera::setFOV(float fov) +{ + fFOV = fov; +} + +void Camera::setMinView(float distance) +{ + fMinView = distance; +} + +void Camera::setMaxView(float distance) +{ + fMaxView = distance; +} + +void Camera::setSize(int width, int height) +{ + iWidth = width; + iHeight = height; +} + +void Camera::add2x(double value) +{ + dTranslationX += value; +} + +void Camera::add2y(double value) +{ + dTranslationY += value; +} + +void Camera::add2z(double value) +{ + dTranslationZ += value; +} diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp new file mode 100644 index 0000000..6549cc4 --- /dev/null +++ b/MshViewer/Source/Object.cpp @@ -0,0 +1,145 @@ +#include +#include +#include +#include "Object.h" +#include "shader.hpp" +#include "import.h" +#include "Texture.h" + +#define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr" +#define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr" +#define TEXTURE_NAME "Textures/dice.tga" + + +///////////////////////////////////////////////////////////////////////// +// public constructor/destructor + +Object::Object(const char* path) : + fRotationX(0), + fRotationY(0), + fRotationZ(0) +{ + m4x4Model = glm::mat4(1.0f); + + glGenVertexArrays(1, &gluiVertexArrayID); + + glGenBuffers(1, &gluiVertexBufferID); + glGenBuffers(1, &gluiUVBufferID); + + gluiShaderPrgmID = LoadShaders(VERTEX_SHADER, FRAGMENT_SHADER); + + vfVertices = loadData(); + vfUV = loadUV(); + + processTexture(); + loadMesh2OGL(); +} + +Object::~Object() +{ + glDeleteBuffers(1, &gluiUVBufferID); + glDeleteBuffers(1, &gluiVertexBufferID); + glDeleteVertexArrays(1, &gluiVertexArrayID); + glDeleteProgram(gluiShaderPrgmID); +} + + +///////////////////////////////////////////////////////////////////////// +// private functions + +void Object::processTexture() +{ + glGenTextures(1, &gluiTextureID); + glBindTexture(GL_TEXTURE_2D, gluiTextureID); + + TextureTGA tempTex(TEXTURE_NAME); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tempTex.getWidth(), tempTex.getHeight(), 0, GL_BGR, GL_UNSIGNED_BYTE, tempTex.getData().data()); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glGenerateMipmap(GL_TEXTURE_2D); +} + +void Object::calcMatrix() +{ + m4x4Model = glm::rotate(m4x4Model, fRotationX, glm::vec3(1, 0, 0)); + m4x4Model = glm::rotate(m4x4Model, fRotationY, glm::vec3(0, 1, 0)); + m4x4Model = glm::rotate(m4x4Model, fRotationZ, glm::vec3(0, 0, 1)); +} + +void Object::loadMesh2OGL() +{ + // load object to OGL + glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID); + glBufferData( + GL_ARRAY_BUFFER, + sizeof(vfVertices) * vfVertices.size(), + vfVertices.data(), + GL_STATIC_DRAW + ); + + // load UV to OGL + glBindBuffer(GL_ARRAY_BUFFER, gluiUVBufferID); + glBufferData( + GL_ARRAY_BUFFER, + sizeof(vfUV) * vfUV.size(), + vfUV.data(), + GL_STATIC_DRAW + ); +} + + +///////////////////////////////////////////////////////////////////////// +// public getter + +glm::mat4 Object::getMatrix() +{ + calcMatrix(); + return m4x4Model; +} + +GLuint Object::getShader() const +{ + return gluiShaderPrgmID; +} + +GLuint Object::getTextureID() const +{ + return gluiTextureID; +} + +GLuint Object::getVertexBufferID() const +{ + return gluiVertexBufferID; +} + +GLuint Object::getUVBufferID() const +{ + return gluiUVBufferID; +} + +int Object::getVertexNumber() const +{ + return 12*3; +} + + +///////////////////////////////////////////////////////////////////////// +// public functions + +void Object::add2x(float value) +{ + fRotationX += value; +} + +void Object::add2y(float value) +{ + fRotationY += value; +} + +void Object::add2z(float value) +{ + fRotationZ += value; +} diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp new file mode 100644 index 0000000..3b6b22c --- /dev/null +++ b/MshViewer/Source/OpenGlController.cpp @@ -0,0 +1,223 @@ +#include +#include +#include +#include "OpenGLController.h" +#include "callback.h" + + +///////////////////////////////////////////////////////////////////////// +// public constructor/destructor + +OpenGLController::OpenGLController() : + iWidth(640), + iHeight(480), + camera(iWidth, iHeight) +{ + initDefault(); + processInit(); +} + +OpenGLController::OpenGLController(int oglMajor, int oglMinor) : + iWidth(640), + iHeight(480), + camera(iWidth, iHeight) +{ + initDefault(); + iOglMajorVersion = oglMajor; + iOglMinorVersion = oglMinor; + processInit(); +} + +OpenGLController::~OpenGLController() +{ + glDeleteTextures(1, &gluiSamplerID); + glfwTerminate(); +} + + +///////////////////////////////////////////////////////////////////////// +// private functions + +void OpenGLController::initDefault() +{ + iOglMajorVersion = 4; + iOglMinorVersion = 5; + iAntiAliasingLevel = 4; + sWindowName = "MeshViewer 2.0 pre-alpha"; + + stcMouse.leftHold = false; + stcMouse.rightHold = false; + stcMouse.middleHold = false; + stcMouse.posX = 0; + stcMouse.posY = 0; + stcMouse.speed = 1; +} + +void OpenGLController::processInit() +{ + startGLFW(); + createWindow(); + startGLEW(); + object = new Object(""); + setCallbackFunctions(); + + // set background color + glClearColor(0.5000f, 0.8000f, 1.0000f, 0.0000f); + + // enable z-order + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + + // draw vertics only from one side + glEnable(GL_CULL_FACE); + + + gluiMatrixID = glGetUniformLocation(object->getShader(), "MVP"); //TODO: color shader + gluiSamplerID = glGetUniformLocation(object->getShader(), "textureSampler"); + +} + +void OpenGLController::startGLFW() +{ + if (!glfwInit()) + { + MessageBox(NULL, "Failed to initialize GLFW", "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR); + exit(0); + } + + glfwWindowHint(GLFW_SAMPLES, iAntiAliasingLevel); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, iOglMajorVersion); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, iOglMinorVersion); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); +} + +void OpenGLController::startGLEW() +{ + glewExperimental = true; + + if (glewInit() != GLEW_OK) + { + MessageBox(NULL, "Failed to initialize GLEW", "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR); + glfwTerminate(); + exit(0); + } +} + +void OpenGLController::createWindow() +{ + pWindow = glfwCreateWindow(iWidth, iHeight, sWindowName.c_str(), NULL, NULL); + + if (pWindow == NULL) + { + std::string message = "Your GPU does not support OpenGL "; + message += iOglMajorVersion; + message += "."; + message += iOglMinorVersion; + message += "\nTry to use older version"; + + MessageBox(NULL, message.c_str(), "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR); + + glfwTerminate(); + exit(0); + } + + glfwSetWindowUserPointer(pWindow, this); + + glfwMakeContextCurrent(pWindow); +} + +void OpenGLController::setCallbackFunctions() +{ + glfwSetMouseButtonCallback(pWindow, mouseButton); + glfwSetCursorPosCallback(pWindow, mouseMove); + glfwSetWindowSizeCallback(pWindow, windowResize); + glfwSetScrollCallback(pWindow, mouseWheel); + glfwSetKeyCallback(pWindow, keyPress); +} + + +///////////////////////////////////////////////////////////////////////// +// public getter + +glm::mat4 OpenGLController::getMVPMatrix() +{ + return camera.getMatrix() * object->getMatrix(); +} + +GLFWwindow * OpenGLController::getWindow() const +{ + return pWindow; +} + + +///////////////////////////////////////////////////////////////////////// +// public functions + +void OpenGLController::resize(int width, int height) +{ + camera.setSize(width, height); +} + +void OpenGLController::addRotX(float value) +{ + object->add2x(value); +} + +void OpenGLController::addRotY(float value) +{ + object->add2y(value); +} + +void OpenGLController::addTransX(double value) +{ + camera.add2x(value); +} + +void OpenGLController::addTransY(double value) +{ + camera.add2y(value); +} + +void OpenGLController::addTransZ(double value) +{ + camera.add2z(value); +} + +void OpenGLController::updateScene() +{ + // get new matrices + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // use shader prgm + glUseProgram(object->getShader()); + + // tell shader transformation + glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &object->getMatrix()[0][0]); + + // bind texture in texture unit 0 + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, object->getTextureID()); + // tell sampler to use texture unit 0 + glUniform1i(gluiSamplerID, 0); + + // open attribute position + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, object->getVertexBufferID()); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); + + // open attribute uv + glEnableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, object->getUVBufferID()); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0); + + //draw objects + glDrawArrays(GL_TRIANGLES, 0, object->getVertexNumber()); + + //close attributes + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + +} + + diff --git a/MshViewer/Source/Texture.cpp b/MshViewer/Source/Texture.cpp new file mode 100644 index 0000000..8ba9c0c --- /dev/null +++ b/MshViewer/Source/Texture.cpp @@ -0,0 +1,106 @@ +#include "Texture.h" +#include +#include +#include + +TextureTGA::TextureTGA(const char * filePath) +{ + // open the file + std::fstream fsPicture(filePath, std::ios::in | std::ios::binary); + + if (!fsPicture.is_open()) + throw std::invalid_argument(std::string("file not found: ") += filePath); + + // read in the header + std::uint8_t ui8x18Header[18] = { 0 }; + fsPicture.read(reinterpret_cast(&ui8x18Header), sizeof(ui8x18Header)); + + // extract all information from header + ui32IDLength = ui8x18Header[0]; + bColorTabel = ui8x18Header[1] == 1; + ui32PicType = ui8x18Header[2]; + ui32PaletteBegin = ui8x18Header[4] * 0x100 + ui8x18Header[3]; + ui32PaletteLength = ui8x18Header[6] * 0x100 + ui8x18Header[5]; + ui32PaletteBpP = ui8x18Header[7]; + ui32Width = ui8x18Header[13] * 0x100 + ui8x18Header[12]; + ui32Height = ui8x18Header[15] * 0x100 + ui8x18Header[14]; + ui32BpP = ui8x18Header[16]; + ui32Attribut = ui8x18Header[17]; + + // calculate some more information + ui32Size = ui32Width * ui32Height * ui32BpP/8; + bCompressed = ui32PicType == 9 || ui32PicType == 10; + vui8Pixels.resize(ui32Size); + + /* consol output of the header + std::cout << "Header\n" + << "ID länge: " << ui32IDLength << std::endl + << "Farbtabelle: " << (int)bColorTabel << std::endl + << "Bildtype: " << ui32PicType << std::endl + << "Palletenbegin: " << ui32PaletteBegin << std::endl + << "Palletenlängen: " << ui32PaletteLength << std::endl + << "Bits pro Palleteneintrag: " << ui32PaletteBpP << std::endl + << "Breite: " << ui32Width << std::endl + << "Höhe: " << ui32Height << std::endl + << "Bit pro Pixel: " << ui32BpP << std::endl + << "Bild Attribute: " << ui32Attribut << std::endl;*/ + + // jump to the data block + fsPicture.seekg(ui32IDLength + ui32PaletteLength, std::ios_base::cur); + + // If not compressed 24 or 32 bit + if (ui32PicType == 2 && (ui32BpP == 24 || ui32BpP == 32)) + { + fsPicture.read(reinterpret_cast(vui8Pixels.data()), ui32Size); + } + // else if compressed 24 or 32 bit + else if (ui32PicType == 10 && (ui32BpP == 24 || ui32BpP == 32)) // compressed + { + throw std::invalid_argument("Invaild File Format! Don't compress the image."); + } + // not useable format + else + { + fsPicture.close(); + throw std::invalid_argument("Invaild File Format! Required 24 or 31 Bit Image."); + } + + + //fix color mix + /*std::uint8_t temp; + std::uint32_t it = 0; + + while (it + 2 < ui32Size) + { + temp = vui8Pixels[it]; + vui8Pixels[it] = vui8Pixels[it + 2]; + vui8Pixels[it + 2] = temp; + ui32BpP == 32 ? it += 4 : it += 3; + }*/ + + fsPicture.close(); +} + +TextureTGA::~TextureTGA() +{ +} + +std::vector TextureTGA::getData() const +{ + return vui8Pixels; +} + +bool TextureTGA::hasAlpha() const +{ + return ui32BpP == 32; +} + +std::uint32_t TextureTGA::getWidth() const +{ + return ui32Width; +} + +std::uint32_t TextureTGA::getHeight() const +{ + return ui32Height; +} diff --git a/MshViewer/Source/callback.cpp b/MshViewer/Source/callback.cpp new file mode 100644 index 0000000..8b36647 --- /dev/null +++ b/MshViewer/Source/callback.cpp @@ -0,0 +1,103 @@ +//#include "callback.h" +#include +#include +#include "OpenGLController.h" + + +#define GLFW_KEY_PLUS_GER GLFW_KEY_RIGHT_BRACKET +#define GLFW_KEY_MINUS_GER GLFW_KEY_SLASH + +struct { + double posX; + double posY; + bool leftHold; + bool middleHold; + bool rightHold; + double speed = 1; +} mouse; + + +void windowResize(GLFWwindow * window, int width, int height) +{ + OpenGLController* controller = reinterpret_cast(glfwGetWindowUserPointer(window)); + + controller->resize(width, height); + + glViewport(0, 0, width, height); +} + +void mouseButton(GLFWwindow *window, int button, int action, int mod) +{ + if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) + { + mouse.leftHold = true; + glfwGetCursorPos(window, &mouse.posX, &mouse.posY); + } + else if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) + { + mouse.leftHold = false; + } + + if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) + { + mouse.rightHold = true; + glfwGetCursorPos(window, &mouse.posX, &mouse.posY); + } + else if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE) + { + mouse.rightHold = false; + } +} + +void mouseMove(GLFWwindow *window, double xpos, double ypos) +{ + OpenGLController* controller = reinterpret_cast(glfwGetWindowUserPointer(window)); + + if (mouse.leftHold) + { + controller->addRotX(static_cast((ypos - mouse.posY) * 0.01 * mouse.speed)); + controller->addRotY(static_cast((xpos - mouse.posX) * 0.01 * mouse.speed)); + + mouse.posX = xpos; + mouse.posY = ypos; + } + + if (mouse.rightHold) + { + controller->addTransX(-(xpos - mouse.posX) * 0.01 * mouse.speed); + controller->addTransY((ypos - mouse.posY) * 0.01 * mouse.speed); + + mouse.posX = xpos; + mouse.posY = ypos; + } +} + +void mouseWheel(GLFWwindow *window, double xoffset, double yoffset) +{ + OpenGLController* controller = reinterpret_cast(glfwGetWindowUserPointer(window)); + + controller->addTransZ(-yoffset * 0.5 * mouse.speed); +} + +void keyPress(GLFWwindow *window, int key, int scancode, int action, int mods) +{ + if (action == GLFW_PRESS || action == GLFW_REPEAT) + { + switch (key) + { + case GLFW_KEY_ESCAPE: + glfwSetWindowShouldClose(window, GLFW_TRUE); + break; + case GLFW_KEY_MINUS_GER: case GLFW_KEY_KP_SUBTRACT: + mouse.speed -= 0.1; + if (mouse.speed < 0.1) + mouse.speed = 0; + break; + case GLFW_KEY_PLUS_GER: case GLFW_KEY_KP_ADD: + mouse.speed += 0.1; + break; + default: + break; + } + } +} diff --git a/MshViewer/Source/import.cpp b/MshViewer/Source/import.cpp new file mode 100644 index 0000000..fbb3d64 --- /dev/null +++ b/MshViewer/Source/import.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include + +std::vector loadData() +{ + return std::vector( + { + -1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, -1.0f, + -1.0f, -1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + 1.0f, -1.0f, 1.0f, + -1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, -1.0f, + -1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, -1.0f, + 1.0f, -1.0f, 1.0f, + -1.0f, -1.0f, 1.0f, + -1.0f, -1.0f, -1.0f, + -1.0f, 1.0f, 1.0f, + -1.0f, -1.0f, 1.0f, + 1.0f, -1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + 1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, 1.0f + } + ); +} + +std::vector loadUV() +{ + return std::vector( + { + 0.000059f, 1.0f - 0.000004f, + 0.000103f, 1.0f - 0.336048f, + 0.335973f, 1.0f - 0.335903f, + 1.000023f, 1.0f - 0.000013f, + 0.667979f, 1.0f - 0.335851f, + 0.999958f, 1.0f - 0.336064f, + 0.667979f, 1.0f - 0.335851f, + 0.336024f, 1.0f - 0.671877f, + 0.667969f, 1.0f - 0.671889f, + 1.000023f, 1.0f - 0.000013f, + 0.668104f, 1.0f - 0.000013f, + 0.667979f, 1.0f - 0.335851f, + 0.000059f, 1.0f - 0.000004f, + 0.335973f, 1.0f - 0.335903f, + 0.336098f, 1.0f - 0.000071f, + 0.667979f, 1.0f - 0.335851f, + 0.335973f, 1.0f - 0.335903f, + 0.336024f, 1.0f - 0.671877f, + 1.000004f, 1.0f - 0.671847f, + 0.999958f, 1.0f - 0.336064f, + 0.667979f, 1.0f - 0.335851f, + 0.668104f, 1.0f - 0.000013f, + 0.335973f, 1.0f - 0.335903f, + 0.667979f, 1.0f - 0.335851f, + 0.335973f, 1.0f - 0.335903f, + 0.668104f, 1.0f - 0.000013f, + 0.336098f, 1.0f - 0.000071f, + 0.000103f, 1.0f - 0.336048f, + 0.000004f, 1.0f - 0.671870f, + 0.336024f, 1.0f - 0.671877f, + 0.000103f, 1.0f - 0.336048f, + 0.336024f, 1.0f - 0.671877f, + 0.335973f, 1.0f - 0.335903f, + 0.667969f, 1.0f - 0.671889f, + 1.000004f, 1.0f - 0.671847f, + 0.667979f, 1.0f - 0.335851f + } + ); +} diff --git a/MshViewer/Source/shader.cpp b/MshViewer/Source/shader.cpp new file mode 100644 index 0000000..02897cf --- /dev/null +++ b/MshViewer/Source/shader.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +#include +#include + +#include + +#include "shader.hpp" + +GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){ + + // Create the shaders + GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); + GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); + + // Read the Vertex Shader code from the file + std::string VertexShaderCode; + std::ifstream VertexShaderStream(vertex_file_path, std::ios::in); + if(VertexShaderStream.is_open()){ + std::string Line = ""; + while(getline(VertexShaderStream, Line)) + VertexShaderCode += "\n" + Line; + VertexShaderStream.close(); + }else{ + printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path); + getchar(); + return 0; + } + + // Read the Fragment Shader code from the file + std::string FragmentShaderCode; + std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in); + if(FragmentShaderStream.is_open()){ + std::string Line = ""; + while(getline(FragmentShaderStream, Line)) + FragmentShaderCode += "\n" + Line; + FragmentShaderStream.close(); + } + + GLint Result = GL_FALSE; + int InfoLogLength; + + + // Compile Vertex Shader + printf("Compiling shader : %s\n", vertex_file_path); + char const * VertexSourcePointer = VertexShaderCode.c_str(); + glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); + glCompileShader(VertexShaderID); + + // Check Vertex Shader + glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); + glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); + if ( InfoLogLength > 0 ){ + std::vector VertexShaderErrorMessage(InfoLogLength+1); + glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); + printf("%s\n", &VertexShaderErrorMessage[0]); + } + + + + // Compile Fragment Shader + printf("Compiling shader : %s\n", fragment_file_path); + char const * FragmentSourcePointer = FragmentShaderCode.c_str(); + glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL); + glCompileShader(FragmentShaderID); + + // Check Fragment Shader + glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); + glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); + if ( InfoLogLength > 0 ){ + std::vector FragmentShaderErrorMessage(InfoLogLength+1); + glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]); + printf("%s\n", &FragmentShaderErrorMessage[0]); + } + + + + // Link the program + printf("Linking program\n"); + GLuint ProgramID = glCreateProgram(); + glAttachShader(ProgramID, VertexShaderID); + glAttachShader(ProgramID, FragmentShaderID); + glLinkProgram(ProgramID); + + // Check the program + glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); + glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); + if ( InfoLogLength > 0 ){ + std::vector ProgramErrorMessage(InfoLogLength+1); + glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); + printf("%s\n", &ProgramErrorMessage[0]); + } + + + glDetachShader(ProgramID, VertexShaderID); + glDetachShader(ProgramID, FragmentShaderID); + + glDeleteShader(VertexShaderID); + glDeleteShader(FragmentShaderID); + + return ProgramID; +} + + diff --git a/MshViewer/Textures/10x10H.tga b/MshViewer/Textures/10x10H.tga new file mode 100644 index 0000000..6b897ce Binary files /dev/null and b/MshViewer/Textures/10x10H.tga differ diff --git a/MshViewer/Textures/10x10V.tga b/MshViewer/Textures/10x10V.tga new file mode 100644 index 0000000..05561ac Binary files /dev/null and b/MshViewer/Textures/10x10V.tga differ diff --git a/MshViewer/Textures/256x256H.tga b/MshViewer/Textures/256x256H.tga new file mode 100644 index 0000000..49beceb Binary files /dev/null and b/MshViewer/Textures/256x256H.tga differ diff --git a/MshViewer/Textures/4x4H16.tga b/MshViewer/Textures/4x4H16.tga new file mode 100644 index 0000000..981a422 Binary files /dev/null and b/MshViewer/Textures/4x4H16.tga differ diff --git a/MshViewer/Textures/dice.tga b/MshViewer/Textures/dice.tga new file mode 100644 index 0000000..722da4f Binary files /dev/null and b/MshViewer/Textures/dice.tga differ diff --git a/MshViewer/Textures/texture16.tga b/MshViewer/Textures/texture16.tga new file mode 100644 index 0000000..0320a47 Binary files /dev/null and b/MshViewer/Textures/texture16.tga differ diff --git a/MshViewer/Textures/texture24.tga b/MshViewer/Textures/texture24.tga new file mode 100644 index 0000000..ebdef93 Binary files /dev/null and b/MshViewer/Textures/texture24.tga differ diff --git a/MshViewer/Textures/texture24R.tga b/MshViewer/Textures/texture24R.tga new file mode 100644 index 0000000..c5d5391 Binary files /dev/null and b/MshViewer/Textures/texture24R.tga differ diff --git a/MshViewer/Textures/texture32.tga b/MshViewer/Textures/texture32.tga new file mode 100644 index 0000000..b54937f Binary files /dev/null and b/MshViewer/Textures/texture32.tga differ diff --git a/MshViewer/Textures/texture32R.tga b/MshViewer/Textures/texture32R.tga new file mode 100644 index 0000000..6b996ed Binary files /dev/null and b/MshViewer/Textures/texture32R.tga differ diff --git a/MshViewer/Textures/uvtemplate.DDS b/MshViewer/Textures/uvtemplate.DDS new file mode 100644 index 0000000..3e9afee Binary files /dev/null and b/MshViewer/Textures/uvtemplate.DDS differ diff --git a/MshViewer/Textures/uvtemplate.tga b/MshViewer/Textures/uvtemplate.tga new file mode 100644 index 0000000..4b0fac0 Binary files /dev/null and b/MshViewer/Textures/uvtemplate.tga differ diff --git a/MshViewer/main.cpp b/MshViewer/main.cpp new file mode 100644 index 0000000..39ef0a7 --- /dev/null +++ b/MshViewer/main.cpp @@ -0,0 +1,20 @@ +#ifndef _DEBUG +#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) +#endif // DEBUG + +#include "OpenGLController.h" + +int main(int argc, char** argv) +{ + OpenGLController scene; + + do { + + scene.updateScene(); + + glfwSwapBuffers(scene.getWindow()); + glfwPollEvents(); + } while (!glfwWindowShouldClose(scene.getWindow())); + + return 0; +} \ No newline at end of file