I'm very new to JOGL, hence please pardon the question's noobness.
I'm trying to write a piece of toy code to output a single square using GLCanvas, which is positioned randomly on the canvas. However, the display function seems to get called twice (and hence two squares appear, rather than one). Here's the code. Where did I go wrong?
RandomPoint class:
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
public class RandomPoints extends Frame implements GLEventListener{
/**
*
*/
private static final long serialVersionUID = 1L;
public int HEIGHT;
public int WIDTH;
private GLCanvas canvas;
//private Animator animator;
public RandomPoints(){
HEIGHT = 300;
WIDTH = 300;
GLProfile profile = GLProfile.getDefault();
GLCapabilities cap = new GLCapabilities(profile);
canvas = new GLCanvas(cap);
add(canvas, BorderLayout.CENTER);
canvas.addGLEventListener(this);
System.out.println("Probe");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
//animator.stop();
System.exit(0);
}
});
}
#Override
public void display(GLAutoDrawable arg0) {
//TODO Auto-generated method stub
GL2 gl = arg0.getGL().getGL2();
float red = (float) Math.random();
float blue = (float) Math.random();
float green = (float) Math.random();
int x = (int)(Math.random()*WIDTH);
int y = (int)(Math.random()*HEIGHT);
gl.glColor3f(red, green, blue);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex2i(x, y);
gl.glVertex2i(x+10, y);
gl.glVertex2i(x+10, y+10);
gl.glVertex2i(x, y+10);
gl.glEnd();
System.out.println(x+" "+y+" "+red+" "+green+" "+blue);
}
#Override
public void dispose(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
#Override
public void init(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
GL2 gl = arg0.getGL().getGL2();
gl.glColor3f(0.0f, 0.0f, 0.0f);
gl.glClearColor(1, 1, 1, 0);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
//animator = new Animator(canvas);
//animator.start();
}
#Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
// TODO Auto-generated method stub
WIDTH = arg3; HEIGHT = arg4;
GL2 gl = arg0.getGL().getGL2();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0, WIDTH, 0, HEIGHT, -1, 1);
}
}
GLTest class (driver)
public class GLTest {
public static void main(String args[]){
RandomPoints point = new RandomPoints();
point.setSize(300,300);
point.setVisible(true);
}
}
Thanks in advance!
I've just tested your code and I see only one square. However, it would be better to clear the color buffer in the display() method too. You should look at my example here. If you really want to animate your drawing, you should use an animator. Please rather post your questions about JOGL on its official forum if you want to get more accurate and faster replies as only a very few maintainers come here.
Related
I'm drawing a knob as a component and I'm using swing. The result seems ok but when I click on the knob and move my mouse (up or down) to change the knob position, the knob repainted is ugly, as if there were 2 layers superposed. More over, I have this result only for the "small sizes" of my knob. If I enlarge my frame, this ugly effect disappear. Does someone could explain me what happens and how to improve my knob ?
Thanks a lot for those who will help me.
Here is my code :
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Knob extends JPanel implements MouseListener {
int length, originX, originY, centerX, centerY, width, height, diameter, squareLength;
int minorTick, majorTick, xTick, yTick, xCursor, yCursor;
int xMouse, yMouse, xMouseOrigin, yMouseOrigin;
float yDeltaMouse;
double angleOrigin, angleRange, angle;
double cursorValue;
double initialCursorValue;
Color backgroundColor, knobColor;
boolean mousePressed;
Thread t;
private double knobValue;
private String title = new String("");
private int titleWidth;
Knob (double initialValue, Color c, int majorTick, int minorTick) {
//System.out.println("Knob");
cursorValue=initialCursorValue=initialValue;
knobColor =c;
this.majorTick=majorTick;
this.minorTick=minorTick;
this.addMouseListener(this);
}
Knob (double initialValue, Color c, int majorTick, int minorTick, String title) {
//System.out.println("Knob");
cursorValue=initialCursorValue=initialValue;
knobColor =c;
this.majorTick=majorTick;
this.minorTick=minorTick;
this.title=title;
this.addMouseListener(this);
}
public void paintComponent (Graphics g) {
width=this.getWidth()/10*10;
height=this.getHeight()/10*10;
Graphics2D g2D = (Graphics2D)g;
System.setProperty("awt.useSystemAAFontSettings", "on");
System.setProperty("swing.aatext", "true");
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (width>height) {
length=height;
} else {
length=width;
}
centerX=width/2;
centerY=height/2;
g.setColor(Color.BLACK);
squareLength = (int )(length*0.9);
originX=(width-squareLength)/2;
originY=(height-squareLength)/2;
/*
//-45 = (-(Math.PI)/4)
angleOrigin= -45;
// 270 = (3*(Math.PI)/2)
angleRange=270;
//g.drawRect(originX, originY, squareLength, squareLength);
//g.fillArc(originX, originY, squareLength, squareLength, (int)angleOrigin, (int)angleRange);
for (int i=0; i<minorTick ; i++) {
angle=((i*angleRange/(minorTick-1))+angleOrigin)-7;
//System.out.println(angle*360/(2*Math.PI));
xTick= (int) (centerX+Math.cos(angle)*squareLength/2);
yTick= (int) (centerY-Math.sin(angle)*squareLength/2);
//g.drawLine(centerX, centerY, xTick, yTick);
//g.fillArc(originX, originY, squareLength, squareLength, (int)angle, (int)14);
}
*/
angleOrigin=-(Math.PI)/4;
angleRange=3*(Math.PI)/2;
g2D.setStroke(new BasicStroke(length/50+1));
for (int i=0; i<minorTick ; i++) {
angle=i*angleRange/(minorTick-1)+angleOrigin;
//System.out.println(angle*360/(2*Math.PI));
xTick= (int) (centerX+Math.cos(angle)*squareLength/2);
yTick= (int) (centerY-Math.sin(angle)*squareLength/2);
//g.drawLine(centerX, centerY, xTick, yTick);
g2D.draw (new Line2D.Float(centerX, centerY, xTick, yTick));
}
backgroundColor = this.getBackground();
g.setColor(backgroundColor);
diameter=(int)(length*0.8);
originX=(width-diameter)/2;
originY=(height-diameter)/2;
g.fillOval(originX, originY, diameter, diameter);
/*
RadialGradientPaint gp;
Point2D center= new Point2D.Float(width/2, height/2);
diameter=(int)(length*0.75);
originX=(width-diameter)/2;
originY=(height-diameter)/2;
float radius=diameter/2;
float[] dist = {0.7f, 1f};
Color[] colors = {Color.BLUE, Color.GRAY};
gp=new RadialGradientPaint(center, radius, dist, colors);
g2D.setPaint(gp);
g2D.fillOval(originX,originY,diameter,diameter);
*/
diameter=(int)(length*0.75);
originX=(width-diameter)/2;
originY=(height-diameter)/2;
g.setColor(Color.GRAY);
g.fillOval(originX,originY,diameter,diameter);
diameter=(int)(length*0.7);
originX=(width-diameter)/2;
originY=(height-diameter)/2;
g.setColor(knobColor);
g.fillOval(originX,originY,diameter,diameter);
g2D.setStroke(new BasicStroke(length/50+3));
angle=(2*Math.PI)*(0.75-cursorValue*0.75)+angleOrigin;
xCursor= (int) (centerX+Math.cos(angle)*length*0.35);
yCursor= (int) (centerY-Math.sin(angle)*length*0.35);
g.setColor(Color.GRAY);
g2D.draw (new Line2D.Float(centerX, centerY, xCursor, yCursor));
g2D.rotate(Math.toRadians(270.0));
g.setFont(new Font(g.getFont().getFontName(),Font.PLAIN,this.getHeight()/3));
titleWidth=g.getFontMetrics().stringWidth(title);
g2D.drawString(title,-this.getHeight()+titleWidth/3,this.getHeight()/4);
//System.out.println(titleWidth);
//System.out.println(this.getHeight()*(-70)/100+" - "+this.getHeight());
}
#Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
//System.out.println("Bouton : "+arg0.getButton());
}
#Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
//System.out.println("Bouton : "+arg0.getButton());
PointerInfo pointer = MouseInfo.getPointerInfo();
Point mouseLocation = pointer.getLocation();
xMouseOrigin = (int) mouseLocation.getX();
yMouseOrigin = (int) mouseLocation.getY();
if (arg0.getButton()==MouseEvent.BUTTON1) {
mousePressed=true;
t= new Thread(new TrackPosition());
t.start();
} else if (arg0.getButton()==MouseEvent.BUTTON3) {
cursorValue=initialCursorValue;
repaint();
knobValue=cursorValue;
}
}
#Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
mousePressed=false;
//System.out.println("Mouse released");
repaint();
}
class TrackPosition implements Runnable {
#Override
public void run() {
// TODO Auto-generated method stub
while (mousePressed==true) {
PointerInfo pointer = MouseInfo.getPointerInfo();
Point mouseLocation = pointer.getLocation();
yMouse = (int) mouseLocation.getY();
yDeltaMouse=(float)(yMouse-yMouseOrigin)/100;
cursorValue=cursorValue+yDeltaMouse;
yMouseOrigin=yMouse;
if (cursorValue >=1) {
cursorValue=1;
} else if (cursorValue <= 0) {
cursorValue=0;
}
//This repaint is a problem if I "uncomment" it
repaint();
knobValue=cursorValue;
}
}
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
frame.setSize(300,70);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Knob(0.5, new Color(0,255,0,255), 3, 9, "Gain"));
frame.setVisible(true);
}
}
You miss to call the paintComponent(Graphics g) method from JComponent.
Changing your code as below will solve the problem.
public void paintComponent(Graphics g) {
super.paintComponent(g);
I'm writing a program to transform, rotate and scale a 2d square. I have the transformation and rotation working but I need help with the scaling. I can't seem to find any help on the internet to help since I have to use a math equation and I can't find the equation needed. Just so you know I don't want to use gl.glScaled(). I need to use a math equation but I can't figure it out.
package lab2;
public class Square {
public double [][] vertices = new double[4][2];
public Square(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
{
vertices[0][0]=x1;
vertices[0][1]=y1;
vertices[1][0]=x2;
vertices[1][1]=y2;
vertices[2][0]=x3;
vertices[2][1]=y3;
vertices[3][0]=x4;
vertices[3][1]=y4;
}
public double area()
{
return (vertices[1][0]-vertices[0][0])*(vertices[1][0]-vertices[0][0])+(vertices[1][1]-vertices[0][1])*(vertices[1][1]-vertices[0][1]);
}
public void translate(double tx, double ty)
{
for(int i=0;i<vertices.length;i++)
{
vertices[i][0]+=tx;
vertices[i][1]+=ty;
}
}
public void rotate(double theta)
{
//double x =0;
double x = (vertices[0][0]+vertices[2][0])/2;
double y = (vertices[0][1]+vertices[2][1])/2;
double oldX;
int i;
for(i = 0; i < 4; i++){
oldX = vertices[i][0];
vertices[i][0] = x + (vertices[i][0]-x)*Math.cos(theta*0.0174532925199)-(vertices[i][1]- y)*Math.sin(theta*0.0174532925199);
vertices[i][1] = y + (oldX-x)*Math.sin(theta*0.0174532925199)+(vertices[i][1]-y)*Math.cos(theta*0.0174532925199);
}
}
public void scale(double sx, double sy)
{
}
}
Then I also have this SquareControl.java so that I can control how I want it to work so for this program I'll be using key events
package lab2;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
public class Square_Control implements GLEventListener, KeyListener {
Square square = new Square(100,100,200,100,200,200,100,200);
boolean rotating = false;
boolean scaling = false;
boolean enlarge = true;
double theta = 1;
double sx = 1.01, sy = 1.01;
GLProfile glp;
GLCapabilities caps;
GLCanvas canvas;
public Square_Control()
{
glp = GLProfile.getDefault();
caps = new GLCapabilities(glp);
canvas = new GLCanvas(caps);
Frame frame = new Frame("AWT Window Test");
frame.setSize(300, 300);
frame.add(canvas);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
canvas.addGLEventListener(this);
canvas.addKeyListener(this);
canvas.requestFocus();
Animator animator = new FPSAnimator(canvas,60);
animator.add(canvas);
animator.start();
}
public static void main(String[] args) {
Square_Control sqc = new Square_Control();
}
public void update()
{
if (rotating)
square.rotate(theta);
if (scaling)
{
square.scale(1, 1);
}
}
#Override
public void display(GLAutoDrawable drawable) {
update();
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glColor3f(1, 0, 0);
gl.glBegin(GL.GL_LINE_LOOP);
gl.glVertex2d(square.vertices[0][0], square.vertices[0][1]);
gl.glVertex2d(square.vertices[1][0], square.vertices[1][1]);
gl.glVertex2d(square.vertices[2][0], square.vertices[2][1]);
gl.glVertex2d(square.vertices[3][0], square.vertices[3][1]);
gl.glEnd();
}
#Override
public void dispose(GLAutoDrawable drawable) {
// TODO Auto-generated method stub
}
#Override
public void init(GLAutoDrawable drawable) {
// TODO Auto-generated method stub
GL2 gl = drawable.getGL().getGL2();
gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0, 300, 0, 300, -1, 1);
gl.glViewport(0, 0, 300, 300);
}
#Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_RIGHT)
square.translate(4, 0);
else if (arg0.getKeyCode() == KeyEvent.VK_LEFT)
square.translate(-4, 0);
else if (arg0.getKeyCode() == KeyEvent.VK_UP)
square.translate(0, 4);
else if (arg0.getKeyCode() == KeyEvent.VK_DOWN)
square.translate(0, -4);
//also add code to toggle rotation/scaling
if(arg0.getKeyCode() == KeyEvent.VK_R)
{
rotating = !rotating;
}
if(arg0.getKeyCode() == KeyEvent.VK_S)
{
scaling = !scaling;
}
}
#Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
To scale an object, you simply have to multiply each vertex by your scaling factor. A scaling factor of 1.0 will do nothing while a scaling factor of 2.0 will double de position of each vertex, hence scaling it AND probably translating it.
If you want the object to stay in place, you'll have to first translate it to your object's center, scale, then translate back to it's original position.
That is if you want to do the job software.
You should consider using Matrix functions. glRotatef, glTranslatef and glScalef.
I'm trying to update the game's viewport when the game resizes, but when I start the game, I get a java.lang.NullPointerException:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.utils.viewport.Viewport.apply(Viewport.java:49)
at com.badlogic.gdx.utils.viewport.ExtendViewport.update(ExtendViewport.java:90)
at com.badlogic.gdx.utils.viewport.Viewport.update(Viewport.java:57)
at me.chrisjosten.testgame.screens.MainScreen.resize(MainScreen.java:82)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at me.chrisjosten.testgame.create(MainScreen.java:13)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
In the class, that implements Screen, I have in the resize method, the place where the exception occurs, the following code:
#Override
public void resize(int w, int h) {
viewport.update(w, h);
}
where the viewport is an ExtendViewport created in the constructor of the class. I've tried putting that in the show method to, but then I get the same result.
The full code of the class:
package me.chrisjosten.testgame.screens;
import me.chrisjosten.testgame.MyGame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class MainScreen implements Screen{
private MyGame game;
private BitmapFont font;
private SpriteBatch batch;
private OrthographicCamera camera;
private Viewport viewport;
private boolean goingUp = false;
private float alpha = 1;
private int gameWidth = 100;
private int gameHeight = 100;
private int screenWidth;
private int screenHeight;
public MainScreen(MyGame g) {
game = g;
System.out.println("screen created");
}
#Override
public void show() {
System.out.println("show");
font = new BitmapFont(Gdx.files.internal("fonts/monospace.fnt"));
font.setColor(1, 1, 1, 1);
batch = new SpriteBatch();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.translate(gameWidth / 2, gameHeight / 2);
viewport = new ExtendViewport(gameWidth, gameHeight, camera);
}
#Override
public void render(float delta) {
camera.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (goingUp) {
font.setColor(1, 1, 1, alpha);
alpha += 0.025;
} else {
font.setColor(1, 1, 1, alpha);
alpha -= 0.025;
}
if (alpha <= 0) {
goingUp = true;
alpha = 0;
} else if (alpha >= 1) {
goingUp = false;
alpha = 1;
}
batch.setProjectionMatrix(camera.combined);
batch.begin();
String press = "Press start";
font.draw(batch, press, gameWidth / 2 - font.getBounds(press).width / 2, 1);
batch.end();
}
#Override
public void resize(int w, int h) {
viewport.update(w, h);
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
font.dispose();
}
}
Does anyone know what I have done wrong?
By moving the code from the show method to the constructor, it works.
Started making a game.
Here's some of my code.
package games.tribe.screens;
import games.tribe.model.World;
import games.tribe.view.WorldRenderer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
public class GameScreen implements Screen {
private World world;
private WorldRenderer renderer;
/** This was the bit I'd missed --------------------------------------**/
#Override
public void show() {
world = new World();
renderer = new WorldRenderer(world);
}
/**------------------------------------------------------------------**/
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
renderer.render();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
}
here's the WorldRenderer Class:
package games.tribe.view;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import games.tribe.model.Block;
import games.tribe.model.TribalHunter;
import games.tribe.model.World;
public class WorldRenderer {
private World world;
private OrthographicCamera cam;
/**for debug rendering**/
ShapeRenderer debugRenderer = new ShapeRenderer();
public WorldRenderer(World world) {
this.world = world;
this.cam = new OrthographicCamera(10, 7);
this.cam.position.set(5, 3.5f, 0);
this.cam.update();
}
public void render() {
//render blocks
debugRenderer.setProjectionMatrix(cam.combined);
debugRenderer.begin(ShapeType.Rectangle);
for(Block block : world.getBlocks()) {
Rectangle rect = block.getBounds();
float x1 = block.getPosition().x + rect.x;
float y1 = block.getPosition().y + rect.y;
debugRenderer.setColor(new Color(1, 0, 0, 1));
debugRenderer.rect(x1, y1, rect.width, rect.height);
}
//render hunter
TribalHunter hunter = world.getHunter();
Rectangle rect = hunter.getBounds();
float x1 = hunter.getPosition().x + rect.x;
float y1 = hunter.getPosition().y + rect.y;
debugRenderer.setColor(new Color(0, 1, 0, 1));
debugRenderer.rect(x1, y1, rect.width, rect.height);
debugRenderer.end();
}
}
This is the exception I'm getting when I run it as a desktop application:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at games.tribe.screens.GameScreen.render(GameScreen.java:19)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:202)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)
AL lib: ReleaseALC: 1 device not closed
Line 46 of gdx.Game.render is this method:
#Override
public void render () {
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());
}
Any help will be appreciated
Thanks in advance
In the GameScreen's render() method, has the renderer been initialized? That could be causing the problem if it hasn't.
Edit: The problem you're having, according to the top two lines of the error, is a NullPointerException on line 19 of the class GameScreen. The NullPointerException only occurs when an object is used for some action when the object itself is null because it likely hasn't been initialized.
Line 19 of the GameScreen is:
renderer.render();
...but the object renderer has not been initialized anywhere, so it is currently null which is the default. To avoid getting this error, you'll need to initialize the renderer object before you run that line of code. Perhaps with something like this:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
renderer = new WorldRenderer();
renderer.render();
}
I am not familiar with libgdx, so I can't be sure that's exactly how a WorldRenderer is initialized, but you need to do something of the sort. I hope this helps.
I am new to using OpenGL and am experimenting with jogl. I am able to draw triangles without a problem however when I try and draw quads (used in many tutorials) eclipse keeping telling me GL.GL_QUADS cannot be resolved.
gl.glBegin(GL.GL_QUADS);
Not sure what I'm doing wrong.
Thanks,
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.*;
public class SimpleScene implements GLEventListener {
public static void main(String[] args) {
GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);
GLCanvas canvas = new GLCanvas(caps);
final Frame frame = new Frame("AWT Window Test");
frame.setSize(300, 300);
frame.add(canvas);
frame.setVisible(true);
// by default, an AWT Frame doesn't do anything when you click
// the close button; this bit of code will terminate the program when
// the window is asked to close
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
System.exit(0);
}
});
canvas.addGLEventListener(new SimpleScene());
FPSAnimator animator = new FPSAnimator(canvas, 60);
animator.add(canvas);
animator.start();
}
#Override
public void display(GLAutoDrawable arg0) {
update();
render(arg0);
}
private void update() {
// TODO Auto-generated method stub
}
private void render(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
// gl.glViewport(0, 0, 300, 300); //Possibly use to move around object
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glPushMatrix();
gl.glTranslatef(-1.5f,1.5f,0.0f); // Move left 1.5 units, up 1.5 units, and back 8 units
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(10, 0, 0);
// Begin drawing triangles
gl.glVertex3f( 0.0f, 1.0f, 0.0f); // Top vertex
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom left vertex
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom right vertex
gl.glEnd(); // Finish drawing triangles
gl.glPopMatrix();
}
#Override
public void dispose(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
#Override
public void init(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
#Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
int arg4) {
// TODO Auto-generated method stub
}
}
The javax.media.opengl.GL interface contains a subset of OpenGL common to GL 3, GL 2, GL ES 2.0 and GL ES 1.x, and GL_QUADS is not part of this subset.
If you use the javax.media.opengl.GL2, you get GL2.GL_QUADS.
It's a static constant in the GL2 class. You need to call:
gl.glBegin(GL2.GL_QUADS);
Rather than:
gl.glBegin(GL.GL_QUADS);