Search code examples
copengl3dglut

How to make a 3D window for drawing 3D points in c using opengl functions?


I can make 2D window and draw dots, lines etc. I want to make a 3D window so that I can draw 3D objects by drawing 3D dots/lines(e.g. glVertex3d(x,y,z))(like as 2D glVertex2d(x,y)).

But I can't make the 3D window. I want to make just a 3D window that give me a platform for drawing 3D points.

My sample code for 2D window -

#include<GL/gl.h>
#include<GL/glut.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

double x_0 = -100;
double y_0 = -25;

double x_1 = 100;
double y_1 = -25;

double x_2 = 100;
double y_2 = 25;

double x_3 = -100;
double y_3 = 25;

void
drawRectangle()
{
    /* Clear the screen */
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glFlush();

    glBegin(GL_LINES);

    glVertex2d(x_0, y_0);
    glVertex2d(x_1, y_1);

    glVertex2d(x_1, y_1);
    glVertex2d(x_2, y_2);

    glVertex2d(x_2, y_2);
    glVertex2d(x_3, y_3);

    glVertex2d(x_3, y_3);
    glVertex2d(x_0, y_0);

    glEnd();
    glFlush();
}

void
init(void)
{
    /*initialize the x-y co-ordinate*/
    glClearColor(0,0,0,0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-320, 319,-240, 239);
    glClear(GL_COLOR_BUFFER_BIT);

    glFlush();
}

int
main(int argc, char *argv[])
{
    double x_0, y_0, x_1, y_1;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(400, 400);

    glutCreateWindow("Rectangle in 2D");
    init();
    drawRectangle();
    glutMainLoop();
}

I also try following but the line is not shown.

#include<GL/gl.h>
#include<GL/glut.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

void
init(void)
{
    /*initialize the x-y co-ordinate*/
    glClearColor(0,0,0,0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-320, 319, -240, 239, 100, -150);
    glClear(GL_COLOR_BUFFER_BIT);

    glFlush();
}

int
main(int argc, char *argv[])
{
    double x_0, y_0, x_1, y_1;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(400, 400);

    glutCreateWindow("Special key");
    init();

    glBegin(GL_LINES);
    glVertex3d(0,0,0);
    glVertex3d(0,0,100);
    glVertex3d(0,100,0);
    glVertex3d(0,100,0);
    glEnd();
    glFlush();

    glutMainLoop();
}

Edit
gluOrtho2D is updated with glOrtho in the above MWE.


Solution

    • You can use gluPerspective() (or glFrustum()) to set up a perspective projection matrix
    • And gluLookAt() to scoot the "camera" away from the origin so you can see what you draw
    • And a little bit of timer-based animation so you can see the perspective in action
    • Double-buffering because GLUT_SINGLE is sometimes weird on modern composited window managers

    All together:

    #include <GL/glut.h>
    
    double rnd( double lo, double hi )
    {
        return lo + ( hi - lo ) * ( rand() / static_cast<double>( RAND_MAX ) );
    }
    
    double angle = 0.0;
    void timer( int value )
    {
        angle += 1.0;
        glutTimerFunc( 16, timer, 0 );
        glutPostRedisplay();
    }
    
    void display()
    {
        glClearColor( 0, 0, 0, 0 );
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        double w = glutGet( GLUT_WINDOW_WIDTH );
        double h = glutGet( GLUT_WINDOW_HEIGHT );
        gluPerspective( 60.0, w / h, 0.1, 1000.0 );
    
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
        gluLookAt( 100, 100, 100, 0, 0, 0, 0, 0, 1 );
    
        glRotated( angle, 0, 0, 1 );
    
        srand( 0 );
        glBegin( GL_LINES );
        for( size_t i = 0; i < 100; i++ )
        {
            glColor3d( rnd( 0.0, 1.0 ), rnd( 0.0, 1.0 ), rnd( 0.0, 1.0 ) );
            glVertex3d( rnd( -50, 50 ), rnd( -50, 50 ), rnd( -50, 50 ) );
            glVertex3d( rnd( -50, 50 ), rnd( -50, 50 ), rnd( -50, 50 ) );
        }
        glEnd();
    
        glutSwapBuffers();
    }
    
    int main( int argc, char *argv[] )
    {
        glutInit( &argc, argv );
        glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
        glutInitWindowSize( 640, 480 );
        glutCreateWindow( "Special key" );
        glutDisplayFunc( display );
        glutTimerFunc( 0, timer, 0 );
        glutMainLoop();
    }