Exemplo n.º 1
0
  public Box() {

    /*
     * create the shader program. If OK, create vertex
     * and fragment shaders
     */
    shader = ARBShaderObjects.glCreateProgramObjectARB();

    if (shader != 0) {
      vertShader = createVertShader("shaders/hello-gl.v.glsl");
      fragShader = createFragShader("shaders/hello-gl.f.glsl");
      // skyFragShader=createFragShader("shaders/sky-gl.f.glsl");
    } else useShader = false;

    /*
     * if the vertex and fragment shaders setup sucessfully,
     * attach them to the shader program, link the sahder program
     * (into the GL context I suppose), and validate
     */
    if (vertShader != 0 && fragShader != 0) {
      ARBShaderObjects.glAttachObjectARB(shader, vertShader);
      ARBShaderObjects.glAttachObjectARB(shader, fragShader);
      ARBShaderObjects.glLinkProgramARB(shader);
      ARBShaderObjects.glValidateProgramARB(shader);
      useShader = printLogInfo(shader, "attach");
    } else useShader = false;
  }
Exemplo n.º 2
0
  /*
   * If the shader was setup succesfully, we use the shader. Otherwise
   * we run normal drawing code.
   */
  public void draw() {
    if (useShader) {
      ARBShaderObjects.glUseProgramObjectARB(shader);
    }
    GL11.glLoadIdentity();
    GL11.glTranslatef(0.0f, 0.0f, -10.0f);
    GL11.glColor3f(1.0f, 1.0f, 1.0f); // red

    GL11.glBegin(GL11.GL_QUADS);
    GL11.glVertex3f(-1.0f, 1.0f, 0.0f);
    GL11.glVertex3f(1.0f, 1.0f, 0.0f);
    GL11.glVertex3f(1.0f, -1.0f, 0.0f);
    GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
    GL11.glEnd();

    // GL11.glColor3f(0.0f, 1.0f, 0.0f);//green

    // if(useShader) {
    // ARBShaderObjects.glUseProgramObjectARB(fragShader);
    // }

    /*GL11.glBegin(GL11.GL_QUADS);
    GL11.glVertex3f(-1.0f, 1.0f, 0.0f);
    GL11.glVertex3f(1.0f, 1.0f, 0.0f);
    GL11.glVertex3f(1.0f, -1.0f, 0.0f);
    GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
    GL11.glEnd();*/

    // release the shader
    ARBShaderObjects.glUseProgramObjectARB(0);
  }
Exemplo n.º 3
0
  // same as per the vertex shader except for method syntax
  private int createFragShader(String filename) {

    fragShader = ARBShaderObjects.glCreateShaderObjectARB(ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
    if (fragShader == 0) {
      return 0;
    }
    String fragCode = "";
    String line;
    try {
      BufferedReader reader = new BufferedReader(new FileReader(filename));
      while ((line = reader.readLine()) != null) {
        fragCode += line + "\n";
      }
    } catch (Exception e) {
      System.out.println("Fail reading fragment shading code" + filename);
      return 0;
    }
    ARBShaderObjects.glShaderSourceARB(fragShader, fragCode);
    ARBShaderObjects.glCompileShaderARB(fragShader);
    if (!printLogInfo(fragShader, filename)) {
      fragShader = 0;
    }

    return fragShader;
  }
Exemplo n.º 4
0
  /*
   * With the exception of syntax, setting up vertex and fragment shaders
   * is the same.
   * @param the name and path to the vertex shader
   */
  private int createVertShader(String filename) {
    // vertShader will be non zero if succefully created

    vertShader = ARBShaderObjects.glCreateShaderObjectARB(ARBVertexShader.GL_VERTEX_SHADER_ARB);
    // if created, convert the vertex shader code to a String
    if (vertShader == 0) {
      return 0;
    }
    String vertexCode = "";
    String line;
    try {
      BufferedReader reader = new BufferedReader(new FileReader(filename));
      while ((line = reader.readLine()) != null) {
        vertexCode += line + "\n";
      }
    } catch (Exception e) {
      System.out.println("Fail reading vertex shading code: " + filename);
      return 0;
    }
    /*
     * associate the vertex code String with the created vertex shader
     * and compile
     */
    ARBShaderObjects.glShaderSourceARB(vertShader, vertexCode);
    ARBShaderObjects.glCompileShaderARB(vertShader);
    // if there was a problem compiling, reset vertShader to zero
    if (!printLogInfo(vertShader, filename)) {
      vertShader = 0;
    }
    // if zero we won't be using the shader
    return vertShader;
  }
 private static void updateShaderUniform(ShaderVariableFloat4 shaderUniform) {
   ARBShaderObjects.glUniform4fARB(
       shaderUniform.variableID,
       shaderUniform.value1,
       shaderUniform.value2,
       shaderUniform.value3,
       shaderUniform.value4);
 }
Exemplo n.º 6
0
  private static boolean printLogInfo(int obj, String filename) {
    IntBuffer iVal = BufferUtils.createIntBuffer(1);
    ARBShaderObjects.glGetObjectParameterARB(
        obj, ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);

    int length = iVal.get();
    if (length > 1) {
      // We have some info we need to output.
      ByteBuffer infoLog = BufferUtils.createByteBuffer(length);
      iVal.flip();
      ARBShaderObjects.glGetInfoLogARB(obj, iVal, infoLog);
      byte[] infoBytes = new byte[length];
      infoLog.get(infoBytes);
      String out = new String(infoBytes);
      System.out.println("Info log for " + filename + ":\n" + out);
    } else return true;
    return false;
  }
Exemplo n.º 7
0
  private int createShader(final String filename, final int shaderType) throws Exception {
    int shader = 0;
    try {
      shader = ARBShaderObjects.glCreateShaderObjectARB(shaderType);

      if (shader == 0) {
        return 0;
      }

      ARBShaderObjects.glShaderSourceARB(shader, this.readFileAsString(filename));
      ARBShaderObjects.glCompileShaderARB(shader);
      if (ARBShaderObjects.glGetObjectParameteriARB(
              shader, ARBShaderObjects.GL_OBJECT_COMPILE_STATUS_ARB)
          == GL11.GL_FALSE) {
        throw new RuntimeException("Error creating shader: " + this.getLogInfo(shader));
      }
      return shader;
    } catch (final Exception exc) {
      ARBShaderObjects.glDeleteObjectARB(shader);
      throw exc;
    }
  }
  /**
   * Update variableID for uniform shadervariable if needed.
   *
   * @param variable shadervaribale to update ID on
   * @param programID shader program context ID
   */
  public static void updateUniformLocation(ShaderVariable variable, int programID) {
    if (variable.variableID == -1) {
      ByteBuffer nameBuf = BufferUtils.createByteBuffer(variable.name.getBytes().length + 1);
      nameBuf.clear();
      nameBuf.put(variable.name.getBytes());
      nameBuf.rewind();

      variable.variableID = ARBShaderObjects.glGetUniformLocationARB(programID, nameBuf);

      if (variable.variableID == -1) {
        logger.severe("Shader uniform [" + variable.name + "] could not be located in shader");
      }
    }
  }
Exemplo n.º 9
0
 public boolean initShader() {
   int vertShader = 0, fragShader = 0;
   try {
     vertShader = this.createShader("./shaders/screen.vert", ARBVertexShader.GL_VERTEX_SHADER_ARB);
     fragShader =
         this.createShader("./shaders/screen.frag", ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
   } catch (final Exception exc) {
     exc.printStackTrace();
     return false;
   } finally {
     if ((vertShader == 0) || (fragShader == 0)) {
       return false;
     }
   }
   this.program = ARBShaderObjects.glCreateProgramObjectARB();
   if (this.program == 0) {
     return false;
   }
   ARBShaderObjects.glAttachObjectARB(this.program, vertShader);
   ARBShaderObjects.glAttachObjectARB(this.program, fragShader);
   ARBShaderObjects.glLinkProgramARB(this.program);
   if (ARBShaderObjects.glGetObjectParameteriARB(
           this.program, ARBShaderObjects.GL_OBJECT_LINK_STATUS_ARB)
       == GL11.GL_FALSE) {
     System.err.println(this.getLogInfo(this.program));
     return false;
   }
   ARBShaderObjects.glValidateProgramARB(this.program);
   if (ARBShaderObjects.glGetObjectParameteriARB(
           this.program, ARBShaderObjects.GL_OBJECT_VALIDATE_STATUS_ARB)
       == GL11.GL_FALSE) {
     System.err.println(this.getLogInfo(this.program));
     return false;
   }
   return true;
 }
Exemplo n.º 10
0
 private static void updateShaderUniform(ShaderVariableMatrix4 shaderUniform) {
   shaderUniform.matrixBuffer.rewind();
   ARBShaderObjects.glUniformMatrix4ARB(
       shaderUniform.variableID, shaderUniform.rowMajor, shaderUniform.matrixBuffer);
 }
Exemplo n.º 11
0
 private static void updateShaderUniform(ShaderVariableFloat shaderUniform) {
   ARBShaderObjects.glUniform1fARB(shaderUniform.variableID, shaderUniform.value1);
 }
Exemplo n.º 12
0
 private static void updateShaderUniform(ShaderVariableInt3 shaderUniform) {
   ARBShaderObjects.glUniform3iARB(
       shaderUniform.variableID, shaderUniform.value1, shaderUniform.value2, shaderUniform.value3);
 }
Exemplo n.º 13
0
 private String getLogInfo(final int obj) {
   return ARBShaderObjects.glGetInfoLogARB(
       obj,
       ARBShaderObjects.glGetObjectParameteriARB(
           obj, ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB));
 }
Exemplo n.º 14
0
 public void render() {
   final Dimension size = this.canvas.getSize();
   if ((size.width > 0) && (size.height > 0)) {
     if ((this.width != size.width) || (this.height != size.height)) {
       this.width = size.width;
       this.height = size.height;
       GL11.glViewport(0, 0, this.width, this.height);
       GL11.glMatrixMode(GL11.GL_PROJECTION);
       GL11.glLoadIdentity();
       final float c =
           (float)
               Math.sqrt(
                   (double) (this.width * this.width) + (double) (this.height * this.height));
       GL11.glOrtho(0.0F, this.width, 0.0F, this.height, -c, c);
       GL11.glMatrixMode(GL11.GL_MODELVIEW);
       final boolean useShader = this.initShader();
       if (ModelConstants.ENABLE_VERTEX_SHADER && useShader) {
         ARBShaderObjects.glUseProgramObjectARB(this.program);
       }
       if (ModelConstants.ENABLE_LIGHTING) {
         GL11.glEnable(GL11.GL_LIGHTING);
         GL11.glEnable(GL11.GL_LIGHT0);
         final FloatBuffer diffuse =
             BufferUtils.createFloatBuffer(4).put(new float[] {1F, 1F, 1F, 1F});
         final FloatBuffer position =
             BufferUtils.createFloatBuffer(4).put(new float[] {3F, 0F, 1F, 0F});
         final FloatBuffer ambient =
             BufferUtils.createFloatBuffer(4).put(new float[] {.1F, .1F, .2F, .3F});
         final FloatBuffer specular =
             BufferUtils.createFloatBuffer(4).put(new float[] {1F, 1F, 1F, 1F});
         GL11.glMaterial(GL11.GL_FRONT, GL11.GL_SPECULAR, (FloatBuffer) specular.flip());
         GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, (FloatBuffer) ambient.flip());
         GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, (FloatBuffer) diffuse.flip());
         GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, (FloatBuffer) position.flip());
         GL11.glMaterialf(GL11.GL_FRONT, GL11.GL_SHININESS, 50.0f);
       }
     }
     if (Mouse.isButtonDown(0) && !Mouse.isButtonDown(1)) {
       this.yaw -= Mouse.getDX();
       this.pitch -= Mouse.getDY();
     }
     if (Mouse.isButtonDown(0) && Mouse.isButtonDown(1)) {
       this.offset_z += Mouse.getDY();
     }
     float wheel = Mouse.getDWheel() / 960.0F;
     if (wheel > 1.0F) {
       wheel = 1.0F;
     } else if (wheel < -1.0F) {
       wheel = -1.0F;
     }
     this.scale -= this.scale * wheel;
     if (this.scale < 0.01F) {
       this.scale = 0.01F;
     }
     for (final Model3D dragonModel : this.getDragonList()) {
       final float x = this.width / 1.2F;
       final float y =
           (-((100.0F * (this.scale))) + (this.offset_z + 0.1F))
               + ((this.height - dragonModel.height()) / 2.0F);
       final float z = 0.0F;
       dragonModel.calcDimms(false);
       dragonModel.render(
           x, y, z, this.pitch, this.yaw, this.roll, this.scale, this.scale, this.scale);
     }
     for (final Model3D torvaModel : this.getTorvaList()) {
       final float x = this.width / 1.7F;
       final float y =
           (-((100.0F * (this.scale))) + this.offset_z)
               + ((this.height - torvaModel.height()) / 2.0F);
       final float z = 0.0F;
       torvaModel.calcDimms(false);
       torvaModel.render(
           x, y, z, this.pitch, this.yaw, this.roll, this.scale, this.scale, this.scale);
     }
     for (final Model3D jadModel : this.getJadList()) {
       final float x = this.width / 3.2F;
       final float y =
           (-((100.0F * (this.scale))) + this.offset_z)
               + ((this.height - jadModel.height()) / 2.0F);
       final float z = 0.0F;
       jadModel.calcDimms(false);
       jadModel.render(
           x, y, z, this.pitch, this.yaw, this.roll, this.scale, this.scale, this.scale);
     }
     for (final Model3D trollModel : this.getTrollList()) {
       final float x = this.width / 3.8F;
       final float y =
           (-((100.0F * (this.scale))) + this.offset_z)
               + ((this.height - trollModel.height()) / 2.0F);
       final float z = 0.0F;
       trollModel.calcDimms(false);
       trollModel.render(
           x, y, z, this.pitch, this.yaw, this.roll, this.scale, this.scale, this.scale);
     }
     if (ModelConstants.ENABLE_VERTEX_SHADER && this.initShader()) {
       ARBShaderObjects.glUseProgramObjectARB(0);
     }
     Display.update();
   }
 }