Bubba-3D  0.9.0
Awesome game engine!
GLSquare.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 #include <glutil/glutil.h>
18 #include <GL/glew.h>
19 #include <ShaderProgram.h>
20 #include "GLSquare.h"
21 #include <Texture.h>
22 #include <HUDGraphic.h>
23 #include <FontManager.h>
24 #include <IHudDrawable.h>
25 #include <ResourceManager.h>
26 #include <Globals.h>
27 
28 void GLSquare::render(ShaderProgram* shaderProgram, chag::float4x4* projectionMatrix) {
29 
30  GLint currentDepthFunc;
31  glGetIntegerv(GL_DEPTH_FUNC, &currentDepthFunc);
32 
33  glDepthFunc(GL_ALWAYS);
34  glEnable(GL_BLEND);
35  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
36 
37  shaderProgram->backupCurrentShaderProgram();
38 
39  bindTextureAndDraw(shaderProgram,projectionMatrix);
40 
41  CHECK_GL_ERROR();
42 
43  glDepthFunc(currentDepthFunc);
44  glDisable(GL_BLEND);
45 
46  glEnable(GL_CULL_FACE);
47  shaderProgram->restorePreviousShaderProgram();
48 }
49 
50 void GLSquare::bindTextureAndDraw(ShaderProgram *shaderProgram, chag::float4x4* projectionMatrix) {
51  shaderProgram->use();
52  glBindVertexArray(vao);
53 
54  shaderProgram->setUniform1i("sprite", 0);
55  shaderProgram->setUniformMatrix4fv("modelMatrix", getModelMatrix());
56  shaderProgram->setUniformMatrix4fv("projectionMatrix",*projectionMatrix);
57  shaderProgram->setUniform1i("isFont",false);
58 
59  int* roundedCorners = graphic->getRoundedCorners();
60  chag::float4 calcCorners = chag::make_vector(roundedCorners[0] / height,
61  roundedCorners[1] / height,
62  roundedCorners[2] / height,
63  roundedCorners[3] / height);
64  shaderProgram->setUniform4f("roundedCorners",calcCorners);
65 
66  shaderProgram->setUniform1f("ratioWidthToHeight", width/height);
67 
68  int* borders = graphic->getBorders();
69  chag::float4 calcBorders = chag::make_vector(borders[0] / height,
70  borders[1] / width,
71  borders[2] / height,
72  borders[3] / width);
73  shaderProgram->setUniform4f("border",calcBorders);
74 
75  shaderProgram->setUniform4f("borderColor",graphic->getBorderColor());
76 
77  if(graphic->isTextureElseColor()) {
78  graphic->getTexture()->bind(GL_TEXTURE0);
79  shaderProgram->setUniform1i("isTexture",true);
80  shaderProgram->setUniform1i("isColor",false);
81  }else{
82  shaderProgram->setUniform1i("isTexture",false);
83  shaderProgram->setUniform1i("isColor",true);
84  shaderProgram->setUniform4f("color",graphic->getColor());
85  }
86 
87  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
88 }
89 
90 chag::float4x4 GLSquare::getModelMatrix() {
91  return chag::make_translation(originalPosition + relativePosition)
92  * chag::make_rotation_z<chag::float4x4>(rotation)
93  * chag::make_scale<chag::float4x4>(scale)
94  * chag::make_translation(center)
95  * chag::make_identity<chag::float4x4>();
96 }
97 
98 GLSquare::GLSquare(float posX, float posY, float width, float height, HUDGraphic *image) {
99  init(posX,posY,width,height,image);
100 }
101 
102 void GLSquare::init(float posX, float posY, float width, float height, HUDGraphic *image) {
103  this->posX = posX;
104  this->posY = posY;
105  this->width = width;
106  this->height = height;
107  this->graphic = image;
108  IHudDrawable::setRelativePosition(chag::make_vector(0.0f, 0.0f, 0.0f));
109  center = image->getCenterOffset(width,height);
110  updateOriginalPosition();
111  fillVertexBuffer();
112 }
113 
114 void GLSquare::updateOriginalPosition() {
115  originalPosition = chag::make_vector( posX + width / 2.0f - center.x,
116  -posY - height / 2.0f - center.y,
117  0.0f);
118 }
119 
120 void GLSquare::setCenterOffset(chag::float3 offset) {
121  center = offset;
122  updateOriginalPosition();
123 }
124 
125 void GLSquare::fillVertexBuffer() {
126 
127  float halfWidth = width/2, halfHeight = height/2;
128 
129  HUDGraphic::TexturePosition<float> tp = graphic->isTextureElseColor() ? graphic->getTexturePosition() : HUDGraphic::TexturePosition<float>(0.0f,0.0f,1.0f,1.0f);
130 
131  GLfloat quad[] = {
132  -halfWidth, -halfHeight, 0.0f, tp.botLeftX, tp.botLeftY,0.0,0.0,
133  halfWidth, -halfHeight, 0.0f, tp.topRightX, tp.botLeftY,1.0,0.0,
134  -halfWidth, halfHeight, 0.0f, tp.botLeftX, tp.topRightY,0.0,1.0,
135  halfWidth, halfHeight, 0.0f, tp.topRightX, tp.topRightY,1.0,1.0,
136  };
137 
138  glGenVertexArrays(1, &vao);
139  glBindVertexArray(vao);
140 
141  GLuint pos_vbo;
142  glGenBuffers(1, &pos_vbo);
143  glBindBuffer(GL_ARRAY_BUFFER, pos_vbo);
144  glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW);
145 
146  glEnableVertexAttribArray(0);
147  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), 0);
148 
149  glEnableVertexAttribArray(1);
150  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(float), ((char *)NULL + sizeof(float) * 3));
151 
152  glEnableVertexAttribArray(2);
153  glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(float), ((char *)NULL + sizeof(float) * 5));
154 
155  // CLEANUP
156  glBindVertexArray(0);
157  CHECK_GL_ERROR();
158 }
159 
160 GLSquare::~GLSquare() {
161  glDeleteVertexArrays(1,&vao);
162 }
163 
164 float GLSquare::getX() {
165  return posX;
166 }
167 
168 float GLSquare::getY() {
169  return posY;
170 }
171 
172 float GLSquare::getWidth(){
173  return width;
174 }
175 
176 float GLSquare::getHeight() {
177  return height;
178 }
179 
180 HUDGraphic* GLSquare::getGraphic(){
181  return graphic;
182 }
183 
184 void GLSquare::updateGraphic() {
185  setCenterOffset(graphic->getCenterOffset(width,height));
186  glDeleteVertexArrays(1,&vao);
187  fillVertexBuffer();
188 }
189 
190 void GLSquare::setGraphic(HUDGraphic *graphic) {
191  this->graphic = graphic;
192  updateGraphic();
193 }
virtual void setRelativePosition(chag::float3 position)
void restorePreviousShaderProgram()
Class for maintaining OpenGL shader programs.
Definition: ShaderProgram.h:39
void setCenterOffset(chag::float3 offset)
Definition: GLSquare.cpp:120
chag::float4 getBorderColor()
Definition: HUDGraphic.cpp:102
Texture * getTexture()
Definition: HUDGraphic.cpp:69
int * getBorders()
Definition: HUDGraphic.cpp:106
chag::float4 getColor()
Definition: HUDGraphic.cpp:79
bool isTextureElseColor()
Definition: HUDGraphic.cpp:75
int * getRoundedCorners()
Definition: HUDGraphic.cpp:122
void backupCurrentShaderProgram()