17 #include <glutil/glutil.h> 19 #include <ShaderProgram.h> 22 #include <HUDGraphic.h> 23 #include <FontManager.h> 24 #include <IHudDrawable.h> 25 #include <ResourceManager.h> 28 void GLSquare::render(
ShaderProgram* shaderProgram, chag::float4x4* projectionMatrix) {
30 GLint currentDepthFunc;
31 glGetIntegerv(GL_DEPTH_FUNC, ¤tDepthFunc);
33 glDepthFunc(GL_ALWAYS);
35 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
39 bindTextureAndDraw(shaderProgram,projectionMatrix);
43 glDepthFunc(currentDepthFunc);
46 glEnable(GL_CULL_FACE);
50 void GLSquare::bindTextureAndDraw(
ShaderProgram *shaderProgram, chag::float4x4* projectionMatrix) {
52 glBindVertexArray(vao);
54 shaderProgram->setUniform1i(
"sprite", 0);
55 shaderProgram->setUniformMatrix4fv(
"modelMatrix", getModelMatrix());
56 shaderProgram->setUniformMatrix4fv(
"projectionMatrix",*projectionMatrix);
57 shaderProgram->setUniform1i(
"isFont",
false);
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);
66 shaderProgram->setUniform1f(
"ratioWidthToHeight", width/height);
69 chag::float4 calcBorders = chag::make_vector(borders[0] / height,
73 shaderProgram->setUniform4f(
"border",calcBorders);
75 shaderProgram->setUniform4f(
"borderColor",graphic->
getBorderColor());
79 shaderProgram->setUniform1i(
"isTexture",
true);
80 shaderProgram->setUniform1i(
"isColor",
false);
82 shaderProgram->setUniform1i(
"isTexture",
false);
83 shaderProgram->setUniform1i(
"isColor",
true);
84 shaderProgram->setUniform4f(
"color",graphic->
getColor());
87 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
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>();
98 GLSquare::GLSquare(
float posX,
float posY,
float width,
float height,
HUDGraphic *image) {
99 init(posX,posY,width,height,image);
102 void GLSquare::init(
float posX,
float posY,
float width,
float height,
HUDGraphic *image) {
106 this->height = height;
107 this->graphic = image;
109 center = image->getCenterOffset(width,height);
110 updateOriginalPosition();
114 void GLSquare::updateOriginalPosition() {
115 originalPosition = chag::make_vector( posX + width / 2.0f - center.x,
116 -posY - height / 2.0f - center.y,
122 updateOriginalPosition();
125 void GLSquare::fillVertexBuffer() {
127 float halfWidth = width/2, halfHeight = height/2;
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,
138 glGenVertexArrays(1, &vao);
139 glBindVertexArray(vao);
142 glGenBuffers(1, &pos_vbo);
143 glBindBuffer(GL_ARRAY_BUFFER, pos_vbo);
144 glBufferData(GL_ARRAY_BUFFER,
sizeof(quad), quad, GL_STATIC_DRAW);
146 glEnableVertexAttribArray(0);
147 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 *
sizeof(
float), 0);
149 glEnableVertexAttribArray(1);
150 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 7 *
sizeof(
float), ((
char *)NULL +
sizeof(
float) * 3));
152 glEnableVertexAttribArray(2);
153 glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 7 *
sizeof(
float), ((
char *)NULL +
sizeof(
float) * 5));
156 glBindVertexArray(0);
160 GLSquare::~GLSquare() {
161 glDeleteVertexArrays(1,&vao);
164 float GLSquare::getX() {
168 float GLSquare::getY() {
172 float GLSquare::getWidth(){
176 float GLSquare::getHeight() {
184 void GLSquare::updateGraphic() {
186 glDeleteVertexArrays(1,&vao);
190 void GLSquare::setGraphic(
HUDGraphic *graphic) {
191 this->graphic = graphic;
virtual void setRelativePosition(chag::float3 position)
void restorePreviousShaderProgram()
Class for maintaining OpenGL shader programs.
void setCenterOffset(chag::float3 offset)
chag::float4 getBorderColor()
bool isTextureElseColor()
int * getRoundedCorners()
void backupCurrentShaderProgram()