Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8988403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:00:25+00:00 2026-06-15T22:00:25+00:00

I want to find normals for height map data. I am using gl_triangles in

  • 0

I want to find normals for height map data. I am using gl_triangles in my code for indices. How would I find normals for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:00:26+00:00Added an answer on June 15, 2026 at 10:00 pm

    Given a triangle (vert1, vert2, vert3) its normal is ((vert2 - vert1).cross(vert3 - vert1)).normalize().

    For smooth, per-vertex normals: Foreach vertex, sum together the face normals for each triangle that vertex is a part of, then normalize the sum.

    EDIT: Example:

    #include <GL/glut.h>
    #include <vector>
    #include <cmath>
    #include <Eigen/Core>
    #include <Eigen/Geometry>
    
    using namespace std;
    using namespace Eigen;
    
    typedef Matrix< Vector3f, Dynamic, Dynamic > VecMat;
    
    // given a matrix of heights returns a matrix of vertices
    VecMat GetVerts( const MatrixXf& hm )
    {
        VecMat verts( hm.rows(), hm.cols() );
        for( int col = 0; col < hm.cols(); ++col )
            for( int row = 0; row < hm.rows(); ++row )
                verts( row, col ) = Vector3f( col, row, hm( row, col ) );
        return verts;
    }
    
    VecMat GetNormals( const VecMat& hm )
    {
        VecMat normals( hm );
        for( int col = 0; col < hm.cols(); ++col )
            for( int row = 0; row < hm.rows(); ++row )
            {
                Vector3f sum( Vector3f::Zero() );
                const Vector3f& cur = hm( row, col );
                if( row+1 < hm.rows() && col+1 < hm.cols() )
                    sum += ( hm( row+0, col+1 ) - cur ).cross( hm( row+1, col+0 ) - cur ).normalized();
                if( row+1 < hm.rows() && col > 0 )
                    sum += ( hm( row+1, col+0 ) - cur ).cross( hm( row+0, col-1 ) - cur ).normalized();
                if( row > 0 && col > 0 )
                    sum += ( hm( row+0, col-1 ) - cur ).cross( hm( row-1, col+0 ) - cur ).normalized();
                if( row > 0 && col+1 < hm.cols() )
                    sum += ( hm( row-1, col+0 ) - cur ).cross( hm( row+0, col+1 ) - cur ).normalized();
                normals( row, col ) = sum.normalized();
            }
        return normals;
    }
    
    // returns an index array for a GL_TRIANGLES heightmap
    vector< unsigned int > GetIndices( int rows, int cols )
    {
        vector< unsigned int > indices;
        for( int col = 1; col < cols; ++col )
            for( int row = 1; row < rows; ++row )
            {
                // Eigen default storage order is column-major
                // lower triangle
                indices.push_back( (col-1) * rows + (row-1) );
                indices.push_back( (col-0) * rows + (row-1) );
                indices.push_back( (col-1) * rows + (row-0) );
                // upper triangle
                indices.push_back( (col-1) * rows + (row-0) );
                indices.push_back( (col-0) * rows + (row-1) );
                indices.push_back( (col-0) * rows + (row-0) );
            }
        return indices;
    }
    
    VecMat heightmap;
    VecMat normals;
    vector< unsigned int > indices;
    void init()
    {
        // wavy heightmap
        MatrixXf hm( 64, 64 );
        for( int col = 1; col < hm.cols(); ++col )
            for( int row = 1; row < hm.rows(); ++row )
            {
                float x = ( col - ( hm.cols() / 2.0f ) ) / 2.0f;
                float y = ( row - ( hm.rows() / 2.0f ) ) / 2.0f;
                hm( row, col ) = cos( sqrt( x * x + y * y ) );
            }
    
        heightmap = GetVerts( hm );
        heightmap.array() -= Vector3f( hm.cols() / 2.0f, hm.rows() / 2.0f, 0 );
        for( int col = 0; col < hm.cols(); ++col )
            for( int row = 0; row < hm.rows(); ++row )
                heightmap( row, col ).array() *= Vector3f( 1 / 4.0f, 1 / 4.0f, 1.0f ).array();
    
        normals = GetNormals( heightmap );
        indices = GetIndices( heightmap.rows(), heightmap.cols() );
    }
    
    void display()
    {
        glEnable( GL_DEPTH_TEST );
        glEnable( GL_CULL_FACE );
        glShadeModel( GL_SMOOTH );
    
        glEnable( GL_LIGHTING );
        GLfloat global_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
        glLightModelfv( GL_LIGHT_MODEL_AMBIENT, global_ambient );
        glEnable( GL_COLOR_MATERIAL );
        glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
    
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        double w = glutGet( GLUT_WINDOW_WIDTH );
        double h = glutGet( GLUT_WINDOW_HEIGHT );
        gluPerspective( 60, w / h, 1, 100 );
    
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
        gluLookAt( 8, 8, 8, 0, 0, 0, 0, 0, 1 );
    
        // spinning light
        glEnable( GL_LIGHT0 );
        float angle = 20 * ( glutGet( GLUT_ELAPSED_TIME ) / 1000.0f ) * (3.14159f / 180.0f);
        float x = cos( -angle ) * 6;
        float y = sin( -angle ) * 6;
        GLfloat light_position[] = { x, y, 2, 1.0 };
        glLightfv( GL_LIGHT0, GL_POSITION, light_position );    
        glDisable( GL_LIGHTING );
        glPointSize( 5 );
        glBegin(GL_POINTS);
        glColor3ub( 255, 255, 255 );
        glVertex3fv( light_position );
        glEnd();
        glEnable( GL_LIGHTING );
    
        glColor3ub(255,0,0);
        glEnableClientState( GL_VERTEX_ARRAY );
        glEnableClientState( GL_NORMAL_ARRAY );
        glVertexPointer( 3, GL_FLOAT, sizeof( Vector3f ), heightmap(0,0).data() );
        glNormalPointer( GL_FLOAT, sizeof( Vector3f ), normals(0,0).data() );
        glDrawElements( GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, &indices[0] );
        glDisableClientState( GL_VERTEX_ARRAY );
        glDisableClientState( GL_NORMAL_ARRAY );
    
        glutSwapBuffers();
    }
    
    void timer( int extra )
    {
        glutPostRedisplay();
        glutTimerFunc( 16, timer, 0 );
    }
    
    int main( int argc, char **argv )
    {
        glutInit( &argc, argv );
        glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
        glutInitWindowSize( 640, 480 );
        glutCreateWindow( "Heightmap" );
        init();
        glutDisplayFunc( display );
        glutTimerFunc( 0, timer, 0 );
        glutMainLoop();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to find all the demo words using PHP and regEx. $input ='demo';
Edit: Here is the new code: $(#normalcontent).hide(fast).load($this.attr(href) + #normalcontent,,function(){ $(this).slideDown(normal); $(this).find(img).each(function(){ if($(this).hasClass(large)) { var
Following this thread: How to handle functions deprecation in library? I want to find
I'm trying to find a way to make this smaller by using functions/variables in
I just want to find the location name using cell tower in j2me. Also
I want find all Saturdays and Sundays in A given month. How can I
I want to find a struct in a vector, but I'm having some troubles.
I want to find the sum and count of specific values in my table.
I want to find out the position of an element which is in iframe.
I want to find the Xelement attribute.value which children have a concrete attribute.value. string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.