void LoadTextures(final GL gl) {
    // There is only one texture needed here--we'll set up a basic
    // checkerboard--which is used to modulate the diffuse channel in the
    // fragment shader.
    final int[] handle = new int[1];
    gl.glGenTextures(1, handle, 0);

    // Basic OpenGL texture state setup
    gl.glBindTexture(GL.GL_TEXTURE_2D, handle[0]);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_GENERATE_MIPMAP_SGIS, GL.GL_TRUE);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);

    // Fill in the texture map.
    final int RES = 512;
    final float[] data = new float[RES * RES * 4];
    int dp = 0;
    for (int i = 0; i < RES; ++i)
      for (int j = 0; j < RES; ++j) {
        if ((i / 32 + j / 32) % 2 != 0) {
          data[dp++] = .7f;
          data[dp++] = .7f;
          data[dp++] = .7f;
        } else {
          data[dp++] = .1f;
          data[dp++] = .1f;
          data[dp++] = .1f;
        }
        data[dp++] = 1.0f;
      }

    gl.glTexImage2D(
        GL.GL_TEXTURE_2D,
        0,
        GL.GL_RGBA,
        RES,
        RES,
        0,
        GL.GL_RGBA,
        GL.GL_FLOAT,
        FloatBuffer.wrap(data));

    // Tell Cg which texture handle should be associated with the sampler2D
    // parameter to the fragment shader.
    CgGL.cgGLSetTextureParameter(
        CgGL.cgGetNamedParameter(fragmentProgram, "diffuseMap"), handle[0]);
  }
Example #2
0
  public static Object createCGResource(int type, CGcontext context, String source, int profile) {
    Object id = null;

    if (type == CG_CONTEXT) {
      id = CgGL.cgCreateContext();
      cgContexts.add((CGcontext) id);
    } else if (type == CG_PROGRAM) {
      id = CgGL.cgCreateProgram(context, CgGL.CG_SOURCE, source, profile, null, null);
      cgPrograms.add((CGprogram) id);
    } else if (type == CG_EFFECT) {
      id = CgGL.cgCreateEffect(context, source, null);
      cgEffects.add((CGeffect) id);
    }

    return id;
  }
Example #3
0
 public static void deleteCGResource(Object id, int type) {
   if (type == CG_CONTEXT) {
     if (cgContexts.contains(id)) {
       CgGL.cgDestroyContext((CGcontext) id);
       cgContexts.remove(id);
     }
   } else if (type == CG_PROGRAM) {
     if (cgPrograms.contains(id)) {
       CgGL.cgDestroyProgram((CGprogram) id);
       cgPrograms.remove(id);
     }
   } else if (type == CG_EFFECT) {
     if (cgEffects.contains(id)) {
       CgGL.cgDestroyEffect((CGeffect) id);
       cgEffects.remove(id);
     }
   }
 }
  void ChooseProfiles() {
    // Make sure that the appropriate profiles are available on the
    // user's system.
    if (CgGL.cgGLIsProfileSupported(CgGL.CG_PROFILE_ARBVP1)) vertexProfile = CgGL.CG_PROFILE_ARBVP1;
    else // try VP30
    if (CgGL.cgGLIsProfileSupported(CgGL.CG_PROFILE_VP30)) vertexProfile = CgGL.CG_PROFILE_VP30;
    else {
      System.out.println("Neither arbvp1 or vp30 vertex profiles supported on this system.\n");
      System.exit(1);
    }

    if (CgGL.cgGLIsProfileSupported(CgGL.CG_PROFILE_ARBFP1))
      fragmentProfile = CgGL.CG_PROFILE_ARBFP1;
    else // try FP30
    if (CgGL.cgGLIsProfileSupported(CgGL.CG_PROFILE_FP30)) fragmentProfile = CgGL.CG_PROFILE_FP30;
    else {
      System.out.println("Neither arbfp1 or fp30 fragment profiles supported on this system.\n");
      System.exit(1);
    }
  }
  public void init(final GLAutoDrawable drawable) {
    // Use debug pipeline
    // drawable.setGL(new DebugGL(drawable.getGL()));

    final GL gl = drawable.getGL();

    // Basic Cg setup; register a callback function for any errors
    // and create an initial context
    // cgSetErrorCallback(handleCgError); // not yet exposed in Cg binding
    context = CgGL.cgCreateContext();

    // Do one-time setup only once; setup Cg programs and textures
    // and set up OpenGL state.
    ChooseProfiles();
    LoadCgPrograms();
    LoadTextures(gl);

    gl.glEnable(GL.GL_DEPTH_TEST);
  }
  void DrawGeometry(final GL gl) {
    // Cache the sphere positions, normals, texture coordinates, and
    // vertex indices in a local array; we only need to fill them in the
    // first time through this function.
    final int nu = 30, nv = 30;
    final int nTris = 2 * (nu - 1) * (nv - 1), nVerts = nu * nv;
    if (P == null) {
      int u, v;

      P = BufferUtil.newFloatBuffer(3 * nVerts);
      N = BufferUtil.newFloatBuffer(3 * nVerts);
      uv = BufferUtil.newFloatBuffer(2 * nVerts);

      // Fill in the position, normal, and texture coordinate arrays.
      // Just loop over all of the vertices, compute their parametreic
      // (u,v) coordinates (which we use for texture coordinates as
      // well), and call the ParametricEval() function, which turns (u,v)
      // coordinates into positions and normals on the surface of the
      // object.
      int pp = 0, np = 0, uvp = 0;
      for (v = 0; v < nv; ++v) {
        final float fv = (float) v / (float) (nv - 1);
        for (u = 0; u < nu; ++u) {
          final float fu = (float) u / (float) (nu - 1);
          uv.put(uvp, fu);
          uv.put(uvp + 1, fv);
          ParametricEval(fu, fv, pp, P, np, N);
          pp += 3;
          np += 3;
          uvp += 2;
        }
      }

      // Now fill in the vertex index arrays
      indices = BufferUtil.newIntBuffer(3 * nTris);
      int ip = 0;
      for (v = 0; v < nv - 1; ++v)
        for (u = 0; u < nu - 1; ++u) {
          indices.put(ip++, VERTEX(u, v, nu));
          indices.put(ip++, VERTEX(u + 1, v, nu));
          indices.put(ip++, VERTEX(u + 1, v + 1, nu));

          indices.put(ip++, VERTEX(u, v, nu));
          indices.put(ip++, VERTEX(u + 1, v + 1, nu));
          indices.put(ip++, VERTEX(u, v + 1, nu));
        }
      // Tell Cg which of these data pointers are associated with which
      // parameters to the vertex shader, so that when we call
      // cgGLEnableClientState() and then glDrawElements(), the shader
      // gets the right input information.
      CGparameter param = CgGL.cgGetNamedParameter(vertexProgram, "Pobject");
      CgGL.cgGLSetParameterPointer(param, 3, GL.GL_FLOAT, 0, P);
      param = CgGL.cgGetNamedParameter(vertexProgram, "Nobject");
      CgGL.cgGLSetParameterPointer(param, 3, GL.GL_FLOAT, 0, N);
      param = CgGL.cgGetNamedParameter(vertexProgram, "TexUV");
      CgGL.cgGLSetParameterPointer(param, 2, GL.GL_FLOAT, 0, uv);
    }

    // And now, each time through, enable the bindings to the parameters
    // that we set up the first time through
    CGparameter param = CgGL.cgGetNamedParameter(vertexProgram, "Pobject");
    CgGL.cgGLEnableClientState(param);
    param = CgGL.cgGetNamedParameter(vertexProgram, "Nobject");
    CgGL.cgGLEnableClientState(param);
    param = CgGL.cgGetNamedParameter(vertexProgram, "TexUV");
    CgGL.cgGLEnableClientState(param);

    // Enable the texture parameter as well.
    param = CgGL.cgGetNamedParameter(fragmentProgram, "diffuseMap");
    CgGL.cgGLEnableTextureParameter(param);

    // And now, draw the geometry.
    gl.glDrawElements(GL.GL_TRIANGLES, 3 * nTris, GL.GL_UNSIGNED_INT, indices);

    // Be a good citizen and disable the various bindings we set up above.
    param = CgGL.cgGetNamedParameter(vertexProgram, "Pobject");
    CgGL.cgGLDisableClientState(param);
    param = CgGL.cgGetNamedParameter(vertexProgram, "Nobject");
    CgGL.cgGLDisableClientState(param);
    param = CgGL.cgGetNamedParameter(vertexProgram, "TexUV");
    CgGL.cgGLDisableClientState(param);

    param = CgGL.cgGetNamedParameter(fragmentProgram, "diffuseMap");
    CgGL.cgGLDisableTextureParameter(param);
  }
  void LoadCgPrograms() {
    assert CgGL.cgIsContext(context);

    // Load and compile the vertex program from demo_vert.cg; hold on to the
    // handle to it that is returned.
    try {
      vertexProgram =
          CgGL.cgCreateProgramFromStream(
              context,
              CgGL.CG_SOURCE,
              getClass()
                  .getClassLoader()
                  .getResourceAsStream("demos/cg/runtime_ogl_vertex_fragment/demo_vert.cg"),
              vertexProfile,
              null,
              null);
    } catch (final IOException e) {
      throw new RuntimeException("Error loading Cg vertex program", e);
    }
    if (!CgGL.cgIsProgramCompiled(vertexProgram)) CgGL.cgCompileProgram(vertexProgram);

    // Enable the appropriate vertex profile and load the vertex program.
    CgGL.cgGLEnableProfile(vertexProfile);
    CgGL.cgGLLoadProgram(vertexProgram);

    // And similarly set things up for the fragment program.
    try {
      fragmentProgram =
          CgGL.cgCreateProgramFromStream(
              context,
              CgGL.CG_SOURCE,
              getClass()
                  .getClassLoader()
                  .getResourceAsStream("demos/cg/runtime_ogl_vertex_fragment/demo_frag.cg"),
              fragmentProfile,
              null,
              null);
    } catch (final IOException e) {
      throw new RuntimeException("Error loading Cg fragment program", e);
    }
    if (!CgGL.cgIsProgramCompiled(fragmentProgram)) CgGL.cgCompileProgram(fragmentProgram);

    CgGL.cgGLEnableProfile(fragmentProfile);
    CgGL.cgGLLoadProgram(fragmentProgram);
  }
  // display callback function
  public void display(final GLAutoDrawable drawable) {

    final GL gl = drawable.getGL();

    // The usual OpenGL stuff to clear the screen and set up viewing.
    gl.glClearColor(.25f, .25f, .25f, 1.0f);
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(30.0f, 1.0f, .1f, 100);

    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
    glu.gluLookAt(4, 4, -4, 0, 0, 0, 0, 1, 0);

    // Make the object rotate a bit each time the display function
    // is called
    gl.glRotatef(curTime, 0, 1, 0);

    // Now make sure that the vertex and fragment programs, loaded
    // in LoadCgPrograms() are bound.
    CgGL.cgGLBindProgram(vertexProgram);
    CgGL.cgGLBindProgram(fragmentProgram);

    // Bind uniform parameters to vertex shader
    CgGL.cgGLSetStateMatrixParameter(
        CgGL.cgGetNamedParameter(vertexProgram, "ModelViewProj"),
        CgGL.CG_GL_MODELVIEW_PROJECTION_MATRIX,
        CgGL.CG_GL_MATRIX_IDENTITY);
    CgGL.cgGLSetStateMatrixParameter(
        CgGL.cgGetNamedParameter(vertexProgram, "ModelView"),
        CgGL.CG_GL_MODELVIEW_MATRIX,
        CgGL.CG_GL_MATRIX_IDENTITY);
    CgGL.cgGLSetStateMatrixParameter(
        CgGL.cgGetNamedParameter(vertexProgram, "ModelViewIT"),
        CgGL.CG_GL_MODELVIEW_MATRIX,
        CgGL.CG_GL_MATRIX_INVERSE_TRANSPOSE);

    // We can also go ahead and bind varying parameters to vertex shader
    // that we just want to have the same value for all vertices. The
    // vertex shader could be modified so that these were uniform for
    // better efficiency, but this gives us flexibility for the future.
    final float Kd[] = {.7f, .2f, .2f}, Ks[] = {.9f, .9f, .9f};
    CgGL.cgGLSetParameter3fv(CgGL.cgGetNamedParameter(vertexProgram, "diffuse"), Kd, 0);
    CgGL.cgGLSetParameter3fv(CgGL.cgGetNamedParameter(vertexProgram, "specular"), Ks, 0);

    // Now bind uniform parameters to fragment shader
    final float lightPos[] = {3, 2, -3};
    CgGL.cgGLSetParameter3fv(CgGL.cgGetNamedParameter(fragmentProgram, "Plight"), lightPos, 0);
    final float lightColor[] = {1, 1, 1};
    CgGL.cgGLSetParameter3fv(
        CgGL.cgGetNamedParameter(fragmentProgram, "lightColor"), lightColor, 0);
    CgGL.cgGLSetParameter1f(CgGL.cgGetNamedParameter(fragmentProgram, "shininess"), 40);

    // And finally, enable the approprate texture for fragment shader; the
    // texture was originally set up in LoadTextures().
    CgGL.cgGLEnableTextureParameter(CgGL.cgGetNamedParameter(fragmentProgram, "diffuseMap"));
    // And go ahead and draw the scene geometry
    DrawGeometry(gl);

    // Disable the texture now that we're done with it.
    CgGL.cgGLDisableTextureParameter(CgGL.cgGetNamedParameter(fragmentProgram, "diffuseMap"));

    ++curTime;
  }
Example #9
0
  // Releases any remaining OpenGL resources (including CG resources).
  public static void deleteAllGLResources() {
    if (!glTextureObjects.isEmpty()) {
      Object[] glids = glTextureObjects.toArray();
      for (int i = 0; i < glids.length; i++) {
        int id = ((Integer) glids[i]).intValue();
        int[] temp = {id};
        gl.glDeleteTextures(1, temp, 0);
      }
      glTextureObjects.clear();
    }

    if (!glVertexBuffers.isEmpty()) {
      Object[] glids = glVertexBuffers.toArray();
      for (int i = 0; i < glids.length; i++) {
        int id = ((Integer) glids[i]).intValue();
        int[] temp = {id};
        gl.glDeleteBuffersARB(1, temp, 0);
      }
      glVertexBuffers.clear();
    }

    if (!glFrameBuffers.isEmpty()) {
      Object[] glids = glFrameBuffers.toArray();
      for (int i = 0; i < glids.length; i++) {
        int id = ((Integer) glids[i]).intValue();
        int[] temp = {id};
        gl.glDeleteFramebuffersEXT(1, temp, 0);
      }
      glFrameBuffers.clear();
    }

    if (!glRenderBuffers.isEmpty()) {
      Object[] glids = glRenderBuffers.toArray();
      for (int i = 0; i < glids.length; i++) {
        int id = ((Integer) glids[i]).intValue();
        int[] temp = {id};
        gl.glDeleteRenderbuffersEXT(1, temp, 0);
      }
      glRenderBuffers.clear();
    }

    if (!glslPrograms.isEmpty()) {
      Object[] glids = glslPrograms.toArray();
      for (int i = 0; i < glids.length; i++) {
        int id = ((Integer) glids[i]).intValue();
        gl.glDeleteProgram(id);
      }
      glslPrograms.clear();
    }

    if (!glslShaders.isEmpty()) {
      Object[] glids = glslShaders.toArray();
      for (int i = 0; i < glids.length; i++) {
        int id = ((Integer) glids[i]).intValue();
        gl.glDeleteShader(id);
      }
      glslShaders.clear();
    }

    if (!cgContexts.isEmpty()) {
      Object[] glids = cgContexts.toArray();
      for (int i = 0; i < glids.length; i++) {
        Object id = glids[i];
        CgGL.cgDestroyContext((CGcontext) id);
      }
      cgContexts.clear();
    }

    if (!cgPrograms.isEmpty()) {
      Object[] glids = cgPrograms.toArray();
      for (int i = 0; i < glids.length; i++) {
        Object id = glids[i];
        CgGL.cgDestroyProgram((CGprogram) id);
      }
      cgPrograms.clear();
    }

    if (!cgEffects.isEmpty()) {
      Object[] glids = cgEffects.toArray();
      for (int i = 0; i < glids.length; i++) {
        Object id = glids[i];
        CgGL.cgDestroyEffect((CGeffect) id);
      }
      cgEffects.clear();
    }
  }