Ejemplo n.º 1
0
gl_context::gl_context(std::string const& name, int width, int height) {
   init_glfw();
   create_glfw_window(name, width, height);
   glfwMakeContextCurrent(_main_window);
   TwInit(TW_OPENGL_CORE, nullptr);
   TwWindowSize(width, height);
   _tweak_bar = TwNewBar("MainBar");
   init_glew();
   glGenVertexArrays(1, &_vertex_array_id);
   glBindVertexArray(_vertex_array_id);
}
Ejemplo n.º 2
0
int main(int argc, char const *argv[]) {

    struct window w;
    if(create_glfw_window(&w)) {
        return -1;
    }

    struct file vertex_file;
    vertex_file.filename = "shaders/shader.vert";
    if(read_file(&vertex_file)) {
        return -1;
    }

    struct file fragment_file;
    fragment_file.filename = "shaders/shader.frag";
    if(read_file(&fragment_file)) {
        return -1;
    }

    GLuint program = create_gl_program(vertex_file.content, fragment_file.content);
    glCallWrapper(glUseProgram(program));

    free(vertex_file.content);
    free(fragment_file.content);

    GLuint vao;
    glCallWrapper(glGenVertexArrays(1, &vao));
    glCallWrapper(glBindVertexArray(vao));

    // Position
    glEnableVertexAttribArray(0);

    // UV
    glEnableVertexAttribArray(3);

    GLuint vbo_1;
    glCallWrapper(glGenBuffers(1, &vbo_1));
    glCallWrapper(glBindBuffer(GL_ARRAY_BUFFER, vbo_1));

    GLfloat position[] = {
        -0.50,  0.50, 0.00,
         0.50, -0.50, 0.00,
        -0.50, -0.50, 0.00,
        -0.50,  0.50, 0.00,
         0.50,  0.50, 0.00,
         0.50, -0.50, 0.00,
    };

    glCallWrapper(glBufferData(GL_ARRAY_BUFFER, sizeof(position), position, GL_DYNAMIC_DRAW));
    glCallWrapper(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL));

    mat4 mvp;
    cglm_mvp(mvp);

    GLuint mvp_id = glGetUniformLocation(program, "mvp");
    glCallWrapper(glUniformMatrix4fv(mvp_id, 1, GL_FALSE, &mvp[0][0]));

    GLuint vbo_3;
    glCallWrapper(glGenBuffers(1, &vbo_3));
    glCallWrapper(glBindBuffer(GL_ARRAY_BUFFER, vbo_3));

    const GLfloat uvs[] = {
        0.00, 0.40,
        0.40, 0.00,
        0.00, 0.00,
        0.00, 0.40,
        0.40, 0.40,
        0.40, 0.00,
    };

    glCallWrapper(glBufferData(GL_ARRAY_BUFFER, sizeof(uvs), uvs, GL_DYNAMIC_DRAW));
    glCallWrapper(glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, NULL));

    struct image img;
    img.imagename = "textures/uvtemplate.bmp";
    if(read_bmp(&img)) {
        return -1;
    }

    GLuint img_tex;
    glCallWrapper(glGenTextures(1, &img_tex));
    glCallWrapper(glBindTexture(GL_TEXTURE_2D, img_tex));
    glCallWrapper(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.width, img.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img.content));
    glCallWrapper(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
    glCallWrapper(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));

    free(img.content);

    GLuint img_tex_id = glGetUniformLocation(program, "img_sampler");
    glCallWrapper(glUniform1i(img_tex_id, 0 /* Default active texture */));

    while(1) {
        glCallWrapper(glDrawArrays(GL_TRIANGLES, 0, 6));
		glfwSwapBuffers(w.window);
        glfwPollEvents();
    }

    return 0;
}