Skip to content

How to draw a mesh in an OpenGL context?

Sk. Mohammadul Haque edited this page May 2, 2020 · 5 revisions

Create an ellipsoid mesh and draw it in an OpenGL context.

#include <meshlib.h>
#include <GL/glut.h>

MESH m;

void initgl()
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    mesh_draw_mesh(m);
    glutSwapBuffers();
}

void reshape(GLsizei width, GLsizei height)
{
    GLfloat aspect = (GLfloat)width/(GLfloat)height;
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}

int main(int argc, char* argv[])
{
    mesh_vector3 sz = {1.0, 1.0, 1.0}, pos = {0.0, 0.0, -1.5};
    m = mesh_create_mesh_new_ellipsoid(&sz, &pos);
    const char title[] = "Mesh Display";

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowSize(640, 480);
    glutCreateWindow(title);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    initgl();
    glutMainLoop();

    mesh_free_mesh(m);
    return 0;
}
Clone this wiki locally