sources:


website:
more info here


screenshot:
studies/grafik2/Computergrafik-Code3/Aufgabe8/cgraster.cpp
download file

  1 //
  2 // Computergraphik II
  3 // Prof. Dr. Juergen Doellner
  4 // Wintersemester 2001/2002
  5 //
  6 // Rahmenprogramm fuer Aufgabenzettel 3
  7 //
  8 // Autoren: Florian Kirsch (kirsch@hpi.uni-potsdam.de)
  9 // Marc Nienhaus (nienhaus@hpi.uni-potsdam.de)
 10 // Juergen Doellner (doellner@hpi.uni-potsdam.de)
 11 //
 12
 13 #include "cgraster.h"
 14
 15 CGRaster::CGRaster(int width, int height)
 16     : width_(width), height_(height) {
 17     raster_ = new Color[width_ * height_];
 18     clear();
 19 }
 20
 21 CGRaster::~CGRaster() {
 22     delete [] raster_;
 23 }
 24
 25 int CGRaster::width() const {
 26     return(width_);
 27 }
 28
 29 int CGRaster::height() const {
 30     return(height_);
 31 }
 32
 33 void CGRaster::clear(const Color& c) {
 34     for(int i = 0; i < width_ * height_; ++i) {
 35         raster_[i] = c;
 36     }
 37 }
 38
 39 Color& CGRaster::getPixel(int x, int y) const {
 40     assert(x < width_);
 41     assert(y < height_);
 42     return raster_[x + y * width_];
 43 }
 44
 45 void CGRaster::setPixel(int x, int y, const Color& c) {
 46     assert(x < width_);
 47     assert(y < height_);
 48     raster_[x + y * width_] = c;
 49 }
 50
 51 void CGRaster::draw() const {
 52     int x,y;
 53     // draw pixels
 54     glColor3f(0, 0, 0);
 55     glBegin(GL_QUADS);
 56     for(y = 0; y < height(); ++y) {
 57         for(x = 0; x < width(); ++x) {
 58             const Color& c = getPixel(x,y);
 59             glColor4d(c[0], c[1], c[2], c[3]);
 60             glVertex2f(x    , y);
 61             glVertex2f(x + 1, y);
 62             glVertex2f(x + 1, y + 1);
 63             glVertex2f(x    , y + 1);
 64         }
 65     }
 66     glEnd();
 67        
 68     // draw grid
 69     glColor3f(0.5, 0.5, 0.5);
 70     glBegin(GL_LINES);
 71     for(x = 0; x <= width(); ++x) {
 72         glVertex2f(x, 0);
 73         glVertex2f(x, height());
 74     }
 75     for(y = 0; y <= height(); ++y) {
 76         glVertex2f(0, y);
 77         glVertex2f(width(), y);
 78     }
 79     glEnd();
 80 }
 81
 82