Question
I need to mapping a cylinder with a 2D image by add texture coordinates and normals to the function that creates a cylinder. My question
I need to mapping a cylinder with a 2D image by add texture coordinates and normals to the function that creates a cylinder.
My question is how to modify the shader or cylinder function to do the mapping?
Vertex shader:
// Vertex buffer in vec3 vertex; in vec3 normal; in vec3 color; in vec2 uv;
// Uniform (global) buffer uniform mat4 world_mat; uniform mat4 view_mat; uniform mat4 projection_mat; uniform mat4 normal_mat;
// Attributes forwarded to the fragment shader out vec3 position_interp; out vec3 normal_interp; out vec4 color_interp; out vec2 uv_interp; out vec3 light_pos;
// Material attributes (constants) uniform vec3 light_position = vec3(-0.5, -0.5, 1.5);
void main() { gl_Position = projection_mat * view_mat * world_mat * vec4(vertex, 1.0);
position_interp = vec3(view_mat * world_mat * vec4(vertex, 1.0)); normal_interp = vec3(normal_mat * vec4(normal, 0.0));
color_interp = vec4(color, 1.0);
uv_interp = uv;
light_pos = vec3(view_mat * vec4(light_position, 1.0)); }
Fragment shader:
// Attributes passed from the vertex shader in vec3 position_interp; in vec3 normal_interp; in vec4 color_interp; in vec2 uv_interp; in vec3 light_pos;
// Uniform (global) buffer uniform sampler2D texture_map;
void main() { // Retrieve texture value vec4 pixel = texture(texture_map, uv_interp);
// Use texture in determining fragment colour gl_FragColor = pixel; }
Cylinder function:
void ResourceManager::CreateCylinder(std::string object_name, float circle_radius, int num_point_on_circle, int height)
{
// Number of vertices and faces to be created
const GLuint vertex_num = num_point_on_circle * 2;
const GLuint face_num = num_point_on_circle * 4;
// Number of attributes for vertices and faces
const int vertex_att = 11; // 11 attributes per vertex: 3D position (3), 3D normal (3), RGB color (3)
const int face_att = 3; // Vertex indices (3)
// Data buffers for the torus
GLfloat *vertex = NULL;
GLuint *face = NULL;
// Allocate memory for buffers
try {
vertex = new GLfloat[vertex_num * vertex_att];
face = new GLuint[face_num * face_att];
}
catch (std::exception &e) {
throw e;
}
// Create vertices
float theta; // Angles for circles
glm::vec3 vertex_position;
glm::vec3 vertex_normal;
glm::vec3 vertex_color;
glm::vec3 loop_center;
for (int i = 0; i
loop_center = glm::vec3(0, i, 0);//center of bottom and top
for (int j = 0; j
theta = 2.0*glm::pi
// Define position, normal and color of vertex
vertex_normal = glm::vec3(cos(theta), 0, sin(theta));
vertex_position = loop_center + vertex_normal * circle_radius;
vertex_color = glm::vec3(1 - (j % 10)*0.1, (j % 10)*0.1, (j % 10)*0.1);
// Add vectors to the data buffer
for (int k = 0; k
vertex[(i*num_point_on_circle + j)*vertex_att + k] = vertex_position[k];
vertex[(i*num_point_on_circle + j)*vertex_att + k + 3] = vertex_normal[k];
vertex[(i*num_point_on_circle + j)*vertex_att + k + 6] = vertex_color[k];
}
}
// Create triangles
for (int i = 0; i
for (int j = 0; j
// Two triangles per quad
glm::vec3 t1(
((i + 1) % height)*num_point_on_circle + j,
i*num_point_on_circle + ((j + 1) % num_point_on_circle),
i*num_point_on_circle + j);
glm::vec3 t2(
((i + 1) % height)*num_point_on_circle + j,
((i + 1) % height)*num_point_on_circle + ((j + 1) % num_point_on_circle),
i*num_point_on_circle + ((j + 1) % num_point_on_circle));
//glm::vec3 t3((()))
// Add two triangles to the data buffer
for (int k = 0; k
face[(i*num_point_on_circle + j)*face_att * 2 + k] = (GLuint)t1[k];
face[(i*num_point_on_circle + j)*face_att * 2 + k + face_att] = (GLuint)t2[k];
}
}
}
}
GLuint vbo, ebo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertex_num * vertex_att * sizeof(GLfloat), vertex, GL_STATIC_DRAW);
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, face_num * face_att * sizeof(GLuint), face, GL_STATIC_DRAW);
// Free data buffers
delete[] vertex;
delete[] face;
// Create resource
AddResource(Mesh, object_name, vbo, ebo, face_num * face_att);
}
void ResourceManager::LoadTexture(const std::string name, const char *filename){
// Load texture from file
GLuint texture = SOIL_load_OGL_texture(filename, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, 0);
if (!texture){
throw(std::ios_base::failure(std::string("Error loading texture ")+std::string(filename)+std::string(": ")+std::string(SOIL_last_result())));
}
// Create resource
AddResource(Texture, name, texture, 0);
}
Texture Mapping Simplest object: cylinder (sides only) v coordinais heighi (normalized ditto) ."Label on a CHIK'N NOODLE SOUP Nutrition Facts can of soup" Texture Mapping Simplest object: cylinder (sides only) v coordinais heighi (normalized ditto) ."Label on a CHIK'N NOODLE SOUP Nutrition Facts can of soupStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started