-
Notifications
You must be signed in to change notification settings - Fork 0
/
SourceGLCanvas.cpp
162 lines (127 loc) · 5.21 KB
/
SourceGLCanvas.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* SourceGLCanvas.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SourceGLCanvas.h"
#include "MainWindow.h"
SourceGLCanvas::SourceGLCanvas(QWidget* parent)
: MapperGLCanvas(parent)
{
}
Shape* SourceGLCanvas::getShapeFromMappingId(int mappingId)
{
if (mappingId >= 0)
{
Mapping::ptr mapping = MainWindow::getInstance().getMappingManager().getMapping(mappingId);
std::tr1::shared_ptr<TextureMapping> textureMapping = std::tr1::static_pointer_cast<TextureMapping>(mapping);
Q_CHECK_PTR(textureMapping);
return textureMapping->getInputShape().get();
}
else
return NULL;
}
//Quad& SourceGLCanvas::getQuad()
//{
// std::tr1::shared_ptr<TextureMapping> textureMapping = std::tr1::static_pointer_cast<TextureMapping>(Common::currentMapping);
// Q_CHECK_PTR(textureMapping);
//
// std::tr1::shared_ptr<Quad> inputQuad = std::tr1::static_pointer_cast<Quad>(textureMapping->getInputShape());
// Q_CHECK_PTR(inputQuad);
//
// return (*inputQuad);
//}
void SourceGLCanvas::doDraw()
{
if (!MainWindow::getInstance().hasCurrentPaint())
return;
uint paintId = MainWindow::getInstance().getCurrentPaintId();
// Retrieve paint (as texture) and draw it.
Paint::ptr paint = MainWindow::getInstance().getMappingManager().getPaintById(paintId);
Q_CHECK_PTR(paint);
std::tr1::shared_ptr<Texture> texture = std::tr1::static_pointer_cast<Texture>(paint);
Q_CHECK_PTR(texture);
// Load texture if needed.
if (texture->getTextureId() == 0) {
texture->loadTexture();
}
// Draw the texture.
glPushMatrix();
// Enable blending mode (for alphas).
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable (GL_LIGHTING);
glEnable (GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->getWidth(), texture->getHeight(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, texture->getBits());
std::cout << texture->getX() << "x" << texture->getY() << " : " << texture->getWidth() << "x" << texture->getHeight() << " " << texture->getTextureId() << std::endl;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// TODO: Exact projection of texture
// see http://stackoverflow.com/questions/15242507/perspective-correct-texturing-of-trapezoid-in-opengl-es-2-0
// Draw source texture (not moving) in the center of the area.
// float centerX = (float) width() / 2.0f;
// float centerY = (float) height() / 2.0f;
// float textureHalfWidth = (float) texture->getWidth() / 2.0f;
// float textureHalfHeight = (float) texture->getHeight() / 2.0f;
glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
// FIXME: Does this draw the quad counterclockwise?
glBegin (GL_QUADS);
{
Util::correctGlTexCoord(0, 0);
glVertex3f (texture->getX(), texture->getY(), 0);
Util::correctGlTexCoord(1, 0);
glVertex3f (texture->getX()+texture->getWidth(), texture->getY(), 0);
Util::correctGlTexCoord(1, 1);
glVertex3f (texture->getX()+texture->getWidth(), texture->getY() + texture->getHeight(), 0);
Util::correctGlTexCoord(0, 1);
glVertex3f (texture->getX(), texture->getY() + texture->getHeight(), 0);
}
glEnd ();
glDisable(GL_TEXTURE_2D);
// Retrieve all mappings associated to paint.
std::map<uint, Mapping::ptr> mappings = MainWindow::getInstance().getMappingManager().getPaintMappingsById(paintId);
for (std::map<uint, Mapping::ptr>::iterator it = mappings.begin(); it != mappings.end(); ++it)
{
// TODO: Ceci est un hack necessaire car tout est en fonction de la width/height de la texture.
// Il faut changer ca.
std::tr1::shared_ptr<TextureMapping> textureMapping = std::tr1::static_pointer_cast<TextureMapping>(it->second);
Q_CHECK_PTR(textureMapping);
std::tr1::shared_ptr<Shape> inputShape = std::tr1::static_pointer_cast<Quad>(textureMapping->getInputShape());
Q_CHECK_PTR(inputShape);
if (it->first == MainWindow::getInstance().getCurrentMappingId())
glColor4f(0.0f, 0.0f, 0.7f, 1.0f);
else
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
// Source shape.
glLineWidth(5);
glBegin (GL_LINE_STRIP);
{
for (int i = 0; i < inputShape->nVertices()+1; i++)
{
glVertex2f(
inputShape->getVertex(i % inputShape->nVertices()).x,
inputShape->getVertex(i % inputShape->nVertices()).y
);
}
}
glEnd ();
}
glPopMatrix();
}