Subversion Repositories seema-scanner

Rev

Rev 1 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "ProjectorOpenGL.h"

ProjectorOpenGL::ProjectorOpenGL(unsigned int _screenNum){

    // Create the OpenGL context
    context = new OpenGLContext(_screenNum);

    // OpenGL setup
    context->makeContextCurrent();

    glEnable(GL_TEXTURE_2D);

    glClearColor(1.0, 1.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    
    // Set up for 1:1 pixel mapping in the z=0 plane. Upper left corner is (0,0).
    glViewport(0, 0, context->getScreenResX(), context->getScreenResY());
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, context->getScreenResX(), context->getScreenResY(), 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    context->flush();
}

void ProjectorOpenGL::setPattern(unsigned int patternNumber, const unsigned char *tex, unsigned int texWidth, unsigned int texHeight){

    context->makeContextCurrent();

    if(patternNumber+1 == textures.size()+1){
        texture tex;
        glGenTextures(1, &tex.texName);
        tex.width = texWidth;
        tex.height = texHeight;
        textures.push_back(tex);
    } else if (patternNumber+1 > textures.size()+1){
        std::cerr << "ProjectorOpenGL: cannot set pattern " << patternNumber << " before setting " << textures.size() << " -- " << patternNumber-1 << std::endl;
    }

    glBindTexture(GL_TEXTURE_2D, textures[patternNumber].texName);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, tex);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

}



void ProjectorOpenGL::displayPattern(unsigned int patternNumber){

    if(patternNumber+1 > textures.size()){
        std::cerr << "ProjectorOpenGL: cannot display texture " << patternNumber << "! Out of bounds." << std::endl;
        return;
    }

    glClear(GL_COLOR_BUFFER_BIT);

    glBindTexture(GL_TEXTURE_2D, textures[patternNumber].texName);

    float texWidthf = (float)context->getScreenResX()/textures[patternNumber].width;
    float texHeightf = (float)context->getScreenResY()/textures[patternNumber].height;

    glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex2i(0, 0);
        glTexCoord2f(texWidthf, 0); glVertex2i(context->getScreenResX(), 0);
        glTexCoord2f(texWidthf, texHeightf); glVertex2i(context->getScreenResX(), context->getScreenResY());
        glTexCoord2f(0, texHeightf); glVertex2i(0, context->getScreenResY());
    glEnd();

    context->flush();
}

void ProjectorOpenGL::displayTexture(const unsigned char *tex, unsigned int texWidth, unsigned int texHeight){

    context->makeContextCurrent();

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, tex);
    
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    
    float texWidthf = (float)context->getScreenResX()/texWidth;
    float texHeightf = (float)context->getScreenResY()/texHeight;
    
    glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex2i(0, 0);
        glTexCoord2f(texWidthf, 0); glVertex2i(context->getScreenResX(), 0);
        glTexCoord2f(texWidthf, texHeightf); glVertex2i(context->getScreenResX(), context->getScreenResY());
        glTexCoord2f(0, texHeightf); glVertex2i(0, context->getScreenResY());
    glEnd();
    
    context->flush();
}

void ProjectorOpenGL::displayBlack(){
    context->makeContextCurrent();
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    context->flush();
}

void ProjectorOpenGL::displayWhite(){
    context->makeContextCurrent();
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    context->flush();
}

void ProjectorOpenGL::getScreenRes(unsigned int *nx, unsigned int *ny){
    *nx = context->getScreenResX();
    *ny = context->getScreenResY();
}

ProjectorOpenGL::~ProjectorOpenGL(){
    context->makeContextCurrent();
    delete context;
    std::cout << "Closed projector!" << std::endl << std::flush;
}