Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
Store white noise background pixels in pixel buffer objects
Browse files Browse the repository at this point in the history
  • Loading branch information
cstawarz committed Jan 25, 2011
1 parent 2a637c9 commit 0cc4291
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
49 changes: 42 additions & 7 deletions WhiteNoiseBackground/WhiteNoiseBackground.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,33 @@ void WhiteNoiseBackground::load(shared_ptr<StimulusDisplay> display) {
GLint width, height;
display->getCurrentViewportSize(width, height);
dims[i] = DisplayDimensions(width, height);
if (i == 0) {
pixels.resize(width * height);
}

glGenBuffers(1, &(buffers[i]));
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffers[i]);
glBufferData(GL_PIXEL_UNPACK_BUFFER, (width * height * sizeof(PixelType)), NULL, GL_DYNAMIC_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}

loaded = true;
}


void WhiteNoiseBackground::unload(shared_ptr<StimulusDisplay> display) {
if (!loaded)
return;

for (int i = 0; i < display->getNContexts(); i++) {
display->setCurrent(i);
glDeleteBuffers(1, &(buffers[i]));
}

dims.clear();
buffers.clear();

loaded = false;
}


void WhiteNoiseBackground::draw(shared_ptr<StimulusDisplay> display) {
glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);

Expand All @@ -53,9 +71,13 @@ void WhiteNoiseBackground::draw(shared_ptr<StimulusDisplay> display) {
glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

DisplayDimensions &currentDims = dims[display->getCurrentContextIndex()];
int index = display->getCurrentContextIndex();
DisplayDimensions &currentDims = dims[index];
glWindowPos2i(0, 0);
glDrawPixels(currentDims.first, currentDims.second, GL_LUMINANCE, PIXEL_TYPE, &(pixels[0]));

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffers[index]);
glDrawPixels(currentDims.first, currentDims.second, GL_LUMINANCE, PIXEL_TYPE, (GLvoid *)0);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

glPopClientAttrib();
}
Expand All @@ -69,8 +91,21 @@ Datum WhiteNoiseBackground::getCurrentAnnounceDrawData() {


void WhiteNoiseBackground::randomizePixels() {
for (size_t i = 0; i < pixels.size(); i++) {
pixels[i] = randDist();
shared_ptr<StimulusDisplay> display(StimulusDisplay::getCurrentStimulusDisplay());

for (int i = 0; i < display->getNContexts(); i++) {
display->setCurrent(i);
DisplayDimensions &currentDims = dims[i];

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffers[i]);
PixelType *pixels = (PixelType *)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);

for (size_t i = 0; i < (currentDims.first * currentDims.second); i++) {
pixels[i] = randDist();
}

glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}
}

Expand Down
25 changes: 24 additions & 1 deletion WhiteNoiseBackground/WhiteNoiseBackground.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class WhiteNoiseBackground : public Stimulus {
virtual ~WhiteNoiseBackground();

virtual void load(shared_ptr<StimulusDisplay> display);
virtual void unload(shared_ptr<StimulusDisplay> display);
virtual void draw(shared_ptr<StimulusDisplay> display);
virtual Datum getCurrentAnnounceDrawData();

Expand All @@ -38,7 +39,7 @@ class WhiteNoiseBackground : public Stimulus {
typedef GLfloat PixelType;

std::map<int, DisplayDimensions> dims;
std::vector<PixelType> pixels;
std::map<int, GLuint> buffers;

boost::mt19937 randGen;
boost::uniform_01< boost::mt19937&, PixelType > randDist;
Expand All @@ -47,3 +48,25 @@ class WhiteNoiseBackground : public Stimulus {


#endif






















0 comments on commit 0cc4291

Please sign in to comment.