Question: Add a triangle that appears in the lower left corner that is the same size as the other triangles but is displayed point down. The triangle should be in the same relative position in its corner as the other triangles. Color the triangle brown. Add a triangle that appears in the upper left corner that is the same size as the other triangles but is displayed point down. The triangle should be in the same relative position in its corner as the other triangles. Color the triangle slate gray. Remembering the painter's algorithm, arrange the code so that this new triangle appears behind the red-violet triangle. Change the title of the window to 'Five Triangles' Your solution should look like the screenshot given.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Question:

  1. Add a triangle that appears in the lower left corner that is the same size as the other triangles but is displayed point down.
    1. The triangle should be in the same relative position in its corner as the other triangles.
    2. Color the triangle brown.
  2. Add a triangle that appears in the upper left corner that is the same size as the other triangles but is displayed point down.
    1. The triangle should be in the same relative position in its corner as the other triangles.
    2. Color the triangle slate gray.
    3. Remembering the painter's algorithm, arrange the code so that this new triangle appears behind the red-violet triangle.
  3. Change the title of the window to 'Five Triangles'

Your solution should look like the screenshot given.

 

Code that requires modification:

 

#include <iostream>

#include <glad/gl.h>

#include <GLM/glm.hpp>
#include <GLFW/glfw3.h>

#include "learnopengl/shader.h"

#include "cs4722/x11.h"

const auto  number_of_vertices = 3;

const unsigned int color_loc = 1;

const auto b_position = 1;

static const auto background_color = cs4722::x11::gray70;

auto const verts_per_triangle = 3;

glm::vec4 positions0[] = {
        glm::vec4(-.5, .8, 0, 1),
        glm::vec4(-.8, .2,0, 1),
        glm::vec4(-.2, .2, 0, 1)
};
auto size0 = 4 * 4 * verts_per_triangle;
auto offset0 = 0;
auto bOffset0 = 0;
auto color0 = cs4722::x11::medium_violet_red;


glm::vec4 positions1[] = {
        glm::vec4(.5, .8, 0, 1),
        glm::vec4(.2, .2,0, 1),
        glm::vec4(.8, .2, 0, 1)
};
auto size1 = 4 * 4 * verts_per_triangle;
auto offset1 = offset0 + verts_per_triangle;
auto bOffset1 = bOffset0 + size0;
auto color1 = cs4722::x11::slate_blue;


glm::vec4 positions2[] = {
        glm::vec4(.5, -.2, 0, 1),
        glm::vec4(.2, -.8,0, 1),
        glm::vec4(.8, -.8, 0, 1)
};
auto size2 = 4 * 4 * verts_per_triangle;
auto offset2 = offset1 + verts_per_triangle;
auto bOffset2 = bOffset1 + size1;
auto color2 = cs4722::x11::goldenrod1;

 

const auto number_of_shapes = 3;

 

void init(void)
{
    Shader shader("vertex_shader.glsl","fragment_shader.glsl");
    auto program = shader.ID;
    if(program < 0) {
        printf("Error in shader compilation");
        std::exit(-1);
    }
    shader.use();


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


    const auto number_of_buffers = 1;
    GLuint  buffers[number_of_buffers];
    glGenBuffers(number_of_buffers, buffers);

    glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
    glBufferData(GL_ARRAY_BUFFER, 4 * 4 * (number_of_shapes * verts_per_triangle),
                 nullptr,  GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, bOffset0, size0, positions0);
    glBufferSubData(GL_ARRAY_BUFFER, bOffset1, size1, positions1);
    glBufferSubData(GL_ARRAY_BUFFER, bOffset2, size2, positions2);


    glVertexAttribPointer(b_position, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
    glEnableVertexAttribArray(b_position);
}


void render(void)
{
    glUniform4fv(color_loc, 1, color0.as_float().get());
    glDrawArrays(GL_TRIANGLES, offset0, verts_per_triangle);

    glUniform4fv(color_loc, 1, color1.as_float().get());
    glDrawArrays(GL_TRIANGLES, offset1, verts_per_triangle);

    glUniform4fv(color_loc, 1, color2.as_float().get());
    glDrawArrays(GL_TRIANGLES, offset2, verts_per_triangle);

}


static void error_callback(int error, const char* description)
{
    fprintf(stderr, "Error: %s\n", description);
}

int
main(int argc, char** argv)
{

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

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);


    auto* primary = glfwGetPrimaryMonitor();
    int xpos, ypos, width, height;
    glfwGetMonitorWorkarea(primary, &xpos, &ypos, &width, &height);
    std::cout << xpos << " " << ypos << " " << width << " " << height << std::endl;
    const auto size = glm::min(width, height);
    const auto ratio = .9f;
    const auto w_width = static_cast<int>(ratio * size);
    const auto w_height = w_width;
    const auto w_x = static_cast<int>(size * (1 - ratio) / 2);
    const auto w_y = w_x;
    auto* window = glfwCreateWindow(w_width, w_height, "Three Triangles", NULL, NULL);
    glfwSetWindowPos(window, w_x, w_y);

    glfwMakeContextCurrent(window);
    gladLoadGL(glfwGetProcAddress);

    init();

    while (!glfwWindowShouldClose(window))
    {
        glClearBufferfv(GL_COLOR, 0, background_color.as_float().get());
        render();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();
}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Please modify the given code based on the directions given.

#include <iostream>

#include <glad/gl.h>

#include <GLM/glm.hpp>
#include <GLFW/glfw3.h>

#include "learnopengl/shader.h"

#include "cs4722/x11.h"

const auto  number_of_vertices = 3;

const unsigned int color_loc = 1;

const auto b_position = 1;

static const auto background_color = cs4722::x11::gray70;

auto const verts_per_triangle = 3;

glm::vec4 positions0[] = {
        glm::vec4(-.5, .8, 0, 1),
        glm::vec4(-.8, .2,0, 1),
        glm::vec4(-.2, .2, 0, 1)
};
auto size0 = 4 * 4 * verts_per_triangle;
auto offset0 = 0;
auto bOffset0 = 0;
auto color0 = cs4722::x11::medium_violet_red;


glm::vec4 positions1[] = {
        glm::vec4(.5, .8, 0, 1),
        glm::vec4(.2, .2,0, 1),
        glm::vec4(.8, .2, 0, 1)
};
auto size1 = 4 * 4 * verts_per_triangle;
auto offset1 = offset0 + verts_per_triangle;
auto bOffset1 = bOffset0 + size0;
auto color1 = cs4722::x11::slate_blue;


glm::vec4 positions2[] = {
        glm::vec4(.5, -.2, 0, 1),
        glm::vec4(.2, -.8,0, 1),
        glm::vec4(.8, -.8, 0, 1)
};
auto size2 = 4 * 4 * verts_per_triangle;
auto offset2 = offset1 + verts_per_triangle;
auto bOffset2 = bOffset1 + size1;
auto color2 = cs4722::x11::goldenrod1;

 

const auto number_of_shapes = 3;

 

void init(void)
{
    Shader shader("vertex_shader.glsl","fragment_shader.glsl");
    auto program = shader.ID;
    if(program < 0) {
        printf("Error in shader compilation");
        std::exit(-1);
    }
    shader.use();


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


    const auto number_of_buffers = 1;
    GLuint  buffers[number_of_buffers];
    glGenBuffers(number_of_buffers, buffers);

    glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
    glBufferData(GL_ARRAY_BUFFER, 4 * 4 * (number_of_shapes * verts_per_triangle),
                 nullptr,  GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, bOffset0, size0, positions0);
    glBufferSubData(GL_ARRAY_BUFFER, bOffset1, size1, positions1);
    glBufferSubData(GL_ARRAY_BUFFER, bOffset2, size2, positions2);


    glVertexAttribPointer(b_position, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
    glEnableVertexAttribArray(b_position);
}


void render(void)
{
    glUniform4fv(color_loc, 1, color0.as_float().get());
    glDrawArrays(GL_TRIANGLES, offset0, verts_per_triangle);

    glUniform4fv(color_loc, 1, color1.as_float().get());
    glDrawArrays(GL_TRIANGLES, offset1, verts_per_triangle);

    glUniform4fv(color_loc, 1, color2.as_float().get());
    glDrawArrays(GL_TRIANGLES, offset2, verts_per_triangle);

}


static void error_callback(int error, const char* description)
{
    fprintf(stderr, "Error: %s\n", description);
}

int
main(int argc, char** argv)
{

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

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);


    auto* primary = glfwGetPrimaryMonitor();
    int xpos, ypos, width, height;
    glfwGetMonitorWorkarea(primary, &xpos, &ypos, &width, &height);
    std::cout << xpos << " " << ypos << " " << width << " " << height << std::endl;
    const auto size = glm::min(width, height);
    const auto ratio = .9f;
    const auto w_width = static_cast<int>(ratio * size);
    const auto w_height = w_width;
    const auto w_x = static_cast<int>(size * (1 - ratio) / 2);
    const auto w_y = w_x;
    auto* window = glfwCreateWindow(w_width, w_height, "Three Triangles", NULL, NULL);
    glfwSetWindowPos(window, w_x, w_y);

    glfwMakeContextCurrent(window);
    gladLoadGL(glfwGetProcAddress);

    init();

    while (!glfwWindowShouldClose(window))
    {
        glClearBufferfv(GL_COLOR, 0, background_color.as_float().get());
        render();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();
}

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Form and its Elements
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education