代码辅助,OpenGL VAO / VBO类不绘图

时间:2011-02-04 10:57:13

标签: c++ class opengl opengl-3 vertex-buffer

  

编辑II:
  当前的代码很棒!感谢大家。我继续前进并在底部包含了我的着色器代码以供参考,尽管它们现在完全没有做任何事情。

我正在尝试使用OpenGL 4.1并且仍处于开发阶段。目前我在这个项目中还没有真正使用4.0功能,所以这也是一个OpenGL 3问题。

我首先要完成的目标只是设计两个类来处理VAO和VBO。我有一些误解,但终于超越了空白屏幕。

/* THIS CODE IS NOW FULLY FUNCTIONAL */
/* well, fully is questionable lol, should work out of the box with glew and glfw */

/* A simple function that will read a file into an allocated char pointer buffer */
/* Borrowed from OpenGL.org tutorial */
char* filePull(char *file)
{
    FILE *fptr;
    long length;
    char *buf;

    fptr = fopen(file, "r"); /* Open file for reading */
    if (!fptr) /* Return NULL on failure */
        return NULL;
    fseek(fptr, 0, SEEK_END); /* Seek to the end of the file */
    length = ftell(fptr); /* Find out how many bytes into the file we are */
    buf = (char*)malloc(length+1); /* Allocate a buffer for the entire length of the file and a null terminator */
    fseek(fptr, 0, SEEK_SET); /* Go back to the beginning of the file */
    fread(buf, length, 1, fptr); /* Read the contents of the file in to the buffer */
    fclose(fptr); /* Close the file */
    buf[length] = 0; /* Null terminator */

    return buf; /* Return the buffer */
}


class VBO
{
    public:

    GLuint buffer;
    bool isBound;
    vector<void*> belongTo;
    vector<GLfloat> vertex;
    GLenum usage;

    void Load()
    {   glBufferData(GL_ARRAY_BUFFER, vertex.size()*sizeof(GLfloat), &vertex[0], usage);    }
    void Create(void* parent)
    {
        glGenBuffers(1, &buffer);
        glBindBuffer(GL_ARRAY_BUFFER, buffer);
        glBufferData(GL_ARRAY_BUFFER, vertex.size()*sizeof(GLfloat), &vertex[0], usage);
        isBound=true;
        belongTo.push_back(parent);

    }
    void Activate()
    {
        if(!isBound)    glBindBuffer(GL_ARRAY_BUFFER, buffer);
        isBound=true;
    }
    void Deactivate(){  glBindBuffer(GL_ARRAY_BUFFER, 0);  }

    VBO() : isBound(false), usage(GL_STATIC_DRAW)
    {      }
    ~VBO()  {   }

    private:
};

class VAO
{
    public:
    GLuint buffer;
    string key;
    unsigned long long cursor;

    vector<VBO> child;

    void Create()
    {
        glGenVertexArrays(1, &buffer);
        for(unsigned int i=0; i<child.size(); i++)
            child[i].Create(this);
    }
    void Activate()
    {
        glBindVertexArray(buffer);
        for(unsigned int i=0; i<child.size(); i++)
            child[i].Activate();
    }
    void Release(){ glBindVertexArray(0); }
    void Remove(){  glDeleteVertexArrays(1, &buffer);   }

    VAO() : buffer(1)   {     }
    ~VAO()  {       }

    private:
};

int main()
{
    int     width=640, height=480, frame=1;    bool    running = true;

    glfwInit();

    if( !glfwOpenWindow( width, height, 0, 0, 0, 0, 0, 0, GLFW_WINDOW ) )
    {        glfwTerminate();   return 13;   }
    glfwSetWindowTitle("Genesis");

    glewInit();
    cout<<(GLEW_VERSION_4_1?"yes":"no"); //yes


    GLchar *vsource, *fsource;
    GLuint _vs, _fs;
    GLuint Shader;

    vsource = filePull("base.vert");
    fsource = filePull("base.frag");

    /* Compile Shaders */
    _vs = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(_vs, 1, (const GLchar**)&vsource, 0);
    glCompileShader(_vs);
//    glGetShaderiv(_vs, GL_COMPILE_STATUS, &IsCompiled_VS);
    _fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(_fs, 1, (const GLchar**)&fsource, 0);
    glCompileShader(_fs);
/***************** ^ Vertex | Fragment v *********************/
    glAttachShader(Shader, _vs);
    glAttachShader(Shader, _fs);
//    glGetShaderiv(_fs, GL_COMPILE_STATUS, &IsCompiled_FS);
    glBindAttribLocation(Shader, 0, "posIn");
    glLinkProgram(Shader);
//    glGetProgramiv(shaderprogram, GL_LINK_STATUS, (int *)&IsLinked);

    VAO Object3D;
    VBO myVBO[3];

    glUseProgram(Shader);

    for(int i=0; i<9; i++)
        myVBO[0].vertex.push_back((i%9)*.11); //Arbitrary vertex values

    Object3D.child.push_back(myVBO[0]);
    Object3D.Create();

    glClearColor( 0.7f, 0.74f, 0.77f, 0.0f ); //Black got lonely

   int i=0; while(running)
    {
        frame++;

        glfwGetWindowSize( &width, &height );
        height = height > 0 ? height : 1;
        glViewport( 0, 0, width, height );
        glClear( GL_COLOR_BUFFER_BIT );

        /*   Bind, Draw, Unbind */
        Object3D.Activate();
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 9);

        Object3D.Release();
        glfwSwapBuffers();

        // exit if ESC was pressed or window was closed
        running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED);
        i++;
    }

    glUseProgram(0); glDisableVertexAttribArray(0);
    glDetachShader(Shader, _vs); glDetachShader(Shader, _fs);
    glDeleteProgram(Shader); glDeleteShader(_vs); glDeleteShader(_fs);
    glDeleteVertexArrays(1, &Object3D.buffer);

    glfwTerminate();
    return 0;
}

基本上我只是希望此时屏幕上有任何内容。我正在使用glfw和glew。我完全遗漏了一些东西还是我只需要纠正一些事情?对不起,代码有点受损了。

base.vert

// Fragment Shader – file "base.vert"    
#version 300

in  vec3 posIn;
out vec4 colorOut;

void main(void)
{
    gl_Position = vec4(posIn, 1.0);
    colorOut = vec4(3.0,6.0,4.0,1.0);
}

base.frag

// Vertex Shader – file "base.frag"
#version 300

out vec3 colorOut;

void main(void)
{
    colorOut = vec3(1.0,10,1.0);
}

2 个答案:

答案 0 :(得分:5)

&vertex

顶点是一个向量。取其地址不会为您提供指向数据的指针。

编辑添加: 对。它仍然不起作用,因为你至少还有两个问题:

  1. 您不会拨打任何gl *指针呼叫。 GL不知道它需要从顶点缓冲区对象中提取什么
  2. 放在顶点数组中的顶点数据是同一顶点的3倍。在同一位置有3个点的三角形:

    for(int i = 0; i&lt; 9; i ++)     myVBO [0] .vertex.push_back(第(i%3)* 2); //任意顶点值

  3. 它创建3个(.0 .2 .4)向量,所有这些都在同一位置。

答案 1 :(得分:1)

VBO的iBound成员看起来很可疑。 OpenGL绑定状态可能会发生变化,例如在切换绑定的VAO之后,但VBO类实例仍然认为它是活动的。只需完全删除iBound并在每次需要对象时重新绑定。使用现代驱动程序重新绑定已经绑定的对象几乎是免费的。