[英]Swap Y and Z Axis in OpenGL
So basically i have created an application that renders a curve in a 2D-Dimension (X and Y).所以基本上我已经创建了一个在二维(X 和 Y)中呈现曲线的应用程序。 This application also uses a haptic device, which is represented as a cursor inside of the frame where the curve is also rendered.此应用程序还使用了一个触觉设备,它在也渲染曲线的框架内表示为 cursor。
The only thing i want to achieve is to map the y-axis of my projection to my z-axis of the haptic workspace of my haptic device.我唯一想要实现的是 map 我的投影的 y 轴到我的触觉设备的触觉工作区的 z 轴。 So basically i want to mantain the logic of the curve and cursor rendering and only change the used axis for my haptic workspace.所以基本上我想保持曲线的逻辑和 cursor 渲染,并且只更改我的触觉工作区的使用轴。 I used this question: Swap axis y and z我用了这个问题: 交换轴 y 和 z
as a guidance.作为指导。 Practically i just have to use glRotatef(90, 1, 0, 0) to achieve what i want.实际上我只需要使用 glRotatef(90, 1, 0, 0) 来实现我想要的。 I tried rotating only the projection matrix of my haptic workspace, but this sadly doesnt work for me.我尝试只旋转我的触觉工作区的投影矩阵,但遗憾的是这对我不起作用。
This is how my normal curve looks:这是我的正常曲线的样子:
Those are the results, while trying different options:这些是结果,同时尝试不同的选项:
This is my code这是我的代码
reshapeCallback:重塑回调:
void reshapeCallback(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(minX, maxX, minY, maxY, -1, 11);
// glRotatef(90, 1, 0, 0) [4th Try]
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// glRotatef(90, 1, 0, 0) [5th Try]
updateWorkspace();
}
updateWorkspace():更新工作区():
void updateWorkspace() {
GLdouble modelview[16];
GLdouble projection[16];
GLint viewport[4];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
//glRotatef(90, 1, 0, 0) [2nd try]
glGetDoublev(GL_PROJECTION_MATRIX, projection);
//glRotatef(90, 1, 0, 0) [1st try]
glGetIntegerv(GL_VIEWPORT, viewport);
hlMatrixMode(HL_TOUCHWORKSPACE);
hlLoadIdentity();
//glRotatef(90, 1, 0, 0) [3rd try]
// Fit haptic workspace to view volume.
hluFitWorkspace(projection);
// Compute cursor scale.
gCursorScale = hluScreenToModelScale(modelview, projection, viewport);
gCursorScale *= CURSOR_SIZE_PIXELS;
}
drawLine():画线():
void drawLine() {
static GLuint displayList = 0;
if (displayList)
{
glCallList(displayList);
}
else
{
displayList = glGenLists(1);
glNewList(displayList, GL_COMPILE_AND_EXECUTE);
glPushAttrib(GL_ENABLE_BIT | GL_LIGHTING_BIT);
glDisable(GL_LIGHTING);
glLineWidth(2.0);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < vertices.size(); i += 2) {
glVertex2f(vertices[i], vertices[i + 1]);
}
glEnd();
glPopAttrib();
glEndList();
}
}
I numerated the tries of glRotatef() for different positions.我计算了 glRotatef() 对不同位置的尝试。 I hope someone can give me a hint where my thought process went wrong.我希望有人能给我一个提示,我的思维过程出错了。
Point the first: For the love of God USE SHADERS AND NOT OLD OPENGL.第一点:看在上帝的份上,使用着色器而不是旧的 OPENGL。
With shaders, it is a trivial task of swapping 2 arguments, changing使用着色器,交换 2 个 arguments,改变
gl_Position = vec4(x, y, z, 1);
to至
gl_Position = vec4(x, z, y, 1);
I would recommend looking at this tutorial for shaders.我建议您查看本教程的着色器。
Other that that, in my opinion the code looks like it should work, but if it doesn't I know how finicky glRotate
can be, so I really have no advice other that to try things until it works.除此之外,在我看来,代码看起来应该可以工作,但如果我不知道glRotate
可以有多挑剔,所以我真的没有其他建议可以尝试直到它工作。
Sorry I couldn't give a full, direct answer.抱歉,我无法给出完整、直接的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.