简体   繁体   中英

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). This application also uses a haptic device, which is represented as a cursor inside of the frame where the curve is also rendered.

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. So basically i want to mantain the logic of the curve and cursor rendering and only change the used axis for my haptic workspace. I used this question: Swap axis y and z

as a guidance. Practically i just have to use glRotatef(90, 1, 0, 0) to achieve what i want. 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. 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.

With shaders, it is a trivial task of swapping 2 arguments, changing

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.

Sorry I couldn't give a full, direct answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM