-
Notifications
You must be signed in to change notification settings - Fork 0
/
spriterenderer.cpp
113 lines (93 loc) · 3.43 KB
/
spriterenderer.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
#include "spriterenderer.h"
#include <iostream>
SpriteRenderer::SpriteRenderer(QVector3D pos):spritePath("sprites.png"),spriteCoords(0,0),indexBuf(QOpenGLBuffer::IndexBuffer),position(pos),width(1){
initializeOpenGLFunctions();
arrayBuf.create();
indexBuf.create();
initText=spriteCoords;
}
SpriteRenderer::SpriteRenderer(std::string p,QVector2D coords,QVector3D pos):spritePath(p),spriteCoords(coords),indexBuf(QOpenGLBuffer::IndexBuffer),position(pos),width(1){
initializeOpenGLFunctions();
arrayBuf.create();
indexBuf.create();
initText=spriteCoords;
}
void SpriteRenderer::ReleaseBuffers(){
arrayBuf.release();
indexBuf.release();
}
SpriteRenderer::~SpriteRenderer(){
arrayBuf.destroy();
indexBuf.destroy();
}
float SpriteRenderer::getWidth()
{
return width;
}
void SpriteRenderer::SetPosition(QVector3D pos){
position = pos;
}
std::string SpriteRenderer::GetSpritePath(){
return spritePath;
}
void SpriteRenderer::ChangeSprite(){
}
QVector2D SpriteRenderer::GetTextCoords(){
return spriteCoords;
}
void SpriteRenderer::SetXSpriteCoord(float x){
spriteCoords.setX(x);
}
void SpriteRenderer::SetYSpriteCoord(float y){
spriteCoords.setY(y);
}
//construit la "géométrie" du sprite
void SpriteRenderer::CreateGeometry(){
//compute a VertexData array
VertexData v2[4] ={
{position,QVector2D(spriteCoords.x(),spriteCoords.y()+1.0/16.0)},
{QVector3D(position.x()+width,position.y(),0),QVector2D(spriteCoords.x()+1.0/16.0,spriteCoords.y()+1.0/16.0)},
{QVector3D(position.x(),position.y()+1,0),spriteCoords },
{QVector3D(position.x()+width,position.y()+1,0),QVector2D(spriteCoords.x()+1.0/16.0,spriteCoords.y())}
};
//compute indices
GLushort indices[6] = {//2 triangles
0 , 1 , 2 , 1 , 3, 2
};//(top left top right bottom left) (top right bottom left bottom right)
// Transfer vertex data to VBO 0
arrayBuf.bind();
arrayBuf.allocate(v2, 4 * sizeof(VertexData));
// Transfer index data to VBO 1
indexBuf.bind();
indexBuf.allocate(indices, 6 * sizeof(GLushort));
}
//s'occupe du rendu opengl
void SpriteRenderer::Render(QOpenGLShaderProgram *program,QOpenGLTexture *text){
// Tell OpenGL which VBOs to use
if (arrayBuf.bind() == false){
std::cout << "Sprite renderer : arrayBind bug" << std::endl;
};
if (indexBuf.bind() == false){
std::cout << "Sprite renderer : indexBind bug" << std::endl;
};
// Offset for position
quintptr offset = 0;
text->bind();
// Tell OpenGL programmable pipeline how to locate vertex position data
int vertexLocation = program->attributeLocation("a_position");
program->enableAttributeArray(vertexLocation);
program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
// Offset for texture coordinate
offset += sizeof(QVector3D);
program->setUniformValue("objectwcoord",QVector4D(position.x(),position.y(),0,0));
// Tell OpenGL programmable pipeline how to locate vertex texture coordinate data
int texcoordLocation = program->attributeLocation("a_texcoord");
program->enableAttributeArray(texcoordLocation);
program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));
// Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
}
void SpriteRenderer::setWidth(float f){
width = f;
this->CreateGeometry();
}