How do I render with OpenGL/Vulkan/DirectX from C++?

Rendering graphics using OpenGL, Vulkan, or DirectX in C++ involves understanding the graphics APIs and setting up an appropriate development environment. Below are examples of how to initialize and render a simple triangle using OpenGL, along with brief descriptions of Vulkan and DirectX approaches.

OpenGL Example


#include <GL/glut.h>

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f(0.5f, -0.5f);
        glVertex2f(0.0f, 0.5f);
    glEnd();
    glFlush();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(640, 480);
    glutCreateWindow("OpenGL Triangle");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}
    

Vulkan Example

Vulkan is a more modern graphics API that requires a more complex setup, but offers greater control over GPU resources.


// Vulkan example would be more extensive; refer to Vulkan's setup tutorials.
    

DirectX Example

DirectX also provides powerful graphics capabilities but is mainly used for Windows applications.


// DirectX example would also require a detailed setup; refer to DirectX's documentation.
    

OpenGL Vulkan DirectX C++ Graphics Rendering 3D Graphics Game Development Graphics API