Commit 847b3cde authored by Anakin's avatar Anakin
Browse files

managed old tutorial implementation with classes.

Bugs: nothing is displayed
parents
#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);
};
\ No newline at end of file
#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);
};
#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();
};
#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;
};
#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
#pragma once
#include <vector>
#include <gl\glew.h>
extern std::vector<GLfloat> loadData();
extern std::vector<GLfloat> loadUV();
#ifndef SHADER_HPP
#define SHADER_HPP
GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path);
#endif
#version 450 core
// Input
in vec3 fragmentColor;
// Ouput data
out vec3 color;
void main()
{
color = fragmentColor;
}
\ No newline at end of file
#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
#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;
}
#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;
}
#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;
}
#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;
}
#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)