Question: * Sorry, due to Chegg having issues, I can't comment on the answer supplied below. Line 17 - 25 is incomplete, but completed in lines
* Sorry, due to Chegg having issues, I can't comment on the answer supplied below. Line 17 - 25 is incomplete, but completed in lines 173-193, line 104 and 160 is a fragment, error stating that lastX and lastY isn't defined, just to name a few. Please make sure this code runs before replying.
DO NOT USE GLUT/FREEGLUT/GLCORE. I need help with the following: The goal of this assignment is to write commented modern OpenGL code that allows for panning, zooming, and orbiting a pyramid. Use the keyboard, mouse, and movement combinations below: QE keys: These keys should be used to control the upward and downward movement. Mouse cursor: This should be used to change the orientation of the camera so it can look up and down or right and left. Mouse scroll: This should be used to adjust the speed of the movement, or the speed the camera travels around the scene. No Glut/FreeGlut/GLCore Should include:
#include
#include
#include
#include
#include
#include
#include
// Camera position and orientation
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
// Camera movement speed
float cameraSpeed = 0.05f;
// Callback function for key presses
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_Q) {
cameraPos += cameraSpeed * cameraUp;
}
if (key == GLFW_KEY_E) {
cameraPos -= cameraSpeed * cameraUp;
}
}
// Callback function for mouse cursor movement
void cursorCallback(GLFWwindow* window, double xpos, double ypos) {
// Calculate the offset of the cursor position
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
// Adjust the camera's front vector based on the cursor offset
cameraFront.x += xoffset * sensitivity;
cameraFront.y += yoffset * sensitivity;
}
// Callback function for mouse scroll input
void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
cameraSpeed += yoffset * 0.05f;
}
int main() {
// Initialize GLFW
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return EXIT_FAILURE;
}
// Create a GLFW window
GLFWwindow* window = glfwCreateWindow(800, 600, "Pyramid", NULL, NULL);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return EXIT_FAILURE;
}
// Make the context of the window current
glfwMakeContextCurrent(window);
// Initialize GLEW
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
return EXIT_FAILURE;
}
// Set GLFW's key callback function
glfwSetKeyCallback(window, keyCallback);
// Set GLFW's cursor callback function
glfwSetCursorPosCallback(window, cursorCallback);
// Set GLFW's scroll callback function
glfwSetScrollCallback(window, scrollCallback);
// Set the initial position of the cursor
glfwSetCursorPos(window, 800 / 2, 600 / 2);
// Enable depth testing
glEnable(GL_DEPTH_TEST);
// Set up vertex data (and buffer(s)) and attribute pointers
float vertices[] = {
// Pyramid
0.0f, 1.0f, 0.0f, // Top
-1.0f, -1.0f, 1.0f, // Front left
1.0f, -1.0f, 1.0f, // Front right
1.0f, -1.0f, -1.0f, // Back right
-1.0f, -1.0f, -1.0f // Back left
};
unsigned int indices[] = {
0, 1, 2, // front
0, 2, 3, // right
0, 3, 4, // back
0, 4, 1, // left
1, 4, 3, // bottom
3, 2, 1 // bottom
};
unsigned int VBO, VAO, EBO;
glGenVer
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Render loop
while (!glfwWindowShouldClose(window)) {
// Clear the screen
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Bind the VAO
glBindVertexArray(VAO);
// Create the camera matrix
glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
// Create the model matrix
glm::mat4 model = glm::mat4(1.0f);
// Create the projection matrix
glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);
// Create the MVP matrix
glm::mat4 MVP = projection * view * model;
// Draw the pyramid
glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_INT, 0);
// Swap the buffers
glfwSwapBuffers(window);
// Poll for events
glfwPollEvents();
}
// Deallocate resources
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
// Terminate GLFW
glf
glfwTerminate();
// Exit the program
return 0;
}
// Function to handle keyboard input
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
// Handle movement
GLfloat cameraSpeed = 0.05f;
if (key == GLFW_KEY_W) {
cameraPos += cameraSpeed * cameraFront;
}
if (key == GLFW_KEY_S) {
cameraPos -= cameraSpeed * cameraFront;
}
if (key == GLFW_KEY_A) {
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}
if (key == GLFW_KEY_D) {
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}
if (key == GLFW_KEY_Q) {
cameraPos += cameraSpeed * cameraUp;
}
if (key == GLFW_KEY_E) {
cameraPos -= cameraSpeed * cameraUp;
}
}
// Function to handle mouse cursor input
void cursorCallback(GLFWwindow* window, double xpos, double ypos) {
// Calculate the horizontal and vertical offset
GLfloat xoffset = xpos - lastX;
GLfloat yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
// Set the sensitivity of the cursor
GLfloat sensitivity = 0.05f;
xoffset *= sensitivity;
yoffset *= sensitivity;
// Update the yaw and pitch
yaw += xoffset;
pitch += yoffset;
// Make sure that the pitch stays within bounds
if (pitch > 89.0f) {
pitch = 89.0f;
}
if (pitch < -89.0f) {
pitch = -89.0f;
}
// Update the camera front vector
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
// Function to handle mouse scroll input
void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
// Update the movement speed
GLfloat speed = 0.05f;
cameraSpeed += yoffset * speed;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
