0
I have a class where, within a non-static method, I want to make a call to another non-static one as well. However, by doing so, Visual Studio already gives this warning: "A reference to a non-static member must be relative to the specific object".
The point is that I’m calling the method the same class, within itself. In Java I do this without having to declare an instance of the class itself within it (that would be a dumb redundancy).
How to solve this?
Just follow my methods:
Method that is called:
//4- Cria os vértices e seu VBO e VAO. Define a cor a partir de um Uniform
void SceneManager::setupScene()
{
GLuint VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
GLint colorLoc = glGetUniformLocation(shader->Program, "color");
//assert(colorLoc > -1);
glUseProgram(shader->Program);
glUniform4f(colorLoc, 1.0f, 0.0, 0.2f, 1.0f);
Method that calls:
void SceneManager::key_callback(GLFWwindow * window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (key >= 0 && key < 1024)
{
if (action == GLFW_PRESS)
keys[key] = true;
else if (action == GLFW_RELEASE)
keys[key] = false;
}
// O ERRO É SINALIZADO PELA IDE AQUI: QUANDO EU CHAMO O SETUPSCENE
setupScene();
switch (key)
{
case 65: //tecla A
case 97: //tecla a
vertices[0] = vertices[0]--;
break;
case 68: //tecla D
case 100: //tecla d
cout << "direita" << endl;
break;
case 83: //tecla S
case 115: //tecla s
cout << "baixo" << endl;
break;
case 87: //tecla W
case 119: //tecla w
cout << "cima" << endl;
}
}
In a previous method, within the same class, setupScene() was already being called, without any error. Because in the other method it accuses this error? ::
*2 -INICIALIZA A JANELA GRÁFICA, INICIA O MÉTODO addShader() BUSCA OS DOIS SHADERS E SALVA NUMA VARIÁVEL,
, INICIA O MÉTODO setupScene(), que cira o VBO e VAO dos vértices e define a cor via Uniform*/
void SceneManager::initializeGraphics()
{
// Init GLFW
glfwInit();
// Create a GLFWwindow object that we can use for GLFW's functions
window = glfwCreateWindow(width, height, "Hello Transform", nullptr, nullptr);
glfwMakeContextCurrent(window);
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
//Setando a callback de redimensionamento da janela
glfwSetWindowSizeCallback(window, resize);
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
glewInit();
// Build and compile our shader program
addShader("../shaders/transformations.vs", "../shaders/transformations.frag");
//setup the scene -- LEMBRANDO QUE A DESCRIÇÃO DE UMA CENA PODE VIR DE ARQUIVO(S) DE
// CONFIGURAÇÃO
setupScene();
resized = true; //para entrar no setup da câmera na 1a vez
}
Certainty that the method Scenemanager::key_callback is not declared static in class definition? Function callback signature glfwSetKeyCallback should not accept this method because its first parameter is the class instance (*this) if it is not static, and it differs from the signature defined as a parameter of glfwSetKeyCallback:
typedef void(* GLFWkeyfun) (GLFWwindow *, int, int, int, int)
– Gomiero
@Gomiero You’re right. In the class definition Scenemanager::key_callback is static. But how can I then capture the keystroke and make it trigger changes in my VAO? I can’t declare Setupscene static either? Because it initializes this VAO object, and it’s a class object...
– Lucas Pletsch
The idea is that by pressing certain keys, transformations are applied to my triangle stored in the VAO.
– Lucas Pletsch
Has a function glfwSetWindowUserPointer which allows you to store a pointer throughout the window life cycle (
GLFWwindow
). I don’t know if the output is good, but you can store the instance pointer (this) with this function right after creating the window and inside the callback, get that value and make a cast for a class type variable and access it from there– Gomiero