private void readMesh() throws IOException { InputStream in = ResourceRetriever.getResourceAsStream("demos/data/models/Model.txt"); polyNum = byteToInt(readNextFourBytes(in)); polyData = new Polygon[polyNum]; for (int i = 0; i < polyData.length; i++) { polyData[i] = new Polygon(); for (int j = 0; j < 3; j++) { polyData[i].Verts[j].Nor.X = byteToFloat(readNextFourBytes(in)); polyData[i].Verts[j].Nor.Y = byteToFloat(readNextFourBytes(in)); polyData[i].Verts[j].Nor.Z = byteToFloat(readNextFourBytes(in)); polyData[i].Verts[j].Pos.X = byteToFloat(readNextFourBytes(in)); polyData[i].Verts[j].Pos.Y = byteToFloat(readNextFourBytes(in)); polyData[i].Verts[j].Pos.Z = byteToFloat(readNextFourBytes(in)); } } }
public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); // Storage for the 96 Shader Values ( NEW ) FloatBuffer shaderData = GLBuffers.newDirectFloatBuffer(96); // Start Of User Initialization // Really Nice perspective calculations Light Grey Background gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); gl.glClearColor(0.7f, 0.7f, 0.7f, 0.0f); // Depth Buffer Setup gl.glClearDepth(1.0f); // Enable Depth Testing gl.glEnable(GL2.GL_DEPTH_TEST); // The Type Of Depth Test To Do gl.glDepthFunc(GL2.GL_LESS); // Enables Smooth Color Shading ( NEW ) gl.glShadeModel(GL2.GL_SMOOTH); // Initially Disable Line Smoothing ( NEW ) gl.glDisable(GL2.GL_LINE_SMOOTH); // Enable OpenGL Face Culling ( NEW ) gl.glEnable(GL2.GL_CULL_FACE); // Disable OpenGL Lighting ( NEW ) gl.glDisable(GL2.GL_LIGHTING); StringBuffer readShaderData = new StringBuffer(); try { InputStream inputStream = ResourceRetriever.getResourceAsStream("demos/data/models/Shader.txt"); int info; while ((info = inputStream.read()) != -1) readShaderData.append((char) info); inputStream.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } StringTokenizer tokenizer = new StringTokenizer(readShaderData.toString()); // Loop Though The 32 Greyscale Values while (tokenizer.hasMoreTokens()) { float value = Float.parseFloat(tokenizer.nextToken()); shaderData.put(value); shaderData.put(value); shaderData.put(value); } shaderData.flip(); gl.glGenTextures(1, shaderTexture, 0); // Get A Free Texture ID ( NEW ) gl.glBindTexture(GL2.GL_TEXTURE_1D, shaderTexture[0]); // Bind This // Texture. From // Now On It // Will Be 1D // For Crying Out Loud Don't Let OpenGL Use Bi/Trilinear Filtering! gl.glTexParameteri(GL2.GL_TEXTURE_1D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST); gl.glTexParameteri(GL2.GL_TEXTURE_1D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST); gl.glTexImage1D( GL2.GL_TEXTURE_1D, 0, GL2.GL_RGB, 32, 0, GL2.GL_RGB, GL2.GL_FLOAT, shaderData); // Upload // Set The X Direction lightAngle.X = 0.0f; // Set The Y Direction lightAngle.Y = 0.0f; // Set The Z Direction lightAngle.Z = 1.0f; lightAngle.normalize(); try { // Return The Value Of ReadMesh readMesh(); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Loads a TGA file into memory * * @param gl * @param texture * @param filename * @throws IOException */ private void loadTGA(GL gl, TextureImage texture, String filename) throws IOException { // Used To Compare TGA Header ByteBuffer TGAcompare = GLBuffers.newDirectByteBuffer(12); // Uncompressed TGA Header byte[] TGAheader = new byte[] {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}; ByteBuffer header = GLBuffers.newDirectByteBuffer(6); // First 6 Useful // Bytes // From The // Header int bytesPerPixel, // Holds Number Of Bytes Per Pixel Used In The TGA // File imageSize, // Used To Store The Image Size When Setting Aside Ram type = GL.GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP) ReadableByteChannel file = null; try { file = Channels.newChannel(ResourceRetriever.getResourceAsStream(filename)); readBuffer(file, TGAcompare); readBuffer(file, header); for (int i = 0; i < TGAcompare.capacity(); i++) // Does The Header Match What We Want? if (TGAcompare.get(i) != TGAheader[i]) { throw new IOException("Invalid TGA header"); } texture.width = header.get(1) << 8 | header.get(0); // Determine The // TGA // Width(highbyte*256+lowbyte) texture.height = header.get(3) << 8 | header.get(2); // Determine // The TGA // Height(highbyte*256+lowbyte) if (texture.width <= 0) { // Is The Width Less Than Or Equal To Zero throw new IOException("Image has negative width"); } if (texture.height <= 0) { // Is The Height Less Than Or Equal To // Zero throw new IOException("Image has negative height"); } if (header.get(4) != 24 && header.get(4) != 32) { // Is The TGA 24 // or 32 Bit? throw new IOException("Image is not 24 or 32-bit"); } texture.bpp = header.get(4); // Grab The TGA's Bits Per Pixel (24 or // 32) bytesPerPixel = texture.bpp / 8; // Divide By 8 To Get The Bytes Per // Pixel // Calculate the memory required for the TGA Data imageSize = texture.width * texture.height * bytesPerPixel; // Reserve memory to hold the TGA Data texture.imageData = GLBuffers.newDirectByteBuffer(imageSize); readBuffer(file, texture.imageData); // Loop Through The Image Data for (int i = 0; i < imageSize; i += bytesPerPixel) { // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue) // Temporarily Store The Value At Image Data 'i' byte temp = texture.imageData.get(i); // Set the 1st Byte to the value Of the 3rd Byte texture.imageData.put(i, texture.imageData.get(i + 2)); // Set The 3rd Byte To The Value In 'temp' (1st Byte Value) texture.imageData.put(i + 2, temp); } // Build A Texture From The Data gl.glGenTextures(1, texture.texID, 0); // Generate OpenGL texture // IDs gl.glBindTexture(GL.GL_TEXTURE_2D, texture.texID[0]); // Bind Our // Texture gl.glTexParameterf( GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); // Linear Filtered gl.glTexParameterf( GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); // Linear Filtered if (texture.bpp == 24) // Was The TGA 24 Bits type = GL.GL_RGB; // If So Set The 'type' To GL_RGB gl.glTexImage2D( GL.GL_TEXTURE_2D, 0, type, texture.width, texture.height, 0, type, GL.GL_UNSIGNED_BYTE, texture.imageData); } finally { if (file != null) { try { file.close(); } catch (IOException n) { } } } }