我有这样做的代码,但它根本不起作用!
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
怎么了?抱歉我的愚蠢!
答案 0 :(得分:2)
不是以x +/-尺寸,y +/-尺寸,z +/-尺寸绘制顶点,为什么不用glTranslatef()转换为x,y,z,然后进行绘图?
尝试将此作为绘图功能:
void drawCube(float x, float y, float z)
{
const float sizex = 0.5f;
const float sizey = 0.5f;
const float sizez = 0.5f
glTranslatef(-x, -y, -z);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.0);
// FRONT
glVertex3f(-sizex, -sizey, sizez);
glVertex3f(sizex, -sizey, sizez);
glVertex3f(sizex, sizey, sizez);
glVertex3f(-sizex, sizey, sizez);
// BACK
glVertex3f(-sizex, -sizey, -sizez);
glVertex3f(-sizex, sizey, -sizez);
glVertex3f(sizex, sizey, -sizez);
glVertex3f(sizex, -sizey, -sizez);
glColor3f(0.0, 1.0, 0.0);
// LEFT
glVertex3f(-sizex, -sizey, sizez);
glVertex3f(-sizex, sizey, sizez);
glVertex3f(-sizex, sizey, -sizez);
glVertex3f(-sizex, -sizey, -sizez);
// RIGHT
glVertex3f(sizex, -sizey, -sizez);
glVertex3f(sizex, sizey, -sizez);
glVertex3f(sizex, sizey, sizez);
glVertex3f(sizex, -sizey, sizez);
glColor3f(0.0, 0.0, 1.0);
// TOP
glVertex3f(-sizex, sizey, sizez);
glVertex3f(sizex, sizey, sizez);
glVertex3f(sizex, sizey, -sizez);
glVertex3f(-sizex, sizey, -sizez);
// BOTTOM
glVertex3f(-sizex, -sizey, sizez);
glVertex3f(-sizex, -sizey, -sizez);
glVertex3f(sizex, -sizey, -sizez);
glVertex3f(sizex, -sizey, sizez);
glEnd();
glTranslatef(x, y, z);
}