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

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

Related

Cube doesn't render properly

So I'm using LWJGL3 and I'm trying to render a cube, however I have 2 issues:
The orthographic camera is too close and I tried increasing/decreasing the values in the parameters but none achieve the effect I want which is to eventually show lots of cubes stacked side-by-side so it needs to be zoomed out,
The cube renders in 2D, I would like it to initially look 2D but then I will use keys to rotate the camera so you can get a 3D view also.
Here is the code I have so far:
package io.ryankshah;
import io.ryankshah.util.Constants;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;
import java.nio.*;
import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;
public class Window
{
// Create a keycallback to check for key actions
private static GLFWKeyCallback keyCallback = new GLFWKeyCallback()
{
#Override
public void invoke(long window, int key, int scancode, int action, int mods) {
// If ESC is pressed, close the window
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
};
private static long window;
private static void init() {
glfwSetErrorCallback(Constants.ERROR_CALLBACK);
// Initialize GLFW
if(!glfwInit()) throw new IllegalStateException("Failed to initialize GLFW");
// Create window
window = glfwCreateWindow(Constants.WINDOW_WIDTH, Constants.WINDOW_HEIGHT,
Constants.WINDOW_TITLE, 0, 0);
if(window == 0) {
glfwTerminate();
throw new RuntimeException("Failed to create the GLFW window");
}
// Center the window
GLFWVidMode vm = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vm.width() - Constants.WINDOW_WIDTH) / 2,
(vm.height() - Constants.WINDOW_HEIGHT) / 2);
// Create OpenGL context
glfwMakeContextCurrent(window);
GL.createCapabilities();
// Enbale vsync
glfwSwapInterval(1);
// Set key callback
glfwSetKeyCallback(window, keyCallback);
}
private static void render() {
/* Declare buffers for using inside the loop */
IntBuffer width = MemoryUtil.memAllocInt(1);
IntBuffer height = MemoryUtil.memAllocInt(1);
/* Loop until window gets closed */
while (!glfwWindowShouldClose(window)) {
float ratio;
/* Get width and height to calcualte the ratio */
glfwGetFramebufferSize(window, width, height);
ratio = width.get() / (float) height.get();
/* Rewind buffers for next get */
width.rewind();
height.rewind();
/* Set viewport and clear screen */
glViewport(0, 0, width.get(), height.get());
glClear(GL_COLOR_BUFFER_BIT);
/* Set ortographic projection */
float zoom = 50.0f;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1f, 1f, 1f, -1f);
glMatrixMode(GL_MODELVIEW);
/* Rotate matrix */
glLoadIdentity();
glRotatef((float) glfwGetTime() * 50f, 0f, 0f, 1f);
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_COLOR_BUFFER_BIT);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glColor3f(1, 0, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
GL11.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
GL11.glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
GL11.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
GL11.glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
GL11.glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
GL11.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Back)
GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Back)
GL11.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Back)
GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Back)
GL11.glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
GL11.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
GL11.glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
GL11.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
GL11.glEnd();
GL11.glLoadIdentity();
/* Swap buffers and poll Events */
glfwSwapBuffers(window);
glfwPollEvents();
/* Flip buffers for next loop */
width.flip();
height.flip();
}
MemoryUtil.memFree(width);
MemoryUtil.memFree(height);
}
private static void loop() {
render();
}
public static void main(String[] args) {
init();
loop();
glfwDestroyWindow(window);
keyCallback.free();
glfwTerminate();
Constants.ERROR_CALLBACK.free();
}
}
Also if anyone has pointers as to the way I have structured my program or any calls that are unnecessary, would be great to have some sort of boiler-plate knowledge of how I should structure my lwjgl program.

OpenGL Change Width Height Of Bitmap Image

I can't figure out how to increase the size of the image, I know how to rotate, but how would I adjust the size? Also, I would like to know how to prevent the bitmap from looking squished. Right now when I load it on screen it looks like the sides are being squished.
#Override
public void onDrawFrame(GL10 gl) {
// clear Screen and Depth Buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Reset the Modelview Matrix
gl.glLoadIdentity();
// Drawing
gl.glTranslatef(0.0f, 0.0f, -5.0f); // move 5 units INTO the screen
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glRotatef(mAngle, 0, 1, 0);
gl.glRotatef(mAngle*0.25f, 1, 0, 0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glRotatef(mAngle*2.0f, 0, 1, 1);
gl.glTranslatef(0.5f, 0.5f, 0.5f);
mAngle += 1.2f;
bitmap_image.draw(gl);
}
In another class I'm loading the bitmap with:
private FloatBuffer vertexBuffer; // buffer holding the vertices
private float vertices[] = {
-1.0f, -1.0f, 0.0f, // V1 - bottom left
-1.0f, 1.0f, 0.0f, // V2 - top left
1.0f, -1.0f, 0.0f, // V3 - bottom right
1.0f, 1.0f, 0.0f // V4 - top right
};
private FloatBuffer textureBuffer; // buffer holding the texture coordinates
private float texture[] = {
// Mapping coordinates for the vertices
0.0f, 1.0f, // top left (V2)
0.0f, 0.0f, // bottom left (V1)
1.0f, 1.0f, // top right (V4)
1.0f, 0.0f // bottom right (V3)
};
/** The texture pointer */
private int[] textures = new int[1];
public ImageLoader() {
// a float has 4 bytes so we allocate for each coordinate 4 bytes
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
// allocates the memory from the byte buffer
vertexBuffer = byteBuffer.asFloatBuffer();
// fill the vertexBuffer with the vertices
vertexBuffer.put(vertices);
// set the cursor position to the beginning of the buffer
vertexBuffer.position(0);
byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
textureBuffer = byteBuffer.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
}

Giving different texture to each face in a pyramid

I am beginner in openGL.
i am created a prism ( each face is equilateral triangle ) in android using openGL library and i am able to rotate the prism successfully.
but my requirment is to put three different images in each face of the prism and i am not able to put the images. when i am putting the image it is scaling and mapping to all faces.
MyRenderer Class
public class MyRenderer implements Renderer {
/** Cube instance */
/* Rotation values for all axis */
private float xrot; //X Rotation ( NEW )
private float yrot; //Y Rotation ( NEW )
private float zrot; //Z Rotation ( NEW )
/** The Activity Context ( NEW ) */
private Context context;
private Pyramid pyramid;
/**
* Instance the Cube object and set
* the Activity Context handed over
*/
public MyRenderer(Context context) {
this.context = context;
pyramid = new Pyramid(this.context);
}
/**
* The Surface is created/init()
*/
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//Load the texture for the cube once during Surface creation
gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping ( NEW )
gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(1.0f, 1.0f, 1.0f, 0.5f); //Black Background
gl.glClearDepthf(1.0f); //Depth Buffer Setup
gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing
gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do
//Really Nice Perspective Calculations
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
/**
* Here we do our drawing
*/
public void onDrawFrame(GL10 gl) {
//Clear Screen And Depth Buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity(); //Reset The Current Modelview Matrix
//Drawing
gl.glTranslatef(0.0f, -1.0f, -5.0f); //Move 5 units into the screen
gl.glScalef(1.0f, 1.0f, 1.0f); //Scale the Cube to 80 percent, otherwise it would be too large for the screen
//Rotate around the axis based on the rotation matrix (rotation, x, y, z)
gl.glRotatef(yrot, 0.0f, 1.65f, 0.0f); //X
pyramid.draw(gl, context);
yrot += 1.0f;
}
/**
* If the surface changes, reset the view
*/
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(height == 0) { //Prevent A Divide By Zero By
height = 1; //Making Height Equal One
}
gl.glViewport(0, 0, width, height); //Reset The Current Viewport
gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix
gl.glLoadIdentity(); //Reset The Projection Matrix
//Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix
gl.glLoadIdentity(); //Reset The Modelview Matrix
}
}
MyPyramid class
public class Pyramid {
/** The buffer holding the vertices */
private FloatBuffer vertexBuffer;
/** The buffer holding the color values */
private FloatBuffer colorBuffer;
private ByteBuffer indexBuffer;
private FloatBuffer textureBuffer;
private int noOfFaces = 3;
private int[] texturesID = new int[3];
private float PyramidVertices [] = {
0.0f, 1.65f, 0.0f,
-1.3f, 0.0f, 1.0f,
1.3f, 0.0f, 1.0f,
0.0f, 0.0f, -1.65f,
};
private float textures[] = {
//Mapping coordinates for the vertices
0.0f, 1.65f,
0.0f, 1.65f,
-1.3f, 0.0f,
1.3f, 0.0f,
};
private float colors[] = {
1.0f, 0.0f, 0.0f, 1.0f, //Red
0.0f, 1.0f, 0.0f, 1.0f, //Green
0.0f, 0.0f, 1.0f, 1.0f, //Blue
1.0f, 0.0f, 0.0f, 1.0f, //Red
};
private byte indices [] = { 0, 2, 1,
0, 2, 3,
0, 1, 3,
};
/**
* The Pyramid constructor.
*
* Initiate the buffers.
*/
public Pyramid( Context context) {
//
ByteBuffer byteBuf = ByteBuffer.allocateDirect(PyramidVertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(PyramidVertices);
vertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
colorBuffer = byteBuf.asFloatBuffer();
colorBuffer.put(colors);
colorBuffer.position(0);
indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(textures.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(textures);
textureBuffer.position(0);
}
/**
* The object own drawing function.
* Called from the renderer to redraw this instance
* with possible changes in values.
*
* #param gl - The GL Context
*/
public void draw(GL10 gl, Context context) {
//Set the face rotation
// gl.glFrontFace(GL10.GL_CW);
gl.glCullFace(GL10.GL_CCW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
loadTexture(gl, context);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glEnable(GL10.GL_TEXTURE_2D);
// Enable the texture state
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Point to our buffers
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
public void loadTexture(GL10 gl, Context context) {
Bitmap bitmap;
gl.glGenTextures(3, texturesID, 0); // Generate texture-ID array for 6 IDs
gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesID[2]);
InputStream is = context.getResources().openRawResource(R.drawable.forward);
try {
//BitmapFactory is an Android graphics utility for images
bitmap = BitmapFactory.decodeStream(is);
} finally {
//Always clear and close
try {
is.close();
is = null;
} catch (IOException e) {
}
}
// Generate OpenGL texture images
// Create Nearest Filtered Texture
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
// Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
GL10.GL_REPEAT);
// Build Texture from loaded bitmap for the currently-bind texture ID
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
}
and i took the help from
http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_3D.html
http://nehe.gamedev.net/
How to give different images on each face?
There are few ways of doing this but here are the simplest two for you to try:
Modify object to your needs
It's quite tricky to use few textures at a time and select them per-face, but the simple solution is to divide your pyramid into few objects. Then, you can assign different texture for each of your objects as you like.
Modify texture to your needs
You can use a technique known as Texture Atlas. In this solution you can take few textures and stitch them together into one, bigger bitmap. Then you use this bitmap as your main texture. You also need to modify your vertices UVs, so one of your triangles uses some part of your bigger texture, while another triangle uses different part of your texture. Using this technique you can get an appearance that different triangles use totally different images as their texture (even that there's just one real texture during rendering).
Let us know if you need more details about it.
Thanks for response.
I found the solution for my problem. I created a equilateral triangle and then by giving the proper rotation angle i am able to rotate it as well as i put the different texture in each face.
MyPyramid class
public class PyramidNew {
int []texturesID = new int[3];
Bitmap []bitmap = new Bitmap[3];
private FloatBuffer textureBuffer;
private FloatBuffer vertexBuffer; // Buffer for vertex-array
private float[][] colors = { // Colors of the 6 faces
{1.0f, 0.5f, 0.0f, 1.0f}, // 0. orange
{1.0f, 0.0f, 1.0f, 1.0f}, // 1. violet
{0.0f, 1.0f, 0.0f, 1.0f}, // 2. green
{0.0f, 0.0f, 1.0f, 1.0f}, // 3. blue
{1.0f, 0.0f, 0.0f, 1.0f}, // 4. red
{1.0f, 1.0f, 0.0f, 1.0f} // 5. yellow
};
/* private float[] vertices = { // Vertices for the front face
-1.5f, 0.0f, 0.86f, // 0. left-bottom-front
1.5f, 0.0f, 0.86f, // 1. right-bottom-front
0.0f, 1.86f, 0.0f, // 2. left-top-front
// 3. right-top-front
};*/
private float[] vertices = { // Vertices for the front face
-1.0f, 0.0f, 0.86f, // 0. left-bottom-front
1.0f, 0.0f, 0.86f, // 1. right-bottom-front
0.0f, 1.86f, 0.0f, // 2. left-top-front
// 3. right-top-front
};
private float textures[] = {
//Mapping coordinates for the vertices
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
};
// Constructor - Set up the buffers
public PyramidNew( Context context) {
// Setup vertex-array buffer. Vertices in float. An float has 4 bytes
System.out.println("calling Pyramid:::::::::");
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder()); // Use native byte order
vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
vertexBuffer.put(vertices); // Copy data into buffer
vertexBuffer.position(0); // Rewind
ByteBuffer byteBuf = ByteBuffer.allocateDirect(textures.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(textures);
textureBuffer.position(0);
InputStream stream1 = context.getResources().openRawResource(R.drawable.splash_screen);
InputStream stream2 = context.getResources().openRawResource(R.drawable.bg);
InputStream stream3 = context.getResources().openRawResource(R.drawable.bg1);
try {
//BitmapFactory is an Android graphics utility for images
bitmap[0] = BitmapFactory.decodeStream(stream1);
bitmap[1] = BitmapFactory.decodeStream(stream2);
bitmap[2] = BitmapFactory.decodeStream(stream3);
} finally {
//Always clear and close
try {
stream1.close();
stream2.close();
stream3.close();
stream1 = stream2 = stream3 = null;
} catch (IOException e) {
}
}
}
// Draw the color cube
public void draw(GL10 gl, Context context) {
gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise orientation
gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display)
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//loading the first texture in first face
loadTexture(gl, context, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
// rotating the face and puting the second texture in face
gl.glRotatef(120.0f, 0.0f, 1.0f, 0.0f);
//gl.glColor4f(colors[1][0], colors[1][1], colors[1][2], colors[1][3]);
loadTexture(gl, context, 1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
// Back - Rotate another 120 degree about y-axis and then put different texture
gl.glRotatef(120.0f, 0.0f, 1.0f, 0.0f);
//gl.glColor4f(colors[2][0], colors[2][1], colors[2][2], colors[2][3]);
loadTexture(gl, context, 2);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}
public void loadTexture(GL10 gl, Context context, int currentImage) {
// Bitmap []bitmap = new Bitmap[3];
gl.glGenTextures(3, texturesID, 0); // Generate texture-ID array for 6 IDs
gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesID[currentImage]);
// Generate OpenGL texture images
// Create Nearest Filtered Texture
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
// Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
GL10.GL_REPEAT);
// Build Texture from loaded bitmap for the currently-bind texture ID
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap[currentImage], 0);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glEnable(GL10.GL_TEXTURE_2D);
// Enable the texture state
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Point to our buffers
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
}
}

Enable anti-aliasing with lwjgl

I'm running this lwjgl application:
Display.setDisplayMode(new DisplayMode(500, 500));
Display.create();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5, 5, -5, 5, -10, 5);
glMatrixMode(GL_MODELVIEW);
float x = 0;
while (!Display.isCloseRequested()) {
Display.update();
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glColor3f(1, 0, 0);
x += 0.01f;
glRotatef(x, x, 3 * x, 0.5f * x);
glBegin(GL_QUADS);
drawCube();
glEnd();
glLoadIdentity();
}
Display.destroy();
Which basically draws a 1x1x1 cube in the window. Method drawCube() is this:
public void drawCube() {
glColor3f(0.0f, 1.0f, 0.0f); // Set The Color To Green
glVertex3f(1.0f, 1.0f, 0f); // Top Right Of The Quad (Top)
glVertex3f(0f, 1.0f, 0f); // Top Left Of The Quad (Top)
glVertex3f(0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
glColor3f(1.0f, 0.5f, 0.0f); // Set The Color To Orange
glVertex3f(1.0f, 0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(0f, 0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(0f, 0f, 0f); // Bottom Left Of The Quad (Bottom)
glVertex3f(1.0f, 0f, 0f); // Bottom Right Of The Quad (Bottom)
glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red
glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(0f, 0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f(1.0f, 0f, 1.0f); // Bottom Right Of The Quad (Front)
glColor3f(1.0f, 1.0f, 0.0f); // Set The Color To Yellow
glVertex3f(1.0f, 0f, 0f); // Bottom Left Of The Quad (Back)
glVertex3f(0f, 0f, 0f); // Bottom Right Of The Quad (Back)
glVertex3f(0f, 1.0f, 0f); // Top Right Of The Quad (Back)
glVertex3f(1.0f, 1.0f, 0f); // Top Left Of The Quad (Back)
glColor3f(0.0f, 0.0f, 1.0f); // Set The Color To Blue
glVertex3f(0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(0f, 1.0f, 0f); // Top Left Of The Quad (Left)
glVertex3f(0f, 0f, 0f); // Bottom Left Of The Quad (Left)
glVertex3f(0f, 0f, 1.0f); // Bottom Right Of The Quad (Left)
glColor3f(1.0f, 0.0f, 1.0f); // Set The Color To Violet
glVertex3f(1.0f, 1.0f, 0f); // Top Right Of The Quad (Right)
glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f(1.0f, 0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f(1.0f, 0f, 0f); // Bottom Right Of The Quad (Right)
}
It outputs this:
To me lines are pretty horrible, Does lwjgl have anti aliasing? If yes, how can i enable it?
For Anti-Aliasing, you can use multisampling. To enable it, add a PixelFormat parameter to Display.create();. Here's an example:
Display.create(new PixelFormat(/*Alpha Bits*/8, /*Depth bits*/ 8, /*Stencil bits*/ 0, /*samples*/8)); I'm not sure what the first 3 parameters do - these are values I found here. I've also seen people use Display.create(new PixelFormat(8,0,0,8)).
NOTE: If you set the AA too high (or other things an incorrect value) it will throw an LWJGLException.
Hope this helps.
PS apologies for the late answer.
Comments
Antialiasing can be achieved by creating a multisample framebuffer, either in a framebuffer object or through the default framebuffer when the LWJGL window is created for the first time.
If you are learning LWJGL and OpenGL then learn how to use VBOs, because glBegin, glVertex, etc. are removed from the core profile of OpenGL.
VBO Example
Here is a little example of a VBO storing Vertices and Texture Coordinates for two triangles and rendering it!
I assume that you know how to load and bind textures already.
Creating the VBO
This is the code where you create the actual Vertex and Texture Coordinate Buffer and store them onto the GPU.
int vertices = 6;
int vertex_size = 3; // X, Y, Z,
int texture_size = 2; // U, V,
FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * vertex_size);
vertex_data.put(new float[] { -1f, 1f, 0f, }); // Vertex
vertex_data.put(new float[] { 1f, 1f, 0f, }); // Vertex
vertex_data.put(new float[] { -1f, -1f, 0f, }); // Vertex
vertex_data.put(new float[] { 1f, -1f, 0f, }); // Vertex
vertex_data.put(new float[] { -1f, -1f, 0f, }); // Vertex
vertex_data.put(new float[] { 1f, 1f, 0f, }); // Vertex
FloatBuffer texture_data = BufferUtils.createFloatBuffer(vertices * texture_size);
texture_data.put(new float[] { 0f, 1f, }); // Texture Coordinate
texture_data.put(new float[] { 1f, 1f, }); // Texture Coordinate
texture_data.put(new float[] { 0f, 0f, }); // Texture Coordinate
texture_data.put(new float[] { 1f, 0f, }); // Texture Coordinate
texture_data.put(new float[] { 0f, 0f, }); // Texture Coordinate
texture_data.put(new float[] { 1f, 1f, }); // Texture Coordinate
vertex_data.flip();
texture_data.flip();
int vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
int vbo_texture_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_texture_handle);
glBufferData(GL_ARRAY_BUFFER, texture_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Rendering the VBO
Then when you want to render the VBO, you need to do the following.
texture.bind();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(vertex_size, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, vbo_texture_handle);
glTexCoordPointer(texture_size, GL_FLOAT, 0, 0l);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, vertices); // The vertices is of course the max vertices count, in this case 6
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
texture.unbind();
Deleting the VBO
Then when you're done with the VBO and you don't need it anymore, you can delete it by doing the following.
glDeleteBuffers(vbo_vertex_handle);
glDeleteBuffers(vbo_texture_handle);
LWJGL/OpenGL - Static Imports
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
Info
You only have to create a VBO once (It is really bad to create one for each frame), so if you used VBO to store your terrain. Then if some changes happens then only at that point you want to update the VBO, else just keep it as it is. If you use the VBO for terrain and the terrain is really huge, then split the terrain into different chunks of terrain with it's own VBO.
Extra
If you have an Wavefront OBJ Model, and you want to render it multiple times, the best way to do that, would be to load the whole model to one VBO. Then you would do some Instancing to render it multiple times in multiple position, etc.
Instancing Tutorial 1
Instancing Tutorial 2
Update
A little example of a VBO storing Vertices and Colors for a Triangle and rendering it!
Creating the VBO.
This is the code where you create the actual Vertex and Color Buffer and bind them to the VBO.
int vertices = 3;
int vertex_size = 3; // X, Y, Z,
int color_size = 3; // R, G, B,
FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * vertex_size);
vertex_data.put(new float[] { -1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, 1f, 0f, });
vertex_data.flip();
FloatBuffer color_data = BufferUtils.createFloatBuffer(vertices * color_size);
color_data.put(new float[] { 1f, 0f, 0f, });
color_data.put(new float[] { 0f, 1f, 0f, });
color_data.put(new float[] { 0f, 0f, 1f, });
color_data.flip();
int vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
int vbo_color_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glBufferData(GL_ARRAY_BUFFER, color_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Rendering the VBO.
This is the code you need to call, to render the VBO.
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(vertex_size, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glColorPointer(color_size, GL_FLOAT, 0, 0l);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, vertices);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
The imports and the code for disposing/deleting the buffers are the same as the previous example!
Update 2 - Complete Example
Okay, so here is a complete example of a VBO containing Vertices and Colors. The example is 100% LWJGL only!
When I run the following code this is the desired result I get.
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class VBOTest
{
public final static void main(String[] args)
{
int width = 1280;
int height = 720;
try
{
Display.setTitle("VBO Test");
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
}
catch (Exception ex)
{
ex.printStackTrace();
System.exit(0);
}
/*
* Initialize OpenGL States
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0f, width, height, 0f, -1f, 1f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
/*
* Creating the Vertex & Color VBO
*/
final int VERTEX_SIZE = 3; // X, Y, Z,
final int COLOR_SIZE = 4; // R, G, B, A,
int vertices = 6;
int vbo_vertex_handle;
int vbo_color_handle;
FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * VERTEX_SIZE);
float half_width = 200f;
float half_height = 200f;
vertex_data.put(new float[] { -half_width, -half_height, 0f, });
vertex_data.put(new float[] { -half_width, half_height, 0f, });
vertex_data.put(new float[] { half_width, -half_height, 0f, });
vertex_data.put(new float[] { half_width, half_height, 0f, });
vertex_data.put(new float[] { half_width, -half_height, 0f, });
vertex_data.put(new float[] { -half_width, half_height, 0f, });
vertex_data.flip();
FloatBuffer color_data = BufferUtils.createFloatBuffer(vertices * COLOR_SIZE);
color_data.put(new float[] { 1f, 0f, 0f, 1f, });
color_data.put(new float[] { 1f, 0f, 1f, 1f, });
color_data.put(new float[] { 1f, 1f, 0f, 1f, });
color_data.put(new float[] { 0f, 1f, 0f, 1f, });
color_data.put(new float[] { 1f, 1f, 0f, 1f, });
color_data.put(new float[] { 1f, 0f, 1f, 1f, });
color_data.flip();
vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
vbo_color_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glBufferData(GL_ARRAY_BUFFER, color_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/*
* Main Rendering Loop
*/
boolean running = true;
while (running)
{
running = (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
{
glTranslatef(width / 2f, height / 2f, 0f);
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(VERTEX_SIZE, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glColorPointer(COLOR_SIZE, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, vertices);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
glPopMatrix();
glFlush();
Display.sync(60);
Display.update();
}
/*
* Dispose Elements
*/
glDeleteBuffers(vbo_vertex_handle);
glDeleteBuffers(vbo_color_handle);
Display.destroy();
System.exit(0);
}
}

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!

Categories