A
A
Airat19952016-10-07 04:38:22
OpenGL
Airat1995, 2016-10-07 04:38:22

Where did I go wrong with the binding?

Hello, for almost 2 weeks I have been suffering in order to write a simple wrapper over OpenGL (I am writing my thesis on the development of game engines). Can someone tell me what my mistake is (I know that it’s impudent of me to ask to see someone else’s code like this, but I have been suffering for a long time and nothing comes into my head)
This is the code of the main method

GLfloat vertex_positions[] = {
            0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
            0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
            -0.5f, -0.5f, 0.0f, 0.0f, 1.0f
    };
    GLchar *vertexShader = (GLchar *)
                    "#version 150\n"
                    "\n"
                    "in vec2 position;\n"
                    "\n"
                    "void main()\n"
                    "{\n"
                    "    gl_Position = vec4(position, 0.0, 1.0);\n"
                    "}";
    GLchar *fragmentShader = (GLchar *)
            "#version 400 core\n"
            "\n"
            "out vec4 fColor;\n"
            "\n"
            "void main()\n"
            "{\n"
            "    fColor = vec4(0.5, 0.4, 0.8, 1.0);\n"
            "}";
    static const GLfloat vertex_colors[] =
            {
                    1.0f, 1.0f, 1.0f, 1.0f,
                    1.0f, 1.0f, 0.0f, 1.0f,
                    1.0f, 0.0f, 1.0f, 1.0f,
                    0.0f, 1.0f, 1.0f, 1.0f
            };
    Shader shader;
    shader.CompileShader(Shader::ShaderType::Fragment, fragmentShader);
    shader.CompileShader(Shader::ShaderType::Vertex, vertexShader);
    glBindFragDataLocation(*shader.GetProgram(), 0, "fColor");
    shader.Link();
    Texture texture((GLfloat *) vertex_colors, 16);
    Model model (vertex_positions, &shader, &texture);
    while (!glfwWindowShouldClose(window))
    {
        static const float black[] = { 0.2f, 0.3f, 1.0f, 1.0f };
        glClearBufferfv(GL_COLOR, 0, black);
        shader.Bind();
        model.Draw();
        glfwPollEvents();
        glfwSwapBuffers(window);
    }

Here is the Model class code
void Model::Draw() {
    glBindVertexArray(_vao);
    glBindBuffer(GL_ARRAY_BUFFER, _vbo);
    glDrawArrays(GL_TRIANGLES, 0, 3);
}

Model::Model(GLfloat meshVert[], int m_size, GLuint vertexInd[], int v_size, Shader *material, Texture *texture) {
    glGenVertexArrays(1, &_vao);
    glBindVertexArray(_vao);

    glGenBuffers(1, &_vbo);
    glBindBuffer(GL_ARRAY_BUFFER, _vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*m_size, meshVert, GL_STATIC_DRAW);

    GLint posAttrib = glGetAttribLocation(*material->GetProgram(), "position");
    glEnableVertexAttribArray((GLuint) posAttrib);
    glVertexAttribPointer((GLuint) posAttrib, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0);

    GLint colAttrib = glGetAttribLocation(*material->GetProgram(), "color");
    glEnableVertexAttribArray((GLuint) colAttrib);
    glVertexAttribPointer((GLuint) colAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));

}

The profiler throws an error that I'm using glDrawArrays incorrectly, but I checked the uploaded data and it was empty. Everything else seems to be in place (shaders and the program are linked and displayed in the profiler)
PS: MacOS X Sierra, Intel HD 6000. I think this data is enough.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question