Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The goal of the assignment is to add source code that maps two different textures to at least two of the shapes in the existing

The goal of the assignment is to add source code that maps two different textures to at least two of the shapes in the existing 3D scene. code wont run completely
* CreateGLTexture()
bool SceneManager::CreateGLTexture(const char* filename, std::string tag){
int width =0;
int height =0;
int colorChannels =0;
GLuint textureID =0;
// indicate to always flip images vertically when loaded
stbi_set_flip_vertically_on_load(true);
// try to parse the image data from the specified image file
unsigned char* image = stbi_load(
filename,
&width,
&height,
&colorChannels,
0);
// if the image was successfully read from the image file
if (image){
std::cout << "Successfully loaded image:" << filename <<", width:" << width <<", height:" << height <<", channels:" << colorChannels << std::endl;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// if the loaded image is in RGB format
if (colorChannels ==3)
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
// if the loaded image is in RGBA format - it supports transparency
else if (colorChannels ==4)
glTexImage2D(GL_TEXTURE_2D,0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
else{
std::cout << "Not implemented to handle image with "<< colorChannels <<" channels" << std::endl;
return false;}
// generate the texture mipmaps for mapping textures to lower resolutions
glGenerateMipmap(GL_TEXTURE_2D);
// free the image data from local memory
stbi_image_free(image);
glBindTexture(GL_TEXTURE_2D,0); // Unbind the texture
// register the loaded texture and associate it with the special tag string
m_textureIDs[m_loadedTextures].ID = textureID;
m_textureIDs[m_loadedTextures].tag = tag;
m_loadedTextures++;
return true;}
std::cout << "Could not load image:" << filename << std::endl;
// Error loading the image
return false;}
* BindGLTextures()
void SceneManager::BindGLTextures(){
for (int i =0; i < m_loadedTextures; i++){
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE0+ i);
glBindTexture(GL_TEXTURE_2D, m_textureIDs[i].ID); }}
* DestroyGLTextures()
void SceneManager::DestroyGLTextures(){
for (int i =0; i < m_loadedTextures; i++){
glGenTextures(1, &m_textureIDs[i].ID);}}
* FindTextureID()
int SceneManager::FindTextureID(std::string tag){
int textureID =-1;
int index =0;
bool bFound = false;
while ((index < m_loadedTextures) && (bFound == false)){
if (m_textureIDs[index].tag.compare(tag)==0){
textureID = m_textureIDs[index].ID;
bFound = true;}
else
index++;}
return(textureID);}
* FindTextureSlot()
int SceneManager::FindTextureSlot(std::string tag){
int textureSlot =-1;
int index =0;
bool bFound = false;
while ((index < m_loadedTextures) && (bFound == false)){
if (m_textureIDs[index].tag.compare(tag)==0){
textureSlot = index;
bFound = true;}
else
index++;}
return(textureSlot);}
* SetTransformations()
void SceneManager::SetTransformations(
glm::vec3 scaleXYZ,
float XrotationDegrees,
float YrotationDegrees,
float ZrotationDegrees,
glm::vec3 positionXYZ){
// variables for this method
glm::mat4 modelView;
glm::mat4 scale;
glm::mat4 rotationX;
glm::mat4 rotationY;
glm::mat4 rotationZ;
glm::mat4 translation;
// set the scale value in the transform buffer
scale = glm::scale(scaleXYZ);
// set the rotation values in the transform buffer
rotationX = glm::rotate(glm::radians(XrotationDegrees), glm::vec3(1.0f,0.0f,0.0f));
rotationY = glm::rotate(glm::radians(YrotationDegrees), glm::vec3(0.0f,1.0f,0.0f));
rotationZ = glm::rotate(glm::radians(ZrotationDegrees), glm::vec3(0.0f,0.0f,1.0f));
// set the translation value in the transform buffer
translation = glm::translate(positionXYZ);
modelView = translation * rotationZ * rotationY * rotationX * scale;
if (NULL != m_pShaderManager){
m_pShaderManager->setMat4Value(g_ModelName, modelView);}}
* SetShaderColor()
void SceneManager::SetShaderColor(
float redColorValue,
float greenColorValue,
float blueColorValue,
float alphaValue)
// variables for this method
glm::vec4 currentColor;
currentColor.r = redColorValue;
currentColor.g = greenColorValue;
currentColor.b = blueColorValue;
currentColor.a = alphaValue;
if (NULL != m_pShaderManager){
m_pShaderManager->setIntValue(g_UseTextureName, false);
m_pShaderManager->setVec4Value(g_ColorValueName, currentColor);}}
* SetShaderTexture()
void SceneManager::SetShaderTexture(
std::string textu

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions

Question

Find the derivative. Simplify where possible. f(x) = sinh -1 (2x)

Answered: 1 week ago