1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-26 04:12:41 +01:00

Primitive water rendering

This commit is contained in:
Daniel Evans 2014-06-08 19:04:47 +01:00
parent ad6eda9024
commit 208a2a9897
5 changed files with 117 additions and 45 deletions

View File

@ -229,10 +229,22 @@ public:
*/ */
std::map<std::string, std::shared_ptr<DynamicObjectData>> dynamicObjectData; std::map<std::string, std::shared_ptr<DynamicObjectData>> dynamicObjectData;
/** /**
* Water Rectangles * @struct WaterArea
* Stores Water Rectangle Information
*/ */
std::vector<RWTypes::WaterRect> waterRects; struct WaterArea
{
float height;
float xLeft, yBottom;
float xRight, yTop;
};
/**
* Water Areas
*/
std::vector<WaterArea> waterBlocks;
/** /**
* Water heights * Water heights

View File

@ -46,16 +46,6 @@ struct RGBA
uint8_t r, g, b, a; uint8_t r, g, b, a;
}; };
/**
* @struct WaterRect
* Rectangle with water information
*/
struct WaterRect
{
float height;
float xLeft, yBottom;
float xRight, yTop;
};
} }
#endif #endif

View File

@ -59,12 +59,13 @@ public:
GLint uniMatDiffuse, uniMatAmbient, uniFogStart, uniFogEnd; GLint uniMatDiffuse, uniMatAmbient, uniFogStart, uniFogEnd;
GLuint worldProgram; GLuint worldProgram;
GLuint skyProgram; GLuint skyProgram;
GLuint waterProgram, waterMVP, waterHeight, waterTexture, waterSize;
GLint skyUniView, skyUniProj, skyUniTop, skyUniBottom; GLint skyUniView, skyUniProj, skyUniTop, skyUniBottom;
/// Internal VAO to avoid clobbering global state. /// Internal VAO to avoid clobbering global state.
GLuint vao, debugVAO; GLuint vao, debugVAO;
GLuint planeVBO, skydomeVBO, skydomeIBO, debugVBO; GLuint skydomeVBO, skydomeIBO, debugVBO;
GLuint debugTex; GLuint debugTex;
/** /**

View File

@ -353,6 +353,7 @@ void GameData::loadWaterpro(const std::string& path)
ifstr.seekg(0x03C4); ifstr.seekg(0x03C4);
ifstr.read(reinterpret_cast<char*>(&visibleWater), sizeof(float)*64*64); ifstr.read(reinterpret_cast<char*>(&visibleWater), sizeof(float)*64*64);
ifstr.read(reinterpret_cast<char*>(&realWater), sizeof(float)*128*128);
} }
} }
@ -377,7 +378,7 @@ void GameData::loadWater(const std::string& path)
fc = atof(c.c_str()); fc = atof(c.c_str());
fd = atof(d.c_str()); fd = atof(d.c_str());
fe = atof(e.c_str()); fe = atof(e.c_str());
waterRects.push_back({ waterBlocks.push_back({
fa, fa,
fb, fb,
fc, fc,

View File

@ -80,29 +80,50 @@ const char *skydomeFragmentShaderSource = "#version 130\n"
"}"; "}";
const size_t skydomeSegments = 8, skydomeRows = 10; const size_t skydomeSegments = 8, skydomeRows = 10;
struct WaterVertex {
float planedata[] = { static const AttributeList vertex_attributes() {
// Vertices return {
1.0f, 1.0f, 0.f, {ATRS_Position, 2, sizeof(WaterVertex), 0ul}
-0.0f, 1.0f, 0.f,
1.0f,-0.0f, 0.f,
-0.0f,-0.0f, 0.f,
// UV coords
1.f, 1.f,
0.f, 1.f,
1.f, 0.f,
0.f, 0.f,
// Normals
0.f, 0.f, 1.f,
0.f, 0.f, 1.f,
0.f, 0.f, 1.f,
0.f, 0.f, 1.f,
// Colours
1.f, 1.f, 1.f,
1.f, 1.f, 1.f,
1.f, 1.f, 1.f,
1.f, 1.f, 1.f
}; };
}
float x, y;
};
std::vector<WaterVertex> planeVerts = {
{1.0f, 1.0f},
{0.0f, 1.0f},
{1.0f,-0.0f},
{0.0f,-0.0f}
};
GeometryBuffer waterBuffer;
DrawBuffer waterDraw;
const char *waterVSSource = R"(
#version 130
#extension GL_ARB_explicit_attrib_location : enable
layout(location = 0) in vec2 position;
out vec2 TexCoords;
uniform float height;
uniform float size;
uniform mat4 MVP;
void main()
{
TexCoords = position;
gl_Position = MVP * vec4(position * size, height, 1.0);
})";
const char *waterFSSource = R"(
#version 130
in vec3 Normal;
in vec2 TexCoords;
uniform sampler2D texture;
void main() {
vec4 c = texture2D(texture, TexCoords);
gl_FragColor = c;
})";
GLuint compileShader(GLenum type, const char *source) GLuint compileShader(GLenum type, const char *source)
{ {
@ -118,7 +139,7 @@ GLuint compileShader(GLenum type, const char *source)
GLchar *buffer = new GLchar[len]; GLchar *buffer = new GLchar[len];
glGetShaderInfoLog(shader, len, NULL, buffer); glGetShaderInfoLog(shader, len, NULL, buffer);
std::cerr << "ERROR compiling shader: " << buffer << std::endl; std::cerr << "ERROR compiling shader: " << buffer << "\nSource: " << source << std::endl;
delete[] buffer; delete[] buffer;
exit(1); exit(1);
} }
@ -163,10 +184,21 @@ GameRenderer::GameRenderer(GameWorld* engine)
glGenVertexArrays( 1, &vao ); glGenVertexArrays( 1, &vao );
// prepare our special internal plane. // Upload water plane
glGenBuffers(1, &planeVBO); waterBuffer.uploadVertices(planeVerts);
glBindBuffer(GL_ARRAY_BUFFER, planeVBO); waterDraw.addGeometry(&waterBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(planedata), planedata, GL_STATIC_DRAW); waterDraw.setFaceType(GL_TRIANGLE_STRIP);
GLuint waterVS = compileShader(GL_VERTEX_SHADER, waterVSSource);
GLuint waterFS = compileShader(GL_FRAGMENT_SHADER, waterFSSource);
waterProgram = glCreateProgram();
glAttachShader(waterProgram, waterVS);
glAttachShader(waterProgram, waterFS);
glLinkProgram(waterProgram);
waterHeight = glGetUniformLocation(waterProgram, "height");
waterTexture = glGetUniformLocation(waterProgram, "texture");
waterSize = glGetUniformLocation(waterProgram, "size");
waterMVP = glGetUniformLocation(waterProgram, "MVP");
// And our skydome while we're at it. // And our skydome while we're at it.
glGenBuffers(1, &skydomeVBO); glGenBuffers(1, &skydomeVBO);
@ -212,6 +244,11 @@ float mix(uint8_t a, uint8_t b, float num)
return a+(b-a)*num; return a+(b-a)*num;
} }
#define GL_PLS() \
{ auto errc = glGetError(); \
if(errc != GL_NO_ERROR) std::cout << __LINE__ << ": " << errc << std::endl;\
}
void GameRenderer::renderWorld(float alpha) void GameRenderer::renderWorld(float alpha)
{ {
_renderAlpha = alpha; _renderAlpha = alpha;
@ -370,6 +407,37 @@ void GameRenderer::renderWorld(float alpha)
} }
transparentDrawQueue.clear(); transparentDrawQueue.clear();
// Draw the water.
glBindVertexArray( waterDraw.getVAOName() );
glUseProgram( waterProgram );
// TODO: label all the variables!
#define NO_WATER_INDEX 48
glm::mat4 waterModel;
glUniform1i(waterTexture, 0);
float blockSize = 4096.f/64.f;
auto waterTex = engine->gameData.textures["water_old"];
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, waterTex.texName);
// TODO: high / low quality water. Projected grid?
for( int x = 0; x < 64; x++ ) {
for( int y = 0; y < 64; y++ ) {
int i = (x*64) + y;
int hI = engine->gameData.visibleWater[i];
if( hI >= NO_WATER_INDEX ) continue;
float h = engine->gameData.waterHeights[hI];
waterModel = glm::mat4();
waterModel = glm::translate(waterModel, {-2048.f + x * blockSize, -2048.f + y * blockSize, 0.f});
glUniform1f(waterSize, blockSize);
glUniform1f(waterHeight, h);
auto MVP = proj * view * waterModel;
glUniformMatrix4fv(waterMVP, 1, GL_FALSE, glm::value_ptr(MVP));
glDrawArrays(waterDraw.getFaceType(), 0, 4);
}
}
glBindVertexArray( vao ); glBindVertexArray( vao );
glUseProgram(skyProgram); glUseProgram(skyProgram);