CHAPTER 8 - Implementation 1

Objectives


Buffer


OpenGL Frame Buffer


OpenGL Buffers


Writing in Buffers


Writing Model



Writing Modes


XOR mode


The Pixel Pipeline


Raster Position


Buffer Selection


Bitmaps


Raster Color


Drawing Bitmaps


glBitmap(width, height, x0, y0, xi, yi, bitmap)

Example: Checker Board


GLubyte wb[2] = {0 x 00, 0 x ff};
GLubyte check[512];
int i, j;
for(i=0; i<64; i++) for (j=0; j<64, j++) check[i*8+j] = wb[(i/8+j)%2];
glBitmap( 64, 64, 0.0, 0.0, 0.0, 0.0, check);


Pixel Maps


OpenGL Pixel Functions (see readPixels.c)


Image Formats


Displaying a PPM Image


Reading the Header

FILE *fd;
int k, nm;
char c;
int i;
char b[100];
float s;
int red, green, blue;
printf("enter file name\n");
scanf("%s", b);
fd = fopen(b, "r");
fscanf(fd,"%[^\n] ",b);
if(b[0]!='P'|| b[1] != '3'){
printf("%s is not a PPM file!\n", b);
exit(0);
}
printf("%s is a PPM file\n",b);


Reading the Header (cont)

fscanf(fd, "%c",&c);
while(c == '#')
{
fscanf(fd, "%[^\n] ", b);
printf("%s\n",b);
fscanf(fd, "%c",&c);
}
ungetc(c,fd);


Reading the Data

fscanf(fd, "%d %d %d", &n, &m, &k);
printf("%d rows %d columns max value= %d\n",n,m,k);
nm = n*m;
image=malloc(3*sizeof(GLuint)*nm);
s=255./k;
for(i=0;i<nm;i++)
{
fscanf(fd,"%d %d %d",&red, &green, &blue );
image[3*nm-3*i-3]=red;
image[3*nm-3*i-2]=green;
image[3*nm-3*i-1]=blue;
}


Scaling the Image Data

  • We can scale the image in the pipeline

    glPixelTransferf(GL_RED_SCALE, s);

    glPixelTransferf(GL_GREEN_SCALE, s);

    glPixelTransferf(GL_BLUE_SCALE, s);

  • We may have to swap bytes when we go from processor memory to the frame buffer depending on the processor. If so, we can use

    glPixelStorei(GL_UNPACK_SWAP_BYTES,GL_TRUE);


The display callback

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0,0);
glDrawPixels(n,m,GL_RGB,
GL_UNSIGNED_INT, image);
glFlush();
}


<next>