2014-10-29 40 views
-1

我正在使用openGL部分工作。場景圖部分基於核心OpenGL1.0,仍然使用glBegin和glEnd。 現在我正在添加新的東西,並且必須使用VAO。我是一個初學者,並使用QOpenGLFunctions_3_0,因爲我無法從openGL註冊表中找到gl3.h 現在的問題是,我使用GL_TEXTURE_2DglBegin(GL_QUADS),它的工作原理, 和我繪製單一glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, 0);它的工作原理。 但是當我使用GL_TEXTURE_2DglDrawElements(GL_QUADS...時,紋理不能正確生成。請告訴我正確的使用方法。 這裏是THD代碼爲什麼紋理(GL_TEXTURE_2D)無法與(VertexArray)一起工作VAO

/** draws Texture */ 

class TextureNode 
    : public Node 
{ 

public: 

TextureNode(const cv::Mat& mat) 
{ 
    m_mat = mat; 
} 
TextureNode(const std::string& id, const cv::Mat& mat) 
    : Node(id) 
{ 
    m_mat = mat; 
} 

~TextureNode() 
{ 
    //glDeleteTextures(1, &m_texture[0]); 
} 

void init(TraversalContext& context) 
{ 
    initializeOpenGLFunctions(); 


    struct Vertex { 
     GLfloat position[3]; 
     GLfloat texcoord[2]; 
     GLfloat normal[3]; 
    }; 
    const int NUM_VERTS = 4; 
    const int NUM_INDICES = 4; 
    Vertex vertexdata[NUM_VERTS] = { 
     {{0, 0, 0},   {0,0}, {0,0,1}}, 
     {{0, 100, 0},  {0,1}, {0,0,1}}, 
     {{100, 100, 0},  {1,1}, {0,0,1}}, 
     {{100, 0, 0},   {1,0}, {0,0,1}}, 
    }; 
    GLubyte indexdata[NUM_INDICES] = { 0, 1, 2, 3 }; 

      // Create and bind a VAO 
    glGenVertexArrays(1, &m_quadVAO); 
    glBindVertexArray(m_quadVAO); 

    glGenBuffers(1, &m_quadPositionVBO); 
    glBindBuffer(GL_ARRAY_BUFFER, m_quadPositionVBO); 

    // copy data into the buffer object 
    glBufferData(GL_ARRAY_BUFFER, NUM_VERTS * sizeof(Vertex), vertexdata, GL_STATIC_DRAW); 


    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position)); 
    glEnableVertexAttribArray(1); 
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoord)); 
    glEnableVertexAttribArray(2); 
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal)); 

    glGenBuffers(1, &m_quadIndexVBO); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_quadIndexVBO); 

    // copy data into the buffer object 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, NUM_INDICES * sizeof(GLubyte), indexdata, GL_STATIC_DRAW); 


    // Create and bind a texture 

    glGenTextures(1, &m_texture);     // Create The Texture 
    glBindTexture(GL_TEXTURE_2D, m_texture); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_mat.cols, m_mat.rows, 0, GL_BGR, GL_UNSIGNED_BYTE, m_mat.data); 

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP); 

    //////// At this point the VAO is set up with two vertex attributes 
    //////// referencing the same buffer object, and another buffer object 
    //////// as source for index data. We can now unbind the VAO, go do 
    //////// something else, and bind it again later when we want to render 
    //////// with it. 

    glBindTexture(GL_TEXTURE_2D, 0); 
    glBindVertexArray(NULL);  

    //////glBindBuffer(GL_ARRAY_BUFFER, NULL); 
    //glBindTexture(GL_TEXTURE_2D, NULL); 
    //////glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL); 


} 

/** apply transformation */ 
void doWork(TraversalContext& context) 
{ 

//QTime time; 
//time.start(); 
    initializeOpenGLFunctions(); 
    glDisable(GL_LIGHTING); 

    glEnable(GL_TEXTURE_2D); 
    glBindVertexArray(m_quadVAO); 



    //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 



    glActiveTexture(GL_TEXTURE0 + m_texture - 1); 
    glBindTexture(GL_TEXTURE_2D, m_texture); 
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, 0); 

    //glBegin(GL_QUADS); 
    // glColor3d(0,0,0); 
    // glTexCoord2f(1.0f, 0.0f); glVertex2d(0, 0); 
    // glTexCoord2f(1.0f, 1.0f); glVertex2d(0, m_mat.rows); 
    // glTexCoord2f(0.0f, 1.0f); glVertex2d(m_mat.cols, m_mat.rows); 
    // glTexCoord2f(0.0f, 0.0f); glVertex2d(m_mat.cols, 0); 
    //glEnd(); 

    glBindTexture(GL_TEXTURE_2D, 0); 
    glDisable(GL_TEXTURE_2D); 


    glBindVertexArray(0); 
    glEnable(GL_LIGHTING); 
} 
void destroy(TraversalContext& context) 
{ 

    glDeleteVertexArrays(1, &m_quadVAO); 
    glDeleteTextures(1,&m_texture); 
    glDeleteBuffers(1, &m_quadPositionVBO); 
    glDeleteBuffers(1, &m_quadTexcoordVBO); 
    glDeleteBuffers(1, &m_quadIndexVBO); 
} 


protected: 

GLuint m_quadVAO; 
GLuint m_quadPositionVBO; 
GLuint m_quadTexcoordVBO; 
GLuint m_quadIndexVBO; 
GLuint m_texture; 
///** list of vertices */ 
GLfloat m_vertices[4][2]; 
cv::Mat m_mat; 
}; 
+0

沒有'gl3.h'這樣的東西,至少在OpenGL中不是。您必須手動或使用類似'GLEW'的方式加載擴展函數。 – 2014-10-29 14:10:24

+0

@ColonelThirtyTwo這隻在Windows上纔是正確的。在其他平臺上,您只需包含OpenGL標頭,然後調用函數。 – 2014-10-29 14:16:21

+0

在許多使用GLX的平臺(例如Linux)上也是如此。 OS X實際上是OpenGL實現中的奇怪之處,因爲在構建時鏈接到的庫包含所有符號,無論安裝的是什麼硬件驅動程序。在任何情況下,'OpenGL/gl3.h'都是蘋果構造。如果你想從OpenGL註冊表中得到相同的結果,它會是'GL/glcorearb.h' – 2014-10-29 16:21:45

回答

0
glEnableVertexAttribArray(0); 
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position)); 
glEnableVertexAttribArray(1); 
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoord)); 
glEnableVertexAttribArray(2); 
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal)); 

這是使用通用頂點屬性,但使用時的共享者,並AFAIK不與固定功能管線的工作,纔有意義。請創建着色器來使用它們,或者使用glVertexPointer,glTexCoordPointer和/或glNormalPointer來替代。

glActiveTexture(GL_TEXTURE0 + m_texture - 1); 

這是不正確的。 glActiveTexture選擇要綁定的紋理單元;它根本不使用紋理名稱。既然你不使用着色器,這應該只是glActiveTexture(GL_TEXTURE0)

備註:您不需要使用索引緩衝區來繪製單個四元組;您可以改用glDrawArrays

+0

我試過glActiveTexture(GL_TEXTURE0),它的結果是一樣的。而且,我也試過glDrawArrays(GL_QUADS,0,4);它仍然是一樣的,只有一種顏色,沒有紋理 – langbeibei 2014-10-29 14:18:08

+0

最後一個參數是要繪製多少個頂點,您應該使用'glDrawArrays(GL_QUADS,0,4);' – 2014-10-29 14:23:31

+0

現在我使用{glVertexPointer(3,GL_FLOAT,sizeof (頂點),(void *)offsetof(頂點,位置)); glTexCoordPointer(2,GL_FLOAT,sizeof(Vertex),(void *)offsetof(Vertex,texcoord)); glNormalPointer(GL_FLOAT,sizeof(Vertex),(void *)offsetof(Vertex,normal)); }和dann \t \t glActiveTexture(GL_TEXTURE0); \t \t glBindTexture(GL_TEXTURE_2D,m_texture); \t \t glDrawArrays(GL_QUADS,0,4);那麼我看不到所有的灰色 – langbeibei 2014-10-29 14:35:54

相關問題