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
|
#include <asura-utils/exceptions/exception.h>
#include "../core_config.h"
#include "image.h"
#include "gl.h"
using namespace AEIO;
namespace AsuraEngine
{
namespace Graphics
{
Image::Image()
: mVBO(nullptr)
, mWidth(0)
, mHeight(0)
{
}
Image::~Image()
{
if (mVBO)
delete mVBO;
}
bool Image::Load(ImageData* imgData)
{
if (!imgData) return false;
if (mTex == 0)
{
glGenTextures(1, &mTex);
if (mTex == 0)
throw Exception("OpenGL glGenTextures failed.");
}
glBindTexture(GL_TEXTURE_2D, mTex);
imgData->Lock();
int width = imgData->width;
int height = imgData->height;
TextureFormat tf = ConvertColorFormat(imgData->format);
glTexImage2D(
GL_TEXTURE_2D
, 0
, tf.internalformat
, width, height
, 0
, tf.externalformat
, tf.type
, imgData->pixels
);
// ʼbuffer
if (!mVBO)
mVBO = new GPUBuffer(BUFFER_TYPE_VERTEX, BUFFER_USAGE_STATIC, 16*sizeof(float)); // positionuv
if (mWidth != imgData->width || mHeight != imgData->height)
{
float w = imgData->width,
h = imgData->height;
float buffer[] = {
0, 0, 0, 0,
0, h, 0, 1,
w, h, 1, 1,
w, 0, 1, 0
};
mVBO->Fill(buffer, sizeof(buffer));
}
mWidth = imgData->width;
mHeight = imgData->height;
imgData->Unlock();
GLenum err = glGetError();
if (err != GL_NO_ERROR)
throw Exception("OpenGL glTexImage2D cause error, error code=%d", err);
glBindTexture(GL_TEXTURE_2D, 0);
return true;
}
bool Image::Load(ImageData* imgData, const AEMath::Vector2i& pos)
{
if (!imgData) return false;
glBindTexture(GL_TEXTURE_2D, mTex);
imgData->Lock();
int width = imgData->width;
int height = imgData->height;
TextureFormat tf = ConvertColorFormat(imgData->format);
glTexSubImage2D(
GL_TEXTURE_2D
, 0
, pos.x
, pos.y
, imgData->width
, imgData->height
, tf.externalformat
, tf.type
, imgData->pixels
);
imgData->Unlock();
GLenum err = glGetError();
if (err != GL_NO_ERROR)
throw Exception("OpenGL glTexSubImage2D cause error, error code=%d", err);
glBindTexture(GL_TEXTURE_2D, 0);
return true;
}
uint32 Image::GetWidth()
{
return mWidth;
}
uint32 Image::GetHeight()
{
return mHeight;
}
void Image::UpdateBuffer()
{
if (!mVBO)
return;
float w = mWidth,
h = mHeight;
float buffer[] = {
0, 0, 0, 0,
0, h, 0, 1,
w, h, 1, 1,
w, 0, 1, 0
};
mVBO->Fill(buffer, sizeof(buffer));
}
}
}
|