Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Terrain Lighting  (Read 1084 times) 0 Members and 1 Guest are viewing this topic.

Senior Newbie

 « Posted 2011-07-30 21:10:39 »

Hi,

I am sure this topic is a very well worn one, but I seem to be having a specific problem. I can't for the life of me seem to be able to get my textured terrain to light properly. Or at all for that matter. I do some vector math to calculate the normals for each point.
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57 `    private void computeNormals() {        if (!computedNormals) {            //only do it if the normals aren't computed already            //Vec3f[][] normals2 = new Vec3f[width][height];            normals = new Vec3f[width][height];                        for (int z=0; z 0) {                        out = new Vec3f(0.0f, getHeight(x,z-1) - getHeight(x,z),                                -1.0f);                    }                                        Vec3f in = new Vec3f(0.0f,0.0f,0.0f);                    if (z < height - 1) {                        in = new Vec3f(0.0f, getHeight(x,z+1) - getHeight(x,z),                                1.0f);                    }                                        Vec3f left = new Vec3f(0.0f,0.0f,0.0f);                    if (x > 0) {                        left = new Vec3f(-1.0f, getHeight(x-1,z) - getHeight(x,z),                                 0.0f);                    }                                        Vec3f right = new Vec3f(0.0f,0.0f,0.0f);                    if (x < width - 1) {                        right = new Vec3f(1.0f, getHeight(x+1,z) - getHeight(x,z),                                0.0f);                    }                                        if (x > 0 && z > 0) {                        sum = sum.add(out.cross(left).normalize());                    }                    if (x > 0 && z < height - 1) {                        sum = sum.add(left.cross(in).normalize());                    }                    if (x < width - 1 &&z < height -1) {                        sum = sum.add(in.cross(right).normalize());                    }                    if (x < width - 1 && z > 0) {                        sum = sum.add(right.cross(out).normalize());                    }                                        sum = sum.normalize();                                        normals[x][z] = sum;                }            }                        computedNormals = true;                    }    }`

And then I pass it to my function to draw the actual terrain
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34 `    public void drawTerrain(Terrain terr, float bumpup) {        /*if (!changeColor) {            glColor4f(1.0f, 1.0f, 1.0f, 1.0f);        } else {            glColor4f(0.0f, 0.0f, 0.0f, 0.2f);        }*/        if (terr != null) {            for (int z = 0; z < terr.height; z++) {                //we are drawing rows of x's                glBegin(GL_QUAD_STRIP);                for (int x = 0; x < terr.width; x++) {                    if (bumpup != 0 && (terr.getHeight(x,z) <= 4.9)) {                        glColor4f(0.0f, 0.0f, 0.0f, 0.0f);                    } else if(bumpup == 0) {                        glColor4f(1.0f, 1.0f, 1.0f, 1.0f);                    } else if (bumpup != 0) {                        glColor4f(0.0f,0.0f,0.0f,0.2f);                    }                    Vec3f normal = terr.getNormal(x, z);                    //System.out.println(normal);                    glNormal3f(normal.x, normal.y, normal.z);                    glVertex3f(x+terr.xOffset, terr.getHeight(x,z)+bumpup-20.0f,                             z+terr.zOffset);                    if (z != terr.height - 1) {                        normal = terr.getNormal(x, z+1);                        glNormal3f(normal.x, normal.y, normal.z);                        glVertex3f(x+terr.xOffset, terr.getHeight(x,z+1)+bumpup-20.0f,                                 z+1+terr.zOffset);                    }                }                glEnd();            }        }    }`

But I end up with a plain green sheet without definition. If it means anything, each Terrain has a x and z offset value, as the larger terrain is split into chunks. The ground is also textured, but I do not know if that means anything.

I would appreciate any help,
Jacob

You can see some height change in the above image.

But it cannot be seen without the wireframe overlayed.
lhkbob

JGO Knight

Medals: 32

 « Reply #1 - Posted 2011-08-01 15:28:55 »

Have you enabled lighting with glEnable(GL_LIGHTING) and enabled a particular light, such as glEnable(GL_LIGHT0)?

Senior Newbie

 « Reply #2 - Posted 2011-08-01 18:56:10 »

Yeah. The lighting related code is below;
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19 `        glEnable(GL_LIGHTING);        glEnable(GL_LIGHT0);        //glEnable(GL_LIGHT1);        glEnable(GL_NORMALIZE);        glEnable(GL_CULL_FACE);        glShadeModel(GL_SMOOTH);                ...             //setup lighting        //add some ambient light. [special lwjgl fixes]        ByteBuffer temp = ByteBuffer.allocateDirect(20);        temp.order(ByteOrder.nativeOrder());        glLightModel(GL_LIGHT_MODEL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(new float[]{0.2f,0.2f,0.2f,1.0f}).flip());                //add positioned light        glLight(GL_LIGHT0, GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(new float[]{0.5f,0.5f,0.5f,1.0f}).flip());        glLight(GL_LIGHT0, GL_POSITION, (FloatBuffer)temp.asFloatBuffer().put(new float[]{4.0f,0.0f,8.0f,1.0f}).flip());`
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 BurntPizza (30 views) 2015-04-23 03:42:11 theagentd (35 views) 2015-04-22 16:23:07 Riven (49 views) 2015-04-16 10:48:47 Duke0200 (59 views) 2015-04-16 01:59:01 Fairy Tailz (42 views) 2015-04-14 20:13:12 Riven (44 views) 2015-04-12 21:36:37 bus hotdog (60 views) 2015-04-10 02:39:32 CopyableCougar4 (66 views) 2015-04-10 00:51:04 BurntPizza (70 views) 2015-04-06 22:06:58 ags1 (69 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 17x wessles 15x kingroka123 11x alwex 11x 65K 11x kevglass 8x Rayvolution 7x Roquen 7x Riven 7x chrislo27 7x Hanksha 7x Olo 7x Ecumene 7x ra4king 7x KevinWorkman 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org