Work on assignment 2a

This commit is contained in:
Michael Zhang 2023-04-04 02:22:46 -05:00
parent 4e550893bb
commit e8a5758103
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
13 changed files with 616 additions and 525 deletions

Binary file not shown.

View file

@ -0,0 +1,2 @@
---
BasedOnStyle: LLVM

View file

@ -1,5 +1,6 @@
# Set the minimum required version of cmake for this project # Set the minimum required version of cmake for this project
cmake_minimum_required (VERSION 3.1) cmake_minimum_required (VERSION 3.1)
set(CMAKE_CXX_STANDARD 17)
# Generate the `compile_commands.json` file. # Generate the `compile_commands.json` file.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
@ -15,6 +16,7 @@ project(HW2a)
# Define in the C++ code what the variable "SRC_DIR" should be equal to the current_path/src # Define in the C++ code what the variable "SRC_DIR" should be equal to the current_path/src
add_definitions( -DSRC_DIR="${CMAKE_CURRENT_SOURCE_DIR}/src" ) add_definitions( -DSRC_DIR="${CMAKE_CURRENT_SOURCE_DIR}/src" )
find_package(spdlog REQUIRED)
# Find OpenGL, and set link library names and include paths # Find OpenGL, and set link library names and include paths
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
@ -33,15 +35,15 @@ add_subdirectory(ext/glfw)
# Make a list of all the source files # Make a list of all the source files
set( set(
SOURCES SOURCES
src/HW2a.cpp src/main.cpp
src/controls.cpp
src/util.cpp
ext/glad/src/glad.c ext/glad/src/glad.c
) )
# Make a list of all the header files (optional-- only necessary to make them appear in IDE) # Make a list of all the header files (optional-- only necessary to make them appear in IDE)
set( set(
INCLUDES INCLUDES
src/ShaderStuff.hpp
) )
# Make a list of all of the directories to look in when doing #include "whatever.h" # Make a list of all of the directories to look in when doing #include "whatever.h"
@ -55,6 +57,7 @@ set(
set( set(
LIBS LIBS
glfw glfw
spdlog::spdlog
${OPENGL_LIBRARIES} ${OPENGL_LIBRARIES}
) )

View file

@ -1,4 +1,4 @@
{ stdenv, cmake, ninja, libglvnd, libGLU, xorg }: { stdenv, cmake, ninja, libglvnd, libGLU, xorg, spdlog }:
stdenv.mkDerivation { stdenv.mkDerivation {
name = "assignment-2a"; name = "assignment-2a";
@ -15,6 +15,7 @@ stdenv.mkDerivation {
xorg.libXinerama xorg.libXinerama
xorg.libXrandr xorg.libXrandr
xorg.libXrender xorg.libXrender
spdlog
]; ];
preBuild = '' preBuild = ''

View file

@ -0,0 +1,60 @@
#include "controls.h"
#include "GLFW/glfw3.h"
#include <spdlog/spdlog.h>
void key_callback(GLFWwindow *window, int key, int scancode, int action,
int mods) {
int state = glfwGetKey(window, GLFW_KEY_E);
// checks to see if the escape or q key was pressed
if ((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if ((mods & GLFW_MOD_CONTROL) > 0) {
if (action == GLFW_PRESS) {
ctrl_pressed = true;
spdlog::debug("Press ctrl");
} else if (action == GLFW_RELEASE) {
ctrl_pressed = false;
spdlog::debug("Release ctrl");
}
} else {
}
}
void mouse_button_callback(GLFWwindow *window, int button, int action,
int mods) {
glfwGetCursorPos(window, &mouse_x, &mouse_y);
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
}
// Pressed the left mouse button, starting drag
if (ctrl_pressed && button == GLFW_MOUSE_BUTTON_LEFT &&
action == GLFW_PRESS) {
is_dragging = true;
drag_start_x = mouse_x;
drag_start_y = mouse_y;
spdlog::debug("Start drag at ({}, {})!", drag_start_x, drag_start_y);
}
// Check which mouse button triggered the event, e.g. GLFW_MOUSE_BUTTON_LEFT,
// etc. and what the button action was, e.g. GLFW_PRESS, GLFW_RELEASE, etc.
// (Note that ordinary trackpad click = mouse left button)
// Also check if any modifier keys were active at the time of the button
// press, e.g. GLFW_MOD_ALT, etc. Take the appropriate action, which could
// (optionally) also include changing the cursor's appearance
}
void cursor_pos_callback(GLFWwindow *window, double xpos, double ypos) {
// determine the direction of the mouse or cursor motion
// update the current mouse or cursor location
mouse_x = xpos;
mouse_y = ypos;
// (necessary to quantify the amount and direction of cursor motion)
// take the appropriate action
if (is_dragging) {
}
}

View file

@ -0,0 +1,25 @@
#ifndef PERIPHERALS_H_
#define PERIPHERALS_H_
#include "GLFW/glfw3.h"
extern GLdouble mouse_x, mouse_y;
extern GLfloat M[16];
extern bool is_dragging, ctrl_pressed;
extern GLdouble drag_start_x, drag_start_y;
// function that is called whenever a keyboard event occurs; defines how
// keyboard input will be handled
void key_callback(GLFWwindow *window, int key, int scancode, int action,
int mods);
// function that is called whenever a mouse or trackpad button press event
// occurs
void mouse_button_callback(GLFWwindow *window, int button, int action,
int mods);
// function that is called whenever a cursor motion event occurs
void cursor_pos_callback(GLFWwindow *window, double xpos, double ypos);
#endif

View file

@ -3,7 +3,7 @@
#version 150 #version 150
in vec4 vcolor; in vec4 vcolor;
out vec4 color; out vec4 color;
void main()
{ void main() {
color = vcolor; // set output color to interpolated color from vshader color = vcolor; // set output color to interpolated color from vshader
} }

View file

@ -2,8 +2,11 @@
// Based on example code from: Interactive Computer Graphics: A Top-Down // Based on example code from: Interactive Computer Graphics: A Top-Down
// Approach with Shader-Based OpenGL (6th Edition), by Ed Angel // Approach with Shader-Based OpenGL (6th Edition), by Ed Angel
#include <fmt/core.h>
#include <fmt/format.h>
#include <iostream> #include <iostream>
#include <math.h> #include <math.h>
#include <spdlog/spdlog.h>
#include <sstream> #include <sstream>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -12,9 +15,10 @@
0 // repetitive of the debug flag in the shader loading code, included here 0 // repetitive of the debug flag in the shader loading code, included here
// for clarity only // for clarity only
// This file contains the code that reads the shaders from their files and #include "shaders.h"
// compiles them
#include "ShaderStuff.hpp" #include "controls.h"
#include "util.h"
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -36,53 +40,16 @@ GLint window_width = 500;
GLint window_height = 500; GLint window_height = 500;
GLdouble pi = 4.0 * atan(1.0); GLdouble pi = 4.0 * atan(1.0);
bool is_dragging = false;
bool ctrl_pressed = false;
GLdouble drag_start_x, drag_start_y;
double figure_x, figure_y, figure_z;
GLFWcursor *hand_cursor, *arrow_cursor; // some different cursors GLFWcursor *hand_cursor, *arrow_cursor; // some different cursors
GLint NVERTICES = 9; // part of the hard-coded model GLint NVERTICES = 9; // part of the hard-coded model
//----------------------------------------------------------------------------
// function that is called whenever an error occurs
static void error_callback(int error, const char *description) {
fputs(description, stderr); // write the error description to stderr
}
//----------------------------------------------------------------------------
// function that is called whenever a keyboard event occurs; defines how
// keyboard input will be handled
static void key_callback(GLFWwindow *window, int key, int scancode, int action,
int mods) {
if (key == GLFW_KEY_ESCAPE &&
action == GLFW_PRESS) // checks to see if the escape key was pressed
glfwSetWindowShouldClose(window, GL_TRUE); // closes the window
}
//----------------------------------------------------------------------------
// function that is called whenever a mouse or trackpad button press event
// occurs
static void mouse_button_callback(GLFWwindow *window, int button, int action,
int mods) {
glfwGetCursorPos(window, &mouse_x, &mouse_y);
// Check which mouse button triggered the event, e.g. GLFW_MOUSE_BUTTON_LEFT,
// etc. and what the button action was, e.g. GLFW_PRESS, GLFW_RELEASE, etc.
// (Note that ordinary trackpad click = mouse left button)
// Also check if any modifier keys were active at the time of the button
// press, e.g. GLFW_MOD_ALT, etc. Take the appropriate action, which could
// (optionally) also include changing the cursor's appearance
}
//----------------------------------------------------------------------------
// function that is called whenever a cursor motion event occurs
static void cursor_pos_callback(GLFWwindow *window, double xpos, double ypos) {
// determine the direction of the mouse or cursor motion
// update the current mouse or cursor location
// (necessary to quantify the amount and direction of cursor motion)
// take the appropriate action
}
//----------------------------------------------------------------------------
void init(void) { void init(void) {
ColorType3D colors[NVERTICES]; ColorType3D colors[NVERTICES];
FloatType2D vertices[NVERTICES]; FloatType2D vertices[NVERTICES];
@ -151,12 +118,13 @@ void init(void) {
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors); glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors);
// Define the names of the shader files // Define the names of the shader files
std::stringstream vshader, fshader; string shader_dir = env("SHADER_DIR").value_or(SRC_DIR);
vshader << SRC_DIR << "/vshader2a.glsl"; string vshader = fmt::format("{}/vshader2a.glsl", shader_dir);
fshader << SRC_DIR << "/fshader2a.glsl"; string fshader = fmt::format("{}/fshader2a.glsl", shader_dir);
spdlog::info("Shaders from: {}", shader_dir);
// Load the shaders and use the resulting shader program // Load the shaders and use the resulting shader program
program = InitShader(vshader.str().c_str(), fshader.str().c_str()); program = InitShader(vshader.c_str(), fshader.c_str());
// Determine locations of the necessary attributes and matrices used in the // Determine locations of the necessary attributes and matrices used in the
// vertex shader // vertex shader
@ -182,6 +150,7 @@ void init(void) {
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
int main(int argc, char **argv) { int main(int argc, char **argv) {
spdlog::set_level(spdlog::level::debug);
int i; int i;
GLFWwindow *window; GLFWwindow *window;
@ -219,9 +188,9 @@ int main(int argc, char **argv) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
glfwSwapInterval( // tells the system to wait for the rendered frame to finish updating
1); // tells the system to wait for the rendered frame to finish updating
// before swapping buffers; can help to avoid tearing // before swapping buffers; can help to avoid tearing
glfwSwapInterval(1);
// Define the keyboard callback function // Define the keyboard callback function
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
@ -231,6 +200,7 @@ int main(int argc, char **argv) {
glfwSetCursorPosCallback(window, cursor_pos_callback); glfwSetCursorPosCallback(window, cursor_pos_callback);
// Create the shaders and perform other one-time initializations // Create the shaders and perform other one-time initializations
spdlog::info("Initializing...");
init(); init();
// event loop // event loop
@ -247,21 +217,22 @@ int main(int argc, char **argv) {
} }
// sanity check that your matrix contents are what you expect them to be // sanity check that your matrix contents are what you expect them to be
if (DEBUG_ON) spdlog::debug(
printf("M = [%f %f %f %f\n %f %f %f %f\n %f %f %f %f\n %f %f " "M = [{} {} {} {}\n {} {} {} {}\n {} {} {} {}\n {} {} "
"%f %f]\n", "{} {}]\n",
M[0], M[4], M[8], M[12], M[1], M[5], M[9], M[13], M[2], M[6], M[0], M[4], M[8], M[12], M[1], M[5], M[9], M[13], M[2], M[6], M[10],
M[10], M[14], M[3], M[7], M[11], M[15]); M[14], M[3], M[7], M[11], M[15]);
glUniformMatrix4fv( // send the updated model transformation matrix to the GPU
m_location, 1, GL_FALSE, glUniformMatrix4fv(m_location, 1, GL_FALSE, M);
M); // send the updated model transformation matrix to the GPU
glDrawArrays( // draw a triangle between the first vertex and each
GL_TRIANGLES, 0,
NVERTICES); // draw a triangle between the first vertex and each
// successive vertex pair in the hard-coded model // successive vertex pair in the hard-coded model
glFlush(); // ensure that all OpenGL calls have executed before swapping glDrawArrays(GL_TRIANGLES, 0, NVERTICES);
// ensure that all OpenGL calls have executed before swapping
// buffers // buffers
glFlush();
glfwSwapBuffers(window); // swap buffers glfwSwapBuffers(window); // swap buffers
glfwWaitEvents(); // wait for a new event before re-drawing glfwWaitEvents(); // wait for a new event before re-drawing

View file

@ -1,8 +1,10 @@
#include <spdlog/spdlog.h>
#include <string> #include <string>
#include <stdio.h> #include <stdio.h>
#include "glad/glad.h" #include "glad/glad.h"
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
// Basic OpenGL program // Basic OpenGL program
@ -27,7 +29,7 @@ static char *readShaderSource(const char *shaderFile) {
// check for errors in opening the file // check for errors in opening the file
if (fp == NULL) { if (fp == NULL) {
printf("can't open shader source file %s\n", shaderFile); spdlog::info(":skull: can't open shader source file {}", shaderFile);
return NULL; return NULL;
} }

View file

@ -0,0 +1,13 @@
#include "util.h"
#include <spdlog/spdlog.h>
optional<string> env(string ident) {
char *result = getenv(ident.c_str());
if (!result)
return {};
return string(result);
}
void error_callback(int error, const char *description) {
spdlog::error("{}", description);
}

12
assignment-2a/src/util.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef UTIL_H_
#define UTIL_H_
#include <optional>
#include <string>
using namespace std;
optional<string> env(string ident);
void error_callback(int error, const char *description);
#endif

View file

@ -31,12 +31,13 @@
cargo-watch cargo-watch
clang-tools clang-tools
cmake cmake
dos2unix
imagemagick imagemagick
linuxPackages_latest.perf linuxPackages_latest.perf
ninja
pandoc pandoc
poppler_utils poppler_utils
texlive.combined.scheme-full texlive.combined.scheme-full
ninja
unzip unzip
zip zip
@ -62,6 +63,7 @@
xorg.libXinerama xorg.libXinerama
xorg.libXrandr xorg.libXrandr
xorg.libXrender xorg.libXrender
spdlog
]); ]);
}; };
}); });