我正在尝试使用 Java 将一些基本的四边形渲染到 LWJGL 中的 FBO,但它实际上并没有渲染,我不知道为什么。
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.EXTFramebufferObject;
import org.lwjgl.opengl.GLContext;
import org.newdawn.slick.Color;
import elec0.utils.Camera;
import elec0.utils.Vector2f;
import elec0.utils.Vector3f;
public class lwjglFBO
{
private Camera camera = new Camera();
private boolean running = true, wireframe = false;
private long lastFPS;
private int fps;
// boolean FBOEnabled = GLContext.getCapabilities().GL_EXT_framebuffer_object;
private int iFBOBuffer;
public void start()
{
initGL();
init();
lastFPS = getTime();
while(!Display.isCloseRequested())
{
updateFPS();
pollInput();
render();
Display.update();
Display.sync(60); // limit FPS to 60
}
Display.destroy();
System.exit(0);
}
private void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/*gluLookAt(0, 0, 5, // Set up the LookAt
0, 0, 0,
0, 1, 0);*/
camera.render();
drawGrid(); // Render this fill, it's lines
int verts = 0;
EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, iFBOBuffer );
glPushAttrib(GL_VIEWPORT_BIT);
glViewport( 0, 0, 800, 600);
glClear(GL_COLOR_BUFFER_BIT);
for(int z = 0; z < 20; ++z)
{
for(int y = 0; y < 20; ++y)
{
for(int x = 0; x < 20; ++x)
{
glPushMatrix();
glTranslatef(x, y, z);
drawBox();
// verts+=24;
glPopMatrix();
}
}
}
// System.out.println(verts);
EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
glPopAttrib();
// Render
if(wireframe)
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
else
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
}
private void drawBox()
{
/* draws the sides of a unit cube (0,0,0)-(1,1,1) */
glBegin(GL_POLYGON);/* f1: front */
glNormal3f(-1.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,0.0f);
glEnd();
glBegin(GL_POLYGON);/* f2: bottom */
glNormal3f(0.0f,0.0f,-1.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glEnd();
glBegin(GL_POLYGON);/* f3:back */
glNormal3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(0.0f,1.0f,1.0f);
glVertex3f(0.0f,1.0f,0.0f);
glEnd();
glBegin(GL_POLYGON);/* f4: top */
glNormal3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,1.0f,1.0f);
glEnd();
glBegin(GL_POLYGON);/* f5: left */
glNormal3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,1.0f);
glVertex3f(0.0f,0.0f,1.0f);
glEnd();
glBegin(GL_POLYGON);/* f6: right */
glNormal3f(0.0f,-1.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,0.0f);
glEnd();
}
private void init()
{
Mouse.setGrabbed(true);
camera.initCamera();
camera.farPlane = 1000;
camera.initPerspective();
}
private void initGL()
{
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
glClearDepth(1.0); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
IntBuffer buffer = ByteBuffer.allocateDirect(1*4).order(ByteOrder.nativeOrder()).asIntBuffer(); // allocate a 1 int byte buffer
EXTFramebufferObject.glGenFramebuffersEXT( buffer ); // generate
iFBOBuffer = buffer.get();
// glEnable(GL_FOG);
// FloatBuffer fogColor = BufferUtils.createFloatBuffer(4);
// fogColor.put(0f).put(0f).put(0f).put(1f).flip();
//
// int fogMode = GL_LINEAR;
// glFogi(GL_FOG_MODE, fogMode);
// glFog(GL_FOG_COLOR, fogColor);
// glFogf(GL_FOG_DENSITY, 0.35f);
// glHint(GL_FOG_HINT, GL_DONT_CARE);
// glFogf(GL_FOG_START, 100.0f);
// glFogf(GL_FOG_END, 500.0f);
// Alpha blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
private long lastF;
private void pollInput()
{
int x = Mouse.getX();
int y = Display.getHeight() - Mouse.getY();
float speed = .1f;
int iKeypressSpeed = 50;
int iDelta = getDelta();
speed *= iDelta;
if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
speed *= 4;
if(Keyboard.isKeyDown(Keyboard.KEY_W))
{
camera.MoveForwards(speed);
}
if(Keyboard.isKeyDown(Keyboard.KEY_S))
{
camera.MoveForwards(-speed);
}
if(Keyboard.isKeyDown(Keyboard.KEY_A))
{
camera.MoveRight(-speed);
}
if(Keyboard.isKeyDown(Keyboard.KEY_D))
{
camera.MoveRight(speed);
}
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE))
{
camera.moveUp(speed);
}
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
{
Display.destroy();
System.exit(0);
}
if(Keyboard.isKeyDown(Keyboard.KEY_P))
{
if(getTime() - lastF > iKeypressSpeed)
wireframe = !wireframe;
lastF = getTime();
}
if(Keyboard.isKeyDown(Keyboard.KEY_N))
{
if(getTime() - lastF > iKeypressSpeed)
lastF = getTime();
}
if(Keyboard.isKeyDown(Keyboard.KEY_C))
{
if(getTime() - lastF > iKeypressSpeed)
lastF = getTime();
}
checkMouseMove(x, y);
}
Vector2f previous = new Vector2f(), mousePos = new Vector2f();
private float MOUSE_SENSITIVITY = 4.0f;
private void checkMouseMove(float x, float y)
{
float DeltX, DeltY;
int centX = Display.getWidth()/2, centY = Display.getHeight()/2;
DeltX = (float)(centX - x) / MOUSE_SENSITIVITY;
DeltY = (float)(centY - y) / MOUSE_SENSITIVITY;
camera.Rotate(DeltY, DeltX, 0);
Mouse.setCursorPosition(centX, centY);
}
public long getTime()
{
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public void updateFPS()
{
if (getTime() - lastFPS > 1000) {
Display.setTitle("FPS: " + fps);
fps = 0; //reset the FPS counter
lastFPS += 1000; //add one second
}
fps++;
}
long lastFrame;
public int getDelta()
{
long time = getTime();
int delta = (int) (time - lastFrame);
lastFrame = time;
return delta;
}
private void drawGrid()
{
glPushMatrix();
glDisable(GL_TEXTURE_2D);
glColor3f(0.25f, 0.25f, 0.25f);
float gridSize = 30;
int mX = 64, mZ = 64;
glTranslatef(-(mX+gridSize)/2, -5, -(mZ+gridSize)/2);
for(int x = 0; x < mX; ++x)
{
for(int z = 0; z < mZ; ++z)
{
glBegin(GL_LINE_LOOP);
glVertex3f(x, 0, z);
glVertex3f(x+gridSize, 0, z);
glVertex3f(x+gridSize, 0, z+gridSize);
glVertex3f(x, 0, z+gridSize);
glEnd();
}
}
glEnable(GL_TEXTURE_2D);
glPopMatrix();
}
public static void main(String[] args)
{
lwjglFBO city = new lwjglFBO();
city.start();
}
}