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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
//
// Copyright 2013 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 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 to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#include "glLoader.h"
#include "glHud.h"
#include "glUtils.h"
#include "font_image.h"
#include "simple_math.h"
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cassert>
#include <iostream>
static const char *s_VS =
#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
"attribute vec2 position;\n"
"attribute vec3 color;\n"
"attribute vec2 uv;\n"
"varying vec4 fragColor;\n"
"varying 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";
#endif
static const char *s_FS =
#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
"varying vec4 fragColor;\n"
"varying vec2 fragUV;\n"
"uniform sampler2D fontTexture;\n"
"void main()\n"
"{\n"
" vec4 c = texture2D(fontTexture, fragUV);\n"
" if (c.a == 0.0) discard;\n"
" gl_FragColor = c*fragColor;\n"
"}\n";
#endif
static const char *s_BG_VS =
#if defined(GL_VERSION_3_1)
"#version 150\n"
"out vec2 uv;\n"
"void main() {\n"
" vec4 pos[4] = vec4[] (vec4(-1,-1,0,1), vec4(1,-1,0,1), \n"
" vec4(-1,1,0,1), vec4(1,1,0,1)); \n"
" uv = pos[gl_VertexID].xy;\n"
" gl_Position = pos[gl_VertexID];\n"
"}\n";
#else
"varying vec2 uv;\n"
"void main() {\n"
" vec4 pos[4] = vec4[] (vec4(-1,-1,0,1), vec4(1,-1,0,1), \n"
" vec4(-1,1,0,1), vec4(1,1,0,1)); \n"
" uv = pos[gl_VertexID].xy;\n"
" gl_Position = pos[gl_VertexID];\n"
"}\n";
#endif
static const char *s_BG_FS =
#if defined(GL_VERSION_3_1)
"#version 150\n"
"in vec2 uv;\n"
"out vec4 color;\n"
"void main() {\n"
" color = vec4(mix(0.1, 0.5, sin((uv.y*0.5+0.5)*3.14159)));\n"
" color.a = 1.0;\n"
"}\n";
#else
"varying vec2 uv;\n"
"void main() {\n"
" gl_FragColor = vec4(mix(0.1, 0.5, sin((uv.y*0.5+0.5)*3.14159)));\n"
" gl_FragColor.a = 1.0;\n"
"}\n";
#endif
GLhud::GLhud() : _fontTexture(0), _vbo(0), _staticVbo(0),
_vao(0), _staticVao(0), _program(0),
_aPosition(0), _aColor(0), _aUV(0), _bgProgram(0) {
}
GLhud::~GLhud() {
if (_program)
glDeleteProgram(_program);
if (_fontTexture)
glDeleteTextures(1, &_fontTexture);
if (_vbo)
glDeleteBuffers(1, &_vbo);
if (_staticVbo)
glDeleteBuffers(1, &_staticVbo);
if (_vao)
glDeleteVertexArrays(1, &_vao);
if (_staticVao)
glDeleteVertexArrays(1, &_staticVao);
if (_bgVao)
glDeleteVertexArrays(1, &_bgVao);
if (_bgProgram)
glDeleteProgram(_bgProgram);
}
void
GLhud::Init(int width, int height, int frameBufferWidth, int frameBufferHeight) {
Hud::Init(width, height, frameBufferWidth, frameBufferHeight);
glGenTextures(1, &_fontTexture);
glBindTexture(GL_TEXTURE_2D, _fontTexture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT,
0, GL_RGBA, GL_UNSIGNED_BYTE, font_image);
glGenBuffers(1, &_vbo);
glGenBuffers(1, &_staticVbo);
glGenVertexArrays(1, &_vao);
glGenVertexArrays(1, &_staticVao);
glGenVertexArrays(1, &_bgVao);
GLuint vertexShader = GLUtils::CompileShader(GL_VERTEX_SHADER, s_VS);
GLuint fragmentShader = GLUtils::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");
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);
// ------ create bg program
vertexShader = GLUtils::CompileShader(GL_VERTEX_SHADER, s_BG_VS);
fragmentShader = GLUtils::CompileShader(GL_FRAGMENT_SHADER, s_BG_FS);
_bgProgram = glCreateProgram();
glAttachShader(_bgProgram, vertexShader);
glAttachShader(_bgProgram, fragmentShader);
glLinkProgram(_bgProgram);
GLUtils::CheckGLErrors("GLhud::Init");
}
void
GLhud::Rebuild(int width, int height,
int framebufferWidth, int framebufferHeight) {
Hud::Rebuild(width, height, framebufferWidth, framebufferHeight);
if (! _staticVbo)
return;
_staticVboSize = (int)getStaticVboSource().size();
glBindBuffer(GL_ARRAY_BUFFER, _staticVbo);
glBufferData(GL_ARRAY_BUFFER, _staticVboSize * sizeof(float),
&getStaticVboSource()[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
bool
GLhud::Flush() {
if (!Hud::Flush())
return false;
// update dynamic text
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, getVboSource().size() * sizeof(float),
&getVboSource()[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// (x, y, r, g, b, u, v) = 7
int numVertices = (int)getVboSource().size()/7;
// reserved space of the vector remains for the next frame.
getVboSource().clear();
glUseProgram(_program);
float proj[16];
ortho(proj, 0, 0, float(GetWidth()), float(GetHeight()));
glUniformMatrix4fv(_mvpMatrix, 1, GL_FALSE, proj);
glDisable(GL_DEPTH_TEST);
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _fontTexture);
glBindVertexArray(_vao);
glDrawArrays(GL_TRIANGLES, 0, numVertices);
glBindVertexArray(_staticVao);
glDrawArrays(GL_TRIANGLES, 0, _staticVboSize/7);
glBindTexture(GL_TEXTURE_2D, 0);
}
glEnable(GL_DEPTH_TEST);
return true;
}
void
GLhud::FillBackground() {
glUseProgram(_bgProgram);
glBindVertexArray(_bgVao);
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glUseProgram(0);
glBindVertexArray(0);
}
|