Question: Each of the surface objects is assigned a diffuse color. The first part of the assignment is to mix the diffuse color in with the

Each of the surface objects is assigned a diffuse color. The first part of the assignment is to mix the diffuse color in with the shape color. See the example 11-use-material in the Module 3 example stream to see how that is done.
Here is a guide:
Define a uniform variable in the fragment shader
Give the variable an initial value vec4(1,1,1,1) so that 0-base still works properly.Mix the shape color and the diffuse color to get the fragment color
Add code to the driver to send the diffuse color value for each artifact to the fragment shader
Source code give:
#include "GLM/gtc/type_ptr.hpp"
#include
#include
#include "learnopengl/shader_4722.h"
#include "cs4722/artifact.h"
#include "cs4722/view.h"
#include "cs4722/window.h"
#include "cs4722/buffer_utilities.h"
static cs4722::View *the_view;
static Shader *shader;
static std::vector artifact_list;
static auto asteroid_center = glm::vec3(0,0,0);
static float asteroid_radius =20.0;
static float pc_height =0.1;
static unsigned int random_seed =2718281828;
void init()
{
the_view = new cs4722::View();
shader = new Shader("vertex_shader.glsl","fragment_shader.glsl");
shader->use();
glEnable(GL_DEPTH_TEST);
// position the camera
the_view->cameraPosition =(asteroid_radius + pc_height)* glm::vec3(1,0,0)+ asteroid_center;
the_view->cameraUp = glm::vec3(1,0,0);
the_view->cameraForward = glm::vec3(0,1,0);
the_view->cameraLeft = glm::cross(the_view->cameraUp, the_view->cameraForward);
cs4722::Shape* b = new cs4722::Sphere(100,200);
b->colorSet = std::vector({cs4722::Color::colorRGB(210,255,210),cs4722::Color::colorRGB(230,255,230)});
auto* transformation = new cs4722::Transformation();
transformation->multiplyScale(asteroid_radius, asteroid_radius, asteroid_radius);
transformation->multiplyTranslation(asteroid_center);
// transformation->rotationAxis = glm::vec3(1,1,0);
// transformation->rotationCenter = asteroid_center;
// transformation->rotationRate =0;
auto* artf = new cs4722::Artifact(transformation);
artf->theShape = b;
artf->surfaceMaterial.diffuseColor = cs4722::X11::white;
artifact_list.push_back(artf);
std::srand(random_seed);
cs4722::Shape* shapes[]={
new cs4722::Block(),
new cs4722::Cylinder(.3,20),
new cs4722::Cylinder(1.2,20),
};
int num_shapes =3;
shapes[0]->colorSet = std::vector({cs4722::X11::gray60, cs4722::X11::gray100});
shapes[1]->colorSet = std::vector({cs4722::X11::gray60, cs4722::X11::gray100});
shapes[2]->colorSet = std::vector({cs4722::X11::gray60, cs4722::X11::gray100});
for(int i =0; i <5000; i++){
float height = glm::linearRand(.01,.2);
auto position = glm::sphericalRand(asteroid_radius + height/2)+ asteroid_center;
// printf("pos (%f,%f,%f) distance %f
", position.x, position.y, position.z,
// glm::length(position - asteroid_center));
auto cToP = position - asteroid_center;
transformation = new cs4722::Transformation();
transformation->multiplyScale(height /4, height, height /4);
transformation->multiplyRotation(glm::acos(position.y / glm::length(position)),
glm::vec3(cToP.z,0,-cToP.x));
transformation->multiplyTranslation(position);
artf = new cs4722::Artifact(transformation);
artf->theShape = shapes[i % num_shapes];
unsigned int r =(int)glm::linearRand(0,256);
unsigned int g =(int)glm::linearRand(0,256);
unsigned int b =(int)glm::linearRand(0,256);
artf->surfaceMaterial.diffuseColor = cs4722::Color::colorRGB(r,g,b);
artifact_list.push_back(artf);
}
cs4722::initBuffers(shader->ID, artifact_list, "b_position", "b_color");
}
void render()
{
auto view_transform = glm::lookAt(the_view->cameraPosition,
the_view->cameraPosition + the_view->cameraForward,
the_view->cameraUp);
auto projection_transform = glm::infinitePerspective(the_view->perspectiveFOVY,
the_view->perspectiveAspect, the_view->perspectiveNear);
auto vp_transform = projection_transform * view_transform;
for (auto artf : artifact_list){
auto model_transform = artf->worldTransformation->getMatrix();
auto transform = vp_transform * model_transform;
shader->setMat4("u_transform", transform);
glDrawArrays(GL_TRIANGLES, artf->theShape->bufferStart, artf->theShape->bufferSize);
}
}
float movement_speed =.005f;
float lr_scale = movement_speed;
float fb_scale = movement_speed;
float ud_scale = movement_speed;
float lr_pan_scale =.01f;
float ud_pan_scale = lr_pan_scale;
void general_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
static int old_width, old_height, old_xpos, old_ypos;
auto* view = static_cast(glfwGetWindowUserPointer(window));
if (action == GLFW_PRESS || action == GLFW_REPEAT){
switch (key){
case GLFW_KEY_A:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!