2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-07-18 21:19:50 +00:00
|
|
|
// Copyright 2013 Pixar
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-07-18 21:19:50 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License
|
|
|
|
// and the following modification to it: Section 6 Trademarks.
|
|
|
|
// deleted and replaced with:
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-07-18 21:19:50 +00:00
|
|
|
// 6. Trademarks. This License does not grant permission to use the
|
|
|
|
// trade names, trademarks, service marks, or product names of the
|
|
|
|
// Licensor and its affiliates, except as required for reproducing
|
|
|
|
// the content of the NOTICE file.
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-07-18 21:19:50 +00:00
|
|
|
// You may obtain a copy of the License at
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-07-18 21:19:50 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing,
|
|
|
|
// software distributed under the License is distributed on an
|
|
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
|
|
// either express or implied. See the License for the specific
|
|
|
|
// language governing permissions and limitations under the
|
|
|
|
// License.
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-01-29 23:54:18 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
#include "gl_hud.h"
|
2013-06-26 07:52:57 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
#include "font_image.h"
|
2013-01-24 17:29:35 +00:00
|
|
|
#include "simple_math.h"
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2013-06-26 07:52:57 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-01-24 17:29:35 +00:00
|
|
|
static const char *s_VS =
|
2013-01-29 23:54:18 +00:00
|
|
|
#if defined(GL_VERSION_3_1)
|
|
|
|
"#version 150\n"
|
|
|
|
"in vec2 position;\n"
|
|
|
|
"in vec3 color;\n"
|
|
|
|
"in vec2 uv;\n"
|
|
|
|
"out vec4 fragColor;\n"
|
|
|
|
"out vec2 fragUV;\n"
|
|
|
|
"uniform mat4 ModelViewProjectionMatrix;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" fragColor = vec4(color, 1);\n"
|
|
|
|
" fragUV = uv;\n"
|
|
|
|
" gl_Position = ModelViewProjectionMatrix * "
|
|
|
|
" vec4(position.x, position.y, 0, 1);\n"
|
|
|
|
"}\n";
|
|
|
|
#else
|
2013-01-24 17:29:35 +00:00
|
|
|
"attribute vec2 position;\n"
|
|
|
|
"attribute vec3 color;\n"
|
|
|
|
"attribute vec2 uv;\n"
|
|
|
|
"varying vec4 fragColor;\n"
|
|
|
|
"varying vec2 fragUV;\n"
|
|
|
|
"uniform mat4 ModelViewProjectionMatrix;\n"
|
2013-01-29 23:54:18 +00:00
|
|
|
"void main() {\n"
|
|
|
|
" fragColor = vec4(color, 1);\n"
|
2013-01-24 17:29:35 +00:00
|
|
|
" fragUV = uv;\n"
|
|
|
|
" gl_Position = ModelViewProjectionMatrix * "
|
|
|
|
" vec4(position.x, position.y, 0, 1);\n"
|
|
|
|
"}\n";
|
2013-01-29 23:54:18 +00:00
|
|
|
#endif
|
2013-01-24 17:29:35 +00:00
|
|
|
|
|
|
|
static const char *s_FS =
|
2013-01-29 23:54:18 +00:00
|
|
|
#if defined(GL_VERSION_3_1)
|
|
|
|
"#version 150\n"
|
|
|
|
"in vec4 fragColor;\n"
|
|
|
|
"in vec2 fragUV;\n"
|
|
|
|
"out vec4 color;\n"
|
|
|
|
"uniform sampler2D fontTexture;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" vec4 c = texture(fontTexture, fragUV);\n"
|
|
|
|
" if (c.a == 0.0) discard;\n"
|
|
|
|
" color = c*fragColor;\n"
|
|
|
|
"}\n";
|
|
|
|
#else
|
2013-01-24 17:29:35 +00:00
|
|
|
"varying vec4 fragColor;\n"
|
|
|
|
"varying vec2 fragUV;\n"
|
2013-01-29 23:54:18 +00:00
|
|
|
"uniform sampler2D fontTexture;\n"
|
2013-01-24 17:29:35 +00:00
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
2013-01-29 23:54:18 +00:00
|
|
|
" vec4 c = texture2D(fontTexture, fragUV);\n"
|
2013-01-24 17:29:35 +00:00
|
|
|
" if (c.a == 0.0) discard;\n"
|
2013-01-29 23:54:18 +00:00
|
|
|
" gl_FragColor = c*fragColor;\n"
|
2013-01-24 17:29:35 +00:00
|
|
|
"}\n";
|
2013-01-29 23:54:18 +00:00
|
|
|
#endif
|
2013-01-24 17:29:35 +00:00
|
|
|
|
2013-01-29 23:54:18 +00:00
|
|
|
GLhud::GLhud() : _fontTexture(0), _vbo(0), _staticVbo(0),
|
|
|
|
_vao(0), _staticVao(0), _program(0),
|
2013-01-24 17:29:35 +00:00
|
|
|
_aPosition(0), _aColor(0), _aUV(0)
|
2012-12-11 01:15:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GLhud::~GLhud()
|
|
|
|
{
|
2013-01-24 17:29:35 +00:00
|
|
|
if (_program)
|
|
|
|
glDeleteProgram(_program);
|
2012-12-11 01:15:13 +00:00
|
|
|
if (_fontTexture)
|
|
|
|
glDeleteTextures(1, &_fontTexture);
|
|
|
|
if (_vbo)
|
|
|
|
glDeleteBuffers(1, &_vbo);
|
|
|
|
if (_staticVbo)
|
|
|
|
glDeleteBuffers(1, &_staticVbo);
|
2013-01-29 23:54:18 +00:00
|
|
|
if (_vao)
|
|
|
|
glDeleteVertexArrays(1, &_vao);
|
|
|
|
if (_staticVao)
|
|
|
|
glDeleteVertexArrays(1, &_staticVao);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 17:29:35 +00:00
|
|
|
static GLuint compileShader(GLenum shaderType, const char *source)
|
|
|
|
{
|
|
|
|
GLuint shader = glCreateShader(shaderType);
|
|
|
|
glShaderSource(shader, 1, &source, NULL);
|
|
|
|
glCompileShader(shader);
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
void
|
|
|
|
GLhud::Init(int width, int height)
|
|
|
|
{
|
|
|
|
Hud::Init(width, height);
|
|
|
|
|
|
|
|
glGenTextures(1, &_fontTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, _fontTexture);
|
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
2013-01-24 17:29:35 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2012-12-11 01:15:13 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
2013-01-24 17:29:35 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
|
2012-12-11 01:15:13 +00:00
|
|
|
FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT,
|
|
|
|
0, GL_RGBA, GL_UNSIGNED_BYTE, font_image);
|
|
|
|
|
|
|
|
glGenBuffers(1, &_vbo);
|
|
|
|
glGenBuffers(1, &_staticVbo);
|
2013-01-24 17:29:35 +00:00
|
|
|
|
2013-01-29 23:54:18 +00:00
|
|
|
glGenVertexArrays(1, &_vao);
|
|
|
|
glGenVertexArrays(1, &_staticVao);
|
|
|
|
|
2013-01-24 17:29:35 +00:00
|
|
|
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, s_VS);
|
|
|
|
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, s_FS);
|
|
|
|
|
|
|
|
_program = glCreateProgram();
|
|
|
|
glAttachShader(_program, vertexShader);
|
|
|
|
glAttachShader(_program, fragmentShader);
|
|
|
|
|
|
|
|
glLinkProgram(_program);
|
|
|
|
|
|
|
|
glDeleteShader(vertexShader);
|
|
|
|
glDeleteShader(fragmentShader);
|
|
|
|
|
|
|
|
GLint status;
|
|
|
|
glGetProgramiv(_program, GL_LINK_STATUS, &status);
|
|
|
|
if (status == GL_FALSE) {
|
|
|
|
GLint infoLogLength;
|
|
|
|
glGetProgramiv(_program, GL_INFO_LOG_LENGTH, &infoLogLength);
|
|
|
|
char *infoLog = new char[infoLogLength];
|
|
|
|
glGetProgramInfoLog(_program, infoLogLength, NULL, infoLog);
|
|
|
|
printf("%s\n", infoLog);
|
|
|
|
delete[] infoLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
_mvpMatrix = glGetUniformLocation(_program, "ModelViewProjectionMatrix");
|
|
|
|
_aPosition = glGetAttribLocation(_program, "position");
|
|
|
|
_aColor = glGetAttribLocation(_program, "color");
|
|
|
|
_aUV = glGetAttribLocation(_program, "uv");
|
2013-01-29 23:54:18 +00:00
|
|
|
|
|
|
|
glBindVertexArray(_vao);
|
|
|
|
glEnableVertexAttribArray(_aPosition);
|
|
|
|
glEnableVertexAttribArray(_aColor);
|
|
|
|
glEnableVertexAttribArray(_aUV);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
|
|
|
glVertexAttribPointer(_aPosition, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
sizeof(GLfloat)*7, (void*)0);
|
|
|
|
glVertexAttribPointer(_aColor, 3, GL_FLOAT, GL_FALSE,
|
|
|
|
sizeof(GLfloat)*7, (void*)(sizeof(GLfloat)*2));
|
|
|
|
glVertexAttribPointer(_aUV, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
sizeof(GLfloat)*7, (void*)(sizeof(GLfloat)*5));
|
|
|
|
|
|
|
|
glBindVertexArray(_staticVao);
|
|
|
|
glEnableVertexAttribArray(_aPosition);
|
|
|
|
glEnableVertexAttribArray(_aColor);
|
|
|
|
glEnableVertexAttribArray(_aUV);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _staticVbo);
|
|
|
|
glVertexAttribPointer(_aPosition, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
sizeof(GLfloat)*7, (void*)0);
|
|
|
|
glVertexAttribPointer(_aColor, 3, GL_FLOAT, GL_FALSE,
|
|
|
|
sizeof(GLfloat)*7, (void*)(sizeof(GLfloat)*2));
|
|
|
|
glVertexAttribPointer(_aUV, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
sizeof(GLfloat)*7, (void*)(sizeof(GLfloat)*5));
|
|
|
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLhud::Rebuild(int width, int height)
|
|
|
|
{
|
|
|
|
Hud::Rebuild(width, height);
|
|
|
|
|
2013-01-31 23:27:31 +00:00
|
|
|
if (not _staticVbo)
|
|
|
|
return;
|
|
|
|
|
2013-09-21 01:19:34 +00:00
|
|
|
_staticVboSize = (int)getStaticVboSource().size();
|
2012-12-11 01:15:13 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _staticVbo);
|
2013-01-24 17:29:35 +00:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, _staticVboSize * sizeof(float),
|
2013-09-21 01:19:34 +00:00
|
|
|
&getStaticVboSource()[0], GL_STATIC_DRAW);
|
2012-12-11 01:15:13 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GLhud::Flush()
|
|
|
|
{
|
|
|
|
if (!Hud::Flush())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// update dynamic text
|
2013-01-29 23:54:18 +00:00
|
|
|
glBindVertexArray(_vao);
|
2012-12-11 01:15:13 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
2013-01-24 17:29:35 +00:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, getVboSource().size() * sizeof(float),
|
|
|
|
&getVboSource()[0], GL_STATIC_DRAW);
|
2012-12-11 01:15:13 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2013-01-24 17:29:35 +00:00
|
|
|
/* (x, y, r, g, b, u, v) = 7*/
|
|
|
|
int numVertices = (int)getVboSource().size()/7;
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
// reserved space of the vector remains for the next frame.
|
|
|
|
getVboSource().clear();
|
|
|
|
|
2013-01-24 17:29:35 +00:00
|
|
|
glUseProgram(_program);
|
|
|
|
float proj[16];
|
2013-01-24 22:16:45 +00:00
|
|
|
ortho(proj, 0, 0, float(GetWidth()), float(GetHeight()));
|
2013-01-24 17:29:35 +00:00
|
|
|
glUniformMatrix4fv(_mvpMatrix, 1, GL_FALSE, proj);
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, _fontTexture);
|
|
|
|
|
2013-01-29 23:54:18 +00:00
|
|
|
glBindVertexArray(_vao);
|
2012-12-11 01:15:13 +00:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, numVertices);
|
|
|
|
|
2013-01-29 23:54:18 +00:00
|
|
|
glBindVertexArray(_staticVao);
|
2012-12-11 01:15:13 +00:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, _staticVboSize/7);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|