Example 20

Example 20

Simple example that loads a ppm and writes it to the frame buffer for viewing.

ex_20/view_ppm.png

Source Code

view_ppm.cc:

// Peter Bui
// CSE 40166 Computer Graphics (Fall 2010)
// Example 20: simple OpenGL PPM viewer 

#include "ppm.h"

#include <cstdio>
#include <cstdlib>

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

// Global variables ------------------------------------------------------------

static GLubyte *ImageBuffer;
static size_t   ImageWidth;
static size_t   ImageHeight;

// Display callback ------------------------------------------------------------

void
display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glDrawBuffer(GL_BACK);
    glRasterPos2i(0, 0);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glDrawPixels((GLsizei)ImageWidth, (GLsizei)ImageHeight, GL_RGB, GL_UNSIGNED_BYTE, ImageBuffer);

    glFlush();
    glutSwapBuffers();
}

// Reshape callback ------------------------------------------------------------

void
reshape(GLsizei nw, GLsizei nh)
{
    glViewport(0, 0, ImageWidth, ImageHeight);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, ImageWidth, 0, ImageHeight);

    glMatrixMode(GL_MODELVIEW);    
    glLoadIdentity();
}

// Main execution ---------------------------------------------------------------

int
main(int argc, char *argv[])
{
    glutInit(&argc, argv);

    if (argc != 2) {
        fprintf(stderr, "use: view_ppm [ppmfile]\n");
        return EXIT_FAILURE;
    }

    ppm_read(argv[1], &ImageBuffer, &ImageWidth, &ImageHeight);

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(ImageWidth, ImageHeight);
    glutCreateWindow("Simple PPM Viewer");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    glutMainLoop();

    return EXIT_SUCCESS;
}

// vim: sts=4 sw=4 ts=8 ft=cpp


Files