请告诉我为什么在没有颜色的情况下渲染立方体,尽管我的VBO包含颜色?我究竟做错了什么?非常感谢提前!
Voxel.java
package cz.rv.cubeworld.mesh;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import java.nio.FloatBuffer;
public class Voxel {
/**
H_______________G
/. /|
/ . / |
D/_____________C/ |
| . | |
| . | |
| ...........|...|
| . E | /F
|______________|/
A B
*/
private static final float[] vertexData = {
// Position // Normal // Color
// Back side - E H G F
-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // E
1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // F
1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // G
-1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // H
// Bottom side - F B A E
1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // F
1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // B
-1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // A
-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // E
// Right side - F G C B
1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // F
1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // G
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // C
1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // B
// Top side - C G H D
1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, // C
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, // G
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, // H
-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, // D
// Left side - A D H E
-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // A
-1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // D
-1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // H
-1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // E
// Front side - A D C B
-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // A
-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // D
1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // C
1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // B
};
private static final int TOTAL_NUMBER_OF_VERTICES = 24;
private static final int VERTEX_ATTRIBUTE_SIZE = 10 * Float.BYTES; // In bytes
private static final int VERTEX_POSITION_SIZE = 3; // In floats
private static final int VERTEX_COLOR_SIZE = 4; // In floats
private static final int FIRST_VERTEX_POSITION_POINTER = 0; // In bytes
private static final int FIRST_VERTEX_NORMAL_POINTER = 3 * Float.BYTES; // In bytes
private static final int FIRST_VERTEX_COLOR_POINTER = 6 * Float.BYTES; // In bytes
private final int vertexBufferObjectHandle;
private final FloatBuffer vertexBufferObject;
public Voxel() {
vertexBufferObject = BufferUtils.createFloatBuffer(vertexData.length); // Allocate direct float buffer
vertexBufferObject.put(vertexData).flip(); // Write data to the buffer and reset the position to zero
vertexBufferObjectHandle = GL15.glGenBuffers(); // Create named buffer object
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferObjectHandle); // Hey, GL, we will be using named buffer object we just generated
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBufferObject, GL15.GL_STATIC_DRAW); // Write vertexBufferObject data to our named buffer object
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); // Tell GL we aren't using any named buffer since now
}
public void render(float x, float y, float z, float xrot, float yrot, float zrot) {
GL11.glLoadIdentity();
GL11.glTranslatef(x, y, z);
GL11.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
GL11.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
GL11.glRotatef(zrot, 0.0f, 0.0f, 1.0f);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferObjectHandle);
GL11.glVertexPointer(VERTEX_POSITION_SIZE, GL11.GL_FLOAT, VERTEX_ATTRIBUTE_SIZE, FIRST_VERTEX_POSITION_POINTER);
GL11.glColorPointer(VERTEX_COLOR_SIZE, GL11.GL_FLOAT, VERTEX_ATTRIBUTE_SIZE, FIRST_VERTEX_COLOR_POINTER);
GL11.glNormalPointer(GL11.GL_FLOAT, VERTEX_ATTRIBUTE_SIZE, FIRST_VERTEX_NORMAL_POINTER);
GL11.glDrawArrays(GL11.GL_QUADS, 0, TOTAL_NUMBER_OF_VERTICES);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
}
}
DisplayExample.java
package cz.rv.cubeworld;
import cz.rv.cubeworld.mesh.Voxel;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
public class DisplayExample {
private float xrot;
private float yrot;
private float zrot;
private float lightAmbient[] = { 1.0f, 1.0f, 1.0f, 1.0f };
private float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
private float lightPosition[] = { 0.0f, 0.0f, 0.1f, 1.0f };
private Voxel voxel;
public static void main(String[] args) {
DisplayExample displayExample = new DisplayExample();
displayExample.start();
}
public void start() {
try {
init();
voxel = new Voxel();
while (!Display.isCloseRequested()) {
render();
Display.update();
Display.sync(60);
xrot += 0.2f;
yrot += 0.3f;
zrot += 0.1f;
}
cleanup();
} catch (LWJGLException | IOException e) {
e.printStackTrace();
System.exit(0);
}
}
private void init() throws LWJGLException, IOException {
createWindow();
initGL();
}
private void createWindow() throws LWJGLException {
DisplayMode displayMode = null;
for (DisplayMode mode : Display.getAvailableDisplayModes()) {
if (mode.getWidth() == 640 && mode.getHeight() == 480 && mode.getBitsPerPixel() == 24) {
displayMode = mode;
break;
}
}
Display.setDisplayMode(displayMode);
Display.setTitle("CubeWorld");
Display.create();
}
private void initGL() {
//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
GLU.gluPerspective(45.0f, (float) Display.getWidth() / (float) Display.getHeight(), 0.1f, 100.0f); // Calculate The Aspect Ratio Of The Window
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
GL11.glEnable(GL11.GL_LIGHTING);
ByteBuffer lightBuffer = ByteBuffer.allocateDirect(16);
lightBuffer.order(ByteOrder.nativeOrder());
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer) lightBuffer.asFloatBuffer().put(lightAmbient).flip()); // Setup The Ambient Light
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer) lightBuffer.asFloatBuffer().put(lightDiffuse).flip()); // Setup The Diffuse Light
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, (FloatBuffer) lightBuffer.asFloatBuffer().put(lightPosition).flip()); // Position The Light
GL11.glEnable(GL11.GL_LIGHT1); // Enable Light One
}
private void cleanup() {
Display.destroy();
}
private void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
voxel.render(0, 0, -5, xrot, yrot ,zrot);
}
}
答案 0 :(得分:0)
看起来你错过了:
GL11.glEnable(GL11.GL_COLOR_MATERIAL);