[66] | 1 | package pl.vorg.mowa.core.graphics; |
---|
| 2 | |
---|
| 3 | import java.io.File; |
---|
| 4 | import java.nio.Buffer; |
---|
| 5 | |
---|
| 6 | import javax.media.opengl.GL; |
---|
| 7 | |
---|
| 8 | import com.framsticks.net.client3D.Log; |
---|
| 9 | import com.sun.opengl.util.texture.Texture; |
---|
| 10 | import com.sun.opengl.util.texture.TextureData; |
---|
| 11 | import com.sun.opengl.util.texture.TextureIO; |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * An environment texture. |
---|
| 15 | */ |
---|
| 16 | public class SkyBox { |
---|
| 17 | |
---|
| 18 | Buffer buf; |
---|
| 19 | |
---|
| 20 | public boolean Init(GL gl) { |
---|
| 21 | |
---|
| 22 | return Init(gl, new String("res/img/Front.PNG"), new String( |
---|
| 23 | "res/img/Back.PNG"), new String("res/img/Left.PNG"), |
---|
| 24 | new String("res/img/Right.PNG"), new String("res/img/Top.PNG"), |
---|
| 25 | new String("res/img/Bottom.PNG")); |
---|
| 26 | |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | public boolean Init(GL gl, String front, String back, String left, |
---|
| 30 | String right, String top, String bottom) { |
---|
| 31 | try { |
---|
| 32 | m_tFront = LoadTex(front); |
---|
| 33 | m_tBack = LoadTex(back); |
---|
| 34 | m_tLeft = LoadTex(left); |
---|
| 35 | ; |
---|
| 36 | m_tRight = LoadTex(right); |
---|
| 37 | m_tTop = LoadTex(top); |
---|
| 38 | m_tBottom = LoadTex(bottom); |
---|
| 39 | CreateCube(gl, front, back, left, right, top, bottom); |
---|
| 40 | } catch (Exception e) { |
---|
| 41 | Log.getInstance().log("err", |
---|
| 42 | "SkyBox: Error while loading textures! " + e.getMessage()); |
---|
| 43 | return false; |
---|
| 44 | } |
---|
| 45 | return true; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | Texture LoadTex(String fileName) throws Exception { |
---|
| 49 | Texture result = TextureIO.newTexture(new File(fileName), false); |
---|
| 50 | |
---|
| 51 | result.setTexParameteri(GL.GL_TEXTURE_WRAP_S, GL.GL_NONE); |
---|
| 52 | result.setTexParameteri(GL.GL_TEXTURE_WRAP_T, GL.GL_NONE); |
---|
| 53 | return result; |
---|
| 54 | |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | public void Draw(GL gl, float boxSize, Vec3 rot) { |
---|
| 58 | boolean light = gl.glIsEnabled(GL.GL_LIGHTING); |
---|
| 59 | gl.glDisable(GL.GL_LIGHTING); |
---|
| 60 | float size = boxSize * (1.72f / 3); |
---|
| 61 | // float pozx = 0 ,pozy = 0 ,pozz = 0; |
---|
| 62 | // float clip = g_CCamera.m_fFarClip; |
---|
| 63 | // g_CCamera.m_fFarClip = 1000; |
---|
| 64 | // g_CCamera.Set(); |
---|
| 65 | gl.glPushMatrix(); |
---|
| 66 | gl.glLoadIdentity(); |
---|
| 67 | // glTranslatef(g_CCamera.GetPosition().x,g_CCamera.GetPosition().y,g_CCamera.GetPosition().z); |
---|
| 68 | gl.glRotatef(rot.getX(), 1, 0, 0); |
---|
| 69 | gl.glRotatef(rot.getY(), 0, 1, 0); |
---|
| 70 | gl.glRotatef(rot.getZ(), 0, 0, 1); |
---|
| 71 | gl.glDepthMask(false); |
---|
| 72 | gl.glEnable(GL.GL_TEXTURE_2D); |
---|
| 73 | m_tFront.bind(); |
---|
| 74 | gl.glColor4f(1.0f, 1.0f, 1.0f, 0.0f); |
---|
| 75 | // gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_DST_COLOR); |
---|
| 76 | // gl.glEnable(GL.GL_BLEND); |
---|
| 77 | gl.glBegin(GL.GL_QUADS); |
---|
| 78 | // front |
---|
| 79 | |
---|
| 80 | gl.glTexCoord2d(1, 1); |
---|
| 81 | gl.glVertex3f((float) -size, (float) -size, (float) -size); |
---|
| 82 | gl.glTexCoord2d(0, 1); |
---|
| 83 | gl.glVertex3f((float) size, (float) -size, (float) -size); |
---|
| 84 | gl.glTexCoord2d(0, 0); |
---|
| 85 | gl.glVertex3f((float) size, (float) size, (float) -size); |
---|
| 86 | gl.glTexCoord2d(1, 0); |
---|
| 87 | gl.glVertex3f((float) -size, (float) size, (float) -size); |
---|
| 88 | gl.glEnd(); |
---|
| 89 | // back |
---|
| 90 | m_tBack.bind(); |
---|
| 91 | gl.glBegin(GL.GL_QUADS); |
---|
| 92 | gl.glTexCoord2d(0, 1); |
---|
| 93 | gl.glVertex3f((float) -size, (float) -size, (float) size); |
---|
| 94 | gl.glTexCoord2d(0, 0); |
---|
| 95 | gl.glVertex3f((float) -size, (float) size, (float) size); |
---|
| 96 | gl.glTexCoord2d(1, 0); |
---|
| 97 | gl.glVertex3f((float) size, (float) size, (float) size); |
---|
| 98 | gl.glTexCoord2d(1, 1); |
---|
| 99 | gl.glVertex3f((float) size, (float) -size, (float) size); |
---|
| 100 | gl.glEnd(); |
---|
| 101 | // left |
---|
| 102 | m_tLeft.bind(); |
---|
| 103 | gl.glBegin(GL.GL_QUADS); |
---|
| 104 | gl.glTexCoord2d(0, 1); |
---|
| 105 | gl.glVertex3f((float) -size, (float) -size, (float) -size); |
---|
| 106 | gl.glTexCoord2d(0, 0); |
---|
| 107 | gl.glVertex3f((float) -size, (float) size, (float) -size); |
---|
| 108 | gl.glTexCoord2d(1, 0); |
---|
| 109 | gl.glVertex3f((float) -size, (float) size, (float) size); |
---|
| 110 | gl.glTexCoord2d(1, 1); |
---|
| 111 | gl.glVertex3f((float) -size, (float) -size, (float) size); |
---|
| 112 | gl.glEnd(); |
---|
| 113 | // Right |
---|
| 114 | m_tRight.bind(); |
---|
| 115 | gl.glBegin(GL.GL_QUADS); |
---|
| 116 | gl.glTexCoord2d(1, 1); |
---|
| 117 | gl.glVertex3f((float) size, (float) -size, (float) -size); |
---|
| 118 | gl.glTexCoord2d(0, 1); |
---|
| 119 | gl.glVertex3f((float) size, (float) -size, (float) size); |
---|
| 120 | gl.glTexCoord2d(0, 0); |
---|
| 121 | gl.glVertex3f((float) size, (float) size, (float) size); |
---|
| 122 | gl.glTexCoord2d(1, 0); |
---|
| 123 | gl.glVertex3f((float) size, (float) size, (float) -size); |
---|
| 124 | gl.glEnd(); |
---|
| 125 | // Top |
---|
| 126 | m_tTop.bind(); |
---|
| 127 | |
---|
| 128 | gl.glBegin(GL.GL_QUADS); |
---|
| 129 | gl.glTexCoord2d(1, 0); |
---|
| 130 | gl.glVertex3f((float) -size, (float) size, (float) size); |
---|
| 131 | gl.glTexCoord2d(1, 1); |
---|
| 132 | gl.glVertex3f((float) -size, (float) size, (float) -size); |
---|
| 133 | gl.glTexCoord2d(0, 1); |
---|
| 134 | gl.glVertex3f((float) size, (float) size, (float) -size); |
---|
| 135 | gl.glTexCoord2d(0, 0); |
---|
| 136 | gl.glVertex3f((float) size, (float) size, (float) size); |
---|
| 137 | gl.glEnd(); |
---|
| 138 | // Bottom |
---|
| 139 | m_tBottom.bind(); |
---|
| 140 | |
---|
| 141 | gl.glBegin(GL.GL_QUADS); |
---|
| 142 | gl.glTexCoord2d(1, 0); |
---|
| 143 | gl.glVertex3f((float) -size, (float) -size, (float) -size); |
---|
| 144 | gl.glTexCoord2d(1, 1); |
---|
| 145 | gl.glVertex3f((float) -size, (float) -size, (float) size); |
---|
| 146 | gl.glTexCoord2d(0, 1); |
---|
| 147 | gl.glVertex3f((float) size, (float) -size, (float) size); |
---|
| 148 | gl.glTexCoord2d(0, 0); |
---|
| 149 | gl.glVertex3f((float) size, (float) -size, (float) -size); |
---|
| 150 | gl.glEnd(); |
---|
| 151 | gl.glBindTexture(GL.GL_TEXTURE_2D, 0); |
---|
| 152 | // gl.glDisable(GL.GL_BLEND); |
---|
| 153 | // glEnable(GL_FOG); |
---|
| 154 | gl.glPopMatrix(); |
---|
| 155 | if (light) |
---|
| 156 | gl.glEnable(GL.GL_LIGHTING); |
---|
| 157 | gl.glDepthMask(true); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | private int[] textureCubeMapId; |
---|
| 161 | |
---|
| 162 | void CreateCube(GL gl, String front, String back, String left, |
---|
| 163 | String right, String top, String bottom) { |
---|
| 164 | textureCubeMapId = new int[1]; |
---|
| 165 | int internalFormat = 0, format = 0, face = 0; |
---|
| 166 | |
---|
| 167 | gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); |
---|
| 168 | gl.glGenTextures(1, textureCubeMapId, 0); |
---|
| 169 | gl.glBindTexture(GL.GL_TEXTURE_CUBE_MAP, textureCubeMapId[0]); |
---|
| 170 | |
---|
| 171 | gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_S, |
---|
| 172 | GL.GL_CLAMP_TO_EDGE); |
---|
| 173 | gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_T, |
---|
| 174 | GL.GL_CLAMP_TO_EDGE); |
---|
| 175 | gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MAG_FILTER, |
---|
| 176 | GL.GL_LINEAR); |
---|
| 177 | gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MIN_FILTER, |
---|
| 178 | GL.GL_LINEAR); |
---|
| 179 | TextureData texDataFront = null; |
---|
| 180 | TextureData texDataBack = null; |
---|
| 181 | TextureData texDataLeft = null; |
---|
| 182 | TextureData texDataRight = null; |
---|
| 183 | TextureData texDataTop = null; |
---|
| 184 | TextureData texDataBottom = null; |
---|
| 185 | try { |
---|
| 186 | texDataFront = TextureIO |
---|
| 187 | .newTextureData(new File(front), true, null); |
---|
| 188 | texDataBack = TextureIO.newTextureData(new File(back), true, null); |
---|
| 189 | texDataLeft = TextureIO.newTextureData(new File(left), true, null); |
---|
| 190 | texDataRight = TextureIO |
---|
| 191 | .newTextureData(new File(right), true, null); |
---|
| 192 | texDataTop = TextureIO.newTextureData(new File(top), true, null); |
---|
| 193 | texDataBottom = TextureIO.newTextureData(new File(bottom), true, |
---|
| 194 | null); |
---|
| 195 | } catch (Exception e) { |
---|
| 196 | Log.getInstance().log( |
---|
| 197 | "err", |
---|
| 198 | "SkyBox: Error while generating cube textures! " |
---|
| 199 | + e.getMessage()); |
---|
| 200 | } |
---|
| 201 | TextureData active = texDataFront; |
---|
| 202 | for (int i = 0; i < 6; i++) { |
---|
| 203 | |
---|
| 204 | switch (i) { |
---|
| 205 | case 0: |
---|
| 206 | face = GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X; |
---|
| 207 | active = texDataRight; |
---|
| 208 | break; |
---|
| 209 | case 1: |
---|
| 210 | face = GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_X; |
---|
| 211 | active = texDataLeft; |
---|
| 212 | break; |
---|
| 213 | case 2: |
---|
| 214 | face = GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Y; |
---|
| 215 | active = texDataTop; |
---|
| 216 | break; |
---|
| 217 | case 3: |
---|
| 218 | face = GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y; |
---|
| 219 | active = texDataBottom; |
---|
| 220 | break; |
---|
| 221 | case 4: |
---|
| 222 | face = GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Z; |
---|
| 223 | active = texDataBack; |
---|
| 224 | break; |
---|
| 225 | case 5: |
---|
| 226 | face = GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; |
---|
| 227 | active = texDataFront; |
---|
| 228 | break; |
---|
| 229 | } |
---|
| 230 | internalFormat = active.getInternalFormat(); |
---|
| 231 | internalFormat = GL.GL_RGB8; |
---|
| 232 | // format = active.getPixelFormat() ; |
---|
| 233 | format = GL.GL_RGB; |
---|
| 234 | |
---|
| 235 | // for (int j=0; j< active.getMipmapData().length; j++) |
---|
| 236 | // gl.glTexImage2D(face, j, internalFormat, active.getWidth(), |
---|
| 237 | // active.getHeight(),0, format, GL.GL_UNSIGNED_BYTE, |
---|
| 238 | // active.getMipmapData()[j]); |
---|
| 239 | gl.glTexImage2D(face, 0, internalFormat, active.getWidth(), |
---|
| 240 | active.getHeight(), 0, format, GL.GL_UNSIGNED_BYTE, |
---|
| 241 | active.getBuffer()); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | public int getCube() { |
---|
| 247 | return textureCubeMapId[0]; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | // void Kill() {m_tFront(); m_tBack.Free(); m_tLeft.Free(); m_tRight.Free(); |
---|
| 251 | // m_tTop.Free(); m_tBottom.Free();} |
---|
| 252 | |
---|
| 253 | Texture m_tFront; |
---|
| 254 | Texture m_tBack; |
---|
| 255 | Texture m_tLeft; |
---|
| 256 | Texture m_tRight; |
---|
| 257 | Texture m_tTop; |
---|
| 258 | Texture m_tBottom; |
---|
| 259 | } |
---|