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]); }
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); }
// 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; }