public static Matrix lookAtLH(Vector eye, Vector target, Vector up) { Matrix res = new Matrix(); Vector zaxis = Vector.sub(target, eye).normalize(); if (zaxis.length() == 0) zaxis.Z = 1; Vector xaxis = Vector.cross(up, zaxis).normalize(); if (xaxis.length() == 0) { zaxis.X += 0.000001; xaxis = Vector.cross(up, zaxis).normalize(); } Vector yaxis = Vector.cross(zaxis, xaxis); res.m[0] = xaxis.X; res.m[1] = xaxis.Y; res.m[2] = xaxis.Z; res.m[3] = -Vector.dot(xaxis, eye); res.m[4] = yaxis.X; res.m[5] = yaxis.Y; res.m[6] = yaxis.Z; res.m[7] = -Vector.dot(yaxis, eye); res.m[8] = zaxis.X; res.m[9] = zaxis.Y; res.m[10] = zaxis.Z; res.m[11] = -Vector.dot(zaxis, eye); // already set during initialization of matrix /* * res.m[12] = 0; res.m[13] = 0; res.m[14] = 0; res.m[15] = 1; */ return res; }
public void rotateVector(Vector V, Vector D) { // Rotate A Vector Using // The Supplied Matrix ( // NEW ) // Rotate Around The X Axis ( NEW ) D.X = (Data[0] * V.X) + (Data[4] * V.Y) + (Data[8] * V.Z); // Rotate Around The Y Axis ( NEW) D.Y = (Data[1] * V.X) + (Data[5] * V.Y) + (Data[9] * V.Z); // Rotate Around The Z Axis ( NEW ) D.Z = (Data[2] * V.X) + (Data[6] * V.Y) + (Data[10] * V.Z); }
public void display(GLAutoDrawable drawable) { update(); GL2 gl = drawable.getGL().getGL2(); // Clear Color Buffer, Depth Buffer gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); Matrix TmpMatrix = new Matrix(); // Temporary MATRIX Structure ( NEW ) Vector TmpVector = new Vector(), TmpNormal = new Vector(); // Temporary // VECTOR // Structures // ( NEW ) gl.glLoadIdentity(); // Reset The Matrix if (outlineSmooth) { // Check To See If We Want Anti-Aliased Lines ( NEW // ) gl.glHint(GL2.GL_LINE_SMOOTH_HINT, GL2.GL_NICEST); // Use The Good // Calculations // ( NEW ) gl.glEnable(GL2.GL_LINE_SMOOTH); // Enable Anti-Aliasing ( NEW ) } else // We Don't Want Smooth Lines ( NEW ) gl.glDisable(GL2.GL_LINE_SMOOTH); // Disable Anti-Aliasing ( NEW ) gl.glTranslatef(0.0f, 0.0f, -2.0f); // Move 2 Units Away From The Screen // ( NEW ) gl.glRotatef(modelAngle, 0.0f, 1.0f, 0.0f); // Rotate The Model On It's // Y-Axis ( NEW ) gl.glGetFloatv(GLMatrixFunc.GL_MODELVIEW_MATRIX, TmpMatrix.Data, 0); // Get // The // Generated // Matrix // ( // NEW // ) // Cel-Shading Code // gl.glEnable(GL2.GL_TEXTURE_1D); // Enable 1D Texturing ( NEW ) gl.glBindTexture(GL2.GL_TEXTURE_1D, shaderTexture[0]); // Bind Our // Texture ( NEW // ) gl.glColor3f(1.0f, 1.0f, 1.0f); // Set The Color Of The Model ( NEW ) gl.glBegin(GL2.GL_TRIANGLES); // Tell OpenGL That We're Drawing // Triangles // Loop Through Each Polygon for (int i = 0; i < polyNum; i++) // Loop Through Each Vertex for (int j = 0; j < 3; j++) { // Fill Up The TmpNormal Structure With TmpNormal.X = polyData[i].Verts[j].Nor.X; // The Current Vertices' Normal Values TmpNormal.Y = polyData[i].Verts[j].Nor.Y; TmpNormal.Z = polyData[i].Verts[j].Nor.Z; // Rotate This By The Matrix TmpMatrix.rotateVector(TmpNormal, TmpVector); // Normalize The New Normal TmpVector.normalize(); // Calculate The Shade Value float TmpShade = Vector.dotProduct(TmpVector, lightAngle); // Clamp The Value to 0 If Negative ( NEW ) if (TmpShade < 0.0f) { TmpShade = 0.0f; } // Set The Texture Co-ordinate As The Shade Value gl.glTexCoord1f(TmpShade); // Send The Vertex Position gl.glVertex3f( polyData[i].Verts[j].Pos.X, polyData[i].Verts[j].Pos.Y, polyData[i].Verts[j].Pos.Z); } gl.glEnd(); // Tell OpenGL To Finish Drawing gl.glDisable(GL2.GL_TEXTURE_1D); // Disable 1D Textures ( NEW ) // Outline Code // Check To See If We Want To Draw The Outline if (outlineDraw) { // Enable Blending gl.glEnable(GL2.GL_BLEND); // Set The Blend Mode gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA); // Draw Backfacing Polygons As Wireframes gl.glPolygonMode(GL2.GL_BACK, GL2.GL_LINE); // Set The Line Width gl.glLineWidth(outlineWidth); // Don't Draw Any Front-Facing Polygons gl.glCullFace(GL2.GL_FRONT); // Change The Depth Mode gl.glDepthFunc(GL2.GL_LEQUAL); // Set The Outline Color gl.glColor3fv(outlineColor, 0); // Tell OpenGL What We Want To Draw gl.glBegin(GL2.GL_TRIANGLES); // Loop Through Each Polygon for (int i = 0; i < polyNum; i++) { // Loop Through Each Vertex for (int j = 0; j < 3; j++) { // Send The Vertex Position gl.glVertex3f( polyData[i].Verts[j].Pos.X, polyData[i].Verts[j].Pos.Y, polyData[i].Verts[j].Pos.Z); } } gl.glEnd(); // Tell OpenGL We've Finished // Reset The Depth-Testing Mode gl.glDepthFunc(GL2.GL_LESS); // Reset The Face To Be Culled gl.glCullFace(GL2.GL_BACK); // Reset Back-Facing Polygon Drawing Mode gl.glPolygonMode(GL2.GL_BACK, GL2.GL_FILL); // Disable Blending gl.glDisable(GL2.GL_BLEND); } // Check To See If Rotation Is Enabled if (modelRotate) { // Update Angle Based On The Clock modelAngle += .2f; } }
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); } }