2013-01-20 95 views
2

我開始學習OpenGL。我可以加載.obj模型並用elementBuffer繪製它。但是我一直在試圖嘗試兩種不同的模型。我想繪製的模型位於實體類中。 我能找到的大部分教程只顯示如何加載和繪製一個sinlge模型。沒有解釋(以我能找到/理解的方式)如何處理多個模型。在OpenGL中繪製多個對象

這裏是我的所有代碼:

public static void main(String[] args) throws LWJGLException, IOException 
{ 
    PixelFormat pixelFormat = new PixelFormat(); 
    ContextAttribs contextAtrributes = new ContextAttribs(3, 2); 
    contextAtrributes.withForwardCompatible(true); 
    contextAtrributes.withProfileCore(true); 

    Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); 
    Display.setTitle("Textured quad!"); 
    Display.create(pixelFormat, contextAtrributes); 

    Mouse.create(); 
    Mouse.setGrabbed(true); 
    Keyboard.create(); 

    glEnable(GL_DEPTH_TEST); 
    glEnable(GL_CULL_FACE); 

    entity = new Entity("planeTex.obj"); 
    entity2 = new Entity("modelTex2.obj"); 

    Shaders.load(); 
    Textures.load(); 
    Camera.create(new Vector3f(0, 1, -0.75f), new Vector3f(-50, 0, 20), HEIGHT, WIDTH); 

    while (!Display.isCloseRequested()) 
    { 

     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

     entity.draw(); 
     entity2.draw(); 

     Display.update(); 
     Display.sync(60); 
    } 

} 

public class Entity 
{ 
    private int vao, vbo, ebo; 
    private int elementSize; 

    public Entity(String name) 
    { 
     vao = glGenVertexArrays(); 
     glBindVertexArray(vao); 
     vbo = glGenBuffers(); 
     *Load vertex data into buffer* 
     glBindBuffer(GL_ARRAY_BUFFER, vbo); 
     glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); 
     ebo = glGenBuffers(); 
     *load data into elementBuffer* 
     *Set elementSize to the element count* 
     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); 
     glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBuffer, GL_STATIC_DRAW); 
    } 

    public void draw() 
    { 
     glBindVertexArray(vao); 
     glBindBuffer(GL_ARRAY_BUFFER, vbo); 
     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); 

     glDrawElements(GL_TRIANGLES, elementSize, GL_UNSIGNED_INT, 0); 
    } 
} 

public class Shaders 
{ 

public static int vertexShader, fragmentShader; 
public static int shaderProgram; 
public static int uniTrans; 

    public static void load() 
    { 
     vertexShader = glCreateShader(GL_VERTEX_SHADER); 
     glShaderSource(vertexShader, loadFile("vertex.shader")); 
     glCompileShader(vertexShader); 

     fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); 
     glShaderSource(fragmentShader, loadFile("fragment.shader")); 
     glCompileShader(fragmentShader); 


     shaderProgram = glCreateProgram(); 
     glAttachShader(shaderProgram, vertexShader); 
     glAttachShader(shaderProgram, fragmentShader); 
     glBindFragDataLocation(shaderProgram, 0, "outColor"); 
     glLinkProgram(shaderProgram); 
     glUseProgram(shaderProgram); 


     // Specify the layout of the vertex data 
     int posAttrib = glGetAttribLocation(shaderProgram, "position"); 
     glEnableVertexAttribArray(posAttrib); 
     glVertexAttribPointer(posAttrib, 3, GL_FLOAT, false, (Float.SIZE/8) * 8, 0); 

     int colAttrib = glGetAttribLocation(shaderProgram, "color"); 
     glEnableVertexAttribArray(colAttrib); 
     glVertexAttribPointer(colAttrib, 3, GL_FLOAT, false, (Float.SIZE/8) * 8, (Float.SIZE/8) * 3); 

     int texAttrib = glGetAttribLocation(shaderProgram, "texcoord"); 
     glEnableVertexAttribArray(texAttrib); 
     glVertexAttribPointer(texAttrib, 2, GL_FLOAT, false, (Float.SIZE/8) * 8, (Float.SIZE/8) * 6); 

     uniTrans = glGetUniformLocation(Shaders.shaderProgram, "model"); 
    } 
} 

的結果是,只有創建的最後實體對象將被繪製。不管畫圖順序如​​何。

+0

「vao,vbo和ebo都是全局整數」 - 你的意思是靜態變量嗎?那麼這就是問題所在。 – kerim

+0

@kerim,它們不是靜態的。我更新了問題以準確顯示課程的外觀。 –

+0

另一個說明 - 你不需要在'draw'的'glBindVertexArray'後面調用'glBindBuffer'。您已經在構造函數中將這些緩衝區與VAO關聯。 – kerim

回答

0

好,我通過將該塊固定它 //指定頂點數據的佈局 INT posAttrib = glGetAttribLocation(Shaders.shaderProgram, 「位置」); glEnableVertexAttribArray(posAttrib); glVertexAttribPointer(posAttrib,3,GL_FLOAT,false,(Float.SIZE/8)* 8,0);

int colAttrib = glGetAttribLocation(Shaders.shaderProgram, "color"); 
    glEnableVertexAttribArray(colAttrib); 
    glVertexAttribPointer(colAttrib, 3, GL_FLOAT, false, (Float.SIZE/8) * 8, (Float.SIZE/8) * 3); 

    int texAttrib = glGetAttribLocation(Shaders.shaderProgram, "texcoord"); 
    glEnableVertexAttribArray(texAttrib); 
    glVertexAttribPointer(texAttrib, 2, GL_FLOAT, false, (Float.SIZE/8) * 8, (Float.SIZE/8) * 6); 

在實體類中,在glDrawElements方法之前繪製循環。配售塊其他地方將導致崩潰的程序:

否則就簡單地畫什麼都沒有「當數組緩衝區對象是殘疾人無法使用補償」。我得到了NicolBolas的感覺,我應該把它放在Entity的構造函數中,但正如我所說的那樣,它在任何地方都不起作用。