Results 1 to 9 of 9

Thread: Mapping texture onto a rendered terrain

  1. #1
    Senior Member Regular Contributor
    Join Date
    Mar 2010
    Posts
    213

    Mapping texture onto a rendered terrain

    Hello,

    im new to textures. im having some trouble mapping textures onto a rendered terrain. im trying to map it onto a rendered terrain the code for which i found online.

    Here is a pic.

    PIC

    The left one is the output. The right one is my texture. i just wanted to map it onto it. The texture is a 256x256 bmp image.

    im using this for mapping texture:

    Code :
    for(int z = 0; z < _terrain->length() - 1; z++) {
    	glBegin(GL_QUAD_STRIP);
    	for(int x = 0; x < _terrain->width(); x++) {
                /* set normal and set vertex */
                glTexCoord2d((double)x / _terrain->width(), (double)z / _terrain->length());
     
                 // Two
                 /* set normal and set vertex */
                 glTexCoord2d((double)x / _terrain->width(), (double)z + 1 / _terrain->length());
            }
    }


    But im not able to map the texture. im using a 50x50 height map for the terrain. Can anyone please correct me?

    Thanks.


  2. #2
    Senior Member Frequent Contributor
    Join Date
    Oct 2009
    Posts
    592

    Re: Mapping texture onto a rendered terrain

    Many glBegin()s but no glEnd()s?

  3. #3
    Member Newbie
    Join Date
    Jan 2011
    Location
    Lincoln, UK
    Posts
    36

    Re: Mapping texture onto a rendered terrain

    Quote Originally Posted by ugluk
    Many glBegin()s but no glEnd()s?
    i agree. You are calling glBegin recursively, but never ending the process. remember, openGL is a state machine, once something like this is initiated, it must be terminated.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Mar 2010
    Posts
    213

    Re: Mapping texture onto a rendered terrain

    Thanks for the replies. Sorry missed out on the glEnd(). Here is the code. Not sure why the texture is not mapped.

    Code :
    for(int z = 0; z < _terrain->length() - 1; z++) {
    	glBegin(GL_QUAD_STRIP);
    	for(int x = 0; x < _terrain->width(); x++) {
                /* set normal and set vertex */
                glTexCoord2d((double)x / _terrain->width(), (double)z / _terrain->length());
     
                 // Two
                 /* set normal and set vertex */
                 glTexCoord2d((double)x / _terrain->width(), (double)z + 1 / _terrain->length());
            }
            glEnd();
    }
     
    //a glutSwapBuffers() here.

  5. #5
    Senior Member Regular Contributor
    Join Date
    Mar 2010
    Posts
    213

    Re: Mapping texture onto a rendered terrain

    Well i managed to map the texture with this:

    Code :
    for(int z = 0; z < _terrain->length() - 1; z++, count1++) {
    	glBegin(GL_QUAD_STRIP);
    	for(int x = 0; x < _terrain->width(); x++, count2++) {
    		// Set Normal and vertex
    		..
    		glTexCoord2d(x / 50.0, z / 50.0);
     
    		// Set normal and vertex
                    ..  
     
    		glTexCoord2d(x/ 50.0, (z + 1) / 50.0);
    	}
    	glEnd();
    }
    glutSwapBuffers();

    Well this is the output im getting:

    IMAGE

    The image on the right is the texture im using.

    im getting "squares" in the terrain. What could be wrong?
    The terrain image is 510x510

    The height map is 50x50(hence the division by 50 in the loop viz the length and width of height map).

    Thanks

  6. #6
    Senior Member Frequent Contributor
    Join Date
    Oct 2009
    Posts
    592

    Re: Mapping texture onto a rendered terrain

    z != _terrain->length - 1, so it is max _terrain->length-2, if you add 1 to this you get _terrain->length-1 and divide by 50, you don't get exactly 1. Maybe try to divide by 49.

  7. #7
    Senior Member Regular Contributor
    Join Date
    Mar 2010
    Posts
    213

    Re: Mapping texture onto a rendered terrain

    Thanks for the reply.

    Well i made it 49. There is little improvement.

    Here is the pic:
    IMAGE
    Can i further improve it?

  8. #8
    Senior Member Regular Contributor
    Join Date
    Mar 2010
    Posts
    213

    Re: Mapping texture onto a rendered terrain

    Well i tried with a larger image: 1024x1024 (different texture)

    IMAGE

    The terrain looks better. But i still don't get why the previous one was not working so well. It was a 510x510

  9. #9
    Senior Member Frequent Contributor
    Join Date
    Oct 2009
    Posts
    592

    Re: Mapping texture onto a rendered terrain

    You weren't calcing the texture coords correctly, obviously.

Similar Threads

  1. Texture being rendered wrong
    By Shedex in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-21-2016, 03:15 PM
  2. Terrain Texture Mapping?
    By GhostMonkey in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 04-14-2012, 09:19 AM
  3. texture mapping terrain with high resolution image
    By Diaren in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 11-19-2010, 03:04 AM
  4. terrain mapping this time, not cube, lol
    By nick2price in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-20-2008, 09:05 AM
  5. Efficient Terrain Mapping
    By Psiborg in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 05-09-2006, 08:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean