tengo un problema, porque tengo que crear un programa que visualice los ejes de coordenadas y un cubo (OpenGL y Microsoft Visual Studio), hasta aquí bien. Pero después tengo que crear de alguna forma una funcionalidad y es que cuando presione una tecla, los ejes de coordenadas se escondan o no. Tengo lo siguiente:
Cabecera de la clase:
#ifndef __CGVINTERFACE
#define __CGVINTERFACE
#include <GL/glut.h>
#include <string>
#include <iostream>
using namespace std;
class cgvInterface {
protected:
// Attributes
int width_window; // initial width of the display window
int height_window; // initial height of the display window
// TODO: Practice 1: Declare the boolean variable to control whether the axes are visualized or not.
bool axes = true;
public:
// Default constructor and destructor
cgvInterface();
~cgvInterface();
// Static methods
// event callbacks
static void set_glutKeyboardFunc(unsigned char key, int x, int y); // method to control keyboard events
static void set_glutReshapeFunc(int w, int h); // method to define the camera and the viewport
// it is automatically called when the window is resized
static void set_glutDisplayFunc(); // method to render the scene
// Methods
// initialize all the parameters to create a display window
void configure_environment(int argc, char** argv, // main parameters
int _width_window, int _height_window, // width and height of the display window
int _pos_X, int _pos_Y, // init position of the display window
string _title // title of the display window
);
void init_callbacks(); // init all the callbacks
void init_rendering_loop(); // render the scene and wait for an event in the interface
// methods get_ and set_ to access the attributes
int get_width_window(){return width_window;};
int get_height_window(){return height_window;};
void set_width_window(int _width_window){width_window = _width_window;};
void set_height_window(int _height_window){height_window = _height_window;};
// TODO: practice 1: specify the corresponding methods get_axes() and set_axes() to get the value of the boolean variable axes and to set it, respctively.
void set_axes(bool *axes);
bool get_axes();
};
#endif
Y este es su implementación:
#include <cstdlib>
#include "cgvInterface.h"
extern cgvInterface interface; // the callbacks must be static and this object is required to access to the variables of the class // ellos a las variables de la clase
// Constructor and destructor methods -----------------------------------
cgvInterface::cgvInterface () {
// TODO: Practice 1: Initialize the variable 'axes' to TRUE.
axes = true;
}
cgvInterface::~cgvInterface () {}
// Public methods ----------------------------------------
void cgvInterface::configure_environment(int argc, char** argv,
int _width_window, int _height_window,
int _pos_X, int _pos_Y,
string _title)
{
// initialization of the interface variables
width_window = _width_window;
height_window = _height_window;
// initialization of the display window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(_width_window,_height_window);
glutInitWindowPosition(_pos_X,_pos_Y);
glutCreateWindow(_title.c_str());
glEnable(GL_DEPTH_TEST); // enable the removal of hidden surfaces by using the z-buffer
glClearColor(1.0,1.0,1.0,0.0); // define the background color of the window
}
void cgvInterface::init_rendering_loop() {
glutMainLoop(); // initialize the visualization loop of OpenGL
}
void cgvInterface::set_glutKeyboardFunc(unsigned char key, int x, int y) {
switch (key) {
// TODO: practice 1: Add the code to enable/disable the visualization of the axes after pressing 'a'.
case 'a':
if (interface.get_axes() == true)
{
bool axes = true;
interface.set_axes(&axes);
}
else
{
bool axes = false;
interface.set_axes(&axes);
}
break;
case 27: // Escape key to exit
exit(1);
break;
}
glutPostRedisplay(); // renew the content of the window
}
void cgvInterface::set_glutReshapeFunc(int w, int h) {
// dimension of the viewport with a new width and a new height of the display window
glViewport(0,0,(GLsizei) w,(GLsizei) h);
// store the new values of the viewport and the display window.
interface.set_width_window(w);
interface.set_height_window(h);
// Set up the kind of projection to be used
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1,1,-1,1,-1,200);
// it defines the camera
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.5,1.0,2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void cgvInterface::set_glutDisplayFunc() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the window and the depth buffer
glPushMatrix(); // store the model matrix
// TODO: Practice 1: Draw the axis
if (interface.get_axes() == true)
{
glColor3f(1.0, 0.0, 0.0); // red x
glBegin(GL_LINES);
// x aix
glVertex3f(-4.0, 0.0f, 0.0f);
glVertex3f(4.0, 0.0f, 0.0f);
// arrow
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.0, 1.0f, 0.0f);
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.0, -1.0f, 0.0f);
glEnd();
glFlush();
// y
glColor3f(0.0, 1.0, 0.0); // green y
glBegin(GL_LINES);
glVertex3f(0.0, -4.0f, 0.0f);
glVertex3f(0.0, 4.0f, 0.0f);
// arrow
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(1.0, 3.0f, 0.0f);
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(-1.0, 3.0f, 0.0f);
glEnd();
glFlush();
// z
glColor3f(0.0, 0.0, 1.0); // blue z
glBegin(GL_LINES);
glVertex3f(0.0, 0.0f, -4.0f);
glVertex3f(0.0, 0.0f, 4.0f);
// arrow
glVertex3f(0.0, 0.0f, 4.0f);
glVertex3f(0.0, 1.0f, 3.0f);
glVertex3f(0.0, 0.0f, 4.0f);
glVertex3f(0.0, -1.0f, 3.0f);
glEnd();
glFlush();
}
// TODO: Practice 1: draw the object of the scene
glColor3f(0.0, 0.0, 0.0);
glutWireCube(1);
glPopMatrix (); // restore the model matrix
glutSwapBuffers(); // it is used instead of glFlush(), in case of double buffer, to avoid flickering
}
bool cgvInterface::get_axes()
{
return axes;
}
void cgvInterface::set_axes(bool *axes)
{
if (*axes == true)
*axes = false;
else
*axes = true;
}
void cgvInterface::init_callbacks() {
glutKeyboardFunc(set_glutKeyboardFunc);
glutReshapeFunc(set_glutReshapeFunc);
glutDisplayFunc(set_glutDisplayFunc);
}
¿Cómo puedo hacer para que cuando pulse la tecla "a", desaparezcan los ejes? (En el que he puesto no desaparecen.
Muchas gracias.
set_glutKeyboardFunc
? ¿Por qué no estableces el valor decgvInterface
directamente en lugar de hacer tantos líos con punteros?