简体   繁体   中英

Drawing 2D Points on a 3D Context

private void init_video () {
    uint32 video_flags = SurfaceFlag.SWSURFACE | SurfaceFlag.OPENGL;
    screen = Screen.set_video_mode (SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, video_flags);
    if (screen == null) stderr.printf ("Could not set video mode.\n");

    glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glFrustum (-2.0f, 2.0f, -2.0f, 2.0f, 1.0f, 300.0f);
    glMatrixMode (GL_MODELVIEW);
    glEnable (GL_DEPTH_TEST);
    glEnable (GL_TEXTURE_2D);

    SDL.WindowManager.set_caption ("Title", "Title");
}

That is the function where I set up the camera for a game I'm working on. I have this function to draw a cube:

public void draw () {
    glLoadIdentity ();
    glTranslatef (x, y, z);
    glScalef (size, size, size);
    glRotatef (x_angle, 1.0f, 0.0f, 0.0f);
    glRotatef (y_angle, 0.0f, 1.0f, 0.0f);
    glRotatef (z_angle, 0.0f, 0.0f, 1.0f);
    glBegin (GL_QUADS);

    /* Front face */
    glColor3f (1.0f, 0.0f, 0.0f);
    glVertex3f (0.5f, 0.5f, 0.5f);
    glVertex3f (-0.5f, 0.5f, 0.5f);
    glVertex3f (-0.5f, -0.5f, 0.5f);
    glVertex3f (0.5f, -0.5f, 0.5f);

    /* Left face */
    glColor3f (0.0f, 1.0f, 0.0f);
    glVertex3f (-0.5f, 0.5f, 0.5f);
    glVertex3f (-0.5f, -0.5f, 0.5f);
    glVertex3f (-0.5f, -0.5f, -0.5f);
    glVertex3f (-0.5f, 0.5f, -0.5f);

    /* Back face */
    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);
    glVertex3f (0.5f, -0.5f, -0.5f);

    /* Right face */
    glColor3f (1.0f, 1.0f, 0.0f);
    glVertex3f (0.5f, 0.5f, 0.5f);
    glVertex3f (0.5f, -0.5f, 0.5f);
    glVertex3f (0.5f, -0.5f, -0.5f);
    glVertex3f (0.5f, 0.5f, -0.5f);

    /* Top face */
    glColor3f (0.0f, 1.0f, 1.0f);
    glVertex3f (0.5f, 0.5f, 0.5f);
    glVertex3f (-0.5f, 0.5f, 0.5f);
    glVertex3f (-0.5f, 0.5f, -0.5f);
    glVertex3f (0.5f, 0.5f, -0.5f);

    /* Bottom face */
    glColor3f (1.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);
    glVertex3f (0.5f, -0.5f, -0.5f);
    glEnd ();
}

How can I draw points as if it was a 2D Context? Any ideas?

There is no thing as a 2D context in OpenGL. Everything is 3d. But you can emulate 2d using a parallel projection with glOrtho

// Setup Orthogonal projection to window coordiantes
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WindowWidth-1, 0, WindowHeight-1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// draw quad in screen coodinates
glBegin(GL_QUADS);
    glColor3f(1.0f, 1.0f, 1.0f);
    glVertex2i(10, 10);
    glVertex2i(10, 80);
    glVertex2i(80, 80);
    glVertex2i(80, 10);
glEnd (); 

glVertex2 is like glVertex3 with the third coorinate set to 0.

With OpenGL 4 all the above commands are removed. But the glOrtho documentation shows you the matrix you need to create to set up rendering in windows coordinates.

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