a
This commit is contained in:
parent
d1eab0d16f
commit
0212326d0a
10 changed files with 120 additions and 361 deletions
|
@ -1,2 +1,4 @@
|
||||||
---
|
---
|
||||||
BasedOnStyle: LLVM
|
BasedOnStyle: LLVM
|
||||||
|
|
||||||
|
AlignAfterOpenBracket: BlockIndent
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
// Unfortunately this must be included first because it does some annoying check
|
// Unfortunately this must be included first because it does some annoying check
|
||||||
// that vomits if GLFW has already been included (dumb)
|
// that vomits if GLFW has already been included (dumb)
|
||||||
|
@ -19,25 +20,41 @@
|
||||||
|
|
||||||
#include "GLFW/glfw3.h"
|
#include "GLFW/glfw3.h"
|
||||||
|
|
||||||
#define DEBUG_ON \
|
// 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
|
#define DEBUG_ON 1
|
||||||
|
|
||||||
// This file contains the code that reads the shaders from their files and
|
// This file contains the code that reads the shaders from their files and
|
||||||
// compiles them
|
// compiles them
|
||||||
#include "ShaderStuff.hpp"
|
#include "ShaderStuff.hpp"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
// Forward-declaring some functions for later implementation
|
// Forward-declaring some functions for later implementation
|
||||||
|
|
||||||
|
/// Print the matrix
|
||||||
|
void printMatrix(string name, GLfloat *matrix);
|
||||||
|
|
||||||
|
/// Transpose a matrix
|
||||||
|
void transposeMatrix(GLfloat *matrix);
|
||||||
|
|
||||||
|
/// Zero-initialize a GLfloat array
|
||||||
|
void zeroInitGlfloats(GLfloat *arr, uint32_t len);
|
||||||
|
|
||||||
/// Multiplies two matrices and puts them into result.
|
/// Multiplies two matrices and puts them into result.
|
||||||
///
|
///
|
||||||
/// - Left matrix must be ROW-order
|
/// - Left matrix must be ROW-order
|
||||||
/// - Right matrix must be COLUMN-order
|
/// - Right matrix must be COLUMN-order
|
||||||
///
|
///
|
||||||
/// Result will be row-order
|
/// Result will be column-order
|
||||||
void slow4x4MatrixMultiplyIntoRowOrder(GLfloat *left, GLfloat *right,
|
void slow4x4MatrixMultiplyIntoColumnOrder(
|
||||||
GLfloat *result);
|
GLfloat *left, GLfloat *right, GLfloat *result
|
||||||
|
);
|
||||||
|
|
||||||
|
void slow4x4MatrixMultiplyAllRowOrder(
|
||||||
|
GLfloat *left, GLfloat *right, GLfloat *result
|
||||||
|
);
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -50,7 +67,8 @@ typedef struct {
|
||||||
GLfloat r, g, b;
|
GLfloat r, g, b;
|
||||||
} ColorType3D;
|
} ColorType3D;
|
||||||
|
|
||||||
GLfloat M[16]; // general transformation matrix
|
// general transformation matrix
|
||||||
|
GLfloat M[16];
|
||||||
|
|
||||||
// define some assorted global variables, to make life easier
|
// define some assorted global variables, to make life easier
|
||||||
GLint m_location;
|
GLint m_location;
|
||||||
|
@ -60,8 +78,11 @@ GLint window_height = 500;
|
||||||
GLdouble pi = 4.0 * atan(1.0);
|
GLdouble pi = 4.0 * atan(1.0);
|
||||||
|
|
||||||
// Added state
|
// Added state
|
||||||
GLdouble scale_x = 1.0, scale_y = 1.0;
|
|
||||||
const GLdouble SCALE_DELTA = 0.05;
|
const GLdouble SCALE_DELTA = 0.05;
|
||||||
|
GLdouble scale_x = 1.0, scale_y = 1.0;
|
||||||
|
|
||||||
|
// Matrices
|
||||||
|
GLfloat rotation_matrix[16], translation_matrix[16], scale_matrix[16];
|
||||||
|
|
||||||
GLFWcursor *hand_cursor, *arrow_cursor; // some different cursors
|
GLFWcursor *hand_cursor, *arrow_cursor; // some different cursors
|
||||||
|
|
||||||
|
@ -76,8 +97,8 @@ static void error_callback(int error, const char *description) {
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
// function that is called whenever a keyboard event occurs; defines how
|
// function that is called whenever a keyboard event occurs; defines how
|
||||||
// keyboard input will be handled
|
// keyboard input will be handled
|
||||||
static void key_callback(GLFWwindow *window, int key, int scancode, int action,
|
static void
|
||||||
int mods) {
|
key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
|
||||||
// checks to see if the escape key was pressed
|
// checks to see if the escape key was pressed
|
||||||
if ((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action == GLFW_PRESS)
|
if ((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action == GLFW_PRESS)
|
||||||
// closes the window
|
// closes the window
|
||||||
|
@ -102,8 +123,8 @@ static void key_callback(GLFWwindow *window, int key, int scancode, int action,
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
// function that is called whenever a mouse or trackpad button press event
|
// function that is called whenever a mouse or trackpad button press event
|
||||||
// occurs
|
// occurs
|
||||||
static void mouse_button_callback(GLFWwindow *window, int button, int action,
|
static void
|
||||||
int mods) {
|
mouse_button_callback(GLFWwindow *window, int button, int action, int mods) {
|
||||||
glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
||||||
|
|
||||||
// Check which mouse button triggered the event, e.g. GLFW_MOUSE_BUTTON_LEFT,
|
// Check which mouse button triggered the event, e.g. GLFW_MOUSE_BUTTON_LEFT,
|
||||||
|
@ -131,6 +152,10 @@ void init(void) {
|
||||||
FloatType2D vertices[NVERTICES];
|
FloatType2D vertices[NVERTICES];
|
||||||
GLuint vao[1], buffer, program, location1, location2;
|
GLuint vao[1], buffer, program, location1, location2;
|
||||||
|
|
||||||
|
zeroInitGlfloats(rotation_matrix, 16);
|
||||||
|
zeroInitGlfloats(translation_matrix, 16);
|
||||||
|
zeroInitGlfloats(scale_matrix, 16);
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
// set up some hard-coded colors and geometry
|
// set up some hard-coded colors and geometry
|
||||||
// this part can be customized to read in an object description from a file
|
// this part can be customized to read in an object description from a file
|
||||||
|
@ -163,8 +188,10 @@ void init(void) {
|
||||||
// position and color data
|
// position and color data
|
||||||
glGenBuffers(1, &buffer);
|
glGenBuffers(1, &buffer);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), vertices,
|
glBufferData(
|
||||||
GL_STATIC_DRAW);
|
GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), vertices,
|
||||||
|
GL_STATIC_DRAW
|
||||||
|
);
|
||||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
|
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
|
||||||
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors);
|
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors);
|
||||||
|
|
||||||
|
@ -183,8 +210,9 @@ void init(void) {
|
||||||
glVertexAttribPointer(location1, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
|
glVertexAttribPointer(location1, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
|
||||||
location2 = glGetAttribLocation(program, "vertex_color");
|
location2 = glGetAttribLocation(program, "vertex_color");
|
||||||
glEnableVertexAttribArray(location2);
|
glEnableVertexAttribArray(location2);
|
||||||
glVertexAttribPointer(location2, 3, GL_FLOAT, GL_FALSE, 0,
|
glVertexAttribPointer(
|
||||||
BUFFER_OFFSET(sizeof(vertices)));
|
location2, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(vertices))
|
||||||
|
);
|
||||||
m_location = glGetUniformLocation(program, "M");
|
m_location = glGetUniformLocation(program, "M");
|
||||||
|
|
||||||
// Define static OpenGL state variables
|
// Define static OpenGL state variables
|
||||||
|
@ -200,7 +228,6 @@ void init(void) {
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
GLFWwindow *window;
|
GLFWwindow *window;
|
||||||
|
|
||||||
|
@ -237,9 +264,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 before
|
||||||
1); // tells the system to wait for the rendered frame to finish updating
|
// 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);
|
||||||
|
@ -257,33 +284,40 @@ int main(int argc, char **argv) {
|
||||||
// fill/re-fill the window with the background color
|
// fill/re-fill the window with the background color
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
rotation_matrix[0] = 1.0;
|
||||||
|
rotation_matrix[5] = 1.0;
|
||||||
|
rotation_matrix[10] = 1.0;
|
||||||
|
rotation_matrix[15] = 1.0;
|
||||||
|
|
||||||
|
scale_matrix[0] = scale_x;
|
||||||
|
scale_matrix[5] = scale_y;
|
||||||
|
scale_matrix[10] = 1.0;
|
||||||
|
scale_matrix[15] = 1.0;
|
||||||
|
|
||||||
// define/re-define the modelview matrix. In this template, we define M to
|
// define/re-define the modelview matrix. In this template, we define M to
|
||||||
// be the identity matrix; you will need define M according to the user's
|
// be the identity matrix; you will need define M according to the user's
|
||||||
// actions.
|
// actions.
|
||||||
/* for (i = 0; i < 16; i++) {
|
|
||||||
M[i] = (i % 5 == 0);
|
slow4x4MatrixMultiplyIntoColumnOrder(rotation_matrix, scale_matrix, M);
|
||||||
}*/
|
|
||||||
M[0] = scale_x;
|
|
||||||
M[5] = scale_y;
|
|
||||||
M[10] = 1.0;
|
|
||||||
M[15] = 1.0;
|
|
||||||
|
|
||||||
// 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)
|
if (DEBUG_ON) {
|
||||||
printf("M = [%f %f %f %f\n %f %f %f %f\n %f %f %f %f\n %f %f "
|
printf("--------------");
|
||||||
"%f %f]\n",
|
printf("scale_x = %f , scale_y = %f\n", scale_x, scale_y);
|
||||||
M[0], M[4], M[8], M[12], M[1], M[5], M[9], M[13], M[2], M[6],
|
printMatrix("rotation_matrix", rotation_matrix);
|
||||||
M[10], M[14], M[3], M[7], M[11], M[15]);
|
printMatrix("scale_matrix", scale_matrix);
|
||||||
|
printMatrix("M", M);
|
||||||
|
}
|
||||||
|
|
||||||
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 successive vertex pair
|
||||||
GL_TRIANGLES, 0,
|
// in the hard-coded model
|
||||||
NVERTICES); // draw a triangle between the first vertex and each
|
glDrawArrays(GL_TRIANGLES, 0, NVERTICES);
|
||||||
// successive vertex pair in the hard-coded model
|
|
||||||
glFlush(); // ensure that all OpenGL calls have executed before swapping
|
// 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
|
||||||
|
@ -292,21 +326,57 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
glfwDestroyWindow(window);
|
glfwDestroyWindow(window);
|
||||||
glfwTerminate(); // destroys any remaining objects, frees resources allocated
|
|
||||||
// by GLFW
|
// destroys any remaining objects, frees resources allocated by GLFW
|
||||||
|
glfwTerminate();
|
||||||
|
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
} // end main
|
} // end main
|
||||||
|
|
||||||
void slow4x4MatrixMultiplyIntoRowOrder(GLfloat *left, GLfloat *right,
|
void printMatrix(string name, GLfloat *matrix) {
|
||||||
GLfloat *result) {
|
printf(
|
||||||
|
"%s = [%f %f %f %f\n"
|
||||||
|
" %f %f %f %f\n"
|
||||||
|
" %f %f %f %f\n"
|
||||||
|
" %f %f %f %f]\n",
|
||||||
|
name.c_str(), matrix[0], matrix[4], matrix[8], matrix[12], matrix[1],
|
||||||
|
matrix[5], matrix[9], matrix[13], matrix[2], matrix[6], matrix[10],
|
||||||
|
matrix[14], matrix[3], matrix[7], matrix[11], matrix[15]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void transposeMatrix(GLfloat *matrix) {}
|
||||||
|
|
||||||
|
void zeroInitGlfloats(GLfloat *arr, uint32_t len) {
|
||||||
|
for (auto i = 0; i < len; ++i) {
|
||||||
|
arr[i] = 0.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void slow4x4MatrixMultiplyIntoColumnOrder(
|
||||||
|
GLfloat *left, GLfloat *right, GLfloat *result
|
||||||
|
) {
|
||||||
for (int i = 0; i < 4; ++i) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
for (int j = 0; j < 4; ++j) {
|
for (int j = 0; j < 4; ++j) {
|
||||||
for (int k = 0; k < 4; ++k) {
|
for (int k = 0; k < 4; ++k) {
|
||||||
const int resultIdx = i * 4 + j;
|
|
||||||
const GLfloat leftVal = left[i * 4 + k];
|
const GLfloat leftVal = left[i * 4 + k];
|
||||||
const GLfloat rightVal = right[j * 4 + k];
|
const GLfloat rightVal = right[j * 4 + k];
|
||||||
result[i * 4 + j] += leftVal * rightVal;
|
result[j * 4 + i] += leftVal * rightVal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void slow4x4MatrixMultiplyAllRowOrder(
|
||||||
|
GLfloat *left, GLfloat *right, GLfloat *result
|
||||||
|
) {
|
||||||
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
for (int j = 0; j < 4; ++j) {
|
||||||
|
for (int k = 0; k < 4; ++k) {
|
||||||
|
const GLfloat leftVal = left[i * 4 + k];
|
||||||
|
const GLfloat rightVal = right[j * 4 + k];
|
||||||
|
result[j * 4 + i] += leftVal * rightVal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#ifndef SHADERSTUFF
|
#ifndef SHADERSTUFF
|
||||||
#define SHADERSTUFF 1
|
#define SHADERSTUFF 1
|
||||||
|
|
||||||
#define DEBUG_ON 0
|
// #define DEBUG_ON 0
|
||||||
#define BUFFER_OFFSET(bytes) ((GLvoid*) (bytes))
|
#define BUFFER_OFFSET(bytes) ((GLvoid*) (bytes))
|
||||||
|
|
||||||
// Create a NULL-terminated string by reading the provided file
|
// Create a NULL-terminated string by reading the provided file
|
||||||
|
|
|
@ -1,60 +0,0 @@
|
||||||
#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) {
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,25 +0,0 @@
|
||||||
#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
|
|
|
@ -1,149 +0,0 @@
|
||||||
#include <spdlog/spdlog.h>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include "glad/glad.h"
|
|
||||||
|
|
||||||
#include "GLFW/glfw3.h"
|
|
||||||
|
|
||||||
// Basic OpenGL program
|
|
||||||
// Based on example code from: Interactive Computer Graphics: A Top-Down
|
|
||||||
// Approach with Shader-Based OpenGL (6th Edition), by Ed Angel
|
|
||||||
|
|
||||||
#ifndef SHADERSTUFF
|
|
||||||
#define SHADERSTUFF 1
|
|
||||||
|
|
||||||
#define DEBUG_ON 0
|
|
||||||
#define BUFFER_OFFSET(bytes) ((GLvoid *)(bytes))
|
|
||||||
|
|
||||||
// Create a NULL-terminated string by reading the provided file
|
|
||||||
static char *readShaderSource(const char *shaderFile) {
|
|
||||||
FILE *fp;
|
|
||||||
long length, count;
|
|
||||||
char *buffer;
|
|
||||||
// struct stat fileinfo;
|
|
||||||
|
|
||||||
// open the file containing the text of the shader code
|
|
||||||
fp = fopen(shaderFile, "rb");
|
|
||||||
|
|
||||||
// check for errors in opening the file
|
|
||||||
if (fp == NULL) {
|
|
||||||
spdlog::info(":skull: can't open shader source file {}", shaderFile);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// determine the file size
|
|
||||||
fseek(fp, 0, SEEK_END); // move position indicator to the end of the file;
|
|
||||||
length = ftell(fp); // return the value of the current position
|
|
||||||
if (DEBUG_ON)
|
|
||||||
fprintf(stdout, "length in bytes of shader file: %ld\n", length);
|
|
||||||
|
|
||||||
// allocate a buffer with the indicated number of bytes, plus one
|
|
||||||
buffer = new char[length + 1];
|
|
||||||
|
|
||||||
// read the appropriate number of bytes from the file
|
|
||||||
fseek(fp, 0, SEEK_SET); // move position indicator to the start of the file
|
|
||||||
count = fread(buffer, 1, length, fp); // read all of the bytes
|
|
||||||
if (DEBUG_ON)
|
|
||||||
fprintf(stdout, "count of bytes successfully read: %ld\n", count);
|
|
||||||
|
|
||||||
// append a NULL character to indicate the end of the string
|
|
||||||
buffer[count] = '\0'; // because on some systems, count != length
|
|
||||||
|
|
||||||
// close the file
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
// return the string
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a GLSL program object from vertex and fragment shader files
|
|
||||||
GLuint InitShader(const char *vShaderFileName, const char *fShaderFileName) {
|
|
||||||
GLuint vertex_shader, fragment_shader;
|
|
||||||
GLuint program;
|
|
||||||
|
|
||||||
// check GLSL version
|
|
||||||
if (DEBUG_ON)
|
|
||||||
printf("GLSL version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
|
|
||||||
|
|
||||||
// Create shader handlers
|
|
||||||
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
|
||||||
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
|
|
||||||
|
|
||||||
// Read source code from file
|
|
||||||
std::string vs_text = readShaderSource(vShaderFileName);
|
|
||||||
std::string fs_text = readShaderSource(fShaderFileName);
|
|
||||||
|
|
||||||
// error check
|
|
||||||
if (vs_text == "") {
|
|
||||||
printf("Failed to read from vertex shader file %s\n", vShaderFileName);
|
|
||||||
exit(1);
|
|
||||||
} else if (DEBUG_ON) {
|
|
||||||
printf("read shader code:\n%s\n", vs_text.c_str());
|
|
||||||
}
|
|
||||||
if (fs_text == "") {
|
|
||||||
printf("Failed to read from fragment shader file %s\n", fShaderFileName);
|
|
||||||
exit(1);
|
|
||||||
} else if (DEBUG_ON) {
|
|
||||||
printf("read shader code:\n%s\n", fs_text.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set shader source
|
|
||||||
const char *vv = vs_text.c_str();
|
|
||||||
const char *ff = fs_text.c_str();
|
|
||||||
glShaderSource(vertex_shader, 1, &vv, NULL);
|
|
||||||
glShaderSource(fragment_shader, 1, &ff, NULL);
|
|
||||||
|
|
||||||
// Compile shaders
|
|
||||||
glCompileShader(vertex_shader);
|
|
||||||
glCompileShader(fragment_shader);
|
|
||||||
|
|
||||||
// Check for errors in compiling shaders
|
|
||||||
GLint compiled;
|
|
||||||
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &compiled);
|
|
||||||
if (!compiled) {
|
|
||||||
printf("vertex_shader failed to compile\n");
|
|
||||||
if (DEBUG_ON) {
|
|
||||||
GLint logMaxSize, logLength;
|
|
||||||
glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, &logMaxSize);
|
|
||||||
printf("printing error message of %d bytes\n", logMaxSize);
|
|
||||||
char *logMsg = new char[logMaxSize];
|
|
||||||
glGetShaderInfoLog(vertex_shader, logMaxSize, &logLength, logMsg);
|
|
||||||
printf("%d bytes retrieved\n", logLength);
|
|
||||||
printf("error message: %s\n", logMsg);
|
|
||||||
delete[] logMsg;
|
|
||||||
}
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &compiled);
|
|
||||||
if (!compiled) {
|
|
||||||
printf("fragment_shader failed to compile\n");
|
|
||||||
if (DEBUG_ON) {
|
|
||||||
GLint logMaxSize, logLength;
|
|
||||||
glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &logMaxSize);
|
|
||||||
printf("printing error message of %d bytes\n", logMaxSize);
|
|
||||||
char *logMsg = new char[logMaxSize];
|
|
||||||
glGetShaderInfoLog(fragment_shader, logMaxSize, &logLength, logMsg);
|
|
||||||
printf("%d bytes retrieved\n", logLength);
|
|
||||||
printf("error message: %s\n", logMsg);
|
|
||||||
delete[] logMsg;
|
|
||||||
}
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the program
|
|
||||||
program = glCreateProgram();
|
|
||||||
|
|
||||||
// Attach shaders to program
|
|
||||||
glAttachShader(program, vertex_shader);
|
|
||||||
glAttachShader(program, fragment_shader);
|
|
||||||
|
|
||||||
// Link and set program to use
|
|
||||||
glLinkProgram(program);
|
|
||||||
glUseProgram(program);
|
|
||||||
|
|
||||||
return program;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,21 +0,0 @@
|
||||||
#include "state.h"
|
|
||||||
|
|
||||||
void State::populate_matrix(GLfloat *matrix) {
|
|
||||||
// 0 1 2 3
|
|
||||||
// 4 5 6 7
|
|
||||||
// 8 9 10 11
|
|
||||||
// 12 13 14 15
|
|
||||||
|
|
||||||
matrix[0] = this->x_scale;
|
|
||||||
matrix[5] = this->y_scale;
|
|
||||||
matrix[10] = 1.0;
|
|
||||||
matrix[15] = 1.0;
|
|
||||||
|
|
||||||
/*
|
|
||||||
for (int i = 0; i < 16; i++) {
|
|
||||||
matrix[i] = (i % 5 == 0);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
void State::start_dragging(pii at) { this->drag_start = at; }
|
|
|
@ -1,33 +0,0 @@
|
||||||
#ifndef STATE_H_
|
|
||||||
#define STATE_H_
|
|
||||||
|
|
||||||
#include <optional>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
#include "GLFW/glfw3.h"
|
|
||||||
|
|
||||||
typedef pair<int, int> pii;
|
|
||||||
|
|
||||||
const double X_DELTA = 0.1, Y_DELTA = 0.1;
|
|
||||||
|
|
||||||
/// The state of the program
|
|
||||||
struct State {
|
|
||||||
pii mouse;
|
|
||||||
|
|
||||||
/// Where the dragging started (if it started)
|
|
||||||
optional<pii> drag_start;
|
|
||||||
|
|
||||||
/// Scaling factors
|
|
||||||
double x_scale = 1.0;
|
|
||||||
double y_scale = 1.0;
|
|
||||||
|
|
||||||
void populate_matrix(GLfloat *matrix);
|
|
||||||
void start_dragging(pii at);
|
|
||||||
|
|
||||||
private:
|
|
||||||
GLfloat M[16];
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,13 +0,0 @@
|
||||||
#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);
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
#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
|
|
Loading…
Reference in a new issue