/** * Binds a single color texture of this FBO for rendering. * * <p>You can write to this texture by setting either `gl_FragColor` or `gl_FragData[0]` from your * shaders. While bound for rendering, the specified texture cannot be used for texturing * operations. */ public void bindOne(GL2 gl, int colorTextureIndex) throws OpenGLException { /* Sanity check. */ if (colorTextureIndex < 0 || colorTextureIndex >= getColorTextureCount()) { throw new AssertionError( "Color texture index out of range: required that 0 <= " + colorTextureIndex + " < " + getColorTextureCount() + "."); } /* Save state and adjust viewport if this is the first bind. */ if (!mIsBound) { gl.glPushAttrib(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_VIEWPORT_BIT); gl.glGetIntegerv(GL2.GL_FRAMEBUFFER_BINDING, mPreviousBinding, 0); gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, getHandle()); gl.glViewport(0, 0, mWidth, mHeight); mIsBound = true; } /* Set draw buffer to the requested color attachment. */ gl.glDrawBuffer(GL2.GL_COLOR_ATTACHMENT0 + colorTextureIndex); /* Make sure it worked. */ OpenGLException.checkOpenGLError(gl); }
/** * Binds several specified color textures of this FBO for rendering. * * <p>You can write to them by using the `gl_FragData[]` array from your shaders. While bound for * rendering, the specified textures cannot be used for texturing operations. */ public void bindSome(GL2 gl, int indices[]) throws OpenGLException { /* Save state and adjust viewport if this is the first bind. */ if (!mIsBound) { gl.glPushAttrib(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_VIEWPORT_BIT); gl.glGetIntegerv(GL2.GL_FRAMEBUFFER_BINDING, mPreviousBinding, 0); gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, getHandle()); gl.glViewport(0, 0, mWidth, mHeight); mIsBound = true; } /* Set draw buffers to all color attachments. */ int bindings[] = new int[indices.length]; for (int i = 0; i < indices.length; ++i) { if (indices[i] >= getColorTextureCount()) { throw new OpenGLException( "FBO bind index " + indices[i] + " out of range; only " + getColorTextureCount() + " color textures."); } bindings[i] = GL2.GL_COLOR_ATTACHMENT0 + indices[i]; } gl.glDrawBuffers(indices.length, bindings, 0); /* Make sure it worked. */ OpenGLException.checkOpenGLError(gl); }
/** * Binds a given texture to the first color target This is useful when we want to render to a * dynamic cube map texture. * * <p>You can write to them by using the `gl_FragData[]` array from your shaders. While bound for * rendering, the specified textures cannot be used for texturing operations. */ public void bindGiven(GL2 gl, int textureTarget, int textureHandle, int colorTextureIndex) throws OpenGLException { /* Save state and adjust viewport if this is the first bind. */ if (!mIsBound) { gl.glPushAttrib(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_VIEWPORT_BIT); gl.glGetIntegerv(GL2.GL_FRAMEBUFFER_BINDING, mPreviousBinding, 0); gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, getHandle()); gl.glViewport(0, 0, mWidth, mHeight); mIsBound = true; } /* Attach the given texture */ gl.glFramebufferTexture2D( GL2.GL_FRAMEBUFFER, GL2.GL_COLOR_ATTACHMENT0 + colorTextureIndex, textureTarget, textureHandle, 0); /* Set draw buffer to the requested color attachment. */ gl.glDrawBuffer(GL2.GL_COLOR_ATTACHMENT0 + colorTextureIndex); /* Make sure it worked. */ OpenGLException.checkOpenGLError(gl); }
private void drawStar(GL2 gl, GLAutoDrawable drawable, Float x, Float y, Float z) { // Pushing to matrix stack gl.glPushMatrix(); // STAR // Moves the figure in the (x, y, z)-axis gl.glTranslatef(x, y, z); // BEGIN Star gl.glBegin(GL_LINE_LOOP); // Pushing to attribute stack gl.glPushAttrib(GL_ALL_ATTRIB_BITS); // Set color to GREEN gl.glColor3f(0.0f, 1.0f, 0.0f); // Draw the sides gl.glVertex3f(0.0f, 1.0f, 0.0f); gl.glVertex3f(0.2f, 0.2f, 0.0f); gl.glVertex3f(1.0f, 0.0f, 0.0f); gl.glVertex3f(0.2f, -0.2f, 0.0f); gl.glVertex3f(0.0f, -1.0f, 0.0f); gl.glVertex3f(-0.2f, -0.2f, 0.0f); gl.glVertex3f(-1.0f, 0.0f, 0.0f); gl.glVertex3f(-0.2f, 0.2f, 0.0f); // END Star gl.glEnd(); // Popping state gl.glPopAttrib(); gl.glPopMatrix(); }
private void drawSquare(GL2 gl, GLAutoDrawable drawable, Float x, Float y, Float z) { // Pushing to matrix stack gl.glPushMatrix(); // SQUARE // Moves the figure in the (x, y, z)-axis gl.glTranslatef(x, y, z); // Makes so that square is filled in gl.glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // BEGIN Square gl.glBegin(GL_QUADS); // Pushing to attribute stack gl.glPushAttrib(GL_ALL_ATTRIB_BITS); // Set color to BLUE gl.glColor3f(0.0f, 0.0f, 1.0f); // The Quad gl.glVertex3f(1.0f, 1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); gl.glVertex3f(1.0f, -1.0f, 1.0f); // END Square gl.glEnd(); // Popping state gl.glPopAttrib(); gl.glPopMatrix(); }
/** Pushes eval bit */ public void bgnmap2f() { if (output_triangles) { // TODO outp triangles surfaceevaluator bgnmap2f // System.out.println("TODO surfaceevaluator.bgnmap2f output triangles"); } else { gl.glPushAttrib(GL2.GL_EVAL_BIT); // System.out.println("TODO surfaceevaluator.bgnmap2f glgetintegerv"); } }
/** * Render the arrow as a line and a _head. * * @param gl OpenGL Context */ public void render(GL2 gl) { // save Light Attributes and remove lighting to get "full" RGB colors // gl.glPushAttrib( GL2.GL_LIGHTING_BIT ); gl.glPushAttrib(GL2.GL_ENABLE_BIT); gl.glDisable(GL2.GL_LIGHTING); if (_fg_verb) { System.out.println( "Arrow pos=" + _pos.toString() + " angZ1=" + _angZ1 + " angY2=" + _angY2 + " length=" + _length); _fg_verb = false; } // save transformation matrix, then translate and scale. gl.glPushMatrix(); gl.glTranslatef(_pos.x, _pos.y, _pos.z); gl.glRotatef(Matrix.rad2Deg(_angZ1), 0, 0, 1); // rotation around 0z gl.glRotatef(Matrix.rad2Deg(_angY2), 0, 1, 0); // rotation around 0y // draw line gl.glColor4f(_color_fg.x, _color_fg.y, _color_fg.z, _color_fg.w); gl.glLineWidth(1.0f); gl.glBegin(GL.GL_LINES); { gl.glVertex3f(0, 0, 0); gl.glVertex3f(_length, 0, 0); } gl.glEnd(); // draw head gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2GL3.GL_FILL); gl.glTranslatef(_length, 0, 0); gl.glBegin(GL.GL_TRIANGLES); { for (float[] face : _head_faces) { gl.glVertex3f(face[0], face[1], face[2]); gl.glVertex3f(face[3], face[4], face[5]); gl.glVertex3f(face[6], face[7], face[8]); } } gl.glEnd(); // restore transformation matrix gl.glPopMatrix(); // restore attributes gl.glPopAttrib(); }
// Support Methods private void drawPyramid(GL2 gl, GLAutoDrawable drawable, Float x, Float y, Float z) { // Pushing to matrix stack gl.glPushMatrix(); // PYRAMID // Moves the figure in the (x, y, z)-axis gl.glTranslatef(x, y, z); // Rotates the pyramid, just for "fun" gl.glRotatef(-55.0f, 0.0f, 1.0f, 0.0f); // Makes only the outlines gl.glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // BEGIN Pyramid gl.glBegin(GL_TRIANGLES); // Pushing current color to stack gl.glPushAttrib(GL_ALL_ATTRIB_BITS); // Set color to RED gl.glColor3f(1.0f, 0.0f, 0.0f); // Front triangle gl.glVertex3f(0.0f, 1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); gl.glVertex3f(1.0f, -1.0f, 1.0f); // Right triangle gl.glVertex3f(0.0f, 1.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, 1.0f); gl.glVertex3f(1.0f, -1.0f, -1.0f); // Left triangle gl.glVertex3f(0.0f, 1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Back triangle gl.glVertex3f(0.0f, 1.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, -1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f); // END Pyramid gl.glEnd(); // Popping state gl.glPopAttrib(); gl.glPopMatrix(); }
/** * Binds all color textures of this FBO for rendering. * * <p>You can write to them by using the `gl_FragData[]` array from your shaders. While bound for * rendering, the specified textures cannot be used for texturing operations. */ public void bindAll(GL2 gl) throws OpenGLException { /* Save state and adjust viewport if this is the first bind. */ if (!mIsBound) { gl.glPushAttrib(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_VIEWPORT_BIT); gl.glGetIntegerv(GL2.GL_FRAMEBUFFER_BINDING, mPreviousBinding, 0); gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, getHandle()); gl.glViewport(0, 0, mWidth, mHeight); mIsBound = true; } /* Set draw buffers to all color attachments. */ int bindings[] = new int[getColorTextureCount()]; for (int i = 0; i < getColorTextureCount(); ++i) { bindings[i] = GL2.GL_COLOR_ATTACHMENT0 + i; } gl.glDrawBuffers(getColorTextureCount(), bindings, 0); /* Make sure it worked. */ OpenGLException.checkOpenGLError(gl); }
@Override public void display(GLAutoDrawable drawable) { gl = drawable.getGL().getGL2(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); // desenha ou nao a bounding sphere de cada objeto if (InputHandler.getInstance().isKeyPressed(KeyEvent.VK_B)) { // tecla B BoundingVolume.isVisible = !BoundingVolume.isVisible; } // habilita ou nao fog if (InputHandler.getInstance().isKeyPressed(KeyEvent.VK_F)) { // tecla F fogEnabled = !fogEnabled; if (fogEnabled) gl.glEnable(GL2.GL_FOG); else gl.glDisable(GL2.GL_FOG); } // atualiza camera camera.update(); // atualiza objetos { Iterator<SceneObject> it = Scene.getInstance().getSceneObjectsIterator(); while (it.hasNext()) { SceneObject so = it.next(); // animacao da porta if (so.getName().contains("door")) { if (Collision.collide( camera.getSceneObject().getBoundingSphere(), so.getBoundingSphere())) so.setAnimating(true); else so.setAnimating(false); } so.update(); } } glu.gluLookAt( FPCamera.position.x, FPCamera.position.y, FPCamera.position.z, FPCamera.position.x + FPCamera.look.x, FPCamera.position.y + FPCamera.look.y, FPCamera.position.z + FPCamera.look.z, FPCamera.up.x, FPCamera.up.y, FPCamera.up.z); if (FPCamera.changed) { // se direcao da camera mudar FPCamera.changed = false; frustum.generate(drawable); // gera o frustum Scene.getInstance().clearVisibleObjectsList(); Iterator<SceneObject> it = Scene.getInstance().getSceneObjectsIterator(); while (it.hasNext()) { SceneObject so = it.next(); if (frustum.sphereInFrustum(so.getBoundingSphere())) Scene.getInstance().addVisibleObject(so); } // System.out.println("Renderable objects: "+ Scene.getInstance().getSize()); // System.out.println(FPCamera.position); } // desenha objetos { Iterator<SceneObject> it = Scene.getInstance().getVisibleObjectsIterator(); SceneObject so; while (it.hasNext()) { so = it.next(); if (so.getName().contains("lake")) { gl.glPushAttrib(GL2.GL_ENABLE_BIT); gl.glEnable(GL.GL_BLEND); gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE); so.draw(drawable); gl.glPopAttrib(); continue; } so.draw(drawable); } } // int e = gl.glGetError(); // if(e != GL.GL_NO_ERROR) { // System.err.println("Erro: "+ gl.glGetString(e)); // } // calculo do fps ++frame; long time = System.currentTimeMillis(); elapsed = time - timebase; if (elapsed > 1000) { long fps = frame * 1000 / (time - timebase); timebase = time; frame = 0; String str = "FPS: " + fps; mainFrame.setTitle(str); } // gl.glFlush(); }