Jump to content
  • Advertisement
Sign in to follow this  

Terrain Problem

This topic is 4746 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is my first attempt at creating a terrain from a height map. Just having a little issue i think possibly with the lighting, the terrains appears to be entirely black in some sections. Here is there terrain building code, and below it is the creation of the light.

 public void buildTerrain(String fileName, int width, int height, int triSize, String glListName, GL11 gl){
        
        loadMap(width, height, fileName);
        
        name = gl.glGenLists(1);
        
        gl.glBindTexture(gl.GL_TEXTURE_2D, texture); // Select Our Texture
        
        gl.glNewList(name,gl.GL_COMPILE);   
        
            gl.glBegin(gl.GL_TRIANGLE_STRIP);
            

            
                for(int y = 0; y < height - 1; y++){
                    
                    for(int x = 0; x < width; x++){
                        
                        float xV = x * 2;
                        float yV = (float)(heightMap[y][x] * .1);
                        float zV = y * 1;
                        
                        float xV1 = x * 2;
                        float yV1 = (float)(heightMap[y + 1][x] * .1);
                        float zV1 = (y + 1) * 1;
    
                        float vx = yV * zV1 - zV * yV1;
        
                        float vy = zV * xV1 - xV * zV1;
        
                        float vz = xV * yV1 - yV * xV1;
                        
                        float i = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
                        
                        if(yV < 0 || yV1 < 0){
                            System.out.println("Bellow 0 Y at: " + x + ", " + y);
                        }
                        
                        vx = vx / i;
                        vy = vy / i;
                        vz = vz / i;
                        
                        gl.glNormal3f(vx, vy, vz);
            

                        gl.glTexCoord2f(0.0f, 1.0f);
                        gl.glVertex3f(xV, yV, zV);
                        
                        gl.glTexCoord2f(0.0f, 0.0f);
                        gl.glVertex3f(xV1, yV1, zV1);
                        
                    }
                    
                }
                        
                        
                
                
            gl.glEnd();
            
        gl.glEndList(); 
        
        
        
        
    }
    
    public void loadMap(int width, int height, String fileName){
        
        heightMap = new int[height][width];
        
        BufferedImage map = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = map.createGraphics();
        
        Image mapImg = new ImageIcon(fileName).getImage();
        
        g.drawImage(mapImg, 0, 0, null);
        
        for(int y = 0; y < height; y++){
            for(int x = 0; x < width; x++){
                
                Color color = new Color(map.getRGB(x,y));
        
                float greyScale = (float)(0.30 * color.getRed() + 0.59 * color.getGreen() + 0.11 * color.getBlue());
                
                heightMap[y][x] = (int)greyScale;
                
            }
        }
    }

And The Lighting in the engine class is set up with:

....

FloatBuffer lightAmbient1 = BufferUtils.createFloatBuffer(4).put(new float[] { 0.5f, 0.5f, 0.5f, 0.5f }); //RGBA
	FloatBuffer lightDiffuse1 = BufferUtils.createFloatBuffer(4).put(new float[] { 1.0f, 1.0f, 1.0f, 1.0f }); //RGBA
	FloatBuffer lightPosition1 = BufferUtils.createFloatBuffer(4).put(new float[] { 30.0f, 10.0f, -30.0f, 0.0f }); //XYZ Unknown

.....
            gl.glEnable(gl.GL_LIGHTING);

			//Set Up Lighting

			gl.glLight(gl.GL_LIGHT1, gl.GL_AMBIENT, lightAmbient1);
			gl.glLight(gl.GL_LIGHT1, gl.GL_DIFFUSE, lightDiffuse1);
			gl.glLight(gl.GL_LIGHT1, gl.GL_POSITION, lightPosition1);

			gl.glEnable(gl.GL_LIGHT1);


To me it appears when i dont set a diffuse light the terrain is rendered correctly (no holes) but with the diffuse there appear to be black holes in the terrain. Any help would be greatly appeciated, if the code ive posted isnt really well explain, i would be more than happy to show u a screen shot if someone could explain to me how i can post an image on these forums from my computer. Cheers, Nick

Share this post


Link to post
Share on other sites
Advertisement
sounds like the "black holes" are actually triangles "facing" the wrong way, and they are just showing up black because N.L is 180degrees

this is caused by your triangle winding, the ones that get lit properly (when diffuse lighting is on) have the normal facing up, but the ones which apear black have their normals facing down.

suppose your black triangle is comprised of verticies a,b,c and you calculate the normal for this triangle using TriangleNormal(a, b, c) to fix it (if this is the problem) change the winding order till it "looks right" eg try TriangleNormal(c, b, a)

hope that's gonna help.

Cheers
-Danu

Share this post


Link to post
Share on other sites
Hmm i just realised something thanks to what you stated. Should i have a normal for every vertex. Im really new to the Triangle Strip so i dont know.

Cheers, Nick

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!