我想在3d查看器中应用选择操作(使用qt-creator)
any.h文件:
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
class any
{
public:
any();
int board[3][3];
void init(void);
static void drawSquares(GLenum mode);
static void processHits (GLint hits, GLuint buffer[]);
static void pickSquares(int button, int state, int x, int y);
static void display(void);
static void reshape(int w, int h);
};
any.ccp文件:
#include "any.h"
any::any()
{
}
void any:: init(void)
{
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j ++)
board[i][j] = 0;
glClearColor (0.0, 0.0, 0.0, 0.0);
}
void any:: drawSquares(GLenum mode)
{
GLuint i, j;
for (i = 0; i < 3; i++) {
if (mode == GL_SELECT)
glLoadName (i);
for (j = 0; j < 3; j ++) {
if (mode == GL_SELECT)
glPushName (j);
glColor3f ((GLfloat) i/3.0, (GLfloat) j/3.0,(GLfloat) board[i][j]/3.0);
glRecti (i, j, i+1, j+1);
if (mode == GL_SELECT)
glPopName ();
}
}
}
/* processHits prints out the contents of the
* selection array.
*/
void any:: processHits (GLint hits, GLuint buffer[])
{
unsigned int i, j;
GLuint ii, jj, names, *ptr;
printf ("hits = %d\n", hits);
ptr = (GLuint *) buffer;
for (i = 0; i < hits; i++) { /* for each hit */
names = *ptr;
printf (" number of names for this hit = %d\n", names);
ptr++;
printf(" z1 is %g;", (float) *ptr/0x7fffffff); ptr++;
printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;
printf (" names are ");
for (j = 0; j < names; j++) { /* for each name */
printf ("%d ", *ptr);
if (j == 0) /* set row and column */
ii = *ptr;
else if (j == 1)
jj = *ptr;
ptr++;
}
printf ("\n");
board[ii][jj] = (board[ii][jj] + 1) % 3;
}
}
#define BUFSIZE 512
void any:: pickSquares(int button, int state, int x, int y)
{
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];
if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
return;
glGetIntegerv (GL_VIEWPORT, viewport);
glSelectBuffer (BUFSIZE, selectBuf);
(void) glRenderMode (GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
/* create 5x5 pixel picking region near cursor location */
gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
5.0, 5.0, viewport);
gluOrtho2D (0.0, 3.0, 0.0, 3.0);
drawSquares (GL_SELECT);
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glFlush ();
hits = glRenderMode (GL_RENDER);
processHits (hits, selectBuf);
glutPostRedisplay();
}
void any:: display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
drawSquares (GL_RENDER);
glFlush();
}
void any:: reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (0.0, 3.0, 0.0, 3.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
主文件:
#include <any.h>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (100, 100);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
any* an=new any();
an->init();
glutMouseFunc (an->pickSquares);
glutReshapeFunc (an->reshape);
glutDisplayFunc(an->display);
glutMainLoop();
return app.exec();
}
错误:
../untitled/any.cpp:60:24: error: invalid use of member ‘any::board’ in static member function
当我尝试将板子固定为静态时,它会显示很多关于:p的错误
26: undefined reference to `any::board'
答案 0 :(得分:1)
board
是实例成员。您不能在drawSquares
之类的静态方法中使用它。您也需要使board
成为静态成员,或者更有可能使drawSquares
为非静态成员。
答案 1 :(得分:0)
静态函数无法访问非静态变量
在您的源代码中,编译器将闪烁一个错误,即非法引用了非静态成员any::board
。
因此,无法通过C++
编程中的静态方法访问非静态成员。
static
方法存储器将只创建一次,而不创建class
的实例。因此您不能在non-static
方法中使用static
类成员,因为non-static
成员在static
方法中已经没有内存。
请在类中将您的方法定义为non-static
或使用static
成员。
请签出this link。