Progress on rendering heightfield buffers.

This commit is contained in:
Andrzej Kapolka 2014-07-29 16:06:35 -07:00
parent 2a7847c5b3
commit 6824479762
4 changed files with 50 additions and 8 deletions

View file

@ -11,6 +11,15 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// the diffuse texture
uniform sampler2D diffuseMap;
// the interpolated normal
varying vec4 normal;
void main(void) {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
// compute the base color based on OpenGL lighting model
vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient +
gl_FrontLightProduct[0].diffuse * max(0.0, dot(normalize(normal), gl_LightSource[0].position)));
gl_FragColor = base * texture2D(diffuseMap, gl_TexCoord[0].st);
}

View file

@ -11,6 +11,23 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// the height texture
uniform sampler2D heightMap;
// the interpolated normal
varying vec4 normal;
void main(void) {
gl_Position = ftransform();
// transform and store the normal for interpolation
normal = normalize(gl_ModelViewMatrix * vec4(0.0, 1.0, 0.0, 0.0));
// pass along the vertex color
gl_FrontColor = gl_Color;
// pass along the texture coordinates
gl_TexCoord[0] = gl_MultiTexCoord0;
// add the height to the position
gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex +
vec4(0.0, texture2D(heightMap, gl_MultiTexCoord0.st).r, 0.0, 0.0));
}

View file

@ -275,6 +275,9 @@ void HeightfieldBuffer::render() {
if (!_heightTexture.isCreated()) {
int heightSize = glm::sqrt(_height.size());
_heightTexture.setSize(heightSize, heightSize);
_heightTexture.setAutoMipMapGenerationEnabled(false);
_heightTexture.setMinificationFilter(QOpenGLTexture::Linear);
_heightTexture.setWrapMode(QOpenGLTexture::ClampToEdge);
_heightTexture.setFormat(QOpenGLTexture::LuminanceFormat);
_heightTexture.allocateStorage();
_heightTexture.setData(QOpenGLTexture::Luminance, QOpenGLTexture::UInt8, _height.data());
@ -284,15 +287,18 @@ void HeightfieldBuffer::render() {
int colorSize = glm::sqrt(_color.size() / 3);
_colorTexture.setSize(colorSize, colorSize);
}
_colorTexture.setAutoMipMapGenerationEnabled(false);
_colorTexture.setMinificationFilter(QOpenGLTexture::Linear);
_colorTexture.setWrapMode(QOpenGLTexture::ClampToEdge);
_colorTexture.setFormat(QOpenGLTexture::RGBFormat);
_colorTexture.allocateStorage();
if (!_color.isEmpty()) {
_colorTexture.setData(QOpenGLTexture::BGR, QOpenGLTexture::UInt8, _color.data());
_colorTexture.setData(QOpenGLTexture::RGB, QOpenGLTexture::UInt8, _color.data());
_color.clear();
} else {
const quint8 WHITE_COLOR[] = { 255, 255, 255 };
_colorTexture.setData(QOpenGLTexture::BGR, QOpenGLTexture::UInt8, const_cast<quint8*>(WHITE_COLOR));
_colorTexture.setData(QOpenGLTexture::RGB, QOpenGLTexture::UInt8, const_cast<quint8*>(WHITE_COLOR));
}
}
// create the buffer objects lazily
@ -310,7 +316,7 @@ void HeightfieldBuffer::render() {
float z = -step;
for (int i = 0; i < sizeWithSkirt; i++, z += step) {
float x = -step;
const float SKIRT_LENGTH = 1.0f;
const float SKIRT_LENGTH = 0.25f;
float baseY = (i == 0 || i == sizeWithSkirt - 1) ? -SKIRT_LENGTH : 0.0f;
for (int j = 0; j < sizeWithSkirt; j++, point++, x += step) {
point->vertex = glm::vec3(x, (j == 0 || j == sizeWithSkirt - 1) ? -SKIRT_LENGTH : baseY, z);
@ -331,8 +337,8 @@ void HeightfieldBuffer::render() {
for (int j = 0; j < rows; j++) {
*index++ = lineIndex + j;
*index++ = lineIndex + j + 1;
*index++ = nextLineIndex + j;
*index++ = nextLineIndex + j + 1;
*index++ = nextLineIndex + j;
}
}
@ -371,6 +377,8 @@ void HeightfieldBuffer::render() {
QHash<int, HeightfieldBuffer::BufferPair> HeightfieldBuffer::_bufferPairs;
void HeightfieldPreview::render(const glm::vec3& translation, float scale) const {
glColor4f(1.0f, 1.0f, 1.0f, 0.75f);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
@ -422,6 +430,11 @@ void DefaultMetavoxelRendererImplementation::init() {
_heightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
"shaders/metavoxel_heightfield.frag");
_heightfieldProgram.link();
_heightfieldProgram.bind();
_heightfieldProgram.setUniformValue("heightMap", 0);
_heightfieldProgram.setUniformValue("diffuseMap", 1);
_heightfieldProgram.release();
}
}
@ -662,6 +675,8 @@ void DefaultMetavoxelRendererImplementation::render(MetavoxelData& data, Metavox
_pointProgram.release();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
_heightfieldProgram.bind();
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

View file

@ -945,14 +945,15 @@ void ImportHeightfieldTool::selectColorFile() {
}
const int BLOCK_SIZE = 64;
const int BLOCK_ADVANCEMENT = BLOCK_SIZE - 1;
void ImportHeightfieldTool::updatePreview() {
QVector<BufferDataPointer> buffers;
if (_heightImage.width() > 0 && _heightImage.height() > 0) {
float z = 0.0f;
for (int i = 0; i < _heightImage.height(); i += BLOCK_SIZE, z++) {
for (int i = 0; i < _heightImage.height(); i += BLOCK_ADVANCEMENT, z++) {
float x = 0.0f;
for (int j = 0; j < _heightImage.width(); j += BLOCK_SIZE, x++) {
for (int j = 0; j < _heightImage.width(); j += BLOCK_ADVANCEMENT, x++) {
QByteArray height(BLOCK_SIZE * BLOCK_SIZE, 0);
int rows = qMin(BLOCK_SIZE, _heightImage.height() - i);
int columns = qMin(BLOCK_SIZE, _heightImage.width() - j);