put everything into one class,

Bug: still nothing displayed
This commit is contained in:
Anakin 2016-09-07 16:07:56 +02:00
parent f824f4eb4c
commit 57472da352
3 changed files with 184 additions and 74 deletions

View File

@ -1,12 +1,14 @@
#pragma once #pragma once
#include <string> #include <string>
#include <gl\glew.h>
#include <gl\glfw3.h>
#include <glm\glm.hpp> #include <glm\glm.hpp>
#include <vector> #include <vector>
#include "Camera.h"
#include "Object.h"
class OpenGLController class OpenGLController
{ {
////////////////////////////////////////////////////////////////////////////////////////////
// constructor/destructor
public: public:
static OpenGLController& getInstance(int oglMajor = 4, int oglMinor = 5); static OpenGLController& getInstance(int oglMajor = 4, int oglMinor = 5);
~OpenGLController(); ~OpenGLController();
@ -15,59 +17,90 @@ private:
OpenGLController() {}; OpenGLController() {};
OpenGLController(int oglMajor, int oglMinor); OpenGLController(int oglMajor, int oglMinor);
////////////////////////////////////////////////////////////////////////////////////////////
// member
private: private:
int iOglMajorVersion; // window
int iOglMinorVersion;
int iAntiAliasingLevel;
std::string sWindowName;
GLFWwindow* pWindow; GLFWwindow* pWindow;
std::string sWindowName;
int iWidth; int iWidth;
int iHeight; int iHeight;
// init glfw
int iOglMajorVersion;
int iOglMinorVersion;
int iAntiAliasingLevel;
GLuint gluiMatrixID; // IDs ==========================
//object data
GLuint gluiVertexArrayID;
GLuint gluiVertexBufferID;
//obj color
GLuint gluiUVBufferID;
GLuint gluiTextureID;
GLuint gluiShaderPrgmID;
GLuint gluiSamplerID; GLuint gluiSamplerID;
//obj transformation
GLuint gluiMatrixID;
// ==============================
// data
std::vector<GLfloat> vfVertices;
std::vector<GLfloat> vfUV;
// transformation ===============
//values
float fRotationX;
float fRotationY;
float fRotationZ;
double dTranslationX;
double dTranslationY;
double dTranslationZ;
//matrices
glm::mat4 m4x4Model; glm::mat4 m4x4Model;
glm::mat4 m4x4MVP; glm::mat4 m4x4View;
glm::mat4 m4x4Projection;
// ==============================
Camera* camera; // camera
Object* object; float fFOV;
float fMinView;
struct { float fMaxView;
double posX;
double posY;
bool leftHold;
bool middleHold;
bool rightHold;
double speed;
} stcMouse;
////////////////////////////////////////////////////////////////////////////////////////////
// private functions
private: private:
void initDefault();
void processInit(); void processInit();
void initDefault();
void startGLFW(); void startGLFW();
void startGLEW();
void createWindow(); void createWindow();
void startGLEW();
void setCallbackFunctions(); void setCallbackFunctions();
public:
glm::mat4 getMVPMatrix(); glm::mat4 getMVPMatrix();
GLFWwindow* getWindow() const;
////////////////////////////////////////////////////////////////////////////////////////////
// public functions
public:
// callback
void resize(int width, int height); void resize(int width, int height);
void addRotX(float value); void addRotX(float value);
void addRotY(float value); void addRotY(float value);
void addTransX(double value); void addTransX(double value);
void addTransY(double value); void addTransY(double value);
void addTransZ(double value); void addTransZ(double value);
// main routine
GLFWwindow* getWindow() const;
void updateScene(); void updateScene();
void loadMsh(const char* path);
}; };

View File

@ -4,6 +4,14 @@
#include "OpenGLController.h" #include "OpenGLController.h"
#include "callback.h" #include "callback.h"
#include <glm\gtc\matrix_transform.hpp>
#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 // public constructor/destructor
@ -24,14 +32,14 @@ OpenGLController::~OpenGLController()
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// private constructor // private constructor
OpenGLController::OpenGLController(int oglMajor, int oglMinor) : OpenGLController::OpenGLController(int oglMajor, int oglMinor)
iWidth(640),
iHeight(480)
{ {
camera = new Camera(iWidth, iHeight); // init variables
initDefault(); initDefault();
iOglMajorVersion = oglMajor; iOglMajorVersion = oglMajor;
iOglMinorVersion = oglMinor; iOglMinorVersion = oglMinor;
// run OGL
processInit(); processInit();
} }
@ -40,17 +48,34 @@ OpenGLController::OpenGLController(int oglMajor, int oglMinor) :
void OpenGLController::initDefault() void OpenGLController::initDefault()
{ {
iOglMajorVersion = 4; pWindow = NULL;
iOglMinorVersion = 5;
iAntiAliasingLevel = 4;
sWindowName = "MeshViewer 2.0 pre-alpha"; sWindowName = "MeshViewer 2.0 pre-alpha";
iWidth = 640;
iHeight = 480;
stcMouse.leftHold = false; iAntiAliasingLevel = 4;
stcMouse.rightHold = false;
stcMouse.middleHold = false; gluiUVBufferID = 0;
stcMouse.posX = 0; gluiTextureID = 0;
stcMouse.posY = 0; gluiShaderPrgmID = 0;
stcMouse.speed = 1; gluiSamplerID = 0;
gluiMatrixID = 0;
fRotationX = 0;
fRotationY = 0;
fRotationZ = 0;
dTranslationX = 0;
dTranslationY = 0;
dTranslationZ = 5;
m4x4Model = glm::mat4(1.0f);
m4x4View = glm::mat4(1.0f);
m4x4Projection = glm::mat4(1.f);
fFOV = 45.0f;
fMinView = 0.1f;
fMaxView = 100.0f;
} }
void OpenGLController::processInit() void OpenGLController::processInit()
@ -58,7 +83,6 @@ void OpenGLController::processInit()
startGLFW(); startGLFW();
createWindow(); createWindow();
startGLEW(); startGLEW();
object = new Object("");
setCallbackFunctions(); setCallbackFunctions();
// set background color // set background color
@ -71,10 +95,30 @@ void OpenGLController::processInit()
// draw vertics only from one side // draw vertics only from one side
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
// generate stuff
gluiMatrixID = glGetUniformLocation(object->getShader(), "MVP"); //TODO: color shader glGenVertexArrays(1, &gluiVertexArrayID);
gluiSamplerID = glGetUniformLocation(object->getShader(), "textureSampler");
glGenBuffers(1, &gluiVertexBufferID);
glGenBuffers(1, &gluiUVBufferID);
gluiShaderPrgmID = LoadShaders(VERTEX_SHADER, FRAGMENT_SHADER);
gluiMatrixID = glGetUniformLocation(gluiShaderPrgmID, "MVP");
gluiSamplerID = glGetUniformLocation(gluiShaderPrgmID, "textureSampler");
vfVertices = loadData();
vfUV = loadUV();
glGenTextures(1, &gluiTextureID);
glBindTexture(GL_TEXTURE_2D, gluiTextureID);
TextureTGA tempTex(TEXTURE_NAME);
glTexImage2D(GL_TEXTURE_2D, 0, tempTex.hasAlpha() ? GL_RGBA : GL_RGB, tempTex.getWidth(), tempTex.getHeight(), 0, tempTex.hasAlpha() ? GL_BGRA : 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 OpenGLController::startGLFW() void OpenGLController::startGLFW()
@ -91,18 +135,6 @@ void OpenGLController::startGLFW()
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 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() void OpenGLController::createWindow()
{ {
pWindow = glfwCreateWindow(iWidth, iHeight, sWindowName.c_str(), NULL, NULL); pWindow = glfwCreateWindow(iWidth, iHeight, sWindowName.c_str(), NULL, NULL);
@ -122,10 +154,21 @@ void OpenGLController::createWindow()
} }
glfwSetWindowUserPointer(pWindow, this); glfwSetWindowUserPointer(pWindow, this);
glfwMakeContextCurrent(pWindow); glfwMakeContextCurrent(pWindow);
} }
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::setCallbackFunctions() void OpenGLController::setCallbackFunctions()
{ {
glfwSetMouseButtonCallback(pWindow, mouseButton); glfwSetMouseButtonCallback(pWindow, mouseButton);
@ -141,7 +184,19 @@ void OpenGLController::setCallbackFunctions()
glm::mat4 OpenGLController::getMVPMatrix() glm::mat4 OpenGLController::getMVPMatrix()
{ {
return camera->getMatrix() * object->getMatrix(); 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)
);
m4x4Model = glm::mat4(1.0f);
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));
return m4x4Projection * m4x4View * m4x4Model;
} }
GLFWwindow * OpenGLController::getWindow() const GLFWwindow * OpenGLController::getWindow() const
@ -155,32 +210,33 @@ GLFWwindow * OpenGLController::getWindow() const
void OpenGLController::resize(int width, int height) void OpenGLController::resize(int width, int height)
{ {
camera->setSize(width, height); iWidth = width;
iHeight = height;
} }
void OpenGLController::addRotX(float value) void OpenGLController::addRotX(float value)
{ {
object->add2x(value); fRotationX += value;
} }
void OpenGLController::addRotY(float value) void OpenGLController::addRotY(float value)
{ {
object->add2y(value); fRotationY += value;
} }
void OpenGLController::addTransX(double value) void OpenGLController::addTransX(double value)
{ {
camera->add2x(value); dTranslationX += value;
} }
void OpenGLController::addTransY(double value) void OpenGLController::addTransY(double value)
{ {
camera->add2y(value); dTranslationY += value;
} }
void OpenGLController::addTransZ(double value) void OpenGLController::addTransZ(double value)
{ {
camera->add2z(value); dTranslationZ += value;
} }
void OpenGLController::updateScene() void OpenGLController::updateScene()
@ -190,34 +246,57 @@ void OpenGLController::updateScene()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// use shader prgm // use shader prgm
glUseProgram(object->getShader()); glUseProgram(gluiShaderPrgmID);
// tell shader transformation // tell shader transformation
glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &object->getMatrix()[0][0]); glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix()[0][0]);
// bind texture in texture unit 0 // bind texture in texture unit 0
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, object->getTextureID()); glBindTexture(GL_TEXTURE_2D, gluiTextureID);
// tell sampler to use texture unit 0 // tell sampler to use texture unit 0
glUniform1i(gluiSamplerID, 0); glUniform1i(gluiSamplerID, 0);
// open attribute position // open attribute position
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, object->getVertexBufferID()); glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// open attribute uv // open attribute uv
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, object->getUVBufferID()); glBindBuffer(GL_ARRAY_BUFFER, gluiUVBufferID);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
//draw objects //draw objects
glDrawArrays(GL_TRIANGLES, 0, object->getVertexNumber()); glDrawArrays(GL_TRIANGLES, 0, 12 * 3);
//close attributes //close attributes
glDisableVertexAttribArray(0); glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1); glDisableVertexAttribArray(1);
glfwSwapBuffers(pWindow);
glfwPollEvents();
}
void OpenGLController::loadMsh(const char * path)
{
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(vfVertices) * vfVertices.size(),
vfVertices.data(),
GL_STATIC_DRAW
);
glBindBuffer(GL_ARRAY_BUFFER, gluiUVBufferID);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(vfUV) * vfUV.size(),
vfUV.data(),
GL_STATIC_DRAW
);
} }

View File

@ -8,12 +8,10 @@ int main(int argc, char** argv)
{ {
OpenGLController scene = OpenGLController::getInstance(); OpenGLController scene = OpenGLController::getInstance();
scene.loadMsh("");
do { do {
scene.updateScene(); scene.updateScene();
glfwSwapBuffers(scene.getWindow());
glfwPollEvents();
} while (!glfwWindowShouldClose(scene.getWindow())); } while (!glfwWindowShouldClose(scene.getWindow()));
return 0; return 0;