Java OpenGL Cube Not Drawing - java

I am trying to create a cube, because that is the thing to do in OpenGl 2.9. However, I had a cube rendering originally, but I am trying to make my design a little better, but I am unable to see my cube now. Help me see my cube. The code will be below. I am not getting any errors, just unable to see my cube.
import java.awt.*;
import org.lwjgl.*;
import javax.swing.*;
public class Scene
{
private static boolean run = false;
private static int width = 640;
private static int height = 480;
long lastFrame; //time at last frame
int fps;
long lastFPS;
/** position of quad */
float x = 1, y = 1, z = 1; //400,300,400
/** angle of quad rotation */
float rotation = 0;
public void start()
{
try
{
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
} catch (LWJGLException e)
{
e.printStackTrace();
System.exit(0);
}
initGL(); // init OpenGL
getDelta(); // call once before loop to initialise lastFrame
lastFPS = getTime(); // call before loop to initialise fps timer
while (run == true)
{
int delta = getDelta();
update(delta);
renderGL();
Display.update();
Display.sync(60); // cap fps to 60fps
if (Keyboard.isKeyDown(Keyboard.KEY_Q))
{
break;
}
}
Display.destroy();
System.exit(0);
}
public void update(int delta) {
// rotate quad
rotation += 0.15f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) x -= 0.35f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) x += 0.35f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) y -= 0.35f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) y += 0.35f * delta;
// keep quad on the screen
if (x < 0) x = 0;
if (x > 800) x = 800;
if (y < 0) y = 0;
if (y > 600) y = 600;
updateFPS(); // update FPS Counter
}
public void renderGL() {
// Clear The Screen And The Depth Buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glColor3f(1, 0, 0);
// draw quad
GL11.glPushMatrix();
GL11.glTranslatef(x, y, z);
GL11.glRotatef(rotation, 0f, 0f, 1f);
GL11.glTranslatef(-x, -y, z);
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
GL11.glVertex3f( x, y,-z); // Top Right Of The Quad (Top)
GL11.glVertex3f(-x, y,-z); // Top Left Of The Quad (Top)
GL11.glVertex3f(-x, y, z); // Bottom Left Of The Quad (Top)
GL11.glVertex3f( x, y, z); // Bottom Right Of The Quad (Top)
GL11.glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
GL11.glVertex3f( x, -y, z); // Top Right Of The Quad (Bottom)
GL11.glVertex3f(-x, -y, z); // Top Left Of The Quad (Bottom)
GL11.glVertex3f(-x, -y,-z); // Bottom Left Of The Quad (Bottom)
GL11.glVertex3f( x,-y,-z); // Bottom Right Of The Quad (Bottom)
GL11.glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
GL11.glVertex3f( x, y, z); // Top Right Of The Quad (Front)
GL11.glVertex3f(-x, y, z); // Top Left Of The Quad (Front)
GL11.glVertex3f(-x,-y, z); // Bottom Left Of The Quad (Front)
GL11.glVertex3f( x,-y, z); // Bottom Right Of The Quad (Front)
GL11.glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
GL11.glVertex3f( x,-y,-z); // Bottom Left Of The Quad (Back)
GL11.glVertex3f(-x,-y,-z); // Bottom Right Of The Quad (Back)
GL11.glVertex3f(-x, y,-z); // Top Right Of The Quad (Back)
GL11.glVertex3f( x, y,-z); // Top Left Of The Quad (Back)
GL11.glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
GL11.glVertex3f(-x, y, z); // Top Right Of The Quad (Left)
GL11.glVertex3f(-x, y,-z); // Top Left Of The Quad (Left)
GL11.glVertex3f(-x,-y,-z); // Bottom Left Of The Quad (Left)
GL11.glVertex3f(-x,-y, z); // Bottom Right Of The Quad (Left)
GL11.glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
GL11.glVertex3f( x, y,-z); // Top Right Of The Quad (Right)
GL11.glVertex3f( x, y, z); // Top Left Of The Quad (Right)
GL11.glVertex3f( x,-y, z); // Bottom Left Of The Quad (Right)
GL11.glVertex3f( x,-y,-z); // Bottom Right Of The Quad (Right)
// GL11.glVertex2f(x - 50, y - 50);
// GL11.glVertex2f(x + 50, y - 50);
// GL11.glVertex2f(x + 50, y + 50);
// GL11.glVertex2f(x - 50, y + 50);
GL11.glEnd();
GL11.glPopMatrix();
}
/**
* Calculate how many milliseconds have passed
* since last frame.
*
* #return milliseconds passed since last frame
*/
public int getDelta() {
long time = getTime();
int delta = (int) (time - lastFrame);
lastFrame = time;
return delta;
}
/**
* Calculate the FPS and set it in the title bar
*/
public void updateFPS() {
if (getTime() - lastFPS > 1000) {
Display.setTitle("FPS: " + fps);
fps = 0;
lastFPS += 1000;
}
fps++;
}
/**
* Get the accurate system time
*
* #return The system time in milliseconds
*/
public long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public void initGL() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public static void main(String[] Args)
{
final JFrame info = new JFrame();
JButton button = new JButton();
JLabel infoText1 = new JLabel();
JLabel infoText2 = new JLabel();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
button.setText("Acknowledge");
button.setSize(20, 10);
infoText1.setText("Press Q to quit");
infoText2.setText("Click Acknowledge to start simulation");
info.setTitle("Operator's Manual");
info.setLayout(new FlowLayout());
info.add(infoText1);
info.add(infoText2);
info.setSize(240, 120);
info.setLocation(dim.width/2-info.getSize().width/2, dim.height/2-info.getSize().height/2);
info.add(button);
info.setVisible(true);
button.addActionListener(new java.awt.event.ActionListener()
{
#Override
public void actionPerformed(java.awt.event.ActionEvent acknowledge)
{
run = true;
Scene sceneStart = new Scene();
sceneStart.start();
}
});
}
}

EDIT: Your near and far values for glOrtho are backwards. Note that your later translate of 7 z-units will also move your object outside of the volume defined by glOrtho, which will cause it not to be drawn.
You are undoing your own translate in your model view matrix.
GL11.glTranslatef(1.5f, 0.0f, -7.0f);
GL11.glLoadIdentity(); // << reloads identity matrix, overriding the translate.
Correct is:
// Ensure model view matrix is starting is nice fresh state.
GL11.glLoadIdentity();
// Put the quad where you want it.
GL11.glTranslatef(1.5f, 0.0f, -7.0f);

Related

Drawing a line_loop in openGl is not drawing one corner of a box

When I use this code.
private void drawSelected() {
int width_border = 5; // border on left and right
int height_border = 5; // border on top and bottom
for (Entity e : selected) {
glPushMatrix();
glPushAttrib(GL_CURRENT_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(e.getX()+cameraX, e.getY()+cameraY, 0);
glBegin(GL_LINE_LOOP);
{
float x1 = -width_border + 1;
float y1 = -height_border;
float x2 = e.getSprite().getWidth() + width_border;
float y2 = e.getSprite().getHeight() + height_border-1;
glVertex2f(x1, y1);
glVertex2f(x1, y2);
glVertex2f(x2, y2);
glVertex2f(x2, y1);
}
glEnd();
glPopMatrix();
glPopAttrib();
}
}
I get this a box drawn like this.
http://imgur.com/bMtjmsc
This is openGL, where 0,0 is the top left corner of the map.
I don't know why this code is leaving out the bottom left corner of the box. Is there anything I can change to make it so it doesn't leave out that corner?

Replacing the background of the main menu in minecraft

I have gotten my code for this mostly working using some code i found for drawing with quads but it seems to only take up the lower left-hand corner of the minecraft screen with my image when it should take up the whole thing.
I think i need some way to scale it but i don't know why i should need that or if i do...
the code for doing the drawing is correct it's just that it's not drawing in the right spot... and i don't know why.
private void drawImageQuad(int textureHandle, int x, int y, float w, float h) {
// activate the specified texture
GL11.glBindTexture(GL11.GL_TEXTURE_2D,textureHandle);
// prepare to render in 2D
setOrthoOn();
// draw the image textured onto a quad
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0f, 0f);
GL11.glVertex3f( (float)x, (float)y, (float)0);
GL11.glTexCoord2f(1f, 0f);
GL11.glVertex3f( (float)x+w, (float)y, (float)0);
GL11.glTexCoord2f(1f, 1f);
GL11.glVertex3f( (float)x+w, (float)y+h, (float)0);
GL11.glTexCoord2f(0f, 1f);
GL11.glVertex3f( (float)x, (float)y+h, (float)0);
GL11.glEnd();
// restore the previous perspective and model views
setOrthoOff();
}
private void setOrthoOn()
{
// prepare to render in 2D
if(GL11.glIsEnabled(GL11.GL_DEPTH_TEST)){
depthTest=true;
GL11.glDisable(GL11.GL_DEPTH_TEST);
}
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPushMatrix(); // preserve perspective view
GL11.glLoadIdentity(); // clear the perspective matrix
GL11.glOrtho(0,width,0,height,-1,1); // turn on 2D
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix(); // Preserve the Modelview Matrix
GL11.glLoadIdentity(); // clear the Modelview Matrix
}
private void setOrthoOff()
{
// restore the original positions and views
if(depthTest){
GL11.glEnable(GL11.GL_DEPTH_TEST);
depthTest = false;
}
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPopMatrix();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPopMatrix();
}
/**
* Draws the screen and all the components in it.
*/
public void drawScreen(int par1, int par2, float par3)
{
//renderSkybox(par1, par2, par3);
Tessellator tessellator = Tessellator.instance;
double zoom = width/(double)384;
int widthOffset = (int)((width - (384*zoom))/2);
int widthOffset2 = widthOffset +(width*(384/256));
this.drawImageQuad(mc.renderEngine.getTexture("/title/background0.png"),widthOffset,(height - (int)(256*zoom))/2,widthOffset+(int)(256*zoom),height+((height - (int)(256*zoom))/2));
this.drawImageQuad(mc.renderEngine.getTexture("/title/background1.png"),widthOffset2,(height - (int)(256*zoom))/2,widthOffset2+(int)(256*zoom),height+((height - (int)(256*zoom))/2));

Billboarded sprites stop working if moved from 0,0,0

I got this code for billboarding I created (With the help of others, and a nehe tutorial), but it seems that when I move my sprites from 0,0,0 it stops working correctly, for example the closer I get to a sprite that's at 50,0,50 the more sideways it gets.
Any idea how to make the sprites billboard properly when not on 0,0,0?
Here is the code:
public void drawBillBoardSprite(Texture texture, Vector3f sprPos, float r, float g, float b, int sprWidth, int sprHeight){
// store the current model matrix
GL11.glPushMatrix();
// bind to the appropriate texture for this sprite
texture.bind();
Vector3f look = new Vector3f(0,0,0);
Vector3f.sub(player.pos, sprPos, look);
look.normalise();
Vector3f right = new Vector3f(0,0,0);
Vector3f.cross(new Vector3f(0,1,0) /* <-- default up vector */, look, right);
right.normalise();
Vector3f up = new Vector3f(0,0,0);
Vector3f.cross(look, right, up);
up.normalise();
Matrix4f m = new Matrix4f();
m.m00 = right.x;
m.m01 = right.y;
m.m02 = right.z;
m.m03 = 0;
m.m10 = up.x;
m.m11 = up.y;
m.m12 = up.z;
m.m13 = 0;
m.m20 = look.x;
m.m21 = look.y;
m.m22 = look.z;
m.m23 = 0;
m.m30 = sprPos.x;
m.m31 = sprPos.y;
m.m32 = sprPos.z;
m.m33 = 1;
java.nio.FloatBuffer fb = BufferUtils.createFloatBuffer(32);
m.store(fb);
fb.flip();
GL11.glMultMatrix(fb);
// draw a quad textured to match the sprite
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0); GL11.glVertex3f( - (sprWidth/2), (sprHeight/2), 0); // Top left Of The Quad (Left)
GL11.glTexCoord2f(texture.getWidth(), 0); GL11.glVertex3f( + (sprWidth/2), (sprHeight/2), 0); // Top right Of The Quad (Left)
GL11.glTexCoord2f(texture.getWidth(), texture.getHeight()); GL11.glVertex3f( + (sprWidth/2), -(sprHeight/2), 0); // Bottom right Of The Quad (Left)
GL11.glTexCoord2f(0, texture.getHeight()); GL11.glVertex3f( - (sprWidth/2), -(sprHeight/2), 0); // Bottom left Of The Quad (Left)
GL11.glEnd();
// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
}
Edit: tried this code based on Knetic's answer, but it didn't even rotate the sprites towards the camera. Here is the code I used:
public void drawBillBoardSprite(Texture texture, Vector3f sprPos, float r, float g, float b, int sprWidth, int sprHeight){
// store the current model matrix
GL11.glPushMatrix();
FloatBuffer tempViewBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, tempViewBuf);
float[] tempView = new float[16];
for(int i = 0; i < 16; i++){
tempView[i] = tempViewBuf.get(i);
}
float[] modelView = new float[6];
modelView[0] = tempView[0] - tempView[1];
modelView[1] = tempView[0] + tempView[1];
modelView[2] = tempView[4] - tempView[5];
modelView[3] = tempView[4] + tempView[5];
modelView[4] = tempView[8] - tempView[9];
modelView[5] = tempView[8] + tempView[9];
// bind to the appropriate texture for this sprite
texture.bind();
// draw a quad textured to match the sprite
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0); GL11.glVertex3f( - (sprWidth/2) + modelView[1], (sprHeight/2) + modelView[3], 0 + modelView[5]); // Top left Of The Quad (Left)
GL11.glTexCoord2f(texture.getWidth(), 0); GL11.glVertex3f( + (sprWidth/2) + modelView[0], (sprHeight/2) + modelView[2], 0 + modelView[4]); // Top right Of The Quad (Left)
GL11.glTexCoord2f(texture.getWidth(), texture.getHeight()); GL11.glVertex3f( + (sprWidth/2) + modelView[1], -(sprHeight/2)+ modelView[3], 0 + modelView[5]); // Bottom right Of The Quad (Left)
GL11.glTexCoord2f(0, texture.getHeight()); GL11.glVertex3f( - (sprWidth/2) + modelView[0], -(sprHeight/2)+ modelView[2], 0 + modelView[4]); // Bottom left Of The Quad (Left)
GL11.glEnd();
// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
}
I dug up an old project of mine which had billboarded particles, and my solution was this.
static float[] tempView;
// ...additional render stuff up here
// If there's anything that needs billboarding
if(getParticleTop() > 0)
{
glGetFloatv(GL_MODELVIEW_MATRIX, tempView);
modelView[0] = tempView[0] - tempView[1];
modelView[1] = tempView[0] + tempView[1];
modelView[2] = tempView[4] - tempView[5];
modelView[3] = tempView[4] + tempView[5];
modelView[4] = tempView[8] - tempView[9];
modelView[5] = tempView[8] + tempView[9];
// ... render each particle
}
And in the individual particle renders
float* modelView = renderer->getModelView();
glBegin(GL_QUADS);
{
glTexCoord2f(0, 1);
glVertex3f(owner->x - (modelView[1] * scale), owner->y - (modelView[3] * scale), owner->z - (modelView[5] * scale));
glTexCoord2f(0, 0);
glVertex3f(owner->x + (modelView[0] * scale), (owner->y + modelView[2] * scale), owner->z + (modelView[4] * scale));
glTexCoord2f(1, 0);
glVertex3f(owner->x + (modelView[1] * scale), owner->y + (modelView[3] * scale), owner->z + (modelView[5] * scale));
glTexCoord2f(1, 1);
glVertex3f(owner->x - (modelView[0] * scale), owner->y + (modelView[2] * scale), owner->z - (modelView[4] * scale));
}
glEnd();
Looking on it now, i see that i wasn't precaching scalar calculation and was using GL_QUADS instead of unrolling it myself, but i do know this works with all angles and all sizes.
I remember going through NeHe's tutorial and finding the same problem, this solution gave up on NeHe and googled out someone else who suggested the GL modelview method, which saves calculations (and time, if your video architecture is close enough to your processor).
I hope this helps.
EDIT; I should have mentioned, but the layout i used in this project was to have the entity class separate from model classes, so each entity could switch or share models as required; so this is a billboard model class, and owner->x/y/z refer to the entity who owns that model's position.
Just had to use a different way of billboarding:
public void drawBillBoardSprite(Texture texture, Vector3f sprPos, float r, float g, float b, int sprWidth, int sprHeight){
// store the current model matrix
GL11.glPushMatrix();
GL11.glTranslatef(sprPos.x, 0, sprPos.z);
// bind to the appropriate texture for this sprite
texture.bind();
FloatBuffer modelview = BufferUtils.createFloatBuffer(16);
// get the current modelview matrix
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
// Note that a row in the C convention is a column
// in OpenGL convention (see the red book, pg.106 in version 1.2)
// right vector is [1,0,0] (1st column)
// lookAt vector is [0,0,1] (3d column)
// leave the up vector unchanged (2nd column)
// notice the increment in i in the first cycle (i+=2)
for(int i=0; i<3; i+=2 )
for(int j=0; j<3; j++ ) {
if ( i==j )
modelview.put(i*4+j, 1.0f);
else
modelview.put(i*4+j, 0.0f);
}
// set the modelview matrix
GL11.glLoadMatrix(modelview);
// draw a quad textured to match the sprite
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0); GL11.glVertex3f( - (sprWidth/2), (sprHeight/2), 0); // Top left Of The Quad (Left)
GL11.glTexCoord2f(texture.getWidth(), 0); GL11.glVertex3f( + (sprWidth/2), (sprHeight/2), 0); // Top right Of The Quad (Left)
GL11.glTexCoord2f(texture.getWidth(), texture.getHeight()); GL11.glVertex3f( + (sprWidth/2), -(sprHeight/2), 0); // Bottom right Of The Quad (Left)
GL11.glTexCoord2f(0, texture.getHeight()); GL11.glVertex3f( - (sprWidth/2), -(sprHeight/2), 0); // Bottom left Of The Quad (Left)
GL11.glEnd();
// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
}

Billboard in LWJGL

I was trying to make a billboard using LWJGL and I succeeded partially. The quad appears and does face the camera when I strafe. However, as soon as I rotate the camera on any of the axises the quad also moves along the axises in different manners. Here is the code I use:
/***************************************************************************************************************************************************************************************************
* All rendering happens here...
**************************************************************************************************************************************************************************************************/
private void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
GL11.glLoadIdentity(); // Reset The View
//GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, lightPosition); // Position The Light
GL11.glRotatef(360.0f - yrot, 0, 1.0f, 0);
GL11.glRotatef(lookupdown, 1.0f, 0, 0);
GL11.glTranslatef(-xpos, 0, -zpos);
GL11.glCallList(blocksList);
GL11.glCallList(tilesList);
GL11.glCallList(roofList);
/* Render billboards */
Billboard bb = lvLoader.currentLevel.billboards[0];
GL11.glPushMatrix();
GL11.glRotatef(360.0f - yrot, 0, -1.0f, 0);
GL11.glRotatef(lookupdown, -1.0f, 0, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(bb.origin.x + 0.5f, bb.origin.y + 0.5f, bb.origin.z);
GL11.glVertex3f(bb.origin.x - 0.5f, bb.origin.y + 0.5f, bb.origin.z);
GL11.glVertex3f(bb.origin.x - 0.5f, bb.origin.y - 0.5f, bb.origin.z);
GL11.glVertex3f(bb.origin.x + 0.5f, bb.origin.y - 0.5f, bb.origin.z);
GL11.glEnd();
GL11.glPopMatrix();
Here are the functions controlling lookupdown and yrot.
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { // Is PageUp Being Pressed?
lookupdown -= 1.0f; // Rotate The Secene Downwards
}
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { // Is PageDown Being Pressed?
lookupdown += 1.0f; // Rotate The Scene Upwards
}
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
{
yrot -= 1.5f; // Rotate The Scene To The Left
}
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
{
yrot += 1.5f; // Rotate The Scene To The Left
}
if (Keyboard.isKeyDown(Keyboard.KEY_W))
{
adjustXZ((float) Math.sin(yrot * piover180) * 0.05f, (float) Math.cos(yrot * piover180) * 0.05f, false, false);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S))
{
adjustXZ((float) Math.sin(yrot * piover180) * 0.05f, (float) Math.cos(yrot * piover180) * 0.05f, true, true);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D))
{
adjustXZ((float) Math.sin((yrot-90) * piover180) * 0.05f, (float) Math.cos((yrot-90) * piover180) * 0.05f, false, false);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A))
{
adjustXZ((float) Math.sin((yrot+90) * piover180) * 0.05f, (float) Math.cos((yrot+90) * piover180) * 0.05f, false, false);
}
private void adjustXZ(float x, float z, boolean xAdd, boolean zAdd)
{
if (collisionCheck(x, z, xAdd, zAdd))
return;
if (xAdd)
xpos += x;
else if (!xAdd)
xpos -= x;
if (zAdd)
zpos += z;
else if (!zAdd)
zpos -= z;
}
I'm not familiar with LWJGL so I'm not 100% sure what it expects, but based on my experience with other 3D API's it sounds very much like you've got your matrix transforms in the wrong order.
Try changing your code as follows:
GL11.glPushMatrix();
GL11.glTranslatef(bb.origin.x, bb.origin.y, bb.origin.z); // Translation here
GL11.glRotatef(360.0f - yrot, 0, -1.0f, 0);
GL11.glRotatef(lookupdown, -1.0f, 0, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(0.5f, 0.5f, 0); // No translation here anymore
GL11.glVertex3f(0.5f, 0.5f, 0;
GL11.glVertex3f(0.5f, 0.5f, 0);
GL11.glVertex3f(0.5f, 0.5f, 0);
GL11.glEnd();
On a separate note, you're not using a conventional billboarding technique. Rather than having the billboards always face the camera, you're having them always be perpendicular to the camera. This will be noticeable for billboard objects near the edges of your view frustrum. I'll leave it up to you to look into this further though!

OpenGL: Drawing text in a 2d fashion on a 3d scene?

When I try to display text on my 3d scene it doesn't display the rest of the scene. What do I have to do to fix this? I tried the answer here: Opengl drawing a 2d overlay on a 3d scene problem but it didn't work for me. Here is my code:
public class GunCraft {
private boolean done = false;
Texture texture;
Camera cam;
private float yspeed;
float legAngle;
float playerX = 0;
float playerZ = 0;
float playerSpeed = 0.01f;
float enemyX = 0;
float enemyZ = 0;
private TrueTypeFont font2;
public static void main(String args[]) {
GunCraft gc = new GunCraft();
gc.run();
}
public void run() {
try {
init();
while (!done) {
mainloop();
render();
Display.update();
}
cleanup();
}
catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
/**
* Very basic for first lesson. Just check for escape key or user
* clicking to close the window.
*/
private void mainloop() {
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { // Exit if Escape is pressed
done = true;
}
if(Display.isCloseRequested()) { // Exit if window is closed
done = true;
}
}
/**
* For rendering all objects to the screen
* #return boolean for success or not
*/
private void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
font2.drawString(100, 100, "NICE LOOKING FONTS!", Color.green);
loadIdentity();
//GL11.glRotatef(yspeed,0.0f,1.0f,0.0f);
drawRect(texture, 1f, 1f, 1f);
}
public void loadIdentity(){
GL11.glLoadIdentity();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
float camAngle = (float) Math.atan2(playerX, playerZ);
float radius = 5;
GLU.gluLookAt(playerX + (float)(Math.sin(camAngle) * radius), 0, playerZ + (float)(Math.cos(camAngle) * radius), 0, 0, 0, 0, 1, 0);
//GL11.glGetMatrix();
}
public void drawRect(Texture texture, float width, float height, float depth){
texture.bind();
GL11.glBegin(GL11.GL_QUADS); // Start Drawing Quads
// Front Face
GL11.glNormal3f( 0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer
GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-width, -height, depth); // Point 1 (Front)
GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( width, -height, depth); // Point 2 (Front)
GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( width, height, depth); // Point 3 (Front)
GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-width, height, depth); // Point 4 (Front)
// Back Face
GL11.glNormal3f( 0.0f, 0.0f,-1.0f); // Normal Pointing Away From Viewer
GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-width, -height, -depth); // Point 1 (Back)
GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-width, height, -depth); // Point 2 (Back)
GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( width, height, -depth); // Point 3 (Back)
GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( width, -height, -depth); // Point 4 (Back)
// Top Face
GL11.glNormal3f( 0.0f, 1.0f, 0.0f); // Normal Pointing Up
GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-width, height, -depth); // Point 1 (Top)
GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-width, height, depth); // Point 2 (Top)
GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( width, height, depth); // Point 3 (Top)
GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( width, height, -depth); // Point 4 (Top)
// Bottom Face
GL11.glNormal3f( 0.0f,-1.0f, 0.0f); // Normal Pointing Down
GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-width, -height, -depth); // Point 1 (Bottom)
GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( width, -height, -depth); // Point 2 (Bottom)
GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( width, -height, depth); // Point 3 (Bottom)
GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-width, -height, depth); // Point 4 (Bottom)
// Right face
GL11.glNormal3f( 1.0f, 0.0f, 0.0f); // Normal Pointing Right
GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( width, -height, -depth); // Point 1 (Right)
GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( width, height, -depth); // Point 2 (Right)
GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( width, height, depth); // Point 3 (Right)
GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( width, -height, depth); // Point 4 (Right)
// Left Face
GL11.glNormal3f(-1.0f, 0.0f, 0.0f); // Normal Pointing Left
GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-width, -height, -depth); // Point 1 (Left)
GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-width, -height, depth); // Point 2 (Left)
GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-width, height, depth); // Point 3 (Left)
GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-width, height, -depth); // Point 4 (Left)
GL11.glEnd(); // Done Drawing The Quad
}
/**
* Create a window for all display
* #throws Exception
*/
private void createWindow() throws Exception {
Display.setFullscreen(false);
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setTitle("GunCraft - By William Starkovich");
Display.create();
}
/**
* Initialize the environment
* #throws Exception
*/
private void init() throws Exception {
createWindow();
try{
FileInputStream fis;
fis = new FileInputStream(new File("font.ttf"));
Font awtFont2 = Font.createFont(Font.TRUETYPE_FONT, fis);
awtFont2 = awtFont2.deriveFont(24f); // set font size
font2 = new TrueTypeFont(awtFont2, false);
} catch (Exception e) {
e.printStackTrace();
}
initGL(640,480);
}
/**
* Initialize OpenGL
*
*/
private void initGL(int width, int height) {
GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
GL11.glClearDepth(1.0); // Depth Buffer Setup
GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(
45.0f,
(float) Display.getDisplayMode().getWidth() / (float) Display.getDisplayMode().getHeight(),
0.1f,
100.0f);
//GLU.gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz)
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
// Really Nice Perspective Calculations
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, -1000, 1000);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glDisable(GL11.GL_CULL_FACE);
try {
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("tex.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Cleanup all the resources.
*
*/
private void cleanup() {
Display.destroy();
}
}
You should really review how OpenGL work you have a bunch of truncated statements in your code that get overwritten a few lines later. You have to get what "OpenGL is a state machine" means. If you don't want to deal with that you should find a more abstract game engine that handles all the draw-calls.
I just copypastad your render() and loadIdentity() methods into something working but this is not the real solution. (Recreating the Projection matrices each frame instead of saving/loading them is not smart). Also OpenGL 1.1 is terribly deprecated and using immediate mode is strongly discouraged.
private void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
loadIdentity();
GL11.glRotatef(yspeed++,0.0f,1.0f,0.0f);
drawRect(texture, 1f, 1f, 1f);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 640, 480, 0, -1000, 1000);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
font2.drawString(100, 100, "NICE LOOKING FONTS!", Color.green);
}
public void loadIdentity(){
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(
45.0f,
(float) Display.getDisplayMode().getWidth() / (float) Display.getDisplayMode().getHeight(),
0.1f,
100.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
float camAngle = (float) Math.atan2(playerX, playerZ);
float radius = 5;
GLU.gluLookAt(playerX + (float)(Math.sin(camAngle) * radius), 0, playerZ + (float)(Math.cos(camAngle) * radius), 0, 0, 0, 0, 1, 0);
//GL11.glGetMatrix();
}

Categories