How to pick objects using jogl? - java

I met some problems about jogl picking. I need to pick each single point and process it, but I always get 0 hit (nothing is picked). Can anyone help me with this?
the display function can correctly get the 2x2 window around the cursor.
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
switch(cmd){
case UPDATE:
gl.glPushMatrix();
gl.glMultMatrixf(rot_matrix, 0);
buildPoints(gl);
buildAxes(gl);
gl.glPopMatrix();
break;
case SELECT:
int buffsize = 512;
double x = mouseX, y = mouseY;
int[] viewPort = new int[4];
IntBuffer selectBuffer = Buffers.newDirectIntBuffer(buffsize);
int hits = 0;
gl.glGetIntegerv(GL2.GL_VIEWPORT, viewPort, 0);
gl.glSelectBuffer(buffsize, selectBuffer);
gl.glRenderMode(GL2.GL_SELECT);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
glu.gluPickMatrix(x, (double) viewPort[3] - y, 2.0d, 2.0d, viewPort, 0);
//draw graph
gl.glPushMatrix();
gl.glMultMatrixf(rot_matrix, 0);
buildPoints(gl);
buildAxes(gl);
gl.glPopMatrix();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glFlush();
hits = gl.glRenderMode(GL2.GL_RENDER);
processHits(hits, selectBuffer);
cmd = UPDATE;
break;
}
}
so I guess maybe the drawing graph part for picking is not correct. Here is the code of buildPoints function.
public void buildPoints(GL2 gl) {
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glPointSize((float) radius / 2);
int pointName = 0;
gl.glBegin(GL.GL_POINTS);
for (point p : pointsList) {
if(cmd == SELECT) gl.glLoadName(pointName);
gl.glPushMatrix();
gl.glTranslatef(p.getX(), p.getY(), p.getZ());
gl.glColor3f(0.95f, 0.207f, 0.031f);
gl.glVertex3f((float) (p.getX() * scaleFactor),
(float) (p.getY() * scaleFactor),
(float) (p.getZ() * scaleFactor));
gl.glPopMatrix();
pointName++;
}
gl.glEnd();
}

I advise you to look at this example of picking using JOGL 2. However, OpenGL build-in picking is deprecated and shouldn't be used. We discussed a lot about it on our official forum, especially in this thread.

Related

First Person camera movement with LibGDL

I've looked for hours but I couldn't find anything but two StackOverflow questions, one of which had one answer, by the author, not really giving proper explanation.
I was working on a game with LWJGL2.9 and now decided to switch to LibGDX for its simplicity. I had working code that allowed me to move through space - but that code can't be converted 1:1 to LibGDX and I can't find any example, explanation, tutorial or anything of the like, not even in LibGDX's wiki.
This is the code that I used for LWJGL:
public Vectorf calculateMovement(MovementDirection direction, float newYaw, float walkSpeed, double deltaTime) {
Vectorf position = new Vectorf(0f, 0f, 0f); // Just a Vector3f
float amount = (walkSpeed) * (float) deltaTime;
switch (direction) {
case FORWARD:
case BACKWARD: {
float dx = (float) (direction.getXAdd() * Math.sin(Math.toRadians(newYaw))) * amount;
float dz = (float) -(direction.getZAdd() * Math.cos(Math.toRadians(newYaw))) * amount;
position.add(dx, direction.getYAdd() * amount, dz);
break;
}
case LEFT:
case RIGHT: {
float dx = (amount * (float) (Math.sin(Math.toRadians(newYaw + 90))) * direction.getXAdd());
float dz = (amount * (float) (Math.cos(Math.toRadians(newYaw + 90))) * direction.getZAdd());
position.add(dx, direction.getYAdd() * amount, dz);
break;
}
default: {
position.add(direction.getXAdd() * amount, direction.getYAdd() * amount, direction.getZAdd() * amount);
}
}
return position;
}
public enum MovementDirection {
FORWARD(1, 1),
BACKWARD(-1, -1),
LEFT(-1, 1),
RIGHT(1, -1),
UP(1),
DOWN(-1);
private final int xAdd;
private final int yAdd;
private final int zAdd;
MovementDirection(int yAdd) {
this(0, yAdd, 0);
}
MovementDirection(int xAdd, int zAdd) {
this(xAdd, 0, zAdd);
}
MovementDirection(int xAdd, int yAdd, int zAdd) {
this.xAdd = xAdd;
this.yAdd = yAdd;
this.zAdd = zAdd;
}
public float getXAdd() {
return xAdd;
}
public float getYAdd() {
return yAdd;
}
public float getZAdd() {
return zAdd;
}
}
I then updated the view matrix before rendering every frame calling the following method:
public static Matrix4f createViewMatrix(Camera camera) {
Matrix4f matrix = new Matrix4f();
matrix.setIdentity();
Matrix4f.rotate((float) Math.toRadians(camera.getYaw()), new Vector3f(1, 0, 0), matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(camera.getPitch()), new Vector3f(0, 1, 0), matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(camera.getDelta()), new Vector3f(0, 0, 1), matrix, matrix);
Matrix4f.translate(camera.getNegativePosition(), matrix, matrix);
return matrix;
}
LibGDX, though, uses matrixes in a totally different way and I have no idea how to configure it (or how to properly update the view matrix, for that matter). Here's what I have:
public static Matrix4 createViewMatrix(Camera camera) {
Matrix4 matrix = new Matrix4();
matrix.setToLookAt(camera.direction, camera.up);
return matrix;
}
I'm not really even sure how I would be using that, I'm completely lost. I can draw a cube on the screen and move my mouse around to see it but as soon as I try to move (W,A,S,D) the cube disappears (I'm guessing it has to do with the view matrix practically not being applied).
I know that it's a long read and the code may even be bad but I already could find little to nothing for LWJGL let alone LibGDX.
I finally got it. I was drawing before moving my camera and that, apparently, was the issue.
My render loop previously looked like this:
#Override
public void render() {
// Other code
Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
Gdx.gl.glClearColor(0, 0, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
// Handle camera input
camera.update();
modelBatch.render(camera);
Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
}
I simply moved the camera input handling and camera update above the "render" code:
#Override
public void render() {
// Other code
// Handle camera input
camera.update();
Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
Gdx.gl.glClearColor(0, 0, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.render(camera);
Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
}

Analog clock in OpenGL 2.0 using Java

Can someone help to make a static analogue clock in OpenGL 2.0 using Java? I used Bresenham Circle to draw a circle for the clock but is there any other way to draw a circle here? Then I drew an hour, minute, and second hand but I got an error in the render part while drawing the numbers. Can someone help me here with how to fix this? The code which I tried is following:
public class MyClock implements GLEventListener {
private int windowWidth = 1000;
private int windowHeight = 900;
public void display(GLAutoDrawable drawable) {
render(drawable);
}
//render
private void render(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glPointSize(5.0f);
//drawing circle
bresenhamCircle(new Point(100,100), 200, new Color(1, 0, 1), gl);
double x, y, d, delta_SE, delta_E;
x = 0;
double r = 200;
y = r;
d = 5-4*r;
setPixel(x ,y, gl);
setPixel(-x,y, gl);
setPixel(x,-y, gl);
setPixel(-x,-y, gl);
setPixel(y,x, gl);
setPixel(-y,x, gl);
setPixel(y,-x, gl);
setPixel(-y,-x, gl);
while (y>x) {
if (d >= 0) {
delta_SE = 4*(2*(x-y)+5);
d+=delta_SE; x++; y--;
}
else {
delta_E = 4*(2*x+3);
d+=delta_E; x++;
}
setPixel(x,y, gl);
setPixel(-x,y, gl);
setPixel(x,-y, gl);
setPixel(-x,-y, gl);
setPixel(y,x, gl);
setPixel(-y,x, gl);
setPixel(y,-x, gl);
setPixel(-y,-x, gl);
}
//hour hand
gl.glColor3d(0, 0, 1);
gl.glBegin(GL2.GL_LINES);
gl.glVertex2d(0, 00);
gl.glVertex2d(70, 70);
gl.glEnd();
//minute hand
gl.glColor3d(0, 0, 1);
gl.glBegin(GL2.GL_LINES);
gl.glVertex2d(0, 00);
gl.glVertex2d(150, 20);
gl.glEnd();
//seconds hand
gl.glColor3d(1, 0, 0);
gl.glBegin(GL2.GL_LINES);
gl.glVertex2d(0, 00);
gl.glVertex2d(120, -120);
gl.glEnd();
//drawing numbers
int AngleX, AngleY;
int radius;
double line;
for (int i=1;i<=12;i++) {
line = i/12*Math.PI*2;
radius=170;
AngleX=(int)((0)+(Math.sin(line)*radius));
AngleY=(int)((0)+(Math.cos(line)*radius));
gl.glColor3d(1, 1, 1);
String a= Integer.toString(i);
g.drawString(a,AngleX,AngleY);
}
}
//Bresenham Circle method
public void bresenhamCircle(Point center, double radius, Color color, GL2 gl) {
gl.glColor3d(0, 0, 1);
gl.glBegin(GL2.GL_POINTS);
gl.glVertex2d(00,200);
gl.glEnd();
}
private void setPixel(double x, double y,GL2 gl) {
gl.glColor3d(0.0, 1.0, 0.0);
gl.glBegin(GL2.GL_POINTS);
gl.glVertex2d(0,0);
gl.glVertex2d( x, y);
gl.glEnd();
}
public void dispose(GLAutoDrawable drawable) {
}
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glViewport(0, 0, windowWidth, windowHeight);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(-windowWidth / 2, windowWidth / 2, -windowHeight / 2, windowHeight / 2, 0, 1);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
gl.glEnable(GL2.GL_LINE_SMOOTH);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
GL2 gl = drawable.getGL().getGL2();
windowWidth = w;
windowHeight = h;
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(-w / 2, w / 2, -h / 2, h / 2, 0, 1);
}
}
As far as I know, there isn't a simple method built into OpenGL ES 2.0 that draws text. That means you have to either find an open-source library that you can use, or you can create your own way of rendering text. This post outlines nicely the different ways that you can render text with OpenGL.
The best way I have found based on my research and which is also stated in the above link, is to render text through images. This tutorial shows how to create a basic text engine and is what I used to get an idea of how to render custom text. The idea is to take a texture atlas (a texture atlas is one image that contains all the letters, numbers, characters that you want to be able to render) and based on what string you want to draw, the engine will crop out the necessary letters, numbers, or characters from the atlas needed for your string and combine them into a square polygon that you can then render to the screen. The tutorial that I linked is a basic text engine, but once you understand how it works, you can then modify and improve it to your needs.

Kinect & Processing: Passing skeleton hand data to mouse position

I've been working on this a while and feel so close! Should be easy, but I'm still new to this.
The skeleton hand data is being passed in as joints[KinectPV2.JointType_HandLeft] and can be accessed through joint.getX() and joint.getY(). I want to pass this data into the update function to replace mouseX and mouseY. I'm guessing I have to create global variables to access it within the update function or maybe I have to pass the skeleton data as parameters into the update function? How can I replace the mouse position data with the hand position?
import KinectPV2.*;
KinectPV2 kinect;
private class MyFluidData implements DwFluid2D.FluidData{
// update() is called during the fluid-simulation update step.
#Override
public void update(DwFluid2D fluid) {
float px, py, vx, vy, radius, vscale, temperature;
radius = 15;
vscale = 10;
px = width/2;
py = 50;
vx = 1 * +vscale;
vy = 1 * vscale;
radius = 40;
temperature = 1f;
fluid.addDensity(px, py, radius, 0.2f, 0.3f, 0.5f, 1.0f);
fluid.addTemperature(px, py, radius, temperature);
particles.spawn(fluid, px, py, radius, 100);
boolean mouse_input = mousePressed;
// add impulse: density + velocity, particles
if(mouse_input && mouseButton == LEFT){
radius = 15;
vscale = 15;
px = mouseX;
py = height-mouseY;
vx = (mouseX - pmouseX) * +vscale;
vy = (mouseY - pmouseY) * -vscale;
fluid.addDensity (px, py, radius, 0.25f, 0.0f, 0.1f, 1.0f);
fluid.addVelocity(px, py, radius, vx, vy);
particles.spawn(fluid, px, py, radius*2, 300);
}
// add impulse: density + temperature, particles
if(mouse_input && mouseButton == CENTER){
radius = 15;
vscale = 15;
px = mouseX;
py = height-mouseY;
temperature = 2f;
fluid.addDensity(px, py, radius, 0.25f, 0.0f, 0.1f, 1.0f);
fluid.addTemperature(px, py, radius, temperature);
particles.spawn(fluid, px, py, radius, 100);
}
// particles
if(mouse_input && mouseButton == RIGHT){
px = mouseX;
py = height - 1 - mouseY; // invert
radius = 50;
particles.spawn(fluid, px, py, radius, 300);
}
}
}
int viewport_w = 1280;
int viewport_h = 720;
int viewport_x = 230;
int viewport_y = 0;
int gui_w = 200;
int gui_x = 20;
int gui_y = 20;
int fluidgrid_scale = 3;
DwFluid2D fluid;
// render targets
PGraphics2D pg_fluid;
//texture-buffer, for adding obstacles
PGraphics2D pg_obstacles;
// custom particle system
MyParticleSystem particles;
// some state variables for the GUI/display
int BACKGROUND_COLOR = 0;
boolean UPDATE_FLUID = true;
boolean DISPLAY_FLUID_TEXTURES = false;
boolean DISPLAY_FLUID_VECTORS = false;
int DISPLAY_fluid_texture_mode = 0;
boolean DISPLAY_PARTICLES = true;
public void settings() {
size(viewport_w, viewport_h, P2D);
smooth(4);
}
public void setup() {
surface.setLocation(viewport_x, viewport_y);
// main library context
DwPixelFlow context = new DwPixelFlow(this);
context.print();
context.printGL();
// fluid simulation
fluid = new DwFluid2D(context, viewport_w, viewport_h, fluidgrid_scale);
// set some simulation parameters
fluid.param.dissipation_density = 0.999f;
fluid.param.dissipation_velocity = 0.99f;
fluid.param.dissipation_temperature = 0.80f;
fluid.param.vorticity = 0.10f;
fluid.param.timestep = 0.25f;
fluid.param.gridscale = 8f;
// interface for adding data to the fluid simulation
MyFluidData cb_fluid_data = new MyFluidData();
fluid.addCallback_FluiData(cb_fluid_data);
// pgraphics for fluid
pg_fluid = (PGraphics2D) createGraphics(viewport_w, viewport_h, P2D);
pg_fluid.smooth(4);
pg_fluid.beginDraw();
pg_fluid.background(BACKGROUND_COLOR);
pg_fluid.endDraw();
// pgraphics for obstacles
pg_obstacles = (PGraphics2D) createGraphics(viewport_w, viewport_h, P2D);
pg_obstacles.smooth(4);
pg_obstacles.beginDraw();
pg_obstacles.clear();
float radius;
radius = 200;
pg_obstacles.stroke(64);
pg_obstacles.strokeWeight(1);
pg_obstacles.fill(0);
pg_obstacles.rect(1*width/2f, 1*height/4f, radius, radius/2, 10);
pg_obstacles.stroke(64);
pg_obstacles.strokeWeight(1);
pg_obstacles.fill(0);
pg_obstacles.rect(1*width/3.5f, 1*height/2.5f, radius, radius/2, 10);
//// border-obstacle
//pg_obstacles.strokeWeight(20);
//pg_obstacles.stroke(64);
//pg_obstacles.noFill();
//pg_obstacles.rect(0, 0, pg_obstacles.width, pg_obstacles.height);
pg_obstacles.endDraw();
fluid.addObstacles(pg_obstacles);
// custom particle object
particles = new MyParticleSystem(context, 1024 * 1024);
kinect = new KinectPV2(this);
//Enables depth and Body tracking (mask image)
kinect.enableDepthMaskImg(true);
kinect.enableSkeletonDepthMap(true);
kinect.init();
background(0);
frameRate(60);
}
public void draw() {
PImage imgC = kinect.getDepthMaskImage();
image(imgC, 0, 0, 320, 240);
//get the skeletons as an Arraylist of KSkeletons
ArrayList<KSkeleton> skeletonArray = kinect.getSkeletonDepthMap();
//individual joints
for (int i = 0; i < skeletonArray.size(); i++) {
KSkeleton skeleton = (KSkeleton) skeletonArray.get(i);
//if the skeleton is being tracked compute the skleton joints
if (skeleton.isTracked()) {
KJoint[] joints = skeleton.getJoints();
color col = skeleton.getIndexColor();
fill(col);
stroke(col);
drawHandState(joints[KinectPV2.JointType_HandRight]);
drawHandState(joints[KinectPV2.JointType_HandLeft]);
}
}
// update simulation
if(UPDATE_FLUID){
fluid.addObstacles(pg_obstacles);
fluid.update();
particles.update(fluid);
}
// clear render target
pg_fluid.beginDraw();
pg_fluid.background(BACKGROUND_COLOR);
pg_fluid.endDraw();
// render fluid stuff
if(DISPLAY_FLUID_TEXTURES){
// render: density (0), temperature (1), pressure (2), velocity (3)
fluid.renderFluidTextures(pg_fluid, DISPLAY_fluid_texture_mode);
}
if(DISPLAY_FLUID_VECTORS){
// render: velocity vector field
fluid.renderFluidVectors(pg_fluid, 10);
}
if( DISPLAY_PARTICLES){
// render: particles; 0 ... points, 1 ...sprite texture, 2 ... dynamic points
particles.render(pg_fluid, BACKGROUND_COLOR);
}
// display
image(pg_fluid , 320, 0);
image(pg_obstacles, 320, 0);
// display number of particles as text
//String txt_num_particles = String.format("Particles %,d", particles.ALIVE_PARTICLES);
//fill(0, 0, 0, 220);
//noStroke();
//rect(10, height-10, 160, -30);
//fill(255,128,0);
//text(txt_num_particles, 20, height-20);
// info
//String txt_fps = String.format(getClass().getName()+ " [size %d/%d] [frame %d] [fps %6.2f]", fluid.fluid_w, fluid.fluid_h, fluid.simulation_step, frameRate);
//surface.setTitle(txt_fps);
}
//draw a ellipse depending on the hand state
void drawHandState(KJoint joint) {
noStroke();
handState(joint.getState());
//println(joint.getState());
pushMatrix();
translate(joint.getX(), joint.getY(), joint.getZ());
//println(joint.getX(), joint.getY(), joint.getZ());
ellipse(joint.getX(), joint.getY(), 70, 70);
popMatrix();
}
/*
Different hand state
KinectPV2.HandState_Open
KinectPV2.HandState_Closed
KinectPV2.HandState_Lasso
KinectPV2.HandState_NotTracked
*/
//Depending on the hand state change the color
void handState(int handState) {
switch(handState) {
case KinectPV2.HandState_Open:
fill(0, 255, 0);
break;
case KinectPV2.HandState_Closed:
fill(255, 0, 0);
break;
case KinectPV2.HandState_Lasso:
fill(0, 0, 255);
break;
case KinectPV2.HandState_NotTracked:
fill(100, 100, 100);
break;
}
}
I'm guessing I have to create global variables to access it within the update function or maybe I have to pass the skeleton data as parameters into the update function?
What happened when you tried those approaches?
Either approach sounds fine. You could store the variables in a sketch-level variable, set those variables from the kinect code, then use those variables in your drawing code. Or you could pass the variables as a parameter to the drawing code. Either should work fine. I'd probably go for the first approach because it sounds easier to me, but that's just my personal preference.
I suggest working in smaller chunks. Create a separate program that ignores the kinect for now. Create a hard-coded sketch-level variable that holds the same type of information you'd get from the kinect. Then write drawing code that uses that hard-coded variable to draw the frame. Get that working perfectly before you try adding the kinect code back in.
Then if you get stuck on a specific step, you can post a MCVE and we can go from there. Good luck.

Display image in JOGL

I'm currently working on an assignment where I need to create a game in JOGL, it's going pretty well, but I've run into a problem:
I want to create a menu function that can be accessed by pressing ESC ingame, when ESC is pressed the display function needs to stop displaying the game and start displaying the menu. The menu consists of a background image with some text overlay.
This is how I tried to implement the menu function, but I haven't managed to let it show anything else than the clear color:
public class OptionsMenu
{
public void display(GL gl) {
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0, 300, 300, 0, 0, 1);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glDisable(GL.GL_DEPTH_TEST);
gl.glClearColor(1f, 0.5f, 0.5f, 0.5f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glBlendFunc (GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable (GL.GL_BLEND);
BufferedImage bufferedImage = null;
int w = 0;
int h = 0;
try {
bufferedImage = ImageIO.read(OptionsMenu.class.getResource("menuBackground.jpg")); //The menu background
w = ceilingPow2(bufferedImage.getWidth());
h = ceilingPow2(bufferedImage.getHeight());
} catch (IOException e) {
e.printStackTrace();
}
WritableRaster raster =
Raster.createInterleavedRaster (DataBuffer.TYPE_BYTE,
w,
h,
4,
null);
ComponentColorModel colorModel=
new ComponentColorModel (ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,8},
true,
false,
ComponentColorModel.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
BufferedImage img =
new BufferedImage (colorModel,
raster,
false,
null);
Graphics2D g = img.createGraphics();
g.drawImage(bufferedImage, null, null);
DataBufferByte imgBuf =
(DataBufferByte)raster.getDataBuffer();
byte[] imgRGBA = imgBuf.getData();
ByteBuffer bb = ByteBuffer.wrap(imgRGBA);
bb.position(0);
bb.mark();
gl.glBindTexture(GL.GL_TEXTURE_2D, 13);
gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
gl.glTexImage2D (GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, w, h, 0, GL.GL_RGBA,
GL.GL_UNSIGNED_BYTE, bb);
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBindTexture (GL.GL_TEXTURE_2D, 13);
gl.glBegin (GL.GL_POLYGON);
gl.glTexCoord2d (0, 0);
gl.glVertex2d (0, 0);
gl.glTexCoord2d(1,0);
gl.glVertex2d (w, 0);
gl.glTexCoord2d(1,1);
gl.glVertex2d (w, h);
gl.glTexCoord2d(0,1);
gl.glVertex2d (0, h);
gl.glEnd ();
gl.glFlush();
}
private static int ceilingPow2(int n) {
int pow2 = 1;
while (n > pow2) {
pow2 = pow2<<1;
}
return pow2;
}
}
This code is based of this tutorial: http://wiki.tankaar.com/index.php?title=Displaying_an_Image_in_JOGL_(Part_1)
I'm calling upon the OptionsMenu like so:
public void display(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
GLU glu = new GLU();
if(state.equals("optionsMenu"))
{
if(menu == null)
menu = new OptionsMenu;
menu.display(gl);
}
else
{
// Calculating time since last frame.
Calendar now = Calendar.getInstance();
long currentTime = now.getTimeInMillis();
int deltaTime = (int)(currentTime - previousTime);
previousTime = currentTime;
// Update any movement since last frame.
updateMovement(deltaTime);
updateCamera();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
gl.glLoadIdentity();
glu.gluLookAt( camera.getLocationX(), camera.getLocationY(), camera.getLocationZ(),
camera.getVrpX(), camera.getVrpY(), camera.getVrpZ(),
camera.getVuvX(), camera.getVuvY(), camera.getVuvZ() );
// Display all the visible objects of MazeRunner.
for( Iterator<VisibleObject> it = visibleObjects.iterator(); it.hasNext(); ) {
it.next().display(gl);
}
gl.glLoadIdentity();
gl.glFlush();
}
}
It doesn't throw any errors, it just won't display the image. It does however show the clear color gl.glClearColor(1f, 0.5f, 0.5f, 0.5f); defined in the display function of the OptionsMenu
I'm pretty much stumped and I have no idea how I'm going to fix this.
Sorry for the long post, but I'd be REALLY grateful if someone would help me.
I guess clear color buffers gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
should be placed at the begining of display method of OptionsMenu.
I have few advices for example posted code above:
Change scope of the GLU glu = new GLU();, because glu assigned each time when opengl display callback method called.
Also move gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); right before if statement, and do not use every objects display method (e.g OptionsMenu.display()). Try keep it single.
Use TextureIO to upload texture, and if its possible just upload it once then use that texture in your display method. Texture loading process has great overhead.

OpenGL - draw line with points

The exercise I have is the following:
In display() add a method drawLine. Probably, you will need something like
drawLine(GL gl, int x1, int y1, int x2, int y2)
now using the equation of a line, plot the individual points that make up the line from x1,y1 to x2,y2 in JOGL
using (for instance) gl.glPointSize(1.0f); gl.glBegin(GL.GL_POINTS);
If you have coloured the points white, and used the following projections
gl.glViewport(-250, -150, 250, 150);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
then with a value of a line from points (0,0) to (100,100) yours will look
something like the following picture:
My code so far for the EventListener where I create my line is the following:
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;
public class ThirdGLEventListener implements GLEventListener {
/**
* Interface to the GLU library.
*/
private GLU glu;
/**
* Take care of initialization here.
*/
public void init(GLAutoDrawable gld) {
GL gl = gld.getGL();
glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glViewport(-250, -150, 250, 150);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
}
/**
* Take care of drawing here.
*/
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
/*
* put your code here
*/
drawLine(gl, 0, 0, 100, 100);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
}
public void displayChanged(GLAutoDrawable drawable,
boolean modeChanged, boolean deviceChanged) {
}
private void drawLine(GL gl, int x1, int y1, int x2, int y2) {
gl.glPointSize(1.0f);
gl.glBegin(GL.GL_POINTS);
// This is where the whole problem lies.
gl.glEnd();//end drawing of points
}
}
My problem is that I don't quite know how to complete the code inside the method for the line equation. I would appreciate it if you could help me with that bit. I think the rest of it is just right, I just need to find how to implement the line equation inside the drawLine() method.
Thats how you do it in OpenGL.
glBegin(GL_POINTS);
for(float i = 0; i <= 100; ){
glVertex2f(i,i);
i+=0.01;
}
glEnd();
Also, a suggestion you want to have your Orthographic projections in GL_PROJECTION matrix and your modelview like the above code in GL_MODELVIEW
Edited play around with your +=0.01 and you will get the results BETTER WAY
glBegin(GL_LINES);
glVertex2i(0,0);
glVertex2i(100,100);
glEnd();
Try using GL_LINES:
private void drawLine(GL gl, int x1, int y1, int x2, int y2) {
gl.glPointSize(1.0f);
gl.glBegin(GL.GL_POINTS);
int samples = 100;
float dx = (x2 - x1) / (float)samples;
float dy = (y2 - y1) / (float)samples;
for( int i = 0; i < samples; i++ )
{
gl.glVertex2f( i * dx, i * dy );
}
gl.glEnd();//end drawing of points
}
Adjust samples to taste.

Categories