У меня есть этот код, который делает это, но он совсем не работает!
GLdouble *posv = Utils::worldToScreen(px, py, pz);
GLdouble x = posv[0];
GLdouble y = posv[1];
GLdouble z = posv[2];
GLdouble *sizev = Utils::worldToScreen(1.0f, 1.0f, 1.0f);
GLdouble sizex = sizev[0];
GLdouble sizey = sizev[1];
GLdouble sizez = sizev[2];
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.0);
// FRONT
glVertex3f(x-sizex, y-sizey, z+sizez);
glVertex3f(x+sizex, y-sizey, z+sizez);
glVertex3f(x+sizex, y+sizey, z+sizez);
glVertex3f(x-sizex, y+sizey, z+sizez);
// BACK
glVertex3f(x-sizex, y-sizey, z-sizez);
glVertex3f(x-sizex, y+sizey, z-sizez);
glVertex3f(x+sizex, y+sizey, z-sizez);
glVertex3f(x+sizex, y-sizey, z-sizez);
glColor3f(0.0, 1.0, 0.0);
// LEFT
glVertex3f(x-sizex, y-sizey, z+sizez);
glVertex3f(x-sizex, y+sizey, z+sizez);
glVertex3f(x-sizex, y+sizey, z-sizez);
glVertex3f(x-sizex, y-sizey, z-sizez);
// RIGHT
glVertex3f(x+sizex, y-sizey, z-sizez);
glVertex3f(x+sizex, y+sizey, z-sizez);
glVertex3f(x+sizex, y+sizey, z+sizez);
glVertex3f(x+sizex, y-sizey, z+sizez);
glColor3f(0.0, 0.0, 1.0);
// TOP
glVertex3f(x-sizex, y+sizey, z+sizez);
glVertex3f(x+sizex, y+sizey, z+sizez);
glVertex3f(x+sizex, y+sizey, z-sizez);
glVertex3f(x-sizex, y+sizey, z-sizez);
// BOTTOM
glVertex3f(x-sizex, y-sizey, z+sizez);
glVertex3f(x-sizex, y-sizey, z-sizez);
glVertex3f(x+sizex, y-sizey, z-sizez);
glVertex3f(x+sizex, y-sizey, z+sizez);
GLdouble* Utils::worldToScreen(float objX, float objY, float objZ) {
GLint realy;
GLdouble win_x, win_y, win_z;
int viewport[4];
double mvmatrix[16], projmatrix[16];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
gluProject(static_cast<GLdouble>(objX), static_cast<GLdouble>(objY), static_cast<GLdouble>(objZ), mvmatrix, projmatrix, viewport, &win_x, &win_y, &win_z);
realy = viewport[3]-(GLint)win_y -1;
GLdouble *temp = new GLdouble[3];
temp[0] = win_x;
temp[1] = realy;
temp[2] = win_z;
cout << "objX:" << objX << endl;
cout << "objY:" << objY << endl;
cout << "objZ:" << objZ << endl;
cout << "temp[0]:" << temp[0] << endl;
cout << "temp[1]:" << temp[1] << endl;
cout << "temp[2]:" << temp[2] << endl;
return temp; }
Вывод очень странный!
objX:100
objY:100
objZ:0
temp[0]:6.95322e-310
temp[1]:-1
temp[2]:6.95322e-310
Что не так?Извините за мою глупость!