简体   繁体   English

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

[英]Code Assist, OpenGL VAO/VBO Classes not drawing

Edit II: 编辑二:
Current Code works great! 当前的代码很棒! Thanks everyone. 感谢大家。 I went ahead and included my shader code for reference at the bottom though they do absolutely nothing at this point really. 我继续把我的着色器代码包含在底部以供参考,尽管它们在这一点上确实没有做任何事情。

I am trying to get up and going with OpenGL 4.1 and am still very early in development. 我正在尝试使用OpenGL 4.1并且仍处于开发阶段。 Currently I'm not even really using 4.0 features yet in this project, so this is just as much an OpenGL 3 question as well. 目前我在这个项目中还没有真正使用4.0功能,所以这也是一个OpenGL 3问题。

The goal I was working on first was simply working out two classes to handle VAOs and VBOs. 我首先要完成的目标只是编写两个类来处理VAO和VBO。 I had some misconceptions but finally got past the blank screen. 我有一些误解,但终于通过了空白屏幕。

/* 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;
}

Basically I'm just hoping to get anything on the screen at this point. 基本上我只是希望在这一点上获得屏幕上的任何内容。 I am using glfw and glew. 我正在使用glfw和glew。 Am I completely leaving some things out or do I only need to correct something? 我完全遗漏了一些东西还是我只需要纠正一些事情? Code is somewhat mangled at the moment, sorry. 对不起,代码有点受损了。

base.vert 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 base.frag

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

out vec3 colorOut;

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

vertex is a vector. 顶点是一个向量。 Taking its address will not give you a pointer to the data. 取其地址不会为您提供指向数据的指针。

Edit to add: Right. 编辑添加:对。 It still does not work, because you have at least 2 more issues: 它仍然无效,因为您至少还有两个问题:

  1. You don't call any gl*Pointer call. 你不打电话给任何gl *指针。 The GL won't know what it needs to pull from your vertex buffer objects GL不会知道它需要从顶点缓冲区对象中提取什么
  2. your vertex data that you put in your vertex array is 3 times the same vertex. 放在顶点数组中的顶点数据是同一顶点的3倍。 A triangle with the 3 points at the same location: 在同一位置有3个点的三角形:

    for(int i=0; i<9; i++) myVBO[0].vertex.push_back((i%3)*.2); for(int i = 0; i <9; i ++)myVBO [0] .vertex.push_back((i%3)*。2); //Arbitrary vertex values //任意顶点值

It creates 3 (.0 .2 .4) vectors, all at the same location. 它创建3个(.0 .2 .4)向量,所有这些向量都在同一位置。

That iBound member of VBO looks suspicious. 那个VBO的iBound成员看起来很可疑。 The OpenGL binding state may change, for example after switching the bound VAO, but the VBO class instance still thinks it's active. OpenGL绑定状态可能会发生变化,例如在切换绑定的VAO之后,但VBO类实例仍然认为它是活动的。 Just drop iBound altogether and re-bind every time you need the object. 只需完全删除iBound并在每次需要对象时重新绑定。 With modern drivers rebinding an already bound object is almost for free. 使用现代驱动程序重新绑定已经绑定的对象几乎是免费的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM