I'm trying to get a simple triangle drawn in Java using LWJGL.
I'm trying to get a simple triangle up, each with a corner of one specific color. Right now it is just giving me a blank screen.
Here is my code:
package com.ex;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.input.Keyboard;
public class ColoredTriangle {
public void start() {
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
// Init OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 640, 480, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
boolean quit = false;
while (!quit) {
// Clear the screen.
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
// Begin drawing
GL11.glBegin(GL11.GL_TRIANGLES);
// Top & Red
GL11.glColor3f(1.0f, 0.0f, 0.0f);
GL11.glVertex2f(0.0f, 1.0f);
// Right & Green
GL11.glColor3f(0.0f, 1.0f, 0.0f);
GL11.glVertex2f(1.0f, 1.0f);
// Left & Blue
GL11.glColor3f(0.0f, 0.0f, 1.0f);
GL11.glVertex2f(1.0f, -1.0f);
GL11.glEnd();
Display.update();
if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
quit = true;
}
Display.destroy();
}
public static void main(String args[]) {
ColoredTriangle ct = new ColoredTriangle();
ct.start();
}
}
It is working perfectly fine, it's just that your triangle is 1 unit high and your window is 480 units high, so it only shows up as one pixel in the corner.
If you replace GL11.glOrtho(0, 640, 480, 0, 1, -1); with GL11.glOrtho(-3.2, 3.2, -2.4, 2.4, -1, 1); then you'll see everything just fine.
Related
This is my drawRect code:
public static void drawRect(float X, float Y, float WIDTH, float HEIGHT, float RED, float GREEN, float BLUE)
{
// clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
// set the color of the quad (R,G,B,A)
GL11.glColor3f(RED, GREEN, BLUE);
// draw quad
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(X,Y);
GL11.glVertex2f(X+WIDTH,Y);
GL11.glVertex2f(X+WIDTH,Y+HEIGHT);
GL11.glVertex2f(X,Y+HEIGHT);
GL11.glEnd();
}
This is what i'm doing
Renderer.drawRect(0, 0, Display.getWidth(), Display.getHeight(), 255, 255, 255);
It fills the entire screen (like it should) but the color is always black.
I suggested reading tutorials because glColor3f() expects 3 floats in the 0...1 range for color components, just like most accelerated graphics API-s. And if this one slipped, there may be confusion about other details too. But nevertheless, 255 would be still clamped to 1, so the routine does not draw a black rectangle, something is missing before (in the setup) and/or after (like a call making the drawing actually appear on screen).
LWJGL wiki has a complete example code for exactly what you are trying to do, by the way: http://wiki.lwjgl.org/wiki/LWJGL_Basics_3_(The_Quad).html
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class QuadExample {
public void start() {
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
// init OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
while (!Display.isCloseRequested()) {
// Clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
// set the color of the quad (R,G,B,A)
GL11.glColor3f(0.5f,0.5f,1.0f);
// draw quad
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(100,100);
GL11.glVertex2f(100+200,100);
GL11.glVertex2f(100+200,100+200);
GL11.glVertex2f(100,100+200);
GL11.glEnd();
Display.update();
}
Display.destroy();
}
public static void main(String[] argv) {
QuadExample quadExample = new QuadExample();
quadExample.start();
}
}
I need to make 3D objects to face up same way no matter their position to the camera. As if camera was very very far away and zoomed in. Here is what I mean (take a look at the pillar and walls): http://youtu.be/Pn9fh93oV-c
Here is what I have so far:
package pack;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import java.util.Random;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;
public class Main {
float viewx, viewy;
float angle;
public static void main(String[] args) {
Main main = new Main();
main.start();
}
private void start() {
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setTitle("Three Dee Demo");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective((float) 30, 640f / 480f, 0.001f, 100);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glTranslatef(0, 0, -20);
while (!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
update();
render();
Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
private void render() {
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(1.0f,1.0f,0.0f);
GL11.glVertex3f( 1.0f, 1.0f,-1.0f);
GL11.glVertex3f(-1.0f, 1.0f,-1.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
GL11.glColor3f(1.0f,0.5f,0.0f);
GL11.glVertex3f( 1.0f,-1.0f, 1.0f);
GL11.glVertex3f(-1.0f,-1.0f, 1.0f);
GL11.glVertex3f(-1.0f,-1.0f,-1.0f);
GL11.glVertex3f( 1.0f,-1.0f,-1.0f);
GL11.glColor3f(1.0f,0.0f,0.0f);
GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
GL11.glVertex3f(-1.0f,-1.0f, 1.0f);
GL11.glVertex3f( 1.0f,-1.0f, 1.0f);
GL11.glColor3f(1.0f,1.0f,0.0f);
GL11.glVertex3f( 1.0f,-1.0f,-1.0f);
GL11.glVertex3f(-1.0f,-1.0f,-1.0f);
GL11.glVertex3f(-1.0f, 1.0f,-1.0f);
GL11.glVertex3f( 1.0f, 1.0f,-1.0f);
GL11.glColor3f(0.0f,0.0f,1.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f,-1.0f);
GL11.glVertex3f(-1.0f,-1.0f,-1.0f);
GL11.glVertex3f(-1.0f,-1.0f, 1.0f);
GL11.glColor3f(1.0f,0.0f,1.0f);
GL11.glVertex3f( 1.0f, 1.0f,-1.0f);
GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
GL11.glVertex3f( 1.0f,-1.0f, 1.0f);
GL11.glVertex3f( 1.0f,-1.0f,-1.0f);
GL11.glEnd();
}
private void update() {
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
viewx -= 0.1f*(float)Math.sin(Math.toRadians(angle));
viewy -= 0.1f*(float)Math.cos(Math.toRadians(angle));
}
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
viewx += 0.1f*(float)Math.sin(Math.toRadians(angle));
viewy += 0.1f*(float)Math.cos(Math.toRadians(angle));
}
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
viewx+= 0.1f*(float)Math.sin(Math.toRadians(angle-90));
viewy+=0.1f*(float)Math.cos(Math.toRadians(angle-90));
}
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
viewx+= 0.1f*(float)Math.sin(Math.toRadians(angle+90));
viewy+=0.1f*(float)Math.cos(Math.toRadians(angle+90));
}
if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
angle += 1;
}
if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
angle -= 1;
}
glLoadIdentity();
glRotatef (angle, 0, 0, 1);
glTranslated(viewx, viewy, -20);
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(1.0f,1.0f,0.0f);
GL11.glVertex3f( -viewx-0.2f, -viewy-0.2f,0);
GL11.glVertex3f(-viewx+0.2f, -viewy-0.2f, 0);
GL11.glVertex3f(-viewx+0.2f, -viewy+0.2f, 0);
GL11.glVertex3f( -viewx-0.2f, -viewy+0.2f, 0);
glEnd();
}
}
That game appeaers to be using an orthographic projection - notice how the thing farther away from the camera are not smaller. That is why the direction of lines doesn't depend on the camera position.
The left half of this picture shows a grid of crates rendered with a perspective projection. The right half shows the same grid rendered with an orthographic projection.
Notice how, with the orthographic projection, all of the crates look identical regardless of their position, and regardless of their distance from the camera - only their orientation matters.
In the OpenGL fixed-function pipeline, you can set up an orthographic projection using glOrtho. If you are using the programmable pipeline, then it depends on your specific program; in general, you'll want to use a projection matrix in the fo
I have a rectangle and this will be the base of my moving object.
I`m trying to rotate the object only when the A button is pressed, when is released the object should stop rotating.
package Tanc;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.LWJGLException;
public class Tanc{
public Tanc(){
try{
Display.setDisplayMode(new DisplayMode(640,480));
Display.setTitle("Tanc");
Display.create();
}catch(LWJGLException e){
e.printStackTrace();
}
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glOrtho(1, 1, 1, 1, 1, -1);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glMatrixMode(GL11.GL_PROJECTION);
float y_angle = 0;
boolean aFlag = false;
while(!Display.isCloseRequested()){
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
// GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glRectd(0, 0, 0.2f, 0.3f);
GL11.glTranslatef(0, 0, 0);
while(Keyboard.next()){
if(Keyboard.getEventKey() == Keyboard.KEY_A){
aFlag = true;
}
}
if(aFlag){
y_angle = 0.1f;
GL11.glRotatef(y_angle, 0, 0, 1);
}
else{
y_angle = 0;
GL11.glRotatef(0, 0, 0, 1);
}
// GL11.glPopMatrix();
Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
public static void main(String[] args){
new Tanc();
}
}
That is because you never really rotate anything. You y_angle doesn't really change other than from 0.0 to 0.1.
Remember that the angle parameter glRotatef() takes, need to be in degrees and not radians. A full circle in radians goes from 0.0 to ~6.2831 radians. Where a full circle using degress goes from 0.0 to 360.0 degress. So your angle isn't noticeable at all, because your only changing it by such a small amount.
I've changed your code. When you now hold the A button, it will rotate and when you release the button it will stop rotating.
float y_angle = 0;
while (!Display.isCloseRequested()) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glLoadIdentity();
GL11.glRectd(0, 0, 0.2f, 0.3f);
GL11.glTranslatef(0, 0, 0);
while (Keyboard.next()){
if (Keyboard.getEventKey() == Keyboard.KEY_A) {
y_angle += 10f;
}
}
GL11.glRotatef(y_angle, 0, 0, 1);
Display.update();
Display.sync(60);
}
I have this code that I understand now:
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class Main {
public void start() {
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
// init OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 0, 800);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
while (!Display.isCloseRequested()) {
// Clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
// set the color of the quad (R,G,B,A)
GL11.glColor3f(1.0f,1.0f,1.0f);
// draw quad
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(100,100);
GL11.glVertex2f(100+200,100);
GL11.glVertex2f(100+200,100+200);
GL11.glVertex2f(100,100+200);
GL11.glEnd();
Display.update();
}
Display.destroy();
}
public static void main(String[] argv) {
Main quadExample = new Main();
quadExample.start();
}
}
I want to know how to draw the box so that it is not drawn on the screen, but setback a bit, how?
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(100,100,-100);
GL11.glVertex3f(100+200,100,-100);
GL11.glVertex3f(100+200,100+200,-100);
GL11.glVertex3f(100,100+200,-100);
GL11.glEnd();
Though that won't make any visual difference with a single quad in ortho. You'll have to switch to a perspective projection matrix for it to look like it's "moved back".
I have an application written in Java with JOGL that works fine on nvidia cards, but when I run it on an ati card, it works perfect until I add textures on 1 or more faces. Eventually, the java vm crashes hard, and it either says it is a crash in the ati driver thread or in the kernel system thread. I managed to recreate it in a simpler bit of code, but it takes a couple minutes to get the crash. up/ down rotates the image, return toggles the textures. keep doing that for a couple of minutes with an ati card and it will crash. I've tried it on about 10 or 15 machines.
package org.yourorghere;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
/**
* SimpleJOGL.java <BR>
* author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) <P>
*
* This version is equal to Brian Paul's version 1.2 1999/10/21
*/
public class SimpleJOGL implements GLEventListener, KeyListener {
GLCanvas canvas;
int lists;
float angle = 0.f;
boolean needsRotate = false;
boolean needsTexture = false;
Texture texture1;
Texture texture2;
Color c1 = Color.red;
Color c2 = Color.green;
public SimpleJOGL(GLCanvas canvas) {
this.canvas = canvas;
}
public static void main(String[] args) {
Frame frame = new Frame("Simple JOGL Application");
GLCanvas canvas = new GLCanvas();
SimpleJOGL sjogl = new SimpleJOGL(canvas);
canvas.addKeyListener(sjogl);
canvas.addGLEventListener(sjogl);
frame.add(canvas);
frame.setSize(640, 480);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
// Center frame
frame.setLocationRelativeTo(null);
frame.setVisible(true);
animator.start();
}
public void init(GLAutoDrawable drawable) {
// Use debug pipeline
// drawable.setGL(new DebugGL(drawable.getGL()));
GL gl = drawable.getGL();
System.err.println("INIT GL IS: " + gl.getClass().getName());
// Enable VSync
gl.setSwapInterval(1);
// double buffer
gl.glEnable(GL.GL_DOUBLEBUFFER);
drawable.setAutoSwapBufferMode(false);
// Enable VSync
gl.setSwapInterval(1);
// Setup the drawing area and shading mode
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glDisable(GL.GL_BLEND);
// -- lighting --
gl.glEnable(GL.GL_COLOR_MATERIAL);
gl.glLightModelf(GL.GL_LIGHT_MODEL_TWO_SIDE, 1.0f);
gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
gl.glEnable(GL.GL_MAP1_VERTEX_3);
gl.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, new float[]{.3f,.3f,.3f,1.f},0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, new float[]{.55f,.55f,.55f,1.f},0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, new float[]{.05f,.05f,.05f,1.f}, 0);
gl.glFrontFace(GL.GL_CCW);
gl.glEnable(GL.GL_LIGHT0);
gl.glEnable(GL.GL_LIGHTING);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE, new float[]{.5f,.5f,.5f,1.0f}, 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, new float[]{.05f, .05f, .05f, 1.0f}, 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_EMISSION, new float[]{0.01f, 0.01f, 0.01f, 1.f}, 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, new float[]{30}, 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_COLOR_INDEXES, new float[]{1.0f, 0.0f, 0.0f, 1.0f}, 0);
gl.glCullFace(GL.GL_BACK);
gl.glDisable(GL.GL_BLEND);
gl.glEnable(GL.GL_RESCALE_NORMAL);
lists = gl.glGenLists(3);
this.needsRotate = true;
this.needsTexture = true;
}
private void reTexture(GL gl) {
BufferedImage image1 = makeImage(c1);
BufferedImage image2 = makeImage(c2);
makeShape(gl, image1, image2);
}
private BufferedImage makeImage(Color c) {
int y = 1200;
int x = 1024;
BufferedImage image = new BufferedImage(y, y, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setBackground(Color.white);
g.clearRect(0, 0, y,y);
Rectangle2D r = new Rectangle2D.Float(128,128,x-256,x-256);
g.setColor(c);
g.fill(r);
g.dispose();
return image;
}
private void makeShape(GL gl, BufferedImage image1, BufferedImage image2) {
float x = 1024.f/1200.f;
texture1 = TextureIO.newTexture(image1, false) ;
texture2 = TextureIO.newTexture(image2, false) ;
gl.glNewList(lists+1, GL.GL_COMPILE);
gl.glColor3f(0f, .8f, .4f);
// Drawing Using Triangles
gl.glBegin(GL.GL_POLYGON);
gl.glTexCoord2f(0, 0);
gl.glVertex3f(0.0f, 0.0f, 0.0f); // Top
gl.glNormal3f(0, 0, 1);
gl.glTexCoord2f(x, 0);
gl.glVertex3f(1.0f, 0.0f, 0.0f); // Bottom Left
gl.glNormal3f(0, 0, 1);
gl.glTexCoord2f(x, x);
gl.glVertex3f(1.0f, 1.0f, 0.0f); // Bottom Right
gl.glNormal3f(0, 0, 1);
gl.glTexCoord2f(0, x);
gl.glVertex3f(0f, 1.0f, 0.0f); // Bottom Right
gl.glNormal3f(0, 0, 1);
gl.glEnd();
texture1.disable();
gl.glEndList();
gl.glNewList(lists+2, GL.GL_COMPILE);
texture2.enable();
texture2.bind();
gl.glBegin(GL.GL_POLYGON);
gl.glTexCoord2f(0, 0);
gl.glVertex3f(0.0f, 0.0f, 0.0f); // Top
gl.glNormal3f(0, 0, 1);
gl.glTexCoord2f(x, 0);
gl.glVertex3f(1.0f, 0.0f, 0.0f); // Bottom Left
gl.glNormal3f(0, 0, 1);
gl.glTexCoord2f(x, x);
gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
gl.glNormal3f(0, 0, 1);
gl.glTexCoord2f(0, x);
gl.glVertex3f(0f, -1.0f, 0.0f); // Bottom Right
gl.glNormal3f(0, 0, 1);
gl.glEnd();
texture2.disable();
gl.glEndList();
}
public void rotate(GL gl) {
gl.glNewList(lists, GL.GL_COMPILE);
gl.glLoadIdentity();
gl.glTranslatef(-2.f, 0.f, -6.f);
gl.glRotatef(angle, 1.0f, 0.f, 0.f);
gl.glCallList(lists+1);
gl.glRotatef(-45, 1.f, 0.f, 0.f);
gl.glCallList(lists+2);
gl.glEndList();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
GLU glu = new GLU();
if (height <= 0) { // avoid a divide by zero error!
height = 1;
}
final float h = (float) width / (float) height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
if (needsTexture) {
reTexture(gl);
needsTexture = false;
}
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[]{1.f, 1.f, 1.f, 0.f}, 0);
gl.glLoadIdentity();
gl.glTranslatef(-2.f, 0.f, -6.f);
gl.glRotatef(angle, 1.0f, 0.f, 0.f);
texture1.enable();
texture1.bind();
gl.glCallList(lists+1);
texture1.disable();
gl.glRotatef(-45, 1.f, 0.f, 0.f);
gl.glCallList(lists+2);
// Flush all drawing operations to the graphics card
drawable.swapBuffers();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
angle -= 5;
needsRotate = true;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
angle += 5;
needsRotate = true;
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
Color c = c1;
c1 = c2;
c2 = c;
needsTexture = true;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
canvas.repaint();
}
}
thank you!
You need to dispose of the textures when you have finished with them, e.g.
private void makeShape(GL gl, BufferedImage image1, BufferedImage image2) {
if (texture1 != null) {
texture1.dispose();
}
texture1 = TextureIO.newTexture(image1, false) ;
if (texture2 != null) {
texture2.dispose();
}
texture2 = TextureIO.newTexture(image2, false) ;
// ...
}
otherwise you will eventually run out of texture names.
Note: Texture does not have a finalizer.