Java LWJGL: When I call "GL11.glEnd" the polygon disappears? - java

When I call "GL11.glEnd" the polygon disappears. If I leave that one line "GL11.glEnd" out, then it stays on the screen, but when I put it in, the polygon I was drawing disappears.
package package01;
import org.lwjgl.opengl.GL11;
public class Graph {
...
void initGraph(){
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1.0);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glOrtho(-500, 500, -281, 281, -1, 1);
GL11.glColor3f(0.8f, 0.8f, 1.0f);
GL11.glBegin(GL11.GL_POLYGON);
GL11.glVertex3f(-60, 110, 0);
GL11.glVertex3f(60, 110, 0);
GL11.glVertex3f(120, 0, 0);
GL11.glVertex3f(60, -110, 0);
GL11.glVertex3f(-60, -110, 0);
GL11.glVertex3f(-120, 0, 0);
GL11.glEnd();
GL11.glFlush();
}
}
And here is the second class if it would help.
package package01;
...
public class DisplayScreen {
Graph g = new Graph();
void start(){
try {
Display.setDisplayMode(new DisplayMode(1000,562));
Display.create();
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
} catch(LWJGLException e) {
e.printStackTrace();
}
while(!Display.isCloseRequested()){
Display.update();
g.initGraph();
}
Display.destroy();
}
public static void main(String[] args){
DisplayScreen ds = new DisplayScreen();
ds.start();
}
}

remove GL11.glOrtho(-500, 500, -281, 281, -1, 1); from the initGraph() method.
add
glMatrixMode(GL11.GL_PROJECTION);
glLoadIdentity();
GL11.glOrtho(-500, 500, -281, 281, -1, 1);
glMatrixMode(GL11.GL_MODELVIEW);
before the game loop.
update needs to be called after initGraph
g.initGraph();
Display.update();

Related

Issue with displaying Textures using LWJGL and SlickUtil

Ive tried a lot but dont find my error. Heres my Code:
public class Renderer {
public static final int WIDTH = 1080, HEIGHT = 720, FPS = 60;
public static void createDisplay()
{
try {
Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// enable alpha blending
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0,WIDTH,HEIGHT);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public static void updateDisplay(){
Display.update();
Display.sync(FPS);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.2f,0.2f,0.2f,1.0f);
}
public static void destroyDisplay()
{
Display.destroy();
}
public static void renderDisplay(ArrayList<GameObject> render, Camera cam)
{
Color.white.bind();
for(GameObject obj: render)
{
obj.getTex().bind();
if(obj instanceof Player)
{
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(obj.getX()+WIDTH/2-cam.getX(), obj.getY()+HEIGHT/2-cam.getY());
glTexCoord2f(obj.getTex().getWidth(),0);
glVertex2f(obj.getX()+WIDTH/2+obj.getSizeX()-cam.getX(), obj.getY()+HEIGHT/2-cam.getY());
glTexCoord2f(obj.getTex().getWidth(),obj.getTex().getHeight());
glVertex2f(obj.getX()+WIDTH/2+obj.getSizeX()-cam.getX(), obj.getY()+HEIGHT/2+obj.getSizeY()-cam.getY());
glTexCoord2f(0,obj.getTex().getHeight());
glVertex2f(obj.getX()+WIDTH/2-cam.getX(), obj.getY()+HEIGHT/2+obj.getSizeY()-cam.getY());
glEnd();
}else{
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f((obj.getX()-cam.getX()), (obj.getY()-cam.getY()));
glTexCoord2f(1,0);
glVertex2f((obj.getX()-cam.getX())+obj.getSizeX(), (obj.getY()-cam.getY()));
glTexCoord2f(1,1);
glVertex2f((obj.getX()-cam.getX())+obj.getSizeX(), (obj.getY()-cam.getY())+obj.getSizeY());
glTexCoord2f(0,1);
glVertex2f((obj.getX()-cam.getX()), (obj.getY()-cam.getY())+obj.getSizeY());
glEnd();
}
obj.getTex().release();
}
}
There is no error the Texture is just not displayed. I use this method to load the images:
public static Texture getTex(String path)
{
Texture texture = null;
try {
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}
return texture;
}
This is my main game loop:
public static Player player = new Player(0, 0, 0, 0, 20, 20, 2, null);
public static Camera cam = new Camera(0, 0);
static ArrayList<GameObject> toRender = new ArrayList<GameObject>();
public static void main(String[] args) {
Renderer.createDisplay();
toRender.add(new Wall(300, 300, 200, 100, IO.getTex("res/Terrain/Grass.png")));
toRender.add(player);
player.setTex(IO.getTex("res/Entities/Player/Player.png"));
while(!Display.isCloseRequested())
{
Renderer.updateDisplay();
Input.updateInput();
player.update();
Renderer.renderDisplay(toRender, cam);
}
Renderer.destroyDisplay();
System.exit(0);
}
Its just displayed a white quad instead of a textured one.
To render Texture load with slick on LWJGL quads you need to bind the texture before draw the quads
just try
texture.bind(); //texture = your Texture name
before
glBegin(GL_QUADS);

OpenGL transparency not working

I am trying to draw my font texture atlas to the screen using LWJGL, but OpenGL instead draws a solid white square.
A working example using my drawing code:
import java.awt.image.*;
import java.io.*;
import java.nio.*;
import javax.imageio.*;
import org.lwjgl.*;
import org.lwjgl.opengl.*;
public class OpenGLImageTest
{
private static int textureID;
public static void main(String[] args) throws Exception
{
Display.setTitle("OpenGL Image Test");
Display.setDisplayMode(new DisplayMode(640, 480));
Display.create();
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 640, 0, 480, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
textureID = bindTextureFile("textures/font.png");
while(!Display.isCloseRequested())
{
Display.sync(60);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glClearColor(0, 0, 0, 1);
GL11.glColor4f(1, 0, 0, 1);
drawFontAtlas(0, 0);
Display.update();
}
Display.destroy();
}
private static void drawFontAtlas(int x, int y)
{
GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2i(0, 0);
GL11.glVertex2i(0, 256);
GL11.glVertex2i(256, 256);
GL11.glVertex2i(256, 0);
GL11.glEnd();
GL11.glDisable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glPopMatrix();
}
private static int bindTextureFile(String file)
{
try
{
BufferedImage image = ImageIO.read(new FileInputStream(file));
int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);
for(int y = 0; y < image.getWidth(); y++)
{
for(int x = 0; x < image.getHeight(); x++)
{
int pixel = pixels[y * image.getWidth() + x];
buffer.put((byte)((pixel >> 16) & 0xFF));
buffer.put((byte)((pixel >> 8) & 0xFF));
buffer.put((byte)(pixel & 0xFF));
buffer.put((byte)((pixel >> 24) & 0xFF));
}
}
buffer.flip();
int textureID = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB8, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
return textureID;
}
catch(Exception e)
{
e.printStackTrace();
}
return -1;
}
}
Can anyone tell me what I am doing wrong and how to fix it?
Edit: font.png is white on transparent white. It's the default Minecraft font for testing purposes.
Your problem can be fixed by adding texture coordinates:
{
GL11.glTexCoord2f(0, 1); // added texture coordinate
GL11.glVertex2i(0, 0);
GL11.glTexCoord2f(0, 0); // added texture coordinate
GL11.glVertex2i(0, 256);
GL11.glTexCoord2f(1, 0); // added texture coordinate
GL11.glVertex2i(256, 256);
GL11.glTexCoord2f(1, 1); // added texture coordinate
GL11.glVertex2i(256, 0);
}
I actually used slick-util.jar to load your texture, but it will work with your code just the same... you can find a working example of my code bellow... slick-util.jar can be found here
In order to get actual transparency, you also need to change this line in your code: GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB8, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer); and what needs to be changed is GL11.GL_RGB8 into GL11.GL_RGBA
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
public class OpenGLImageTest {
private static Texture tex; //private static int textureID;
public static void main(String[] args) throws Exception {
Display.setTitle("OpenGL Image Test");
Display.setDisplayMode(new DisplayMode(640, 480));
Display.create();
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 640, 0, 480, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
tex = loadTexture("textures/font.png"); //textureID = bindTextureFile("textures/font.png");
while (!Display.isCloseRequested()) {
Display.sync(60);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glClearColor(0, 0, 0, 1);
GL11.glColor4f(1, 1, 1, 1);
drawFontAtlas(0, 0);
Display.update();
}
Display.destroy();
}
private static void drawFontAtlas(int x, int y) {
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_BLEND);
tex.bind(); //GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glTexCoord2f(0, 1); // added texture coordinate
GL11.glVertex2i(0, 0);
GL11.glTexCoord2f(0, 0); // added texture coordinate
GL11.glVertex2i(0, 256);
GL11.glTexCoord2f(1, 0); // added texture coordinate
GL11.glVertex2i(256, 256);
GL11.glTexCoord2f(1, 1); // added texture coordinate
GL11.glVertex2i(256, 0);
}
GL11.glEnd();
GL11.glDisable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glPopMatrix();
}
public static Texture loadTexture(String texturePath) {
try {
return TextureLoader.getTexture("png", new FileInputStream(new File(texturePath)));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

Can't draw to FrameBufferObject, then draw to frameBuffer, in LWJGL

My problem is that I have drawn a small quad to a Frame Buffer Object and am trying to draw it to the framebuffer on the window. This is my java code:
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL11.*;
import java.nio.ByteBuffer;
import org.lwjgl.opengl.*;
public class Main
{
private static int fboID;
private static int texID;
private static int depthBuff;
public static void main(String[] args)
{
try
{
Display.setDisplayMode(new DisplayMode(640, 480));
Display.create();
} catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
glEnable(GL_TEXTURE_2D);
initFramebuffer();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
while (!Display.isCloseRequested())
{
glBindTexture(GL_TEXTURE_2D, texID);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(10, 10);
glTexCoord2f(0, 1);
glVertex2i(10, 470);
glTexCoord2f(1, 1);
glVertex2i(630, 470);
glTexCoord2f(1, 0);
glVertex2i(630, 10);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
Display.update();
Display.sync(60);
}
}
public static void initFramebuffer()
{
fboID = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, fboID);
texID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 640, 480, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer) null);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
depthBuff = glGenRenderbuffers();
glBindRenderbuffer(GL_RENDERBUFFER, depthBuff);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 640, 480);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuff);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texID, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
System.out.println("GOD DAMT");
glBindFramebuffer(GL_FRAMEBUFFER, fboID);
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, 640, 480);
//glLoadIdentity();
glBegin(GL_QUADS);
glColor3f(1, 0, 0);
glVertex2i(0, 0);
glVertex2i(100, 0);
glVertex2i(100, 100);
glVertex2i(0, 100);
glEnd();
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
}
Whenever I run this code I get nothing but a black screen.
What am I doing wrong?
I'm not tooooo familar with OpenGL but
Instead of
glEnable(GL_TEXTURE_2D);
initFramebuffer();
you should set texturing enabled after initializing fb:
initFramebuffer();
glEnable(GL_TEXTURE_2D);
this is caused by glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); which seems to render uninitialized (non-fb texture) in front of your fb texture box, which causes blank screen

Problems loading an image with OpenGL

I have a problem loading my image. This is what I get: http://imgur.com/BZaubNz. The black spaces shouldn't be there. I don't know if there's something wrong with my image or my code. I've tried other images and some work just fine with this code, some don't.
import java.io.IOException;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import org.newdawn.slick.Color;
import org.lwjgl.opengl.GL11;
public void init() {
try {
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("sprites/playButton.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void render() {
texture.bind();
Color.white.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(400-getWidth()/2,250);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(400+getWidth()/2,250);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(400+getWidth()/2,250+getHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(400-getWidth()/2,250+getHeight());
GL11.glEnd();
}
This is the main render function.
public void start() {
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.setInitialBackground(255, 255, 255);
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
// init OpenGL here
MainMenu mainMenu = new MainMenu();
mainMenu.init();
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(255.0f, 255.0f, 255.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0,800,600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
while (!Display.isCloseRequested()) {
// render OpenGL here
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
mainMenu.render();
Display.update();
Display.sync(100);
}

Java Open GL lighting problem

I'm just starting to learn Open GL and I've hit a road block. For some reason rotating objects even with pushed and popped matrices seems to transform the lighting. Does anyone know what I'm doing wrong?
Here's my code:
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class DisplayExample {
int cube;
private FloatBuffer pos;
private int cube2;
public void start() {
int width=800;
int height=600;
try {
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle("Abarrow");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
//setup colors
FloatBuffer red= BufferUtils.createFloatBuffer(4).put(new float[] { 1, 0, 0, 1});
FloatBuffer green = BufferUtils.createFloatBuffer(4).put(new float[] { 0, 1, 0, 1});
FloatBuffer blue= BufferUtils.createFloatBuffer(4).put(new float[] { 0, 0, 1, 1});
FloatBuffer yellow= BufferUtils.createFloatBuffer(4).put(new float[] { 1, 1, 0, 1});
red.flip();
green.flip();
blue.flip();
yellow.flip();
//setup positions
pos = BufferUtils.createFloatBuffer(4).put(new float[] { 5.0f, 5.0f, 10.0f, 0.0f});
pos.flip();
//projection
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glFrustum(-4, 4, -3, 3, 5, 60);
//model view
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glTranslatef(0, 0, -10);
//light source
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, pos);
//lighting
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_LIGHT0);
//other
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_NORMALIZE);
cube=GL11.glGenLists(1);
GL11.glNewList(cube, GL11.GL_COMPILE);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_AMBIENT_AND_DIFFUSE, green);
createCube();
GL11.glEndList();
cube2=GL11.glGenLists(1);
GL11.glNewList(cube2, GL11.GL_COMPILE);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_AMBIENT_AND_DIFFUSE, red);
createCube();
GL11.glEndList();
loop();
}
private void createCube() {
GL11.glBegin(GL11.GL_QUADS);
//front
GL11.glVertex3f(-1,-1,-1);
GL11.glVertex3f(-1,1,-1);
GL11.glVertex3f(1,1,-1);
GL11.glVertex3f(1,-1,-1);
//back
GL11.glVertex3f(-1,-1,1);
GL11.glVertex3f(-1,1,1);
GL11.glVertex3f(1,1,1);
GL11.glVertex3f(1,-1,1);
//left
GL11.glVertex3f(-1,-1,-1);
GL11.glVertex3f(-1,1,-1);
GL11.glVertex3f(-1,1,1);
GL11.glVertex3f(-1,-1,1);
//right
GL11.glVertex3f(1,-1,-1);
GL11.glVertex3f(1,1,-1);
GL11.glVertex3f(1,1,1);
GL11.glVertex3f(1,-1,1);
GL11.glEnd();
}
private void loop() {
float spot=0;
while (!Display.isCloseRequested()) {
spot+=1;
// Clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
//reset model view
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glTranslatef(0, 0, -10);
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, pos);
//cube
GL11.glPushMatrix();
GL11.glTranslatef(-2, 0, 0);
GL11.glRotatef(spot, 0, 1, 0);
//render cube
GL11.glCallList(cube);
//undo transform
GL11.glPopMatrix();
//cube2
GL11.glPushMatrix();
GL11.glTranslatef(2, 0, 0);
GL11.glRotatef(-spot, 0, 1, 0);
//render cube
GL11.glCallList(cube2);
//undo transform
GL11.glPopMatrix();
//render the graphics
Display.update();
//cap at 60 fps
Display.sync(60);
}
Display.destroy();
}
public static void main(String[] argv) {
DisplayExample displayExample = new DisplayExample();
displayExample.start();
}
}
Try adding some polygon normals via glNormal3f().
I think you're seeing the default normal of (0, 0, 1).

Categories

Resources