1 |
jakw |
1 |
#include "ProjectorOpenGL.h"
|
|
|
2 |
|
|
|
3 |
ProjectorOpenGL::ProjectorOpenGL(unsigned int _screenNum){
|
|
|
4 |
|
|
|
5 |
// Create the OpenGL context
|
|
|
6 |
context = new OpenGLContext(_screenNum);
|
|
|
7 |
|
|
|
8 |
// OpenGL setup
|
|
|
9 |
context->makeContextCurrent();
|
|
|
10 |
|
|
|
11 |
glEnable(GL_TEXTURE_2D);
|
|
|
12 |
|
|
|
13 |
glClearColor(1.0, 1.0, 1.0, 1.0);
|
|
|
14 |
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
15 |
|
|
|
16 |
// Set up for 1:1 pixel mapping in the z=0 plane. Upper left corner is (0,0).
|
|
|
17 |
glViewport(0, 0, context->getScreenResX(), context->getScreenResY());
|
|
|
18 |
glMatrixMode(GL_PROJECTION);
|
|
|
19 |
glLoadIdentity();
|
|
|
20 |
glOrtho(0, context->getScreenResX(), context->getScreenResY(), 0, -1, 1);
|
|
|
21 |
glMatrixMode(GL_MODELVIEW);
|
|
|
22 |
glLoadIdentity();
|
|
|
23 |
|
|
|
24 |
context->flush();
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
void ProjectorOpenGL::setPattern(unsigned int patternNumber, const unsigned char *tex, unsigned int texWidth, unsigned int texHeight){
|
|
|
28 |
|
|
|
29 |
context->makeContextCurrent();
|
|
|
30 |
|
|
|
31 |
if(patternNumber+1 == textures.size()+1){
|
|
|
32 |
texture tex;
|
|
|
33 |
glGenTextures(1, &tex.texName);
|
|
|
34 |
tex.width = texWidth;
|
|
|
35 |
tex.height = texHeight;
|
|
|
36 |
textures.push_back(tex);
|
|
|
37 |
} else if (patternNumber+1 > textures.size()+1){
|
|
|
38 |
std::cerr << "ProjectorOpenGL: cannot set pattern " << patternNumber << " before setting " << textures.size() << " -- " << patternNumber-1 << std::endl;
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
glBindTexture(GL_TEXTURE_2D, textures[patternNumber].texName);
|
|
|
42 |
|
|
|
43 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
|
44 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, tex);
|
|
|
45 |
|
|
|
46 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
47 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
48 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
49 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
50 |
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
void ProjectorOpenGL::displayPattern(unsigned int patternNumber){
|
|
|
56 |
|
|
|
57 |
if(patternNumber+1 > textures.size()){
|
|
|
58 |
std::cerr << "ProjectorOpenGL: cannot display texture " << patternNumber << "! Out of bounds." << std::endl;
|
|
|
59 |
return;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
63 |
|
|
|
64 |
glBindTexture(GL_TEXTURE_2D, textures[patternNumber].texName);
|
|
|
65 |
|
|
|
66 |
float texWidthf = (float)context->getScreenResX()/textures[patternNumber].width;
|
|
|
67 |
float texHeightf = (float)context->getScreenResY()/textures[patternNumber].height;
|
|
|
68 |
|
|
|
69 |
glBegin(GL_QUADS);
|
|
|
70 |
glTexCoord2f(0, 0); glVertex2i(0, 0);
|
|
|
71 |
glTexCoord2f(texWidthf, 0); glVertex2i(context->getScreenResX(), 0);
|
|
|
72 |
glTexCoord2f(texWidthf, texHeightf); glVertex2i(context->getScreenResX(), context->getScreenResY());
|
|
|
73 |
glTexCoord2f(0, texHeightf); glVertex2i(0, context->getScreenResY());
|
|
|
74 |
glEnd();
|
|
|
75 |
|
|
|
76 |
context->flush();
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
void ProjectorOpenGL::displayTexture(const unsigned char *tex, unsigned int texWidth, unsigned int texHeight){
|
|
|
80 |
|
|
|
81 |
context->makeContextCurrent();
|
|
|
82 |
|
|
|
83 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
|
84 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, tex);
|
|
|
85 |
|
|
|
86 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
87 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
88 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
89 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
90 |
|
|
|
91 |
float texWidthf = (float)context->getScreenResX()/texWidth;
|
|
|
92 |
float texHeightf = (float)context->getScreenResY()/texHeight;
|
|
|
93 |
|
|
|
94 |
glBegin(GL_QUADS);
|
|
|
95 |
glTexCoord2f(0, 0); glVertex2i(0, 0);
|
|
|
96 |
glTexCoord2f(texWidthf, 0); glVertex2i(context->getScreenResX(), 0);
|
|
|
97 |
glTexCoord2f(texWidthf, texHeightf); glVertex2i(context->getScreenResX(), context->getScreenResY());
|
|
|
98 |
glTexCoord2f(0, texHeightf); glVertex2i(0, context->getScreenResY());
|
|
|
99 |
glEnd();
|
|
|
100 |
|
|
|
101 |
context->flush();
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
void ProjectorOpenGL::displayBlack(){
|
|
|
105 |
context->makeContextCurrent();
|
|
|
106 |
glClearColor(0.0, 0.0, 0.0, 1.0);
|
|
|
107 |
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
108 |
context->flush();
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
void ProjectorOpenGL::displayWhite(){
|
|
|
112 |
context->makeContextCurrent();
|
|
|
113 |
glClearColor(1.0, 1.0, 1.0, 1.0);
|
|
|
114 |
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
115 |
context->flush();
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
void ProjectorOpenGL::getScreenRes(unsigned int *nx, unsigned int *ny){
|
|
|
119 |
*nx = context->getScreenResX();
|
|
|
120 |
*ny = context->getScreenResY();
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
ProjectorOpenGL::~ProjectorOpenGL(){
|
|
|
124 |
context->makeContextCurrent();
|
|
|
125 |
delete context;
|
|
|
126 |
}
|
|
|
127 |
|