glDrawPixels: cannot see program output

1.1k views Asked by At

I am trying to follow the glDrawPixels example from Chapter 8 of the red book (which is incomplete) but cannot see anything (I just see a black screen with no pixmapwhen I run the code. Any ideas what I am doing wrong?

#include <GL/glut.h>

#define CHECK_IMAGE_WIDTH 64
#define CHECK_IMAGE_HEIGHT 64
static GLubyte checkImage[CHECK_IMAGE_WIDTH][CHECK_IMAGE_HEIGHT][3];

void makeCheckImage(void) {

  int i, j, c;

  for (i = 0; i < CHECK_IMAGE_HEIGHT; i++)

    for (j = 0; j < CHECK_IMAGE_WIDTH; j++) {

      checkImage[i][j][0] = (GLubyte) 255;
      checkImage[i][j][1] = (GLubyte) i == j ? 255 : 0;
      checkImage[i][j][2] = (GLubyte) 255;

    }

}

void init() {
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glShadeModel(GL_FLAT);
  makeCheckImage();
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}

void display() {
  glClear(GL_COLOR_BUFFER_BIT);
  glRasterPos2i(0, 0);
  glDrawPixels(CHECK_IMAGE_WIDTH, CHECK_IMAGE_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, checkImage);
  glFlush();
}

void reshape(int w, int h) {
  gluOrtho2D(-10.0f, 10.0f, -10.0f, 10.0f);
  glViewport(0, 0, w, h);
}

int main(int argc, char **argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB);
  glutCreateWindow(argv[0]);
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutMainLoop();
  return 0;
}
1

There are 1 answers

1
datenwolf On

Your code misses the init call, which initializes the checkerboard image with content. Add a init(); right before glutMainLoop();