@Override public void draw(final GL2 gl, final RobotState rs, final Camera camera) { if (rs.getLastMessage() != null && rs.getLastMessage() instanceof BHumanMessage) { final BHumanMessage msg = (BHumanMessage) rs.getLastMessage(); final Whistle whistle = msg.queue.getMessage(Whistle.class); if (whistle != null && whistle.confidenceOfLastWhistleDetection > 50 && whistle.lastTimeWhistleDetected >= msg.queue.getTimestamp() - 200) { gl.glPushMatrix(); gl.glTranslatef(msg.pose[0] / 1000.f, msg.pose[1] / 1000.f, 1); camera.turnTowardsCamera(gl); try { final File f = new File("plugins/05/resources/whistle.png").getAbsoluteFile(); Image.drawImage(gl, TextureLoader.getInstance().loadTexture(gl, f), 0, 0, 0.2f); } catch (IOException ex) { JOptionPane.showMessageDialog( null, "Error loading texture: " + ex.getMessage(), ex.getClass().getSimpleName(), JOptionPane.ERROR_MESSAGE); } gl.glPopMatrix(); } } }
/** * //! Draw a pretty arrow on the z-axis using a cone and a cylinder (using GLUT) * * @param aArrowStart * @param aArrowTip * @param aWidth */ public static void jDrawArrow( final JVector3d aArrowStart, final JVector3d aArrowTip, final double aWidth) { GL2 gl = GLContext.getCurrent().getGL().getGL2(); gl.glPushMatrix(); // We don't really care about the up vector, but it can't // be parallel to the arrow... JVector3d up = new JVector3d(0, 1, 0); // JVector3d arrow = aArrowTip-aArrowStart; JVector3d arrow = new JVector3d(0, 0, 0); arrow.normalize(); double d = Math.abs(JMaths.jDot(up, arrow)); if (d > .9) { up = new JVector3d(1, 0, 0); } JMatrixGL.jLookAt(gl, aArrowStart, aArrowTip, up); double distance = JMaths.jDistance(aArrowTip, aArrowStart); // This flips the z axis around gl.glRotatef(180, 1, 0, 0); // create a new OpenGL quadratic object GLUquadric quadObj; quadObj = glu.gluNewQuadric(); // set rendering style glu.gluQuadricDrawStyle(quadObj, GLU.GLU_FILL); // set normal-rendering mode glu.gluQuadricNormals(quadObj, GLU.GLU_SMOOTH); // render a cylinder and a cone gl.glRotatef(180, 1, 0, 0); glu.gluDisk(quadObj, 0, aWidth, 10, 10); gl.glRotatef(180, 1, 0, 0); glu.gluCylinder(quadObj, aWidth, aWidth, distance * ARROW_CYLINDER_PORTION, 10, 10); gl.glTranslated(0, 0, ARROW_CYLINDER_PORTION * distance); gl.glRotatef(180, 1, 0, 0); glu.gluDisk(quadObj, 0, aWidth * 2.0, 10, 10); gl.glRotatef(180, 1, 0, 0); glu.gluCylinder(quadObj, aWidth * 2.0, 0.0, distance * ARRROW_CONE_PORTION, 10, 10); // delete our quadric object glu.gluDeleteQuadric(quadObj); gl.glPopMatrix(); }
/** * Draws the body. * * <p>Only coded for polygons. * * @param gl the OpenGL graphics context */ public void render(GL2 gl) { // save the original transform gl.glPushMatrix(); // transform the coordinate system from world coordinates to local coordinates gl.glTranslated(this.transform.getTranslationX(), this.transform.getTranslationY(), 0.0); // rotate about the z-axis gl.glRotated(Math.toDegrees(this.transform.getRotation()), 0.0, 0.0, 1.0); // loop over all the body fixtures for this body for (BodyFixture fixture : this.fixtures) { // get the shape on the fixture Convex convex = fixture.getShape(); // check the shape type if (convex instanceof Polygon) { // since Triangle, Rectangle, and Polygon are all of // type Polygon in addition to their main type Polygon p = (Polygon) convex; // set the color gl.glColor4fv(this.color, 0); // fill the shape gl.glBegin(GL2.GL_POLYGON); for (Vector2 v : p.getVertices()) { gl.glVertex3d(v.x, v.y, 0.0); } gl.glEnd(); // set the color gl.glColor4f(this.color[0] * 0.8f, this.color[1] * 0.8f, this.color[2] * 0.8f, 1.0f); // draw the shape gl.glBegin(GL.GL_LINE_LOOP); for (Vector2 v : p.getVertices()) { gl.glVertex3d(v.x, v.y, 0.0); } gl.glEnd(); } // circles and other curved shapes require a little more work, so to keep // this example short we only include polygon shapes; see the RenderUtilities // in the Sandbox application } // set the original transform gl.glPopMatrix(); }
@Override public void drawGl(GL2 gl) { // Remember current state of the render system gl.glPushMatrix(); // Apply translation gl.glTranslatef( (float) translation.get(0), (float) translation.get(1), (float) translation.get(2)); // Draw all children for (int childIndex = 0; childIndex < getNumberOfChildren(); childIndex++) { getChildNode(childIndex).drawGl(gl); } // Restore original state gl.glPopMatrix(); }
public void draw(GL2 gl) { GLUT glut = new GLUT(); // Color gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, new float[] {1, 1, 1, 1}, 0); gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_EMISSION, new float[] {0, 0, 0, 1}, 0); gl.glPushMatrix(); gl.glTranslated(x, y, z); glut.glutSolidCylinder(0.05, 0.5, 12, 1); gl.glTranslated(0, 0, 0.5); gl.glRotated(horizontalDir * 180 / Math.PI + 180, 0, 0, 1); gl.glRotated(verticalDir * 180 / Math.PI, 0, 1, 0); ModelTurret.draw(gl); gl.glPopMatrix(); }
@Override public void display(final GLAutoDrawable gLDrawable) { final GL2 gl = gLDrawable.getGL().getGL2(); enableStates(gl, true); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glPushMatrix(); gl.glRotatef(angleZ, 0.0f, 1.0f, 0.0f); gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f); glut.glutSolidTeapot(2.0f); gl.glPopMatrix(); gl.glFlush(); if (angleZ >= 180.0f) { rotDir = -1.0f; } else if (angleZ <= 0.0f) { rotDir = +1.0f; } angleZ += rotIncr * rotDir; enableStates(gl, false); }
private void renderRedTeapot(GL2 gl, float x, float y, float z) { float mat[] = new float[4]; gl.glPushMatrix(); gl.glTranslatef(x, y, z); mat[0] = 0.1745f; mat[1] = 0.01175f; mat[2] = 0.01175f; mat[3] = 1.0f; gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat, 0); mat[0] = 0.61424f; mat[1] = 0.04136f; mat[2] = 0.04136f; gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat, 0); mat[0] = 0.727811f; mat[1] = 0.626959f; mat[2] = 0.626959f; gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat, 0); gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 0.6f * 128.0f); glut.glutSolidTeapot(1.0); gl.glPopMatrix(); }
/** * //! Draw an x-y-z frame. * * @param aAxisLengthScale * @param aAxisThicknessScale * @param aModifyMaterialState */ public static void jDrawFrame( final double aAxisLengthScale, final double aAxisThicknessScale, final boolean aModifyMaterialState) { // Triangle vertices: int nTriangles = 8; // Quad vertices: int nQuads = 16; GL2 gl = GLContext.getCurrent().getGL().getGL2(); // set material properties float[] fnull = {0, 0, 0, 0}; gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, fnull, 0); gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_EMISSION, fnull, 0); gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE); gl.glEnable(GL2.GL_COLOR_MATERIAL); gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL); // enable vertex and normal arrays gl.glEnableClientState(GL2.GL_NORMAL_ARRAY); gl.glEnableClientState(GL2.GL_VERTEX_ARRAY); if (aModifyMaterialState) { gl.glEnable(GL2.GL_COLOR_MATERIAL); gl.glColorMaterial(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE); gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL); } for (int k = 0; k < 3; k++) { gl.glPushMatrix(); // Rotate to the appropriate axis if (k == 0) { gl.glRotatef(-90.0f, 0, 1, 0); gl.glColor3f(1.0f, 0.0f, 0.0f); } else if (k == 1) { gl.glRotatef(90.0f, 1, 0, 0); gl.glColor3f(0.0f, 1.0f, 0.0f); } else { gl.glRotatef(180.0f, 1, 0, 0); gl.glColor3f(0.0f, 0.0f, 1.0f); } // scaling gl.glScaled(aAxisThicknessScale, aAxisThicknessScale, aAxisLengthScale); // render frame object gl.glVertexPointer(3, GL2.GL_FLOAT, 0, triVerticesBuffer); gl.glNormalPointer(GL2.GL_FLOAT, 0, triNormalsBuffer); gl.glDrawArrays(GL2.GL_TRIANGLES, 0, nTriangles * 3); gl.glVertexPointer(3, GL2.GL_FLOAT, 0, quadVerticesBuffer); gl.glNormalPointer(GL2.GL_FLOAT, 0, quadNormalsBuffer); gl.glDrawArrays(GL2.GL_QUADS, 0, nQuads * 4); gl.glPopMatrix(); } // disable vertex and normal arrays gl.glDisableClientState(GL2.GL_NORMAL_ARRAY); gl.glDisableClientState(GL2.GL_VERTEX_ARRAY); }
@Override public void display(GLAutoDrawable drawable) { GL2 gl2 = drawable.getGL().getGL2(); GLU glu = GLU.createGLU(); gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); gl2.glEnable(GL2.GL_DEPTH_TEST); gl2.glDisable(GL2.GL_CULL_FACE); gl2.glMatrixMode(GL2.GL_PROJECTION); gl2.glLoadIdentity(); glu.gluPerspective(60, drawable.getSurfaceWidth() / drawable.getSurfaceHeight(), 0.1f, 100); gl2.glMatrixMode(GL2.GL_MODELVIEW); gl2.glLoadIdentity(); camera_.apply(gl2, glu); light_.apply(gl2, glu); terrain_.draw(gl2, glu); Runnable triangle = () -> { gl2.glBegin(GL2.GL_TRIANGLES); { gl2.glColor3f(1, 1, 1); gl2.glNormal3f(0, 0, 1); gl2.glVertex3f(0, 1, 0); gl2.glColor3f(1, 0, 0); gl2.glNormal3f(0, 0, 1); gl2.glVertex3f(-0.87f, -0.5f, 0); gl2.glColor3f(0, 0, 1); gl2.glNormal3f(0, 0, 1); gl2.glVertex3f(0.87f, -0.5f, 0); gl2.glColor3f(1, 1, 1); gl2.glNormal3f(0.71898836f, 0.4170133f, -0.5560177f); gl2.glVertex3f(0, 1, 0); gl2.glColor3f(0, 0, 1); gl2.glNormal3f(0.71898836f, 0.4170133f, -0.5560177f); gl2.glVertex3f(0.87f, -0.5f, 0); gl2.glColor3f(0, 1, 0); gl2.glNormal3f(0.71898836f, 0.4170133f, -0.5560177f); gl2.glVertex3f(0, 0, -0.75f); gl2.glColor3f(1, 0, 0); gl2.glNormal3f(-0.7189883f, 0.41701326f, -0.5560177f); gl2.glVertex3f(-0.87f, -0.5f, 0); gl2.glColor3f(1, 1, 1); gl2.glNormal3f(-0.7189883f, 0.41701326f, -0.5560177f); gl2.glVertex3f(0, 1, 0); gl2.glColor3f(0, 1, 0); gl2.glNormal3f(-0.7189883f, 0.41701326f, -0.5560177f); gl2.glVertex3f(0, 0, -0.75f); gl2.glColor3f(0, 0, 1); gl2.glNormal3f(0, -1.305f, -0.87f); gl2.glVertex3f(0.87f, -0.5f, 0); gl2.glColor3f(1, 0, 0); gl2.glNormal3f(0, -1.305f, -0.87f); gl2.glVertex3f(-0.87f, -0.5f, 0); gl2.glColor3f(0, 1, 0); gl2.glNormal3f(0, -1.305f, -0.87f); gl2.glVertex3f(0, 0, -0.75f); } gl2.glEnd(); }; gl2.glPushMatrix(); { gl2.glPushMatrix(); gl2.glTranslatef(0, 0, 0); gl2.glRotatef(angle__, 0, 1, 0); triangle.run(); gl2.glPopMatrix(); gl2.glPushMatrix(); gl2.glTranslatef(0, 3.2f, 0); gl2.glRotatef(angle__ + 30, 0, 1, 0); triangle.run(); gl2.glPopMatrix(); gl2.glPushMatrix(); gl2.glTranslatef(3.2f, 0, 0); gl2.glRotatef(angle__ + 60, 0, 1, 0); triangle.run(); gl2.glPopMatrix(); } gl2.glPopMatrix(); angle__ += 1; gl2.glPushMatrix(); gl2.glTranslatef(0.05f, -1.5f, 5); gl2.glScalef(0.05f, 0.05f, 0.05f); for (Face face : glock3__.faces) { gl2.glBegin(GL2.GL_POLYGON); for (FaceVertex vertex : face.vertices) { gl2.glColor3f(1, 1, 1); gl2.glNormal3f(vertex.n.x, vertex.n.y, vertex.n.z); gl2.glVertex3f(vertex.v.x, vertex.v.y, vertex.v.z); } gl2.glEnd(); } gl2.glPopMatrix(); }
@Override public void display(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); gl.glShadeModel(GL2.GL_SMOOTH); // Enable Smooth Shading (Gouraud) // Display the scene according to the current trackball orientation, scale the scene to fit // better tbc.prepareForDisplay(drawable); gl.glScaled(3, 3, 3); // Add a light int lightNumber = 0; float[] position = {lightPos.x, lightPos.y, lightPos.z, 1}; float[] colour = { lightColR.getFloatValue(), lightColG.getFloatValue(), lightColB.getFloatValue(), 1 }; float[] acolour = { colour[0] * ambient.getFloatValue(), colour[1] * ambient.getFloatValue(), colour[2] * ambient.getFloatValue() }; gl.glLightfv(GL2.GL_LIGHT0 + lightNumber, GL2.GL_SPECULAR, colour, 0); gl.glLightfv(GL2.GL_LIGHT0 + lightNumber, GL2.GL_DIFFUSE, colour, 0); gl.glLightfv(GL2.GL_LIGHT0 + lightNumber, GL2.GL_AMBIENT, acolour, 0); gl.glLightfv( GL2.GL_LIGHT0 + lightNumber, GL2.GL_POSITION, position, 0); // transformed by the modelview matrix when glLight is called gl.glEnable(GL2.GL_LIGHT0 + lightNumber); // Determine which shader to display the scene with ShaderState customShader = null; if (viewingMode == 1) { stateCheckerboard.useProgram(gl, false); stateWoodcut.useProgram(gl, false); stateToon.useProgram(gl, false); statePerFragment.useProgram(gl, usePerFragment); if (usePerFragment) customShader = statePerFragment; else customShader = null; } else if (viewingMode == 2) { statePerFragment.useProgram(gl, false); stateWoodcut.useProgram(gl, false); stateToon.useProgram(gl, false); stateCheckerboard.useProgram(gl, useCheckerboard); if (useCheckerboard) customShader = stateCheckerboard; else customShader = null; } else if (viewingMode == 3) { statePerFragment.useProgram(gl, false); stateCheckerboard.useProgram(gl, false); stateToon.useProgram(gl, false); stateWoodcut.useProgram(gl, useWoodcut); if (useWoodcut) customShader = stateWoodcut; else customShader = null; } else if (viewingMode == 4) { statePerFragment.useProgram(gl, false); stateCheckerboard.useProgram(gl, false); stateWoodcut.useProgram(gl, false); stateToon.useProgram(gl, useToon); if (useToon) customShader = stateToon; else customShader = null; } // Setup the uniform values that may be used within the current shader if (customShader != null) { // Blinn-Phong uniforms int specExponentUniformLocation = customShader.getUniformLocation(gl, "shininess"); gl.glUniform1f(specExponentUniformLocation, shininess.getFloatValue()); int ambientUniformLocation = customShader.getUniformLocation(gl, "ambient"); gl.glUniform1f(ambientUniformLocation, ambient.getFloatValue()); // Checkerboard uniforms int colour1UniformLocation = customShader.getUniformLocation(gl, "Color1"); float[] colour1 = {col1R.getFloatValue(), col1G.getFloatValue(), col1B.getFloatValue()}; gl.glUniform3fv(colour1UniformLocation, 1, colour1, 0); int colour2UniformLocation = customShader.getUniformLocation(gl, "Color2"); float[] colour2 = {col2R.getFloatValue(), col2G.getFloatValue(), col2B.getFloatValue()}; gl.glUniform3fv(colour2UniformLocation, 1, colour2, 0); int avgColourUniformLocation = customShader.getUniformLocation(gl, "AvgColor"); float[] avgColour = { (colour1[0] + colour2[0]) / 2, (colour1[1] + colour2[1]) / 2, (colour1[2] + colour2[2]) / 2 }; gl.glUniform3fv(avgColourUniformLocation, 1, avgColour, 0); int frequencyUniformLocation = customShader.getUniformLocation(gl, "Frequency"); double frequencyValue = frequency.getValue(); frequencyValue = useIntFrequency.getValue() ? Math.round(frequencyValue) : frequencyValue; gl.glUniform1f(frequencyUniformLocation, (float) frequencyValue); int useAveragingUniformLocation = customShader.getUniformLocation(gl, "UseAveraging"); gl.glUniform1i(useAveragingUniformLocation, useAveraging.getValue() ? 1 : 0); int useSmoothStepUniformLocation = customShader.getUniformLocation(gl, "UseSmoothStep"); gl.glUniform1i(useSmoothStepUniformLocation, useSmoothStep.getValue() ? 1 : 0); // Woodcut uniforms int timeUniformLocation = customShader.getUniformLocation(gl, "Time"); gl.glUniform1f(timeUniformLocation, time.getFloatValue()); int lightPositionUniformLocation = customShader.getUniformLocation(gl, "LightPosition"); float[] lightPosition = {lightPos.x, lightPos.y, lightPos.z}; gl.glUniform3fv(lightPositionUniformLocation, 1, lightPosition, 0); // Toon uniforms int threshHighUniformLocation = customShader.getUniformLocation(gl, "ThresholdHigh"); gl.glUniform1f(threshHighUniformLocation, thresholdHigh.getFloatValue()); int threshMedUniformLocation = customShader.getUniformLocation(gl, "ThresholdMedium"); gl.glUniform1f(threshMedUniformLocation, thresholdMedium.getFloatValue()); int threshLowUniformLocation = customShader.getUniformLocation(gl, "ThresholdLow"); gl.glUniform1f(threshLowUniformLocation, thresholdLow.getFloatValue()); } // Draw teapot gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, new float[] {1, 1, 0, 1}, 0); gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, new float[] {1, 1, 1, 1}, 0); gl.glMaterialf(GL.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 50); glut.glutSolidTeapot(1); // Draw table gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, new float[] {0, 1, 1, 1}, 0); gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, new float[] {1, 1, 1, 1}, 0); gl.glMaterialf(GL.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 50); gl.glPushMatrix(); gl.glTranslated(0, -0.8, 0); gl.glBegin(GL2.GL_TRIANGLE_FAN); gl.glNormal3d(0, 1, 0); gl.glTexCoord2d(0, 0); gl.glVertex3d(-10, 0, -10); gl.glTexCoord2d(0, 1); gl.glVertex3d(-10, 0, 10); gl.glTexCoord2d(1, 1); gl.glVertex3d(10, 0, 10); gl.glTexCoord2d(1, 0); gl.glVertex3d(10, 0, -10); gl.glEnd(); gl.glPopMatrix(); }
@Override public void draw(final GL2 gl, final boolean picking) { JOGLRenderer renderer = drawer.getRenderer(); gl.glColor3d(1.0, 1.0, 1.0); if (picking) { if (renderer.colorPicking) { gl.glDisable(GL.GL_DITHER); gl.glDisable(GLLightingFunc.GL_LIGHTING); gl.glDisable(GL.GL_TEXTURE); int viewport[] = new int[4]; gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0); FloatBuffer pixels = FloatBuffer.allocate(4); gl.glReadPixels( renderer.camera.getLastMousePressedPosition().x, viewport[3] - renderer.camera.getLastMousePressedPosition().y, 1, 1, GL.GL_RGBA, GL.GL_FLOAT, pixels); gl.glEnable(GL.GL_DITHER); gl.glEnable(GLLightingFunc.GL_LIGHTING); gl.glEnable(GL.GL_TEXTURE); // Color index = new Color(pixels.get(0), pixels.get(1), pixels.get(2)); // System.out.println("color picked " + index.toString()); } else { gl.glPushMatrix(); gl.glInitNames(); gl.glPushName(0); double alpha = 0d; int size = objects.size(); double delta = size == 0 ? 0 : 1d / size; for (final List<T> list : objects) { alpha = alpha + delta; for (T object : list) { if (isFading) { double originalAlpha = object.getAlpha(); object.setAlpha(originalAlpha * alpha); object.draw(gl, drawer, picking); object.setAlpha(originalAlpha); } else { object.draw(gl, drawer, picking); } } } gl.glPopName(); gl.glPopMatrix(); } } else if (drawAsList) { Integer index = openGLListIndex; if (index == null) { index = gl.glGenLists(1); gl.glNewList(index, GL2.GL_COMPILE); double alpha = 0d; int size = objects.size(); double delta = size == 0 ? 0 : 1d / size; for (final List<T> list : objects) { alpha = alpha + delta; for (T object : list) { if (isFading) { double originalAlpha = object.getAlpha(); object.setAlpha(originalAlpha * alpha); object.draw(gl, drawer, picking); object.setAlpha(originalAlpha); } else { object.draw(gl, drawer, picking); } } } gl.glEndList(); } gl.glCallList(index); openGLListIndex = index; } else if (drawAsVBO) { if (vah == null) { GLU glu = renderer.getGlu(); vah = new VertexArrayHandler(gl, glu, renderer); vah.buildVertexArray(getObjects()); } else { // vah.loadCollada(null); } } else { for (final T object : getObjects()) { object.draw(gl, drawer, picking); } } }