Quantcast
Channel: Jobs
Viewing all articles
Browse latest Browse all 18427

Best way to create multi-textured terrain

$
0
0
Hi, I have a heightmap that I’ve generated and have implemented to render a terrain. So far, I can only apply one texture to it. My question is, what is the best way to assign varying textures and render them? I will want to blend textures as well but that can come later. I just need to learn how to render more than one. Searching online certainly has a lot of examples but many are either outdated or conflicting. Most render in immediate mode without shaders. GLSL is something I’m trying to comprehend as well. The following code has been adapted from the Qt OpenGL tutorial. [qt-project.org] In glwidget.cpp::initializeGL()         glEnable(GL_DEPTH_TEST);     glEnable(GL_CULL_FACE);       const float bgColor[4] = {.7, .8, 1.0, 1.0};     glClearColor(bgColor[0], bgColor[1], bgColor[2], bgColor[3]);       camera = world->initCamera();       shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");     shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");       shaderProgram.link();         texture0 = bindTexture(QPixmap(":/textures/grassb512.bmp")); And the rendering occurs in glwidget.cpp::paintGL()     vMatrix.rotate(180.0f, 0.0f, 0.0f, 1.0f);     vMatrix.rotate(yRot, 0.0f, 1.0f, 0.0f);     vMatrix.translate(camera);       shaderProgram.bind();       shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);     shaderProgram.setUniformValue("texture", 0);       glActiveTexture(GL_TEXTURE0);     glBindTexture(GL_TEXTURE_2D, texture0);     glActiveTexture(0);       shaderProgram.setAttributeArray("vertex", world->vertices.constData());     shaderProgram.enableAttributeArray("vertex");       shaderProgram.setAttributeArray("textureCoordinate", world->textures.constData());     shaderProgram.enableAttributeArray("textureCoordinate");       QString log = shaderProgram.log();     if (!log.isEmpty()) {         qDebug("%s", qPrintable(log));     }       glDrawArrays(GL_TRIANGLES, 0, world->vertices.size());       shaderProgram.disableAttributeArray("vertex");     shaderProgram.disableAttributeArray("textureCoordinate");     shaderProgram.release(); As you may be able to tell, I have a class “world” which exposes an array of vertices as well as an array of textures to the rendering method (I also have an array of normals etc.) It would be nice to bind the textures in the world class but I don’t see how they’d be exposed to the rendering method. The only solution that comes to mind is to have the world class generate vertex and texture arrays per texture and then process them in the paintGL method but, what happens when I start adding objects: trees structures people? What happens when I need to blend two textures across mutliple polygons? Is there logic I can perform in the shaders? A link to a perinent and up-to-date reference would be great or just some lucid step in the right direction. Thanks! I am developing in: Qt Creator 2.5.0 Based on Qt 4.8.2 (64 bit) Built on Aug 7 2012 at 11:47:27 running on Linux Debian Wheezy/experimental. The OpenGL specs are mesa which is a little bit behind the current openGL standards.

Viewing all articles
Browse latest Browse all 18427

Latest Images

Trending Articles



Latest Images