Object not rotating - java

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);
}

Related

Rotate OrthographicCamera from a point (LibGdx)

I am studying the example of Orthographic Camera of wiki libgdx :
https://github.com/libgdx/libgdx/wiki/Orthographic-camera#description
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
public class OrthographicCameraExample implements ApplicationListener {
static final int WORLD_WIDTH = 100;
static final int WORLD_HEIGHT = 100;
private OrthographicCamera cam;
private SpriteBatch batch;
private Sprite mapSprite;
private float rotationSpeed;
#Override
public void create() {
rotationSpeed = 0.5f;
mapSprite = new Sprite(new Texture(Gdx.files.internal("sc_map.png")));
mapSprite.setPosition(0, 0);
mapSprite.setSize(WORLD_WIDTH, WORLD_HEIGHT);
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
// Constructs a new OrthographicCamera, using the given viewport width and height
// Height is multiplied by aspect ratio.
cam = new OrthographicCamera(30, 30 * (h / w));
cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0);
cam.update();
batch = new SpriteBatch();
}
#Override
public void render() {
handleInput();
cam.update();
batch.setProjectionMatrix(cam.combined);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
mapSprite.draw(batch);
batch.end();
}
private void handleInput() {
if (Gdx.input.isKeyPressed(Input.Keys.A)) {
cam.zoom += 0.02;
}
if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
cam.zoom -= 0.02;
}
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
cam.translate(-3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
cam.translate(3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
cam.translate(0, -3, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
cam.translate(0, 3, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.W)) {
cam.rotate(-rotationSpeed, 0, 0, 1);
}
if (Gdx.input.isKeyPressed(Input.Keys.E)) {
cam.rotate(rotationSpeed, 0, 0, 1);
}
cam.zoom = MathUtils.clamp(cam.zoom, 0.1f, 100/cam.viewportWidth);
float effectiveViewportWidth = cam.viewportWidth * cam.zoom;
float effectiveViewportHeight = cam.viewportHeight * cam.zoom;
cam.position.x = MathUtils.clamp(cam.position.x, effectiveViewportWidth / 2f, 100 - effectiveViewportWidth / 2f);
cam.position.y = MathUtils.clamp(cam.position.y, effectiveViewportHeight / 2f, 100 - effectiveViewportHeight / 2f);
}
#Override
public void resize(int width, int height) {
cam.viewportWidth = 30f;
cam.viewportHeight = 30f * height/width;
cam.update();
}
#Override
public void resume() {
}
#Override
public void dispose() {
mapSprite.getTexture().dispose();
batch.dispose();
}
#Override
public void pause() {
}
public static void main(String[] args) {
new LwjglApplication(new OrthographicCameraExample());
}
}
On the rotation of the camera, map is rotated with the camera at the midpoint of the screen. I would like to rotate the camera from a certain point. For example, the point 0,0 . I tried to use the method rotateAround ( Vector3 point , Vector3 axis , float angle) , I did not get the expected result.
cam.rotateAround(new Vector3(0,0,0), new Vector3(0,0,1), 1);
I know it's possible to move the camera to the point 0.0 and then rotate . But that's not what I want .
In the game I'm doing the player is in a fixed position at the bottom of the screen in the middle and I turned the screen around it , but without taking the player to the middle of the screen and then rotate .
I appreciate the help .
You just need to update camera after cam.rotateround :)
It works for me.
cam.rotateAround(new Vector3(270, 0, 0), new Vector3(0, 0, 1), 0.1f);
cam.update();
Here is a screenshot of my test. As you can see screen rotating around red dot. (Perspective camera is also rotating around same point that in its own coordinate.)
Also i suggest you to use
cam.setToOrtho(false, cam.viewportWidth, cam.viewportHeight);
instead of
cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0);

Why is LWJGL drawing my images weirdly?

Hi guys i am trying to draw a 100 by 100 image to my screen. However when i draw it, openGL draws it half the size of my square with a kind of border round the right and bottom edges.
This is my code:
package biz.boulter.lwjglgame;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import static org.lwjgl.opengl.GL11.*;
public class Game
{
public static final int WIDTH = 1000;
public static final int HEIGHT = WIDTH / 16 * 9;
private static Texture t;
private static void render()
{
glClear(GL_COLOR_BUFFER_BIT);
t.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(100, 100); // top-left
glTexCoord2f(1, 0);
glVertex2i(200, 100); // top-right
glTexCoord2f(1, 1);
glVertex2i(200, 200); // bottom-right
glTexCoord2f(0, 1);
glVertex2i(100, 200); // bottom-left
glEnd();
}
private static Texture loadTexture(String path) throws Exception{
return TextureLoader.getTexture("PNG", Game.class.getResourceAsStream(path));
}
public static void main(String[] args)
{
try
{
Display.setDisplayMode(new DisplayMode(1000, 1000 / 16*9));
Display.create();
t = loadTexture("/assets/guy.png");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1000, 1000/16*9, 0, 1, -1);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
while(!Display.isCloseRequested())
{
render();
Display.sync(60);
Display.update();
}
}catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
}
Why is it doing this. Also i am using slick-util.jar for the texture loading.
ScreenShot:
http://oi59.tinypic.com/13zyflk.jpg

LWJGL OpenGL 2D Lighting Issue w/ GLSL

I'm having an issue with lighting. I'm using GLSL to achieve this. All seems fine however I'm having an issue I believe its called "banding". Essentially it's giving a gradient like effect to my lights. Perhaps I'm doing something wrong, here is my code that I'm using a long with an image of the problem. I'm a beginner in GLSL and not sure if the code I have is the most efficient way.
Main Code
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.glUseProgram;
import java.util.ArrayList;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;
public class Main {
public final int width = 1280;
public final int height = 720;
public ArrayList<Light> lights = new ArrayList<Light>();
private Shader shader;
private int bufferIndex;
private float[] vertices;
private Light lighter;
private Vector2f lightPos;
private boolean lockedIn = false;
private void render() {
glClear(GL_COLOR_BUFFER_BIT);
for (Light light : lights) {
shader.bind();
shader.setUniformf("lightLocation", light.getLocation().x,
light.getLocation().y);
shader.setUniform("lightColor", new Vector3f(light.getColors()));
shader.setUniform("ambientLight", new Vector3f(0.1f, 0.1f, 0.1f));
shader.setUniform("baseColor", new Vector3f(0.2f, 0.2f, 0.2f));
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
VertexBufferObject.GetInstance().render();
glDisable(GL_BLEND);
glUseProgram(0);
}
Display.update();
Display.sync(60);
}
private void input() {
if (Input.getMouse(0))
lockedIn = true;
else if (Input.getMouse(1))
lockedIn = false;
if (lockedIn) {
Mouse.setGrabbed(true);
lightPos.set(Input.getMousePosition().x, Input.getMousePosition().y);
if (Input.getKey(Input.KEY_UP)) {
lighter.setBlue((float) (lighter.getBlue() + 1));
lighter.setRed(lighter.getRed() + 1);
lighter.setGreen(lighter.getGreen() + 1);
}
if (Input.getKey(Input.KEY_DOWN)) {
lighter.setBlue((float) (lighter.getBlue() - 1));
lighter.setRed(lighter.getRed() - 1);
lighter.setGreen(lighter.getGreen() - 1);
}
if (Input.getKey(Input.KEY_L)) {
lights.add(new Light(new Vector2f(Input.getMousePosition().x,
Input.getMousePosition().y), lighter.getRed(), lighter
.getGreen(), lighter.getBlue()));
lighter.setBlue((float) Math.random() * 10);
lighter.setGreen((float) Math.random() * 10);
lighter.setRed((float) Math.random() * 10);
}
} else {
Mouse.setGrabbed(false);
}
}
private void setUpObjects() {
lightPos = new Vector2f(50, 50);
lighter = new Light(lightPos, 0, 5, 10);
lights.add(lighter);
lights.add(new Light(new Vector2f(60, 60), 120, 0, 120));
}
private void initialize() {
try {
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle("2D Lighting");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
vertices = new float[] { 0, 0, 0, width, 0, 0, 0, height, 0, width,
height, 0, 0, height, 0, width, 0, 0 };
bufferIndex = glGenBuffers();
int b2 = glGenBuffers();
VertexBufferObject.GetInstance().addObject(bufferIndex, vertices);
VertexBufferObject.GetInstance().addObject(
b2, new float[] { 100, 100, 0, 200, 100, 0, 100, 200, 0, 200, 200,
0, 100, 200, 0, 200, 100, 0 });
VertexBufferObject.GetInstance().initObjects();
shader = new Shader();
shader.addFragmentShader(Shader.loadShader("shader.frag"));
shader.addVertexShader(Shader.loadShader("shader.vertex"));
shader.compileShader();
shader.addUniform("ambientLight");
shader.addUniform("baseColor");
shader.addUniform("lightLocation");
shader.addUniform("lightColor");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
private void cleanup() {
shader.unbind();
Display.destroy();
}
public static void main(String[] args) {
Main main = new Main();
main.setUpObjects();
main.initialize();
while (!Display.isCloseRequested()) {
if (Input.getKey(Input.KEY_ESCAPE))
System.exit(1);
main.input();
main.render();
}
main.cleanup();
}
}
GLSL Code
#verson 120
uniform vec2 lightLocation;
uniform vec3 lightColor;
uniform vec3 ambientLight;
uniform vec3 baseColor;
void main() {
vec4 light = vec4(ambientLight, 1);
vec4 color = vec4(baseColor,1);
float distance = length(lightLocation - gl_FragCoord.xy);
float attenuation = 1 / distance;
vec4 lColor = vec4(attenuation, attenuation, attenuation, pow(attenuation, 3)) * vec4(lightColor, 1);
color += lColor;
gl_FragColor = color * light;
}
Vertex Shader
#version 120
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
A result from the code:

Basic LWJGL triangle w/ OpenGL

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.

When using textures in a JOGL application, ati graphics cards all eventually crash

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.

Categories