managed old tutorial implementation with classes.

Bugs: nothing is displayed
This commit is contained in:
Anakin 2016-09-06 15:15:29 +02:00
commit 847b3cdee8
31 changed files with 1168 additions and 0 deletions

44
MshViewer/Header/Camera.h Normal file
View File

@ -0,0 +1,44 @@
#pragma once
#include <gl\glew.h>
#include <gl\glfw3.h>
#include <glm\glm.hpp>
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);
};

47
MshViewer/Header/Object.h Normal file
View File

@ -0,0 +1,47 @@
#pragma once
#include <gl\glew.h>
#include <gl\glfw3.h>
#include <glm\glm.hpp>
#include <vector>
class Object
{
public:
Object(const char* path);
~Object();
private:
GLuint gluiVertexArrayID;
GLuint gluiVertexBufferID;
GLuint gluiUVBufferID;
GLuint gluiShaderPrgmID;
GLuint gluiTextureID;
std::vector<GLfloat> vfVertices;
std::vector<GLfloat> 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);
};

View File

@ -0,0 +1,70 @@
#pragma once
#include <string>
#include <glm\glm.hpp>
#include <vector>
#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();
};

View File

@ -0,0 +1,31 @@
#pragma once
#include <vector>
class TextureTGA
{
public:
TextureTGA(const char* filePath);
~TextureTGA();
private:
std::vector<std::uint8_t> 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<std::uint8_t> getData() const;
bool hasAlpha() const;
std::uint32_t getWidth() const;
std::uint32_t getHeight() const;
};

View File

@ -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);

View File

@ -0,0 +1,7 @@
#pragma once
#include <vector>
#include <gl\glew.h>
extern std::vector<GLfloat> loadData();
extern std::vector<GLfloat> loadUV();

View File

@ -0,0 +1,6 @@
#ifndef SHADER_HPP
#define SHADER_HPP
GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path);
#endif

View File

@ -0,0 +1,12 @@
#version 450 core
// Input
in vec3 fragmentColor;
// Ouput data
out vec3 color;
void main()
{
color = fragmentColor;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -0,0 +1,87 @@
#include "..\header\Camera.h"
#include <glm\gtc\matrix_transform.hpp>
/////////////////////////////////////////////////////////////////////////
// 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;
}

145
MshViewer/Source/Object.cpp Normal file
View File

@ -0,0 +1,145 @@
#include <gl\glew.h>
#include <gl\glfw3.h>
#include <glm\gtc\matrix_transform.hpp>
#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;
}

View File

@ -0,0 +1,223 @@
#include <gl\glew.h>
#include <gl\glfw3.h>
#include <Windows.h>
#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);
}

View File

@ -0,0 +1,106 @@
#include "Texture.h"
#include <iostream>
#include <fstream>
#include <Windows.h>
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<char*>(&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<char*>(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<std::uint8_t> 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;
}

View File

@ -0,0 +1,103 @@
//#include "callback.h"
#include <gl\glew.h>
#include <gl\glfw3.h>
#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<OpenGLController*>(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<OpenGLController*>(glfwGetWindowUserPointer(window));
if (mouse.leftHold)
{
controller->addRotX(static_cast<float>((ypos - mouse.posY) * 0.01 * mouse.speed));
controller->addRotY(static_cast<float>((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<OpenGLController*>(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;
}
}
}

View File

@ -0,0 +1,92 @@
#include <vector>
#include <fstream>
#include <Windows.h>
#include <gl\glew.h>
std::vector<GLfloat> loadData()
{
return std::vector<GLfloat>(
{
-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<GLfloat> loadUV()
{
return std::vector<float>(
{
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
}
);
}

110
MshViewer/Source/shader.cpp Normal file
View File

@ -0,0 +1,110 @@
#include <stdio.h>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#include <stdlib.h>
#include <string.h>
#include <GL/glew.h>
#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<char> 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<char> 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<char> 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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 B

BIN
MshViewer/Textures/dice.tga Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 768 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 768 KiB

20
MshViewer/main.cpp Normal file
View File

@ -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;
}