-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.cpp
78 lines (70 loc) · 2.52 KB
/
sprite.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
#include "sprite.h"
#include "aw2dmath.h"
Sprite::Sprite(const QString& name, const QVector3D& boundsMin, const QVector3D& boundsMax,
QGLWidget& glWidget)
: imageDiffuse_("assets:/demo_data/sprites/" + name + "_diffuse_0.png")
, imageNormal_("assets:/demo_data/sprites/" + name + "_normal_0.png")
, imageOffset_("assets:/demo_data/sprites/" + name + "_offset_0.png")
, boundsMin_(boundsMin)
, boundsMax_(boundsMax)
, pixelSize_(QVector2D(0.0f, 0.0f))
, valid_(false)
, creatorWidget_(glWidget)
{
// All images must be successfully loaded
if(imageDiffuse_.isNull())
{
qDebug() << "Failed to load diffuse image for sprite " << name;
return;
}
if(imageNormal_.isNull())
{
qDebug() << "Failed to load normal image for sprite " << name;
return;
}
if(imageOffset_.isNull())
{
qDebug() << "Failed to load offset image for sprite " << name;
return;
}
// Image sizes must match
if(!(imageDiffuse_.size() == imageNormal_.size() &&
imageNormal_.size() == imageOffset_.size()))
{
qDebug() << "Image sizes in sprite " << name << " don't match";
return;
}
pixelSize_ = QVector2D(imageDiffuse_.size().width(), imageDiffuse_.size().height());
// Image sizes must be powers of two
if(!isPowerOfTwo(imageDiffuse_.size().width()) ||
!isPowerOfTwo(imageDiffuse_.size().height()))
{
qDebug() << "Non-power of two texture size for sprite " << name
<< " - not supported yet";
return;
}
// Upload to GL textures.
textureDiffuse_ = glWidget.bindTexture(imageDiffuse_, GL_TEXTURE_2D, GL_RGBA,
QGLContext::InvertedYBindOption);
textureNormal_ = glWidget.bindTexture(imageNormal_, GL_TEXTURE_2D, GL_RGB,
QGLContext::InvertedYBindOption);
textureOffset_ = glWidget.bindTexture(imageOffset_, GL_TEXTURE_2D, GL_RGB,
QGLContext::InvertedYBindOption);
// Check that the textures were uploaded successfully.
if(textureDiffuse_ == 0)
{
qDebug() << "Failed to create diffuse texture for sprite " << name;
return;
}
if(textureNormal_ == 0)
{
qDebug() << "Failed to create normal texture for sprite " << name;
return;
}
if(textureOffset_ == 0)
{
qDebug() << "Failed to create offset texture for sprite " << name;
return;
}
valid_ = true;
}