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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "MetalExtraction.h"
#include "InfoTextureHandler.h"
#include "Game/GlobalUnsynced.h"
#include "Map/MetalMap.h"
#include "Map/ReadMap.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/Shaders/ShaderHandler.h"
#include "Rendering/Shaders/Shader.h"
#include "Sim/Misc/LosHandler.h"
#include "System/Exceptions.h"
#include "System/Log/ILog.h"
CMetalExtractionTexture::CMetalExtractionTexture()
: CPboInfoTexture("metalextraction")
, updateN(0)
{
texSize = int2(mapDims.hmapx, mapDims.hmapy);
texChannels = 1;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//Info: 32F isn't really needed for the final result, but it allows us
// to upload the CPU array directly to the GPU w/o any (slow) cpu-side
// transformation. The transformation (0..1 range rescaling) happens
// then on the gpu instead.
glSpringTexStorage2D(GL_TEXTURE_2D, 1, GL_R32F, texSize.x, texSize.y);
if (FBO::IsSupported()) {
fbo.Bind();
fbo.AttachTexture(texture);
/*bool status =*/ fbo.CheckStatus("CMetalExtractionTexture");
FBO::Unbind();
}
const std::string vertexCode = R"(
varying vec2 texCoord;
void main() {
texCoord = gl_Vertex.xy * 0.5 + 0.5;
gl_Position = vec4(gl_Vertex.xyz, 1.0);
}
)";
const std::string fragmentCode = R"(
uniform sampler2D tex0;
varying vec2 texCoord;
void main() {
gl_FragColor = texture2D(tex0, texCoord) * 800.0;
}
)";
shader = shaderHandler->CreateProgramObject("[CMetalExtractionTexture]", "CMetalExtractionTexture", false);
shader->AttachShaderObject(shaderHandler->CreateShaderObject(vertexCode, "", GL_VERTEX_SHADER));
shader->AttachShaderObject(shaderHandler->CreateShaderObject(fragmentCode, "", GL_FRAGMENT_SHADER));
shader->Link();
if (!shader->IsValid()) {
const char* fmt = "%s-shader compilation error: %s";
LOG_L(L_ERROR, fmt, shader->GetName().c_str(), shader->GetLog().c_str());
} else {
shader->Enable();
shader->SetUniform("tex0", 0);
shader->Disable();
shader->Validate();
if (!shader->IsValid()) {
const char* fmt = "%s-shader validation error: %s";
LOG_L(L_ERROR, fmt, shader->GetName().c_str(), shader->GetLog().c_str());
}
}
if (!fbo.IsValid() || !shader->IsValid()) {
throw opengl_error("");
}
}
bool CMetalExtractionTexture::IsUpdateNeeded()
{
// update only once per second
return (updateN++ % GAME_SPEED == 0);
}
void CMetalExtractionTexture::Update()
{
// los-checking is done in FBO: when FBO isn't working don't expose hidden data!
if (!fbo.IsValid() || !shader->IsValid())
return;
const CMetalMap* metalMap = readMap->metalMap;
const float* extractDepthMap = metalMap->GetExtractionMap();
// const unsigned short* myAirLos = &losHandler->airLosMaps[gu->myAllyTeam].front();
assert(metalMap->GetSizeX() == texSize.x && metalMap->GetSizeZ() == texSize.y);
// upload raw data to gpu
glBindTexture(GL_TEXTURE_2D, texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texSize.x, texSize.y, GL_RED, GL_FLOAT, extractDepthMap);
// do post-processing on the gpu (los-checking & scaling)
fbo.Bind();
shader->Enable();
glViewport(0,0, texSize.x, texSize.y);
glEnable(GL_BLEND);
glBlendFunc(GL_ZERO, GL_SRC_COLOR);
glBindTexture(GL_TEXTURE_2D, infoTextureHandler->GetInfoTexture("los")->GetTexture());
glBegin(GL_QUADS);
glVertex2f(-1.f, -1.f);
glVertex2f(-1.f, +1.f);
glVertex2f(+1.f, +1.f);
glVertex2f(+1.f, -1.f);
glEnd();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glDisable(GL_BLEND);
glViewport(globalRendering->viewPosX,0,globalRendering->viewSizeX,globalRendering->viewSizeY);
shader->Disable();
FBO::Unbind();
}
|