LWJGL3 ELEMENT_ARRAY图像出不来
Main:
import GameTest.Graphics.Mesh;
import GameTest.Graphics.Renderer;
import GameTest.Graphics.Shader;
import GameTest.Graphics.Vertex;
import GameTest.IO.Input;
import GameTest.Math.Vector3f;
import org.lwjgl.glfw.GLFW;
public final class Main {
private WindowManager window;
private Shader shader;
private final Timer timer=new Timer();
private final Input input=new Input();
private final Mesh mesh=new Mesh(new Vertex[]{
new Vertex(new Vector3f(0.5f, 0.5f,0f)),
new Vertex(new Vector3f(-0.5f, 0.5f,0f)),
new Vertex(new Vector3f(-0.5f, -0.5f,0f))
},new int[]{
0,1,2,
1,2,3
});
private Renderer renderer;
public void start(){
init();
while(!GLFW.glfwWindowShouldClose(window.getWindow())){
update();
render();
GLFW.glfwSwapBuffers(window.getWindow());
}
cleanup();
}
private void init(){//
shader=new Shader("src\\GameTest\\resources\\shaders\\mainVertex.glsl","src\\GameTest\\resources\\shaders\\mainFragment.glsl");
window=new WindowManager("game",1000,700,true);
renderer=new Renderer(shader);
/*各种初始化*/
timer.init();
window.create();
input.init(window.getWindow());
mesh.create();
// shader.create();
// window.setFullScreen(false);
// /*初始化输入组件*/
// long win=window.getWindow();
// GLFW.glfwSetKeyCallback(win,input.getKeyboard());
// GLFW.glfwSetCursorPosCallback(win,input.getMouseMovement());
// GLFW.glfwSetMouseButtonCallback(win,input.getMouseButton());
// GLFW.glfwSetScrollCallback(win,input.getScrollCallback());
/*辅助变量初始化*/
fullscreenShifted=timer.getCurrentTime();
}
private void updateBackGround(){
float[] clear= window.getClearColor().get();
//红
if(input.isHold(GLFW.GLFW_KEY_W)){
clear[0]+=0.01f;
}
if(input.isHold(GLFW.GLFW_KEY_S)){
clear[0]-=0.01f;
}
if(clear[0]>1.0f)clear[0]=1.0f;else if(clear[0]<0.0f) clear[0]=0.0f;
//绿
if(input.isHold(GLFW.GLFW_KEY_A)){
clear[1]+=0.01f;
}
if(input.isHold(GLFW.GLFW_KEY_D)){
clear[1]-=0.01f;
}
if(clear[1]>1.0f)clear[1]=1.0f;else if(clear[1]<0.0f) clear[1]=0.0f;
//蓝
if(input.isHold(GLFW.GLFW_KEY_LEFT_SHIFT)){
clear[2]+=0.01f;
}
if(input.isHold(GLFW.GLFW_KEY_SPACE)){
clear[2]-=0.01f;
}
if(clear[2]>1.0f)clear[2]=1.0f;else if(clear[2]<0.0f) clear[2]=0.0f;
window.setClearColour(clear[0],clear[1],clear[2]);
}
private void updateXps(){
GLFW.glfwSetWindowTitle(window.getWindow(),"Game / FPS: "+timer.getFps()+" UPS: "+timer.getUps());
}
private double fullscreenShifted;
private final static double CONFIRM_DELTA=0.5;
private void update(){
updateBackGround();
updateXps();
window.update();
if(input.isHold(Input.FULLSCREEN_SHIFTER)&&timer.getCurrentTime()-fullscreenShifted>CONFIRM_DELTA){
window.setFullScreen(!window.isFullScreen());
fullscreenShifted=timer.getCurrentTime();
}
if(input.isHold(Input.ESCAPE)){
GLFW.glfwWindowShouldClose(window.getWindow());
}
timer.updaterUpdate();
}
private void cleanup(){
window.cleanup();
input.destroy();
shader.destroy();
}
private void render(){
renderer.renderMesh(mesh);
timer.renderUpdate();
}
Mesh:
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL46;
import org.lwjgl.system.MemoryUtil;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
public class Mesh {
private Vertex[] vertices;
private int[] indices;
private int vao,pbo,ibo;
public Mesh(Vertex[] vertices,int[] indices){
this.indices=indices;
this.vertices=vertices;
}
public void create(){
vao= GL46.glGenVertexArrays();
GL46.glBindVertexArray(vao);
FloatBuffer positionBuffer= MemoryUtil.memAllocFloat(vertices.length*3);
float[] positionData=new float[vertices.length*3];
for (int i = 0; i < vertices.length; i++) {
positionData[i*3] = vertices[i].getPosition().getX();
positionData[i*3+1] = vertices[i].getPosition().getY();
positionData[i*3+2] = vertices[i].getPosition().getZ();
}
positionBuffer.put(positionData).flip();
pbo=GL46.glGenBuffers();
GL46.glBindBuffer(GL46.GL_ARRAY_BUFFER,pbo);
GL46.glBufferData(GL46.GL_ARRAY_BUFFER,positionBuffer,GL46.GL_STATIC_DRAW);
GL46.glVertexAttribPointer(0,3, GL46.GL_FLOAT,false,0,0);
GL46.glBindBuffer(GL46.GL_ARRAY_BUFFER,0);
IntBuffer indicesBuffer=MemoryUtil.memAllocInt(indices.length);
indicesBuffer.put(indices).flip();
ibo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}
public Vertex[] getVertices() {
return vertices;
}
public int[] getIndices() {
return indices;
}
public int getVAO() {
return vao;
}
public int getPBO() {
return pbo;
}
public int getIBO() {
return ibo;
}
Renderer.renderMesh:
public void renderMesh(Mesh mesh){
GL30.glBindVertexArray(mesh.getVAO());
GL20.glEnableVertexAttribArray(0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER,mesh.getIBO());
GL11.glDrawElements(GL11.GL_TRIANGLES,mesh.getIndices().length,GL11.GL_FLOAT,0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER,0);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
}
WindowManager:
import GameTest.Math.Vector3f;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.glfw.GLFWWindowSizeCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL46;
import static org.lwjgl.system.MemoryUtil.NULL;
public final class WindowManager {
private final Vector3f clearColor=new Vector3f(1.0f,0.0f,0.0f);
private final String title;
private int width,height;
private long window;
private int[] winX=new int[1],winY=new int[1];
private boolean resize,vSync,fullScreen;
private GLFWWindowSizeCallback sizeCallback;
public WindowManager(String title, int width, int height, boolean vSync){
this.title=title;
this.height=height;
this.width=width;
this.vSync=vSync;
}
private void createCallBacks(){
GLFW.glfwSetFramebufferSizeCallback(window,(window,width,height)->{
this.width=width;
this.height=height;
this.window=window;
});
GLFW.glfwSetKeyCallback(window,(window,key,scancode,action,mods)->{
if(key==GLFW.GLFW_KEY_ESCAPE&&action==GLFW.GLFW_RELEASE){
GLFW.glfwSetWindowShouldClose(window,true);
}
});
WindowManager t=this;
sizeCallback=new GLFWWindowSizeCallback() {
@Override
public void invoke(long window, int width, int height) {
t.width=width;
t.height=height;
resize=true;
}
};
GLFW.glfwSetWindowSize(window,width,height);
}
public void create(){
GLFWErrorCallback.createPrint(System.err);
if(!GLFW.glfwInit()){
System.err.println("GLFW initialization failed");
System.exit(-1);
}
GLFW.glfwDefaultWindowHints();
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GL46.GL_TRUE);
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GL46.GL_TRUE);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 4);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 0);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE,GLFW.GLFW_OPENGL_CORE_PROFILE);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT,GL46.GL_TRUE);
window=GLFW.glfwCreateWindow(width,height,title, fullScreen ? GLFW.glfwGetPrimaryMonitor() : NULL , NULL);
if(window==0){
System.err.println("window create failed");
System.exit(-1);
}
createCallBacks();
GLFWVidMode vidMode=GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
winX[0]= (vidMode.width()-width)/2;
winY[0]= (vidMode.height()-height)/2;
GLFW.glfwSetWindowPos(window,winX[0], winY[0]);
GLFW.glfwMakeContextCurrent(window);
GL.createCapabilities();
GLFW.glfwSwapInterval(1);
GLFW.glfwShowWindow(window);
GL46.glClear(GL46.GL_COLOR_BUFFER_BIT | GL46.GL_DEPTH_BUFFER_BIT);
GL46.glEnable(GL46.GL_STENCIL_TEST);
GL46.glEnable(GL46.GL_CULL_FACE);
GL46.glCullFace(GL46.GL_BACK);
}
public void update(){
if(resize){
GL46.glViewport(winX[0],winY[0],width,height);
resize=false;
}
GL46.glClear(GL46.GL_COLOR_BUFFER_BIT);
GLFW.glfwPollEvents();
}
public Vector3f getClearColor(){
return this.clearColor;
}
public void cleanup(){
GLFW.glfwDestroyWindow(window);
}
public boolean isFullScreen() {
return fullScreen;
}
public void setFullScreen(boolean fullScreen) {
if(this.fullScreen!=fullScreen) {
this.fullScreen = fullScreen;
resize = true;
if (fullScreen) {
GLFW.glfwGetWindowPos(window,winX,winY);
GLFW.glfwSetWindowMonitor(window, GLFW.glfwGetPrimaryMonitor(), 0, 0, width, height, 0);
} else {
GLFW.glfwSetWindowMonitor(window, 0, winX[0], winY[0], width, height, 0);
}
}
}
public void setClearColour(float r, float g, float b){
GL46.glClearColor(r,g,b,1.0f);
clearColor.set(r,g,b);
}
public long getWindow() {
return window;
}
运行只有窗口没有图形,在网上找了好多教程都没用,每个教程教的还都不一样
希望运行出来是白色的方形