flare.cpp
上传用户:qccn516
上传日期:2013-05-02
资源大小:3382k
文件大小:4k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /* Light Flares
  2.  *
  3.  * Copyright (C) 2003-2004, Alexander Zaprjagaev <frustum@frustum.org>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  */
  19. #include "engine.h"
  20. #include "texture.h"
  21. #include "mesh.h"
  22. #include "flare.h"
  23. int Flare::counter = 0;
  24. Texture *Flare::flare_tex;
  25. /*
  26.  */
  27. Flare::Flare(float min_radius,float max_radius,float sphere_radius) : min_radius(min_radius), max_radius(max_radius), sphere_radius(sphere_radius), time(0) {
  28. if(counter++ == 0) {
  29. flare_tex = new Texture(Engine::findFile(FLARE_TEX),Texture::TEXTURE_2D,Texture::RGB | Texture::LINEAR_MIPMAP_LINEAR | Texture::CLAMP);
  30. }
  31. }
  32. Flare::~Flare() {
  33. if(--counter == 0) {
  34. delete flare_tex;
  35. }
  36. }
  37. /*
  38.  */
  39. void Flare::render(const vec3 &pos,const vec4 &color) {
  40. if(Engine::have_occlusion == 0) return;
  41. time += Engine::ifps; // manualy update time
  42. glDepthMask(GL_FALSE);
  43. glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
  44. glDepthFunc(GL_ALWAYS);
  45. glPushMatrix();
  46. glTranslatef(pos.x,pos.y,pos.z);
  47. glScalef(sphere_radius,sphere_radius,sphere_radius);
  48. glBeginQueryARB(GL_SAMPLES_PASSED_ARB,Engine::query_id);
  49. Engine::sphere_mesh->render(false);
  50. glEndQueryARB(GL_SAMPLES_PASSED_ARB);
  51. glPopMatrix();
  52. GLuint samples_0;
  53. glGetQueryObjectuivARB(Engine::query_id,GL_QUERY_RESULT_ARB,&samples_0);
  54. glDepthFunc(GL_LESS);
  55. if(samples_0 == 0) {
  56. glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
  57. glDepthMask(GL_TRUE);
  58. return;
  59. }
  60. glPushMatrix();
  61. glTranslatef(pos.x,pos.y,pos.z);
  62. glScalef(sphere_radius,sphere_radius,sphere_radius);
  63. glBeginQueryARB(GL_SAMPLES_PASSED_ARB,Engine::query_id);
  64. Engine::sphere_mesh->render(false);
  65. glEndQueryARB(GL_SAMPLES_PASSED_ARB);
  66. glPopMatrix();
  67. GLuint samples_1;
  68. glGetQueryObjectuivARB(Engine::query_id,GL_QUERY_RESULT_ARB,&samples_1);
  69. glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
  70. glDepthMask(GL_TRUE);
  71. if(samples_1 == 0) return;
  72. glDepthFunc(GL_ALWAYS);
  73. float radius = min_radius + (max_radius - min_radius) * (float)samples_1 / (float)samples_0;
  74. if(radius < 0) radius = 0;
  75. vec3 dx = Engine::imodelview.rotation() * vec3(1,0,0);
  76. vec3 dy = Engine::imodelview.rotation() * vec3(0,1,0);
  77. glEnable(GL_BLEND);
  78. glBlendFunc(GL_ONE,GL_ONE);
  79. vec3 c(color);
  80. c.normalize();
  81. glColor3fv(c * (float)samples_1 / (float)samples_0 / 1.5f);
  82. flare_tex->enable();
  83. flare_tex->bind();
  84. glMatrixMode(GL_TEXTURE);
  85. glTranslatef(0.5,0.5,0);
  86. glRotatef(time * 360.0f / 16.0f,0,0,1);
  87. glTranslatef(-0.5,-0.5,0);
  88. glBegin(GL_TRIANGLE_STRIP);
  89. glTexCoord2f(0,1);
  90. glVertex3fv(pos - (dx + dy) * radius);
  91. glTexCoord2f(1,1);
  92. glVertex3fv(pos + (dx - dy) * radius);
  93. glTexCoord2f(0,0);
  94. glVertex3fv(pos - (dx - dy) * radius);
  95. glTexCoord2f(1,0);
  96. glVertex3fv(pos + (dx + dy) * radius);
  97. glEnd();
  98. glLoadIdentity();
  99. glTranslatef(0.5,0.5,0);
  100. glRotatef(-time * 360.0f / 16.0f,0,0,1);
  101. glTranslatef(-0.5,-0.5,0);
  102. glBegin(GL_TRIANGLE_STRIP);
  103. glTexCoord2f(0,1);
  104. glVertex3fv(pos - (dx + dy) * radius);
  105. glTexCoord2f(1,1);
  106. glVertex3fv(pos + (dx - dy) * radius);
  107. glTexCoord2f(0,0);
  108. glVertex3fv(pos - (dx - dy) * radius);
  109. glTexCoord2f(1,0);
  110. glVertex3fv(pos + (dx + dy) * radius);
  111. glEnd();
  112. glLoadIdentity();
  113. glMatrixMode(GL_MODELVIEW);
  114. flare_tex->disable();
  115. glDisable(GL_BLEND);
  116. glDepthFunc(GL_LESS);
  117. }