Skip to content
Snippets Groups Projects
Select Git revision
  • c585a67955640e81b4717e6dbefcea4ccd88a73d
  • main default protected
  • Coverage
  • gitlab-ci-docker
  • test-install-requirementsshell-executor
  • 1.0
6 results

OneDimensionalEquilibrium-AnInstructiveExample.py

Blame
  • Forked from Jan Habscheid / fxdgm
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    OpenGlController.cpp 4.72 KiB
    #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);
    
    }