Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  Environment mapping  (Read 4199 times)
0 Members and 1 Guest are viewing this topic.
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Posted 2009-04-14 09:29:59 »

Hi,

I would like to do some environment mapping on a mesh. I found articles speaking about it but I'm lost right now.
Bascily, I want to use 1 2D textures to make reflexion (metal/glass material feeling). There is functions to generate Texture Coords but I don't really know whish on to use.
- GL_SPHERE_MAP seems to don't seem to work well but and it don't rotate with the view
- GL_EYE_LINEAR seems to be the one to be used but I don't manage to work with it

(It is my 3D engine, I'm making it for learning propose)
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  
GL11.glPushMatrix();
GL11.glMultMatrix(view.getFloatBuffer());  

      GL11.glEnable(GL11.GL_BLEND);
      GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
      GL11.glDisable(GL11.GL_COLOR_MATERIAL);
      GL11.glColor4f(1, 1, 1,environmentMapping);
      GL11.glEnable(GL11.GL_TEXTURE_2D);
      mesh.getWorld().getEnvironmentMapping().bind();
     
      GL11.glTexGeni(GL11.GL_S, GL11.GL_TEXTURE_GEN_MODE, GL11.GL_SPHERE_MAP);
      GL11.glTexGeni(GL11.GL_T, GL11.GL_TEXTURE_GEN_MODE, GL11.GL_SPHERE_MAP);
      GL11.glEnable(GL11.GL_TEXTURE_GEN_S);
      GL11.glEnable(GL11.GL_TEXTURE_GEN_T);
     
        GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        GL11.glVertexPointer(3, 0, model.getVertices());
       
        GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
        GL11.glNormalPointer(3,model.getNormales());
   
        GL11.glDrawElements(GL11.GL_TRIANGLES, model.getIndexes());
   
        GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
        GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);

      GL11.glDisable(GL11.GL_TEXTURE_GEN_S);
      GL11.glDisable(GL11.GL_TEXTURE_GEN_T);
         
GL11.glPopMatrix();


Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #1 - Posted 2009-04-14 15:24:33 »

I found an example using GL_SPHERE_MAP in LWJGL (Nehe lesson 26). It was using glBegin / glEnd.
So I remove glDrawElements for glBegin / glEnd then GL_SPHERE_MAP works...

I don't get why it doesn't work with glDrawElements   Huh
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #2 - Posted 2009-04-14 19:53:01 »

Some more elements :
- tested on another machine same result (so it is not a driver problem)
- nothing in the opengl reference for GL_SPHERE_MAP... can't find anything on this problem right now  Cry

No one is using environment map ?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #3 - Posted 2009-04-14 19:54:43 »

hehe, this is probably gunna end up like teh generator thing b4.

you are gunna be like "ahh I was bored of waiting, so I jsut figured it out :/" lol
Offline EgonOlsen
« Reply #4 - Posted 2009-04-14 20:07:06 »

What exactly is the problem, i.e. what is the visual outcome of your code? I'm using environment mapping in a very similar way, your code looks fine to me at first glance....does the call to bind() binds the texture?

Edit: Looking a bit closer...are you sure that this line:

1  
GL11.glNormalPointer(3,model.getNormales());


is correct? A stride of 3 looks suspicious to me. Try 0 or 12 instead, if your normals are stored one after the other without gaps.

Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #5 - Posted 2009-04-15 06:53:41 »


Edit: Looking a bit closer...are you sure that this line:

1  
GL11.glNormalPointer(3,model.getNormales());


is correct? A stride of 3 looks suspicious to me. Try 0 or 12 instead, if your normals are stored one after the other without gaps.


... no... no I could'nt...
[size=20pt]DOH ![/size]

I was pretty sure that was a stupid error... Thanks a lot Egon  Wink
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #6 - Posted 2009-04-16 13:35:36 »

Ok, it works with SPHERE_MAP. Now I'm going for REFLECTION_MAP with CubeMap (first time I use cube map  Grin).
And of course, I have a problem  persecutioncomplex

If I don't enable GL_TEXTURE_CUBE_MAP, I got (it seems allright  to me) :


But when I enable GL_TEXTURE_CUBE_MAP, the teapot is all white.

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  
      GL11.glEnable(GL11.GL_BLEND);
      GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
      //GL11.glDisable(GL11.GL_COLOR_MATERIAL);
      GL11.glColor4f(1, 1, 1,environmentMapping);
      //GL11.glEnable(GL11.GL_TEXTURE_2D);
     
      GL11.glMatrixMode(GL11.GL_TEXTURE);
      GL11.glPushMatrix();
      GL11.glMultMatrix(camera.getTrans().getFloatBuffer());
     
      GL11.glEnable(GL13.GL_TEXTURE_CUBE_MAP);
           
      GL11.glEnable(GL11.GL_NORMALIZE);
     
      GL11.glEnable(GL11.GL_TEXTURE_GEN_S);
      GL11.glEnable(GL11.GL_TEXTURE_GEN_T);
      GL11.glEnable(GL11.GL_TEXTURE_GEN_R);
     
      GL11.glTexGeni(GL11.GL_S, GL11.GL_TEXTURE_GEN_MODE,GL13.GL_REFLECTION_MAP);
      GL11.glTexGeni(GL11.GL_T, GL11.GL_TEXTURE_GEN_MODE,GL13.GL_REFLECTION_MAP);
      GL11.glTexGeni(GL11.GL_R, GL11.GL_TEXTURE_GEN_MODE,GL13.GL_REFLECTION_MAP);
     
      mesh.pass(false,true);
           
      GL11.glDisable(GL11.GL_TEXTURE_GEN_S);
      GL11.glDisable(GL11.GL_TEXTURE_GEN_T);
      GL11.glDisable(GL11.GL_TEXTURE_GEN_R);
     
      GL11.glDisable(GL11.GL_NORMALIZE);
     
      GL11.glDisable(GL13.GL_TEXTURE_CUBE_MAP);
     
      GL11.glPopMatrix();
      GL11.glMatrixMode(GL11.GL_MODELVIEW);


This code seems good for me, so it must be an error in setting up the cube map.

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  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
public void create()
    {
      GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
       
      IntBuffer scratch = BufferUtils.createIntBuffer(1);
      GL11.glGenTextures(scratch);
   
      int error = GL11.glGetError();
      if( error != GL11.GL_NO_ERROR)
      {
        System.out.println("Texture.create : error OpenGL ("+error+")");
      }
     
      handle = scratch.get(0);
     
      System.out.println("Creating cube map : "+handle);
    }

public void bind()
    {
        GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, handle);
        lastBind = handle;
       
      System.out.println("Binding cube map : "+lastBind);
    }

public void setImages(BufferedImage positiveX,BufferedImage negativeX,
                          BufferedImage positiveY,BufferedImage negativeY,
                          BufferedImage positiveZ,BufferedImage negativeZ)
    {
      bind();
       
      GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_REPLACE);
     
      setImage(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_X,positiveX);
      setImage(GL13.GL_TEXTURE_CUBE_MAP_NEGATIVE_X,negativeX);
      setImage(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_Y,positiveY);
      setImage(GL13.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,negativeY);
      setImage(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_Z,positiveZ);
      setImage(GL13.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,negativeZ);
    }

public void setImage(int place, int width, int height, int type, IntBuffer imageData)
    {    
      System.out.println(lastBind+"Setting image to "+place+" (type :"+type+")");
       
      GL11.glTexImage2D(place, 0, GL11.GL_RGBA, width, height,
                        0, type, GL11.GL_UNSIGNED_BYTE, imageData);
 
      int error = GL11.glGetError();
      if( error != GL11.GL_NO_ERROR)
      {
        System.out.println("Texture.setImage : error OpenGL ("+error+")");
      }
    }

public void setImage(int place,BufferedImage img)
    {
      // System.out.println("IMAGE : opaque("+img.getTransparency()+") type("+img.getType()+")");
       
      if(img.getTransparency() == Transparency.OPAQUE)
      {
        if ((img.getType() == BufferedImage.TYPE_BYTE_BINARY)||(img.getType() == BufferedImage.TYPE_BYTE_INDEXED))
        {
          BufferedImage img2 = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
          Graphics g = img2.getGraphics();
          g.drawImage(img, 0, 0, null);
          g.dispose();
          setImage(place,img2);
        }
        else
        {
          ByteBuffer imageData = ByteBuffer.allocateDirect(3 * img.getWidth() * img.getHeight());
          byte data[] = (byte[]) img.getRaster().getDataElements(0, 0, img.getWidth(), img.getHeight(), null);
          imageData.put(data);
          imageData.flip();
       
          setImage(place,img.getWidth(),img.getHeight(),RGB,imageData);
        }
      }
      else
      {
        if ((img.getType() == BufferedImage.TYPE_BYTE_BINARY)||(img.getType() == BufferedImage.TYPE_BYTE_INDEXED))
        {
          BufferedImage img2 = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_INT_ARGB);
          Graphics g = img2.getGraphics();
          g.drawImage(img, 0, 0, null);
          g.dispose();
          setImage(place,img2);
        }
        else if (img.getType() == BufferedImage.TYPE_INT_ARGB )
        {
          IntBuffer imageData = Util.IntBuffer(img.getWidth() * img.getHeight());
          int data[] = (int[]) img.getRaster().getDataElements(0, 0, img.getWidth(), img.getHeight(), null);
          imageData.put(data);
          imageData.flip();
       
          setImage(place,img.getWidth(),img.getHeight(),RGBA,imageData);
        }
        else
        {
          ByteBuffer imageData = ByteBuffer.allocateDirect(4 * img.getWidth() * img.getHeight());
          byte data[] = (byte[]) img.getRaster().getDataElements(0, 0, img.getWidth(), img.getHeight(), null);
          imageData.put(data);
          imageData.flip();
       
          setImage(place,img.getWidth(),img.getHeight(),RGBA,imageData);
        }
      }
    }


And it give me in the console :
1  
2  
3  
4  
5  
6  
7  
8  
Creating cube map : 8
Binding cube map : 8
8Setting image to 34069 (type :6407)
8Setting image to 34070 (type :6407)
8Setting image to 34071 (type :6407)
8Setting image to 34072 (type :6407)
8Setting image to 34073 (type :6407)
8Setting image to 34074 (type :6407)


So it seems good too  Huh Huh Huh
Any advices to how to debug that ?
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #7 - Posted 2009-04-17 08:06:02 »

Sorry...

I miss a GL_TEXTURE_2D in GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); when I have copy&past...

Still have to make correction of my texture orientation and it is done  Wink
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (6 views)
2014-12-23 03:34:11

rwatson462 (38 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!