0

I am fairly new to opengl and don't see how element buffer vbo and array buffer buffer1 work together in creating this square. I don't understand buffer objects in general and a brief explanation of how they work would be appreciated. Once again there are no errors in this code i just don't understand the how the vertex data and indices data are being carried throughout the program and how the buffers work together. This is source code. Also the fragment shader and vertex shader I am not including.

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>




struct Shaderprogramsource
{
std::string VertexSouce;
std::string FragmentSource;
};

static Shaderprogramsource Parseshader(const std::string& filepath)
{
std::ifstream stream(filepath);

enum class Shadertype
{
    VERTEX = 0,
    FRAGMENT = 1,
    NONE = 5
};

std::string line;
std::stringstream ss[3];

Shadertype type = Shadertype::NONE;

while (getline(stream, line))
{
    if (line.find("#shader") != std::string::npos)
    {
        if (line.find("vertex") != std::string::npos)
            type = Shadertype::VERTEX;

        else if (line.find("fragment") != std::string::npos)
            type = Shadertype::FRAGMENT;
    }
    else
    {
        ss[(int)type] << line << "\n";
    }
}

return Shaderprogramsource{ ss[0].str(), ss[1].str() };
}

static int CompileShader(unsigned int type, const std::string& Source)
{
unsigned int id = glCreateShader(type);
const char* src = Source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);

int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);

if (result == GL_FALSE)
{
    int length;
    glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);

    char* message = (char*)alloca(length * sizeof(char));
    glGetShaderInfoLog(id, length, &length, message);

    std::cout << message;

    return 0;
}

return id;
}

static unsigned int CreateShader(
const std::string& Vertexshader,
const std::string& Fragmentshader)
{
unsigned int program = glCreateProgram();
unsigned int vertex = CompileShader(GL_VERTEX_SHADER, Vertexshader);
unsigned int fragment = CompileShader(GL_FRAGMENT_SHADER, Fragmentshader);

glAttachShader(program, vertex);
glAttachShader(program, fragment);

glLinkProgram(program);
glValidateProgram(program);

return program;
}

int main(void)
{
GLFWwindow* window;

/* Initialize the library */
if (!glfwInit())
    return -1;

/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
    glfwTerminate();
    return -1;
}

/* Make the window's context current */
glfwMakeContextCurrent(window);

if (GLEW_OK == glewInit())
{
}

float vertices[] = { -0.5, -0.5,
                      0.5, -0.5, 
                      0.5, 0.5, 
                     -0.5, 0.5 };

unsigned int indices[] = {

    0, 1, 2,
    2, 3, 0


};


unsigned int buffer1;

unsigned int vbo;

glGenBuffers(1, &buffer1);
glBindBuffer(GL_ARRAY_BUFFER, buffer1);
glBufferData(GL_ARRAY_BUFFER, 6 * 2 * sizeof(float), vertices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);

glEnableVertexAttribArray(0);

glGenBuffers(1, &vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices, GL_STATIC_DRAW);





Shaderprogramsource source = Parseshader("res/shaders/Basic.Shader");

unsigned int shader =
    CreateShader(source.VertexSouce, source.FragmentSource);




glUseProgram(shader);


int location = glGetUniformLocation(shader, "Color_u");
if (location > -1) {
    glUniform4f(location, 0.8f, 0.0f, 0.0f, 1.0f);

}



std::cout << source.VertexSouce;





/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
    /* Render here */

    glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(shader);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);

    /* Swap front and back buffers */
    glfwSwapBuffers(window);

    /* Poll for and process events */
    glfwPollEvents();
}

glDeleteProgram(shader);

glfwTerminate();
return 0;
}
chiefus
  • 31
  • 5

0 Answers0