[65] | 1 | package com.framsticks.net.client3D;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 | import java.io.FileInputStream;
|
---|
| 5 | import java.io.IOException;
|
---|
| 6 | import java.util.ArrayList;
|
---|
| 7 |
|
---|
| 8 | import javax.media.opengl.GL;
|
---|
| 9 |
|
---|
| 10 | import pl.vorg.mowa.core.graphics.GeometryGroup;
|
---|
| 11 | import pl.vorg.mowa.core.graphics.SkyBox;
|
---|
| 12 |
|
---|
| 13 | import com.framsticks.net.client3D.graphics.loaders.OBJLoader;
|
---|
| 14 | import com.sun.opengl.cg.CGannotation;
|
---|
| 15 | import com.sun.opengl.cg.CGcontext;
|
---|
| 16 | import com.sun.opengl.cg.CGeffect;
|
---|
| 17 | import com.sun.opengl.cg.CGparameter;
|
---|
| 18 | import com.sun.opengl.cg.CGpass;
|
---|
| 19 | import com.sun.opengl.cg.CGtechnique;
|
---|
| 20 | import com.sun.opengl.cg.CgGL;
|
---|
| 21 | import com.sun.opengl.util.texture.Texture;
|
---|
| 22 | import com.sun.opengl.util.texture.TextureIO;
|
---|
| 23 |
|
---|
| 24 | public class cgfxEffect {
|
---|
| 25 | static CGcontext cgContext;
|
---|
| 26 |
|
---|
| 27 | public class cgParam {
|
---|
| 28 | cgParam() {
|
---|
| 29 | use = false;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | CGparameter param;
|
---|
| 33 | CGannotation ann;
|
---|
| 34 | boolean use;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public class cgTexture {
|
---|
| 38 | Texture glTex;
|
---|
| 39 | CGparameter param;
|
---|
| 40 |
|
---|
| 41 | void LoadTex(String fileName) throws Exception {
|
---|
| 42 | glTex = TextureIO.newTexture(new File(fileName), false);
|
---|
| 43 | glTex.bind();
|
---|
| 44 | CgGL.cgGLSetTextureParameter(param, glTex.getTextureObject());
|
---|
| 45 | CgGL.cgSetSamplerState(param);
|
---|
| 46 | // glTex.setTexParameteri(GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
|
---|
| 47 | // glTex.setTexParameteri(GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
|
---|
| 48 | // glTex.bind();
|
---|
| 49 | // glTex.enable();
|
---|
| 50 | // CgGL.cgGLSetupSampler(param, glTex.getTextureObject());
|
---|
| 51 |
|
---|
| 52 | // CgGL.cgGLSetTextureParameter(param, glTex.getTextureObject());
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void setSkyBox(SkyBox box) {
|
---|
| 57 | this.box = box;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | SkyBox box;
|
---|
| 61 | final String DEFAULT_MATERIAL = "res/shaders/default.cgfx";
|
---|
| 62 | final String SHADERS_PATH = "res/shaders/";
|
---|
| 63 | final String IMAGES_PATH = "res/img/";
|
---|
| 64 | final String OBJS_PATH = "res/obj/";
|
---|
| 65 | final String DEFAULT_TEXTURE = "res/img/stone.jpg";
|
---|
| 66 | final String DEFAULT_PART_GEO = "res/obj/part.obj";
|
---|
| 67 | final String DEFAULT_JOINT_GEO = "res/obj/stick5red.obj";
|
---|
| 68 | cgParam m_cgModelView;
|
---|
| 69 | cgParam m_cgModelViewProj;
|
---|
| 70 | cgParam m_cgModelViewIT;
|
---|
| 71 | ArrayList<cgTexture> texArray;
|
---|
| 72 | cgParam m_cgDetailScale;
|
---|
| 73 | cgParam m_cgShiness;
|
---|
| 74 | cgParam m_cgLights;
|
---|
| 75 | CGparameter cgSkybox;
|
---|
| 76 |
|
---|
| 77 | private GeometryGroup partGeo;
|
---|
| 78 | private GeometryGroup jointGeo;
|
---|
| 79 |
|
---|
| 80 | CGtechnique m_cgPartTech;
|
---|
| 81 | CGtechnique m_cgJointTech;
|
---|
| 82 | CGtechnique m_cgTechnique;
|
---|
| 83 |
|
---|
| 84 | CGeffect m_cgEffect;
|
---|
| 85 | boolean m_bIsInited;
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * Informs whether the style is initialized properly.
|
---|
| 89 | *
|
---|
| 90 | * @return Boolean value indicating whether the style is initialized
|
---|
| 91 | * properly.
|
---|
| 92 | */
|
---|
| 93 | boolean isInitialized() {
|
---|
| 94 | return m_bIsInited;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | String name;
|
---|
| 98 |
|
---|
| 99 | public boolean Init() {
|
---|
| 100 | return LoadByName(name);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public cgfxEffect(String name) {
|
---|
| 104 | m_bIsInited = false;
|
---|
| 105 | this.name = name;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * Renders creatures, all in the same style.
|
---|
| 110 | *
|
---|
| 111 | * @param gl
|
---|
| 112 | * OpenGL handler.
|
---|
| 113 | * @param creatures
|
---|
| 114 | * An array of creatures to render.
|
---|
| 115 | * @param modelType
|
---|
| 116 | * Type of model.
|
---|
| 117 | */
|
---|
| 118 | public void render(GL gl, Creature[] creatures, Creature.ModelType modelType) {
|
---|
| 119 |
|
---|
| 120 | // gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 5, -5, 5},
|
---|
| 121 | // 0);
|
---|
| 122 |
|
---|
| 123 | gl.glEnable(GL.GL_LIGHT0);
|
---|
| 124 | gl.glEnable(GL.GL_LIGHTING);
|
---|
| 125 |
|
---|
| 126 | // parts
|
---|
| 127 | if (texArray != null && texArray.size() > 0) {
|
---|
| 128 | texArray.get(0).glTex.enable();
|
---|
| 129 | texArray.get(0).glTex.bind();
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | CGpass pass = CgGL.cgGetFirstPass(m_cgPartTech);
|
---|
| 133 | int passNum = 0;
|
---|
| 134 | while (pass != null) {
|
---|
| 135 | gl.glPushAttrib(GL.GL_ALL_ATTRIB_BITS);
|
---|
| 136 | CgGL.cgSetPassState(pass);
|
---|
| 137 | int j = 0;
|
---|
| 138 | for (Creature creature : creatures) {
|
---|
| 139 | float[][] parts = creature.getParts(modelType);
|
---|
| 140 | for (int i = 0; i < creature.getParts(modelType).length; i++) {
|
---|
| 141 | j = i;
|
---|
| 142 | if (j >= creature.getJoints().length)
|
---|
| 143 | j--;
|
---|
| 144 | float[] r = creature.jointRotation(j, modelType);
|
---|
| 145 | gl.glPushMatrix();
|
---|
| 146 | gl.glTranslatef(parts[i][0], parts[i][1], parts[i][2]);
|
---|
| 147 | gl.glRotatef(r[0], r[1], r[2], r[3]);
|
---|
| 148 | gl.glScalef(0.7f, 0.7f, 0.7f);
|
---|
| 149 |
|
---|
| 150 | if (m_cgModelView.use)
|
---|
| 151 | CgGL.cgGLSetStateMatrixParameter(m_cgModelView.param,
|
---|
| 152 | CgGL.CG_GL_MODELVIEW_MATRIX,
|
---|
| 153 | CgGL.CG_GL_MATRIX_IDENTITY);
|
---|
| 154 | if (m_cgModelViewIT.use)
|
---|
| 155 | CgGL.cgGLSetStateMatrixParameter(m_cgModelViewIT.param,
|
---|
| 156 | CgGL.CG_GL_MODELVIEW_MATRIX,
|
---|
| 157 | CgGL.CG_GL_MATRIX_INVERSE_TRANSPOSE);
|
---|
| 158 | if (m_cgModelViewProj.use)
|
---|
| 159 | CgGL.cgGLSetStateMatrixParameter(
|
---|
| 160 | m_cgModelViewProj.param,
|
---|
| 161 | CgGL.CG_GL_MODELVIEW_PROJECTION_MATRIX,
|
---|
| 162 | CgGL.CG_GL_MATRIX_IDENTITY);
|
---|
| 163 |
|
---|
| 164 | partGeo.display(gl);
|
---|
| 165 | gl.glPopMatrix();
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | CgGL.cgResetPassState(pass);
|
---|
| 169 | gl.glPopAttrib();
|
---|
| 170 | pass = CgGL.cgGetNextPass(pass);
|
---|
| 171 |
|
---|
| 172 | passNum++;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | if (texArray != null && texArray.size() > 0) {
|
---|
| 176 | texArray.get(0).glTex.disable();
|
---|
| 177 | }
|
---|
| 178 | // joints
|
---|
| 179 |
|
---|
| 180 | if (texArray != null && texArray.size() > 1) {
|
---|
| 181 | texArray.get(1).glTex.enable();
|
---|
| 182 | texArray.get(1).glTex.bind();
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | pass = CgGL.cgGetFirstPass(m_cgJointTech);
|
---|
| 186 | while (pass != null) {
|
---|
| 187 | CgGL.cgSetPassState(pass);
|
---|
| 188 | for (Creature creature : creatures) {
|
---|
| 189 | int[][] joints = creature.getJoints();
|
---|
| 190 | for (int i = 0; i < joints.length; i++) {
|
---|
| 191 | gl.glPushMatrix();
|
---|
| 192 | float[] t = creature.jointTranslation(i, modelType);
|
---|
| 193 | float[] r = creature.jointRotation(i, modelType);
|
---|
| 194 | float l = creature.jointLength(i, modelType);
|
---|
| 195 | gl.glTranslatef(t[0], t[1], t[2]);
|
---|
| 196 | gl.glRotatef(r[0], r[1], r[2], r[3]);
|
---|
| 197 | gl.glScalef(1.0f, 1.0f, l);
|
---|
| 198 |
|
---|
| 199 | if (m_cgModelView.use)
|
---|
| 200 | CgGL.cgGLSetStateMatrixParameter(m_cgModelView.param,
|
---|
| 201 | CgGL.CG_GL_MODELVIEW_MATRIX,
|
---|
| 202 | CgGL.CG_GL_MATRIX_IDENTITY);
|
---|
| 203 | if (m_cgModelViewIT.use)
|
---|
| 204 | CgGL.cgGLSetStateMatrixParameter(m_cgModelViewIT.param,
|
---|
| 205 | CgGL.CG_GL_MODELVIEW_MATRIX,
|
---|
| 206 | CgGL.CG_GL_MATRIX_INVERSE_TRANSPOSE);
|
---|
| 207 | if (m_cgModelViewProj.use)
|
---|
| 208 | CgGL.cgGLSetStateMatrixParameter(
|
---|
| 209 | m_cgModelViewProj.param,
|
---|
| 210 | CgGL.CG_GL_MODELVIEW_PROJECTION_MATRIX,
|
---|
| 211 | CgGL.CG_GL_MATRIX_IDENTITY);
|
---|
| 212 |
|
---|
| 213 | jointGeo.display(gl);
|
---|
| 214 | gl.glPopMatrix();
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 | CgGL.cgResetPassState(pass);
|
---|
| 218 | pass = CgGL.cgGetNextPass(pass);
|
---|
| 219 | }
|
---|
| 220 | if (texArray != null && texArray.size() > 1) {
|
---|
| 221 | texArray.get(1).glTex.disable();
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | gl.glDisable(GL.GL_LIGHT0);
|
---|
| 225 | gl.glDisable(GL.GL_LIGHTING);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | public boolean LoadFromFile(String fileName) {
|
---|
| 229 | if (cgContext == null) {
|
---|
| 230 | cgContext = CgGL.cgCreateContext();
|
---|
| 231 | CgGL.cgGLSetManageTextureParameters(cgContext, true);
|
---|
| 232 | CgGL.cgGLRegisterStates(cgContext);
|
---|
| 233 | }
|
---|
| 234 | // check if material file exist.
|
---|
| 235 | FileInputStream f;
|
---|
| 236 | try {
|
---|
| 237 | f = new FileInputStream(fileName);
|
---|
| 238 | if (f != null)
|
---|
| 239 | f.close();
|
---|
| 240 | } catch (Exception e) {
|
---|
| 241 | fileName = DEFAULT_MATERIAL;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | m_cgEffect = CgGL.cgCreateEffectFromFile(cgContext, fileName, null);
|
---|
| 245 | if (m_cgEffect == null) {
|
---|
| 246 | Log.getInstance().log(
|
---|
| 247 | "err",
|
---|
| 248 | "cgfxEffect: Error while loading effect file! "
|
---|
| 249 | + CgGL.cgGetLastListing(cgContext));
|
---|
| 250 |
|
---|
| 251 | return false;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | // initing parameters
|
---|
| 255 | CGparameter p = CgGL.cgGetFirstEffectParameter(m_cgEffect);
|
---|
| 256 | while (p != null) {
|
---|
| 257 | if (CgGL.cgGetParameterType(p) == CgGL.CG_SAMPLER2D) {
|
---|
| 258 | // check what type of texture is it ;]
|
---|
| 259 | Log.getInstance().log("dbg", CgGL.cgGetParameterSemantic(p));
|
---|
| 260 | CGannotation ann = CgGL.cgGetFirstParameterAnnotation(p);
|
---|
| 261 | cgTexture newTex = new cgTexture();
|
---|
| 262 | newTex.param = p;
|
---|
| 263 | try {
|
---|
| 264 | newTex.LoadTex(IMAGES_PATH
|
---|
| 265 | + CgGL.cgGetStringAnnotationValue(ann));
|
---|
| 266 | } catch (Exception e) {
|
---|
| 267 | Log.getInstance().log(
|
---|
| 268 | "err",
|
---|
| 269 | "Texture with file name "
|
---|
| 270 | + CgGL.cgGetStringAnnotationValue(ann)
|
---|
| 271 | + " not found! " + e.getMessage());
|
---|
| 272 | try {
|
---|
| 273 | newTex.LoadTex(DEFAULT_TEXTURE);
|
---|
| 274 | } catch (Exception ex) {
|
---|
| 275 | Log.getInstance().log(
|
---|
| 276 | "err",
|
---|
| 277 | "Default texture file not found! "
|
---|
| 278 | + ex.getMessage());
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | if (texArray == null)
|
---|
| 283 | texArray = new ArrayList<cgTexture>();
|
---|
| 284 | texArray.add(newTex);
|
---|
| 285 |
|
---|
| 286 | } else if (CgGL.cgGetParameterType(p) == CgGL.CG_SAMPLERCUBE) {
|
---|
| 287 | CgGL.cgGLSetupSampler(p, box.getCube());
|
---|
| 288 | cgSkybox = p;
|
---|
| 289 | Log.getInstance().log("assigning skybox");
|
---|
| 290 | } else if (CgGL.cgGetParameterType(p) == CgGL.CG_FLOAT4x4) {
|
---|
| 291 | if (CgGL.cgGetParameterSemantic(p).equals("MODELVIEW")) {
|
---|
| 292 | m_cgModelView = new cgParam();
|
---|
| 293 | m_cgModelView.param = p;
|
---|
| 294 | m_cgModelView.use = true;
|
---|
| 295 | } else if (CgGL.cgGetParameterSemantic(p).equals(
|
---|
| 296 | "MODELVIEWPROJECTION")) {
|
---|
| 297 | m_cgModelViewProj = new cgParam();
|
---|
| 298 | m_cgModelViewProj.param = p;
|
---|
| 299 | m_cgModelViewProj.use = true;
|
---|
| 300 | } else if (CgGL.cgGetParameterSemantic(p).equals("MODELVIEWIT")) {
|
---|
| 301 | m_cgModelViewIT = new cgParam();
|
---|
| 302 | m_cgModelViewIT.param = p;
|
---|
| 303 | m_cgModelViewIT.use = true;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | } else if (CgGL.cgGetParameterType(p) == CgGL.CG_STRING) {
|
---|
| 307 | if (CgGL.cgGetParameterName(p).equals("partGeo")) {
|
---|
| 308 | try {
|
---|
| 309 | partGeo = OBJLoader.load(OBJS_PATH
|
---|
| 310 | + CgGL.cgGetStringParameterValue(p));
|
---|
| 311 | } catch (IOException ioe) {
|
---|
| 312 | Log.getInstance().log(
|
---|
| 313 | "err",
|
---|
| 314 | "Object with file name "
|
---|
| 315 | + CgGL.cgGetStringParameterValue(p)
|
---|
| 316 | + " not found!");
|
---|
| 317 | partGeo = null;
|
---|
| 318 | }
|
---|
| 319 | } else if (CgGL.cgGetParameterName(p).equals("jointGeo")) {
|
---|
| 320 | try {
|
---|
| 321 | jointGeo = OBJLoader.load(OBJS_PATH
|
---|
| 322 | + CgGL.cgGetStringParameterValue(p));
|
---|
| 323 | } catch (IOException ioe) {
|
---|
| 324 | Log.getInstance().log(
|
---|
| 325 | "err",
|
---|
| 326 | "Object with file name "
|
---|
| 327 | + CgGL.cgGetStringParameterValue(p)
|
---|
| 328 | + " not found!");
|
---|
| 329 | jointGeo = null;
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | } else // (cgGetParameterType(p) ==
|
---|
| 334 | // cgGetNamedUserType(m_cgEffectVec[lightNum],"LIGHT"))
|
---|
| 335 | {
|
---|
| 336 | if (CgGL.cgGetParameterSemantic(p).equals("lights"))
|
---|
| 337 | // || strcmp(cgGetParameterSemantic(p),"LIGHTS")==0)
|
---|
| 338 | {
|
---|
| 339 | m_cgLights = new cgParam();
|
---|
| 340 | m_cgLights.param = p;
|
---|
| 341 | m_cgLights.use = true;
|
---|
| 342 | // CGparameter array;
|
---|
| 343 | // CGtype t =
|
---|
| 344 | // cgGetNamedUserType(g_CEffectManager.GetDefaultEffect(),"PointLight");
|
---|
| 345 | // array =
|
---|
| 346 | // cgCreateParameterArray(g_CEffectManager.GetCGContext(),
|
---|
| 347 | // t, 2);
|
---|
| 348 |
|
---|
| 349 | // cgConnectParameter(CEffectLight::GetLightsParam(lightNum),
|
---|
| 350 | // m_cgLights[lightNum].param);
|
---|
| 351 | } else
|
---|
| 352 | Log.getInstance().log("err",
|
---|
| 353 | "cgfxEffect: Unknown semantic ");
|
---|
| 354 | }
|
---|
| 355 | p = CgGL.cgGetNextParameter(p);
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | m_cgTechnique = CgGL.cgGetFirstTechnique(m_cgEffect);
|
---|
| 359 | while (m_cgTechnique != null) {
|
---|
| 360 | if (!CgGL.cgValidateTechnique(m_cgTechnique))
|
---|
| 361 | Log.getInstance().log(
|
---|
| 362 | "err",
|
---|
| 363 | "Technique " + CgGL.cgGetTechniqueName(m_cgTechnique)
|
---|
| 364 | + " did not validate. Skipping.\n");
|
---|
| 365 | else {
|
---|
| 366 | CGannotation ann = CgGL
|
---|
| 367 | .cgGetFirstTechniqueAnnotation(m_cgTechnique);
|
---|
| 368 | Log.getInstance().log("dbg",
|
---|
| 369 | CgGL.cgGetStringAnnotationValue(ann));
|
---|
| 370 | if (CgGL.cgGetStringAnnotationValue(ann).equals("parts")) {
|
---|
| 371 | // m_cgJointTech = m_cgTechnique;
|
---|
| 372 | m_cgPartTech = m_cgTechnique;
|
---|
| 373 | } else if (CgGL.cgGetStringAnnotationValue(ann)
|
---|
| 374 | .equals("joints")) {
|
---|
| 375 | m_cgJointTech = m_cgTechnique;
|
---|
| 376 | // m_cgPartTech = m_cgTechnique;
|
---|
| 377 | } else if (CgGL.cgGetStringAnnotationValue(ann).equals("both")) {
|
---|
| 378 | m_cgPartTech = m_cgTechnique;
|
---|
| 379 | m_cgJointTech = m_cgTechnique;
|
---|
| 380 |
|
---|
| 381 | }
|
---|
| 382 | if (m_cgJointTech != null && m_cgPartTech != null)
|
---|
| 383 | break;
|
---|
| 384 |
|
---|
| 385 | }
|
---|
| 386 | m_cgTechnique = CgGL.cgGetNextTechnique(m_cgTechnique);
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | if (m_cgJointTech == null) {
|
---|
| 390 | Log.getInstance().log("err",
|
---|
| 391 | "No valid 'joints' technique in effect file!");
|
---|
| 392 | return false;
|
---|
| 393 | }
|
---|
| 394 | if (m_cgPartTech == null) {
|
---|
| 395 | Log.getInstance().log("err",
|
---|
| 396 | "No valid 'parts' technique in effect file!");
|
---|
| 397 | return false;
|
---|
| 398 | }
|
---|
| 399 | if (partGeo == null) {
|
---|
| 400 | try {
|
---|
| 401 | partGeo = OBJLoader.load(DEFAULT_PART_GEO);
|
---|
| 402 | } catch (IOException ioe) {
|
---|
| 403 | Log.getInstance().log(
|
---|
| 404 | "err",
|
---|
| 405 | "Object with file name " + DEFAULT_PART_GEO
|
---|
| 406 | + " not found!");
|
---|
| 407 | partGeo = null;
|
---|
| 408 | }
|
---|
| 409 | }
|
---|
| 410 | if (jointGeo == null) {
|
---|
| 411 | try {
|
---|
| 412 | jointGeo = OBJLoader.load(DEFAULT_JOINT_GEO);
|
---|
| 413 | } catch (IOException ioe) {
|
---|
| 414 | Log.getInstance().log(
|
---|
| 415 | "err",
|
---|
| 416 | "Object with file name " + DEFAULT_PART_GEO
|
---|
| 417 | + " not found!");
|
---|
| 418 | jointGeo = null;
|
---|
| 419 | }
|
---|
| 420 | }
|
---|
| 421 | // m_cgTechnique = CgGL.cgGetFirstTechnique(m_cgEffect);
|
---|
| 422 | m_bIsInited = true;
|
---|
| 423 | return true;
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | public boolean LoadByName(String name) {
|
---|
| 427 | String path;
|
---|
| 428 | path = SHADERS_PATH;
|
---|
| 429 | path += name;
|
---|
| 430 | path += ".cgfx";
|
---|
| 431 | return LoadFromFile(path);
|
---|
| 432 | }
|
---|
| 433 | }
|
---|