This repository was archived by the owner on Dec 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture2D.cpp
More file actions
88 lines (68 loc) · 2.1 KB
/
Texture2D.cpp
File metadata and controls
88 lines (68 loc) · 2.1 KB
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
//
// Created by firely-pasha on 6/20/17.
//
#include "Texture2D.h"
#include <QDebug>
Texture2D::Texture2D(const QString &fileName, const QRect& rect)
{
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
QImage originalImage(fileName);
glImage = QGLWidget::convertToGLFormat(originalImage);
if (rect.height() != 0 && rect.height() != 0) {
glImage = glImage.copy(rect);
}
glTexImage2D(GL_TEXTURE_2D, 0, 4,
glImage.width(), glImage.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, glImage.bits());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
scale = 1;
width = glImage.width();
height = glImage.height();
}
Texture2D::Texture2D(const QImage image)
{
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glImage = image;
glTexImage2D(GL_TEXTURE_2D, 0, 3,
glImage.width(), glImage.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, glImage.bits());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
scale = 1;
width = glImage.width();
height = glImage.height();
}
Texture2D::~Texture2D()
{
}
void Texture2D::draw(QVector2D& point)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureId);
glBegin(GL_QUADS);
float x = point.x();
float y = point.y();
glTexCoord2f(1, 0), glVertex2f(x + width * scale,
y);
glTexCoord2f(1, 1), glVertex2f(x + width * scale,
y + height * scale);
glTexCoord2f(0, 1), glVertex2f(x,
y + height * scale);
glTexCoord2f(0, 0), glVertex2f(x,
y);
glEnd();
glDisable(GL_TEXTURE_2D);
}
int Texture2D::getWidth() const
{
return width;
}
int Texture2D::getHeight()
{
return height;
}