2

I'm having a problem in OpenGL, when I press W, A, S, D everything works normal, but when I press space or shift it happens:

opengl gif

The cube disappears before the end of the screen. Depending on the value of xrot and yrot another axis gets this problem.

I'm a beginner in openGL 3D and I don't know why that happens.

code:

float xrot = 100.0f;
float yrot = -100.0f;

float tra_x = 0.0f;
float tra_y = 0.0f;
float tra_z = 0.0f;

GLFWwindow* window;
void drawBox()
{
    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
    glEnable(3553);

    glBegin(GL_QUADS);

    glColor3f(1.0f, 0.0f, 0.0f);
    // FRONT
    glVertex3f(-0.5f, -0.5f, 0.5f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.5f, -0.5f, 0.5f);
    glVertex3f(0.5f, 0.5f, 0.5f);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(-0.5f, 0.5f, 0.5f);
    // BACK
    glVertex3f(-0.5f, -0.5f, -0.5f);
    glVertex3f(-0.5f, 0.5f, -0.5f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.5f, 0.5f, -0.5f);
    glVertex3f(0.5f, -0.5f, -0.5f);

    glColor3f(0.0f, 1.0f, 0.0f);
    // LEFT
    glVertex3f(-0.5f, -0.5f, 0.5f);
    glVertex3f(-0.5f, 0.5f, 0.5f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(-0.5f, 0.5f, -0.5f);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(-0.5f, -0.5f, -0.5f);
    // RIGHT
    glVertex3f(0.5f, -0.5f, -0.5f);
    glVertex3f(0.5f, 0.5f, -0.5f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.5f, 0.5f, 0.5f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(0.5f, -0.5f, 0.5f);

    glColor3f(0.0f, 0.0f, 1.0f);
    // TOP
    glVertex3f(-0.5f, 0.5f, 0.5f);
    glVertex3f(0.5f, 0.5f, 0.5f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.5f, 0.5f, -0.5f);
    glVertex3f(-0.5f, 0.5f, -0.5f);
    glColor3f(1.0f, 0.0f, 0.0f);
    // BOTTOM
    glVertex3f(-0.5f, -0.5f, 0.5f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(-0.5f, -0.5f, -0.5f);
    glVertex3f(0.5f, -0.5f, -0.5f);
    glVertex3f(0.5f, -0.5f, 0.5f);
    glEnd();
}

void display(void)
{
    glDisable(GL_BLEND);

    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glRotatef(yrot, -1.0f, 0.0f, 0.0f);
    glRotatef(xrot, 0.0f, 1.0f, 0.0f);

    glTranslatef(-tra_x, tra_y, -tra_z);

    drawBox();

    glFlush();
    glfwSwapBuffers(window);

}
void framebuffer_resize_callback(GLFWwindow* window, int fbW, int fbH)
{
    glViewport(0, 0, fbW, fbH);
}
int main(void)
{   
    if (!glfwInit())
        return -1;

    glfwWindowHint(GLFW_SAMPLES, 2);
    window = glfwCreateWindow(640, 480, "Test", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    glfwSetFramebufferSizeCallback(window, framebuffer_resize_callback);
    glfwMakeContextCurrent(window);

    glClearColor(0.93f, 0.93f, 0.93f, 0.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glClearDepth(1.0f);

    while (!glfwWindowShouldClose(window))
    {
        display();
        if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
            tra_x += 0.1f;
        }
        if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
            tra_x -= 0.1f;
        }

        if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
            tra_z -= 0.1f;
        }
        if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
            tra_z += 0.1f;
        }

        if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
            tra_y += 0.1f;
        }

        if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
            tra_y -= 0.1f;
        }
        glfwPollEvents();
    }
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
CGCM
  • 35
  • 3
  • Maybe you should check out this: https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml Also be aware of the depth passed to glClearDepth. – Adrian Roman Jun 23 '20 at 08:05
  • `glMatrixMode(GL_PROJECTION); glLoadIdentity();` This is a quite small view area. I strongly believe that your cube vanishs "behind" the far clip plane. FYI: [SO: How to use glOrtho() in OpenGL?](https://stackoverflow.com/q/2571402/7478597), [OpenGL Projection Matrix](http://www.songho.ca/opengl/gl_projectionmatrix.html) – Scheff's Cat Jun 23 '20 at 08:06
  • I see that you used the identity matrix for the projection matrix, maybe you should also check this: http://www.songho.ca/opengl/gl_projectionmatrix.html – Adrian Roman Jun 23 '20 at 08:09
  • 1
    @AdrianRoman The link to songho.ca must be good if recommended two times... ;-) – Scheff's Cat Jun 23 '20 at 08:10
  • Sorry, I posted without seeing your answer, I was typing while you replied. – Adrian Roman Jun 23 '20 at 08:11

1 Answers1

3

The object is clipped by the fare plane of the Orthographic projection. The projection matrix defines the volume (clip space) which is projected on to 2 dimensional viewport. Actually you don't set a projection matrix, thus the projection is the Identity matrix. Hence, view space, clip space and normalized device space are the same and the viewing volume is a unique cube, with the left, bottom, near of (-1, -1, -1) and the right, top, far of (1, 1, 1). Hence the near plane is at -1 and the far plane is at 1. All the geometry which is out of this space is clipped.
Use glOrtho to define a clip space with a larger scale. For instance:

void display(void)
{
    // [...]

    # set projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1, 1, -1, -1, -10, 10); // near = -10, far = 10

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // [...]

Alternatively you can use Perspective projection. At perspective projection the viewing volume is a [Frustum](https://en.wikipedia.org/wiki/Viewing_frustum. Hence you have to shift the object along the negative z axis in between the near and far plane.

Create a perspective projection by gluPerspective and move the object in between then near and far plane by gluLookAt:

void display(void)
{
    // [...]

    # set projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //glOrtho(-1, 1, -1, -1, -10, 10); // near = -10, far = 10
    gluPerspective(90.0, 480.0/640.0, 0.1, 20.0) 

    // set view matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, 5.0, 0, 0, 0, 0, 1.0, 0); 

    // [...]

What exactly are eye space coordinates?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174