Functions of a class like callbacks in OpenGL c ++

0

I have had a problem in the development of a graphic engine using OpenGL. The fact is that I have defined the class DB in which I implement the method initContext(int argc, char** argv) in which I must make the corresponding calls to the callbacks. The fact is that these callbacks must be functions without a member, and as I try to introduce them through functions of the class, they are with member and give error. I would like to know how this can be solved. I attach the code of .h and .cpp

CODE OF db.h

#pragma once
#include "mesh.h"

class DB {
    std::vector<Mesh> modelos;
    std::vector<Programa> programas;
    Camara *camara;
public:
    DB();
    void initContext(int argc, char** argv);
    void initOGL();
    void mainLoop();
    void addMesh(Mesh modelo);
    void idle();
    void render();
    void resize(int width, int height);
    void mouse(int button, int state, int x, int y);
    void keyboard(unsigned char key, int x, int y);
};

CODE OF db.cpp

#include "db.h"



DB::DB() 
{
    camara = new Camara();
}



void DB::initContext(int argc, char** argv)
{
    //Creamos el contexto
    glutInit(&argc, argv);
    glutInitContextVersion(3, 3);
    glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    //Definimos el FrameBuffer y creamos la ventana
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Prácticas OGL");
    //Cargamos todas las extensiones
    glewExperimental = GL_TRUE;
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        std::cout << "Error: " << glewGetErrorString(err) << std::endl;
        exit(-1);
    }
    const GLubyte *oglVersion = glGetString(GL_VERSION);
    std::cout << "This system supports OpenGL Version: " << oglVersion << std::endl;
    //Definimos los callback
    glutReshapeFunc(resize);
    glutIdleFunc(idle);
    glutDisplayFunc(render);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
}

void DB::resize(int width, int height)
{
    glViewport(0, 0, width, height);
    DB::camara->setProj(glm::perspective(glm::radians(60.0f), float(width) / float(height), 1.0f, 50.0f));
    glutPostRedisplay();
}

void DB::initOGL()
{
    //Activamos el test de profundidad y establecemos el color del fondo
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    //Indica la orientación de la cara front, configura la etapa de rasterizado y activa el culling
    glFrontFace(GL_CCW);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glEnable(GL_CULL_FACE);
    //Define la matriz de vista y la matriz de proyección
    camara->setProj(camara->getProj() * (glm::perspective(glm::radians(60.0f), 1.0f, 0.1f, 50.0f)));
    camara->getView()[3].z = -6;
}

void DB::idle()
{
    for (int i = 0; i < modelos.size(); i++)
    {
        modelos[i].setModel(glm::rotate(glm::mat4(1), 0.0f, glm::vec3(0, 1, 0))* glm::translate(glm::mat4(1), glm::vec3(0, 0, 3)));
    }
    glutPostRedisplay();
}

void DB::render()
{
    for (int i = 0; i < modelos.size(); i++)
    {
        modelos[i].drawMesh(*(DB::camara));
    }
}

void DB::mainLoop()
{
    glutMainLoop();
}

void DB::addMesh(Mesh modelo)
{
    modelos.push_back(modelo);
}


void DB::mouse(int button, int state, int x, int y)
{

}

void DB::keyboard(unsigned char key, int x, int y)
{

}

The problem arises in the initContext method, specifically in the last five lines, that is, in relation to the callbacks. I would like to know how I can convert these functions into functions without a member.

    
asked by Ouyia 21.12.2018 в 21:55
source

1 answer

0

so I see your problem is in the following lines of the initContext () function

glutReshapeFunc(resize);
glutIdleFunc(idle);
glutDisplayFunc(render);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);

What happens is that the functions resize (), idle () render (), mouse () and keyboard () each implicitly carries an extra parameter, the this parameter. One way to solve this, is to define these functions as static functions, with the static modifier, but then another problem appears, is that you can not access the members of the class, unless you declare these also as static.

    
answered by 24.12.2018 в 04:13