Why is LWJGL drawing my images weirdly? - java

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

Related

Move 2d box along display boundaries

Currently, I am able to move the box to the left and right through the display boundary, but I am unable to figure out how to move the box up and down. I would like the box to navigate in a circle around the display, e.g move right, then down, then to the left, then up, then continuously moving throughout the display. Here is my code for my entity class:
import java.awt.Rectangle;
import org.lwjgl.opengl.GL11;
class Entity
{
private static enum State { START, LEFT, RIGHT, UP, DOWN};
private Rectangle box;
private State state;
private float speed; // pixels / ms
public Entity(float speed)
{
box = new Rectangle(10, 10, 10, 10);
state = State.START;
this.speed = speed;
}
public void draw()
{
float x = (float)box.getX();
float y = (float)box.getY();
float w = (float)box.getWidth();
float h = (float)box.getWidth();
// draw the square
GL11.glColor3f(0,1,0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x, y);
GL11.glVertex2f(x+w, y);
GL11.glVertex2f(x+w, y+w);
GL11.glVertex2f(x, y+w);
GL11.glEnd();
}
public void update(int delta)
{
switch (state)
{
case START:
state = State.RIGHT;
case RIGHT:
box.translate((int)(speed*delta), 0);
if (box.getX() >= 800)
{
state = State.LEFT;
}
break;
case LEFT:
box.translate((int)(-speed*delta), 0);
if (box.getX() <= 0)
{
state = State.RIGHT;
}
break;
}
}
}
Here is my code for GameLoop:
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.input.Keyboard;
import org.lwjgl.LWJGLException;
public class GameLoop
{
public static final int TARGET_FPS=100;
public static final int SCR_WIDTH=800;
public static final int SCR_HEIGHT=600;
public static void main(String[] args) throws LWJGLException
{
initGL(SCR_WIDTH, SCR_HEIGHT);
Entity e = new Entity(.1f);
long time = (Sys.getTime()*4000)/Sys.getTimerResolution(); // ms
while (! Display.isCloseRequested())
{
long time2 = (Sys.getTime()*4000)/
Sys.getTimerResolution(); // ms
int delta = (int)(time2-time);
System.out.println(delta);
e.update(delta);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
e.draw();
// UPDATE DISPLAY
Display.update();
Display.sync(TARGET_FPS);
time = time2;
}
Display.destroy();
}
public static void initGL(int width, int height) throws LWJGLException
{
// open window of appropriate size
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
Display.setVSyncEnabled(true);
// enable 2D textures
GL11.glEnable(GL11.GL_TEXTURE_2D);
// set "clear" color to black
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// enable alpha blending
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// set viewport to entire window
GL11.glViewport(0,0,width,height);
// set up orthographic projectionr
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
// GLU.gluPerspective(90f, 1.333f, 2f, -2f);
// GL11.glTranslated(0, 0, -500);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
}
Any help would be appreciated. I also need to have the box change colors every time it changes direction.
Correct me if i'm wrong but i think you just need to use box.translate(0,value) to move your Rectangle up or down. Just adapt your left-right movement to the y value of the box. You could also use the State to determine which color it should be drawn in.

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

Object not rotating

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

LWJGL Applet Conversion Walkthrough needed

I have tried converting my LWJGL game to an applet but It can't load images, when I delete the code for the images it throws a null pointer exception, so Could SOmeone give me a walkthrough on how to Convert this code to an applet:
package com.kgt.platform.name;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import kuusisto.tinysound.Sound;
import kuusisto.tinysound.Music;
import kuusisto.tinysound.TinySound;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import static org.lwjgl.opengl.GL11.*;
public class Game {
public static Player player;
public static Enemy enemy;
public static ExitButton exitbutton;
public static MenuButton menubutton;
public static ArrayList<Coin> coinlist;
public static ArrayList<Bullet> bulletlist;
public static ArrayList<Bullet2> bulletlist2;
public static ArrayList<Platform> platformlist;
public static ArrayList<OneWayPlatform> onewayplatformlist;
public static int score;
public static boolean p1on;
public static boolean p2on;
static State state = State.GAME;
public int curlvl = 1;
private static Texture logo;
private static Texture pause;
public enum State {
PAUSE, GAME, MAIN_MENU, INTRO;
}
public static void init() {
try {
// load texture from PNG file
logo = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/logo.png"));
System.out.println("Texture 'logo' Loaded. Format: PNG");
pause = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/paused.png"));
System.out.println("Texture 'pause' Loaded. Format: PNG");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void logo() {
Color.white.bind();
logo.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(192,468);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(192+logo.getTextureWidth(),468);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(192+logo.getTextureWidth(),468- logo.getTextureHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(192,468-logo.getTextureHeight());
GL11.glEnd();
}
public static void pause() {
Color.white.bind();
pause.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(256,440);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(256+pause.getTextureWidth(),440);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(256+pause.getTextureWidth(),440- pause.getTextureHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(256,440-pause.getTextureHeight());
GL11.glEnd();
}
public static void main(String[] args) throws Exception
{
System.setProperty("org.lwjgl.librarypath", new File("natives").getAbsolutePath());
System.out.println("Native Path: " + System.getProperty("org.lwjgl.librarypath"));
Display.setDisplayMode(new DisplayMode(640, 480));
Display.create();
init();
score = 0;
player = new Player();
exitbutton = new ExitButton(320, 61);
menubutton = new MenuButton(320, 95);
enemy = new Enemy();
coinlist = new ArrayList<Coin>(0);
bulletlist = new ArrayList<Bullet>(0);
bulletlist2 = new ArrayList<Bullet2>(0);
platformlist = new ArrayList<Platform>(0);
onewayplatformlist = new ArrayList<OneWayPlatform>(0);
Level1();
while(!Display.isCloseRequested())
{
setCamera();
drawBackground1();
for(Coin c: coinlist) c.draw();
for(Bullet b: bulletlist) b.draw();
for(Bullet2 b: bulletlist2) b.draw();
for(Platform b: platformlist) b.draw();
for(OneWayPlatform b: onewayplatformlist) b.draw();
enemy.draw();
player.draw();
states();
stateinput();
exitbutton.draw();
menubutton.draw();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public static void stopsound(){
TinySound.shutdown();
}
public static void states(){
switch (state) {
case INTRO:
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
glColor3f(1.0f, 0.0f, 0.0f);
glRectf(0, 0, 640, 480);
break;
case GAME:
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
break;
case MAIN_MENU:
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
glColor3f(0.0f, 0.0f, 1.0f);
glRectf(0, 0, 640, 480);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, logo.getTextureID());
logo();
break;
case PAUSE:
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
glEnable(GL_COLOR_ARRAY);
glBegin(GL_QUADS);
glColor4d(0.5f, 0.5f, 0.5, 0.7);
glVertex2d(224, 40);
glVertex2d(416, 40);
glColor4d(0.7f, 0.7f, 0.7, 0.7);
glVertex2d(416, 440);
glVertex2d(224, 440);
glEnd();
glDisable(GL_COLOR_ARRAY);
glColor3f(0f, 0f, 0f);
glRectf(224, 40, 226, 440);
glRectf(416, 40, 414, 440);
glRectf(224, 40, 416, 42);
glRectf(224, 438, 416, 440);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, pause.getTextureID());
pause();
break;
}
}
public static void stateinput(){
while (Keyboard.next()) {
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
if(state == State.GAME)
state = State.PAUSE;
else if(state == State.PAUSE)
state = State.GAME;
}
}
}
public static void jumpsound(){
TinySound.init();
Sound jump = TinySound.loadSound("/sound/Jump6.wav");
jump.play();
}
public static void shootsound(){
TinySound.init();
Sound shoot = TinySound.loadSound("/sound/Shoot1.wav");
shoot.play();
}
public static void selectsound(){
TinySound.init();
Sound select = TinySound.loadSound("/sound/select.wav");
select.play();
}
public static void hurtsound(){
TinySound.init();
Sound hurt = TinySound.loadSound("/sound/Hurt1.wav");
hurt.play();
}
public static void music(int track){
TinySound.init();
Music ocean = TinySound.loadMusic("/music/ocean.wav");
Music menu1 = TinySound.loadMusic("/music/menu1.wav");
if(track == 1){
ocean.play(true);
}
else if (track == 0){
menu1.play(true);
}
}
public static void setCamera(){
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL_COLOR_ARRAY);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glEnable( GL11.GL_TEXTURE );
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0,640,480);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 640, 0, 480, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public static void Level1(){
Game.platformlist.add(new Platform(16, 120));
Game.platformlist.add(new Platform(48, 120));
Game.platformlist.add(new Platform(624, 120));
Game.platformlist.add(new Platform(592, 120));
Game.onewayplatformlist.add(new OneWayPlatform(128, 200));
Game.onewayplatformlist.add(new OneWayPlatform(160, 200));
music(1);
}
public static void drawBackground1(){
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
glBegin(GL_QUADS);
glColor3d(0.4, 0.5, 0.7);
glVertex2d(640, 480);
glVertex2d(0, 480);
glColor3d(0.7, 0.8, 0.9);
glVertex2d(0, 0);
glVertex2d(640, 0);
glEnd();
glBegin(GL_QUADS);
glColor3d(0.3, 0.2, 0.1);
glVertex2d(0, 0);
glVertex2d(640, 0);
glColor3d(0.5, 0.2, 0.1);
glVertex2d(640, 32);
glVertex2d(0, 32);
glEnd();
glBegin(GL_QUADS);
glColor3d(0.5, 0.2, 0.1);
glVertex2d(0, 32);
glVertex2d(640, 32);
glColor3d(0.2, 0.5, 0.1);
glVertex2d(640, 47);
glVertex2d(0, 47);
glColor3d(1, 1, 1);
glEnd();
}
}

JFrame + JOGL only displays content when resized or minimised + maximised

I'm currently experimenting with JOGL, and I ran into a problem which is really new to me. After starting the program, I see a blank window. After resize, I can normally see the content of it or if I do a minimise/restore. I suppose there is something with the events. The init() isn't called after the window created, but after the first trigger with resize or minimise.
Here is the code what I'm using for creating a window and setting up OpenGL:
package com.cogwheel.framework.graphics;
import java.awt.BorderLayout;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.*;
import javax.swing.JFrame;
import com.cogwheel.framework.init.CWGPreferences;
import com.cogwheel.framework.util.CWGDebug;
import com.jogamp.opengl.util.Animator;
public class CWGOpenGLScreen extends JFrame implements GLEventListener {
private static final String TAG = "CWGOpenGLScreen";
private GLCanvas mCanvas;
private long fpsLast = System.currentTimeMillis();
public CWGOpenGLScreen(){
this.setTitle(CWGPreferences.WINDOW_NAME);
this.setSize(CWGPreferences.WINDOW_SIZE);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
///this.setResizable(false);
this.setVisible(true);
CWGDebug.info(TAG, "Window created!");
CWGSetupGL();
}
private void CWGSetupGL(){
GLCapabilities mCaps = new GLCapabilities(null);
mCaps.setHardwareAccelerated(true);
mCaps.setDoubleBuffered(true);
mCanvas = new GLCanvas(mCaps);
mCanvas.addGLEventListener(this);
this.add(mCanvas, BorderLayout.CENTER);
Animator animator = new Animator(mCanvas);
animator.start();
}
public void CWGDrawScene(GLAutoDrawable drawable)
{
CWGCalculateFPS();
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(1.0f, 0.0f, 0.0f);
gl.glVertex3f(1.0f / 5 , 0.0f, 0.0f);
gl.glColor3f(0.0f, 1.0f, 0.0f);
gl.glVertex3f(1.0f / 5, 1.0f / 5, 0.0f);
gl.glColor3f(0.0f, 0.0f, 1.0f);
gl.glVertex3f(0.0f, 1.0f / 5, 1.0f / 5);
gl.glEnd();
gl.glFlush();
}
public void CWGCalculateFPS(){
this.setTitle(CWGPreferences.WINDOW_NAME + " [" + 1000 / (System.currentTimeMillis() - fpsLast) + "]");
fpsLast = System.currentTimeMillis();
}
public void init(GLAutoDrawable drawable){
/*GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(0, 0, 0, 0);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0, 1, 0, 1, -1, 1);
*/
CWGDebug.info(TAG, "Init called!");
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height){}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged){}
public void display(GLAutoDrawable drawable){ CWGDrawScene(drawable); }
public void dispose(GLAutoDrawable drawable){}
}
Code quality is poor, I know, had no time to cleanup yet. Sorry for it.
Edit: got the problem, JFrame should not be shown until the GLEventListener is not initialised.
I've found your error (it has nothng to do with eclipse) : you should have set up the viewport (call to glViewport) whenever the canvas is resized => so you call glViewport(x,y, widht, height) in the reshape() method of the GLEventListener. So that OpenGL always knows where to draw scene, and not just when resizing frame.
Also, don't forget to setup screen (call to CWGSetupGL()) before showing it (otherwise you will still have the same problem, but only for a little time).
Here the modified code (I've removed calls to CWGDebug.info as you did not provide this class, but you can put them back) :
package com.cogwheel.framework.graphics;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
import com.jogamp.opengl.util.Animator;
public class CWGOpenGLScreen extends JFrame implements GLEventListener {
public static void main(String[] args) {
new CWGOpenGLScreen().setVisible(true);
}
private static final long serialVersionUID = 635066680731362587L;
private static final String TAG = "CWGOpenGLScreen";
private GLCanvas mCanvas;
private long fpsLast = System.currentTimeMillis();
public CWGOpenGLScreen(){
this.setTitle(TAG);
this.setSize(640,480);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
///this.setResizable(false);
CWGSetupGL();
this.setVisible(true);
// CWGDebug.info(TAG, "Window created!");
}
private void CWGSetupGL(){
GLCapabilities mCaps = new GLCapabilities(null);
mCaps.setHardwareAccelerated(true);
mCaps.setDoubleBuffered(true);
mCanvas = new GLCanvas(mCaps);
mCanvas.addGLEventListener(this);
this.add(mCanvas, BorderLayout.CENTER);
final Animator animator = new Animator(mCanvas);
animator.start();
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
animator.stop();
System.exit(0);
}
});
}
public void CWGDrawScene(GLAutoDrawable drawable)
{
CWGCalculateFPS();
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(1.0f, 0.0f, 0.0f);
gl.glVertex3f(1.0f / 5 , 0.0f, 0.0f);
gl.glColor3f(0.0f, 1.0f, 0.0f);
gl.glVertex3f(1.0f / 5, 1.0f / 5, 0.0f);
gl.glColor3f(0.0f, 0.0f, 1.0f);
gl.glVertex3f(0.0f, 1.0f / 5, 1.0f / 5);
gl.glEnd();
gl.glFlush();
}
public void CWGCalculateFPS(){
this.setTitle(TAG + " [" + 1000 / (System.currentTimeMillis() - fpsLast) + "]");
fpsLast = System.currentTimeMillis();
}
public void init(GLAutoDrawable drawable){
/*GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(0, 0, 0, 0);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0, 1, 0, 1, -1, 1);
*/
// CWGDebug.info(TAG, "Init called!");
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height){
GL2 gl = drawable.getGL().getGL2();
gl.glViewport(x, y, width, height);
}
//public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged){}
public void display(GLAutoDrawable drawable){ CWGDrawScene(drawable); }
public void dispose(GLAutoDrawable drawable){}
}
Regards :)
Edit : apologizes, I haven't seen you replied yourself to your own question, by an edit.
Anyway, I still think you'd better call glViewport in reshape() method.

Categories