We get it. We use ad blockers too. But GameDev.net displays them so we can continue to be a great platform for you.
Please whitelist GameDev.net and our advertisers.
Also consider a GDNet+ Pro subscription to remove all ads from GameDev.net.
OpenGL API Specifications
Documents and Learning ResourcesSupport Libraries and FrameworksFunction Libraries and GeneratorsThis list contains some basic resources for getting started with, and developing for, OpenGL 3.0 and onwards. Contact your forum moderator if you want to contribute to the list or have any comments.Subscribe to GameDev.net's newsletters to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 28 November 2012 - 10:13 AM
Posted 28 November 2012 - 01:27 PM
Posted 29 November 2012 - 06:08 AM
If you calculate normals in the CPU, instead of the shader, the common way is to have one normal for every vertex, even if the three normals are the same for all vertices in a triangle. This extra cost is smaller if you use indexed drawing.
Posted 30 November 2012 - 08:23 AM
// // Calculate normal of the vertex // float step = 1/1024; //where 1024 is texture width, i.e. heightmap width //calculate the neigbour positions vec4 prev_x = vec4(texCoord.x, 0.0, texCoord.y, 1.0); vec4 prev_z = vec4(texCoord.x, 0.0, texCoord.y, 1.0); vec4 next_x = vec4(texCoord.x, 0.0, texCoord.y, 1.0); vec4 next_z = vec4(texCoord.x, 0.0, texCoord.y, 1.0); prev_x.x -= step; prev_z.z -= step; next_x.x += step; next_z.z += step; //calculate neighbour heights / positions, with scaling vec4 q; q = texture2D(hmap_texture, prev_x.xz); prev_x.y = ( (q.x + q.y + q.z) / 3.0 ) * y_scale; q = texture2D(hmap_texture, next_x.xz); next_x.y = ( (q.x + q.y + q.z) / 3.0 ) * y_scale; q = texture2D(hmap_texture, prev_z.xz); prev_z.y = ( (q.x + q.y + q.z) / 3.0 ) * y_scale; q = texture2D(hmap_texture, next_z.xz); next_z.y = ( (q.x + q.y + q.z) / 3.0 ) * y_scale; //apply xz scaling prev_x.xz *= xz_scale; next_x.xz *= xz_scale; prev_z.xz *= xz_scale; next_z.xz *= xz_scale; // // we now have four neighbouring vertices, positions calculated. Now we need a normal. vec3 tangent = next_x - prev_x; vec3 bitangent = next_z - prev_z; vec3 normal = normalize( cross(tangent, bitangent) );
// The slope for this pixel is simply 1 - the input normal Y ( in Direct X this is as simple as float slope = 1 - input.normal.y ) float gradient = 1 - vertexNormal.x; if(gradient <= 0.2) { height_colour = sand_colour; } if(gradient < 0.7 && gradient > 0.2) { height_colour = rock_colour; } if(gradient > 0.7) { height_colour = grass_colour; }
Posted 30 November 2012 - 10:17 AM
It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.
Posted 30 November 2012 - 10:25 AM
Posted Today, 04:17 PM
You might consider doing this in a geometry shader instead. This is available in 330 and would enable you to calculate normals on the fly from your final positions that you're actually going to draw with, so it's also amenable to any hypothetical future LoD schemes you may consider implementing.
Posted 01 December 2012 - 09:37 AM
#version 330 // // This demo uses a texture to provide a basic heightmap, with scaling features. // // It also uses bump mapping techniques (?) to calculate the interpolated normal of a vertex // - given its neighbouring vertices' positions in xyz space, to calculate a tangent and bitangent, // from which a normalized cross can be deduced. // uniform mat4 worldMatrix; uniform mat4 invTransposeWorldMatrix; uniform mat4 viewProjMatrix; uniform CameraMatricesBlock { mat4 worldMx; mat4 viewMx; mat4 projectionMx; } cam; uniform sampler2D hmap_texture; uniform float y_scale; uniform float xz_scale; // // input vertex packet // layout (location=0) in vec4 vertexPos; layout (location=1) in vec2 vertexTexCoord; // // output vertex packet // out vec2 texCoord; out vec3 vertexNormal; //------------------------------------------------------------------- void ScaleVertex(inout vec4 vertex, in float _xz_scale, in float _y_scale){ vertex.x *= _xz_scale; vertex.z *= _xz_scale; vertex.y *= _y_scale; } //------------------------------------------------------------------- void OffsetVertexXPosition(inout vec4 vertex, in float offsetAmount){ vertex.x += offsetAmount; } //------------------------------------------------------------------- void OffsetVertexZPosition(inout vec4 vertex, in float offsetAmount){ vertex.z += offsetAmount; } //------------------------------------------------------------------- void SetYFromHeightMap(inout vec4 vertex, in vec2 textureCoordinate){ vec4 pixel_colour = texture2D(hmap_texture, textureCoordinate); float yOffset = ( ( pixel_colour.x + pixel_colour.y + pixel_colour.z ) / 3.0 ); vertex.y = yOffset; } // // main // void main(void) { // Yoink texCoord = vertexTexCoord; // // Calculate vertex coordinates for this vertex and its neighbouring vertices // // // Now we set up the X and Z components of our vertex, + the neighbour vertices // At first, initialise them all to the postition of this vertex, so we can offset them easily. float step = 1 / 1024; //where 1024 is texture width, i.e. heightmap width vec4 v_Pos = vertexPos; vec4 prev_vertex_x = vertexPos; vec4 prev_vertex_z = vertexPos; vec4 next_vertex_x = vertexPos; vec4 next_vertex_z = vertexPos; //next we offset our neighbours in their relative directions OffsetVertexXPosition(prev_vertex_x, -(step) ); OffsetVertexXPosition(next_vertex_x, step ); OffsetVertexZPosition(prev_vertex_z, -(step) ); OffsetVertexZPosition(next_vertex_z, step ); // // now we know the xz components of all our vertices, we can sample // the heightmap texture at their texture coordinate to ascertain their Y component value. // // // Our actual out vertex's y component // SetYFromHeightMap(v_Pos, texCoord); // Neighbours on X vec2 _tc = texCoord; _tc.x = prev_vertex_x.x; _tc.y = prev_vertex_x.z; SetYFromHeightMap( prev_vertex_x, _tc ); _tc.x = next_vertex_x.x; _tc.y = next_vertex_x.z; SetYFromHeightMap( next_vertex_x, _tc ); // Neighbours on Z _tc.x = prev_vertex_z.x; _tc.y = prev_vertex_z.z; SetYFromHeightMap( prev_vertex_z, _tc ); _tc.x = next_vertex_z.x; _tc.y = next_vertex_z.z; SetYFromHeightMap( next_vertex_z, _tc ); // // Apply xz and y scaling to all vertices // // Our actual vertex ScaleVertex(v_Pos, xz_scale, y_scale); // Our neighbours on X ScaleVertex(prev_vertex_x, xz_scale, y_scale); ScaleVertex(next_vertex_x, xz_scale, y_scale); // Our neighbours on Z ScaleVertex(prev_vertex_z, xz_scale, y_scale); ScaleVertex(next_vertex_z, xz_scale, y_scale); // // We now have four neighbouring vertices, positions calculated, and scaled. // Now we need a normal, from the bitangent and tangent of these neighbour positions. // vec3 tangent; tangent = next_vertex_x - prev_vertex_x; //tangent.x = next_vertex_x.x - prev_vertex_x.x; //tangent.y = next_vertex_x.y - prev_vertex_x.y; //tangent.z = next_vertex_x.z - prev_vertex_x.z; vec3 bitangent; bitangent = next_vertex_z - prev_vertex_z; //bitangent.x = next_vertex_x.x - prev_vertex_x.x; //bitangent.y = next_vertex_x.y - prev_vertex_x.y; //bitangent.z = next_vertex_x.z - prev_vertex_x.z; vec3 normal; normal = normalize( cross(tangent, bitangent) ); //normal.x = ( tangent.y * bitangent.z ) - ( tangent.z * bitangent.y ); //normal.y = ( tangent.z * bitangent.x ) - ( tangent.x * bitangent.z ); //normal.z = ( tangent.x * bitangent.y ) - ( tangent.y * bitangent.x ); //normal = normalize( normal ); // // // Output // // texCoord - the texture coordinate, i.e, the vertex position with no z component. // vertexNormal - the vertex's normal // glPosition - the vertex position vertexNormal = invTransposeWorldMatrix * vec4( normal, 1.0 ); gl_Position = worldMatrix * viewProjMatrix * v_Pos; // set the position for the fragment shader }
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.