get the build process working
This commit is contained in:
parent
66ccaecd8f
commit
4e550893bb
5 changed files with 356 additions and 296 deletions
1
assignment-2a/.gitignore
vendored
1
assignment-2a/.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
/build
|
/build
|
||||||
/result*
|
/result*
|
||||||
|
.cache
|
||||||
|
|
|
@ -1,6 +1,14 @@
|
||||||
# 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)
|
||||||
|
|
||||||
|
# Generate the `compile_commands.json` file.
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
|
||||||
|
|
||||||
|
if(CMAKE_EXPORT_COMPILE_COMMANDS)
|
||||||
|
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES
|
||||||
|
${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
|
||||||
|
endif()
|
||||||
|
|
||||||
# Create a project called 'HW2a'
|
# Create a project called 'HW2a'
|
||||||
project(HW2a)
|
project(HW2a)
|
||||||
|
|
||||||
|
|
1
assignment-2a/compile_commands.json
Symbolic link
1
assignment-2a/compile_commands.json
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
build/compile_commands.json
|
|
@ -1,18 +1,19 @@
|
||||||
// Skeleton code for hw2a
|
// Skeleton code for hw2a
|
||||||
// Based on example code from: Interactive Computer Graphics: A Top-Down Approach with Shader-Based OpenGL (6th Edition), by Ed Angel
|
// Based on example code from: Interactive Computer Graphics: A Top-Down
|
||||||
|
// Approach with Shader-Based OpenGL (6th Edition), by Ed Angel
|
||||||
|
|
||||||
|
|
||||||
#include "glad/glad.h"
|
|
||||||
#include "GLFW/glfw3.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <math.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define DEBUG_ON 0 // repetitive of the debug flag in the shader loading code, included here for clarity only
|
#define DEBUG_ON \
|
||||||
|
0 // repetitive of the debug flag in the shader loading code, included here
|
||||||
|
// for clarity only
|
||||||
|
|
||||||
// This file contains the code that reads the shaders from their files and compiles them
|
// This file contains the code that reads the shaders from their files and
|
||||||
|
// compiles them
|
||||||
#include "ShaderStuff.hpp"
|
#include "ShaderStuff.hpp"
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
@ -33,92 +34,119 @@ GLint m_location;
|
||||||
GLdouble mouse_x, mouse_y;
|
GLdouble mouse_x, mouse_y;
|
||||||
GLint window_width = 500;
|
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);
|
||||||
|
|
||||||
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
|
// function that is called whenever an error occurs
|
||||||
static void
|
static void error_callback(int error, const char *description) {
|
||||||
error_callback(int error, const char* description){
|
|
||||||
fputs(description, stderr); // write the error description to stderr
|
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
|
// function that is called whenever a keyboard event occurs; defines how
|
||||||
static void
|
// keyboard input will be handled
|
||||||
key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
static void key_callback(GLFWwindow *window, int key, int scancode, int action,
|
||||||
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) // checks to see if the escape key was pressed
|
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
|
glfwSetWindowShouldClose(window, GL_TRUE); // closes the window
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
// function that is called whenever a mouse or trackpad button press event occurs
|
// function that is called whenever a mouse or trackpad button press event
|
||||||
static void
|
// occurs
|
||||||
mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
static void 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, etc.
|
// Check which mouse button triggered the event, e.g. GLFW_MOUSE_BUTTON_LEFT,
|
||||||
// and what the button action was, e.g. GLFW_PRESS, GLFW_RELEASE, etc.
|
// etc. and what the button action was, e.g. GLFW_PRESS, GLFW_RELEASE, etc.
|
||||||
// (Note that ordinary trackpad click = mouse left button)
|
// (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.
|
// Also check if any modifier keys were active at the time of the button
|
||||||
// Take the appropriate action, which could (optionally) also include changing the cursor's appearance
|
// 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
|
// function that is called whenever a cursor motion event occurs
|
||||||
static void
|
static void cursor_pos_callback(GLFWwindow *window, double xpos, double ypos) {
|
||||||
cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) {
|
|
||||||
|
|
||||||
// determine the direction of the mouse or cursor motion
|
// determine the direction of the mouse or cursor motion
|
||||||
// update the current mouse or cursor location
|
// update the current mouse or cursor location
|
||||||
// (necessary to quantify the amount and direction of cursor motion)
|
// (necessary to quantify the amount and direction of cursor motion)
|
||||||
// take the appropriate action
|
// take the appropriate action
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
|
||||||
void init( void )
|
void init(void) {
|
||||||
{
|
|
||||||
ColorType3D colors[NVERTICES];
|
ColorType3D colors[NVERTICES];
|
||||||
FloatType2D vertices[NVERTICES];
|
FloatType2D vertices[NVERTICES];
|
||||||
GLuint vao[1], buffer, program, location1, location2;
|
GLuint vao[1], buffer, program, location1, location2;
|
||||||
|
|
||||||
// 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
|
||||||
colors[0].r = 1; colors[0].g = 1; colors[0].b = 1; // white
|
colors[0].r = 1;
|
||||||
colors[1].r = 1; colors[1].g = 0; colors[1].b = 0; // red
|
colors[0].g = 1;
|
||||||
colors[2].r = 1; colors[2].g = 0; colors[2].b = 0; // red
|
colors[0].b = 1; // white
|
||||||
colors[3].r = 1; colors[3].g = 1; colors[3].b = 1; // white
|
colors[1].r = 1;
|
||||||
colors[4].r = 0; colors[4].g = 0; colors[4].b = 1; // blue
|
colors[1].g = 0;
|
||||||
colors[5].r = 0; colors[5].g = 0; colors[5].b = 1; // blue
|
colors[1].b = 0; // red
|
||||||
colors[6].r = 1; colors[6].g = 1; colors[6].b = 1; // white
|
colors[2].r = 1;
|
||||||
colors[7].r = 0; colors[7].g = 1; colors[7].b = 1; // cyan
|
colors[2].g = 0;
|
||||||
colors[8].r = 0; colors[8].g = 1; colors[8].b = 1; // cyan
|
colors[2].b = 0; // red
|
||||||
|
colors[3].r = 1;
|
||||||
|
colors[3].g = 1;
|
||||||
|
colors[3].b = 1; // white
|
||||||
|
colors[4].r = 0;
|
||||||
|
colors[4].g = 0;
|
||||||
|
colors[4].b = 1; // blue
|
||||||
|
colors[5].r = 0;
|
||||||
|
colors[5].g = 0;
|
||||||
|
colors[5].b = 1; // blue
|
||||||
|
colors[6].r = 1;
|
||||||
|
colors[6].g = 1;
|
||||||
|
colors[6].b = 1; // white
|
||||||
|
colors[7].r = 0;
|
||||||
|
colors[7].g = 1;
|
||||||
|
colors[7].b = 1; // cyan
|
||||||
|
colors[8].r = 0;
|
||||||
|
colors[8].g = 1;
|
||||||
|
colors[8].b = 1; // cyan
|
||||||
|
|
||||||
vertices[0].x = 0; vertices[0].y = 0.25; // center
|
vertices[0].x = 0;
|
||||||
vertices[1].x = 0.25; vertices[1].y = 0.5; // upper right
|
vertices[0].y = 0.25; // center
|
||||||
vertices[2].x = -0.25; vertices[2].y = 0.5; // upper left
|
vertices[1].x = 0.25;
|
||||||
vertices[3].x = 0; vertices[3].y = 0.25; // center (again)
|
vertices[1].y = 0.5; // upper right
|
||||||
vertices[4].x = 0.25; vertices[4].y = -0.5; // low-lower right
|
vertices[2].x = -0.25;
|
||||||
vertices[5].x = 0.5; vertices[5].y = -0.25; // mid-lower right
|
vertices[2].y = 0.5; // upper left
|
||||||
vertices[6].x = 0; vertices[6].y = 0.25; // center (again)
|
vertices[3].x = 0;
|
||||||
vertices[7].x = -0.5; vertices[7].y = -0.25; // low-lower left
|
vertices[3].y = 0.25; // center (again)
|
||||||
vertices[8].x = -0.25; vertices[8].y = -0.5; // mid-lower left
|
vertices[4].x = 0.25;
|
||||||
|
vertices[4].y = -0.5; // low-lower right
|
||||||
|
vertices[5].x = 0.5;
|
||||||
|
vertices[5].y = -0.25; // mid-lower right
|
||||||
|
vertices[6].x = 0;
|
||||||
|
vertices[6].y = 0.25; // center (again)
|
||||||
|
vertices[7].x = -0.5;
|
||||||
|
vertices[7].y = -0.25; // low-lower left
|
||||||
|
vertices[8].x = -0.25;
|
||||||
|
vertices[8].y = -0.5; // mid-lower left
|
||||||
|
|
||||||
// Create and bind a vertex array object
|
// Create and bind a vertex array object
|
||||||
glGenVertexArrays( 1, vao );
|
glGenVertexArrays(1, vao);
|
||||||
glBindVertexArray( vao[0] );
|
glBindVertexArray(vao[0]);
|
||||||
|
|
||||||
// Create and initialize a buffer object large enough to hold both vertex position and color data
|
// Create and initialize a buffer object large enough to hold both vertex
|
||||||
glGenBuffers( 1, &buffer );
|
// position and color data
|
||||||
glBindBuffer( GL_ARRAY_BUFFER, buffer );
|
glGenBuffers(1, &buffer);
|
||||||
glBufferData( GL_ARRAY_BUFFER, sizeof(vertices)+sizeof(colors), vertices, GL_STATIC_DRAW );
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
||||||
|
glBufferData(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);
|
||||||
|
|
||||||
|
@ -128,39 +156,42 @@ void init( void )
|
||||||
fshader << SRC_DIR << "/fshader2a.glsl";
|
fshader << SRC_DIR << "/fshader2a.glsl";
|
||||||
|
|
||||||
// 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.str().c_str(), fshader.str().c_str());
|
||||||
|
|
||||||
// Determine locations of the necessary attributes and matrices used in the vertex shader
|
// Determine locations of the necessary attributes and matrices used in the
|
||||||
location1 = glGetAttribLocation( program, "vertex_position" );
|
// vertex shader
|
||||||
glEnableVertexAttribArray( location1 );
|
location1 = glGetAttribLocation(program, "vertex_position");
|
||||||
glVertexAttribPointer( location1, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );
|
glEnableVertexAttribArray(location1);
|
||||||
location2 = glGetAttribLocation( program, "vertex_color" );
|
glVertexAttribPointer(location1, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
|
||||||
glEnableVertexAttribArray( location2 );
|
location2 = glGetAttribLocation(program, "vertex_color");
|
||||||
glVertexAttribPointer( location2, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(vertices)) );
|
glEnableVertexAttribArray(location2);
|
||||||
m_location = glGetUniformLocation( program, "M");
|
glVertexAttribPointer(location2, 3, GL_FLOAT, GL_FALSE, 0,
|
||||||
|
BUFFER_OFFSET(sizeof(vertices)));
|
||||||
|
m_location = glGetUniformLocation(program, "M");
|
||||||
|
|
||||||
// Define static OpenGL state variables
|
// Define static OpenGL state variables
|
||||||
glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white, opaque background
|
glClearColor(1.0, 1.0, 1.0, 1.0); // white, opaque background
|
||||||
|
|
||||||
// Define some GLFW cursors (in case you want to dynamically change the cursor's appearance)
|
// Define some GLFW cursors (in case you want to dynamically change the
|
||||||
// If you want, you can add more cursors, or even define your own cursor appearance
|
// cursor's appearance) If you want, you can add more cursors, or even define
|
||||||
|
// your own cursor appearance
|
||||||
arrow_cursor = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
|
arrow_cursor = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
|
||||||
hand_cursor = glfwCreateStandardCursor(GLFW_HAND_CURSOR);
|
hand_cursor = glfwCreateStandardCursor(GLFW_HAND_CURSOR);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
GLFWwindow* window;
|
GLFWwindow *window;
|
||||||
|
|
||||||
// Define the error callback function
|
// Define the error callback function
|
||||||
glfwSetErrorCallback(error_callback);
|
glfwSetErrorCallback(error_callback);
|
||||||
|
|
||||||
// Initialize GLFW (performs platform-specific initialization)
|
// Initialize GLFW (performs platform-specific initialization)
|
||||||
if (!glfwInit()) exit(EXIT_FAILURE);
|
if (!glfwInit())
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
|
||||||
// Ask for OpenGL 3.2
|
// Ask for OpenGL 3.2
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
@ -171,9 +202,9 @@ int main(int argc, char** argv) {
|
||||||
// Use GLFW to open a window within which to display your graphics
|
// Use GLFW to open a window within which to display your graphics
|
||||||
window = glfwCreateWindow(window_width, window_height, "HW2a", NULL, NULL);
|
window = glfwCreateWindow(window_width, window_height, "HW2a", NULL, NULL);
|
||||||
|
|
||||||
// Verify that the window was successfully created; if not, print error message and terminate
|
// Verify that the window was successfully created; if not, print error
|
||||||
if (!window)
|
// message and terminate
|
||||||
{
|
if (!window) {
|
||||||
printf("GLFW failed to create window; terminating\n");
|
printf("GLFW failed to create window; terminating\n");
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
@ -188,7 +219,9 @@ int main(int argc, char** argv) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
glfwSwapInterval(1); // tells the system to wait for the rendered frame to finish updating before swapping buffers; can help to avoid tearing
|
glfwSwapInterval(
|
||||||
|
1); // tells the system to wait for the rendered frame to finish updating
|
||||||
|
// before swapping buffers; can help to avoid tearing
|
||||||
|
|
||||||
// Define the keyboard callback function
|
// Define the keyboard callback function
|
||||||
glfwSetKeyCallback(window, key_callback);
|
glfwSetKeyCallback(window, key_callback);
|
||||||
|
@ -204,19 +237,31 @@ int main(int argc, char** argv) {
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
// 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 actions.
|
// define/re-define the modelview matrix. In this template, we define M to
|
||||||
for (i=0; i<16; i++) {
|
// be the identity matrix; you will need define M according to the user's
|
||||||
M[i] = (i%5==0);
|
// actions.
|
||||||
|
for (i = 0; i < 16; i++) {
|
||||||
|
M[i] = (i % 5 == 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) printf("M = [%f %f %f %f\n %f %f %f %f\n %f %f %f %f\n %f %f %f %f]\n",M[0],M[4],M[8],M[12], M[1],M[5],M[9],M[13], M[2],M[6],M[10],M[14], M[3],M[7],M[11],M[15]);
|
if (DEBUG_ON)
|
||||||
|
printf("M = [%f %f %f %f\n %f %f %f %f\n %f %f %f %f\n %f %f "
|
||||||
|
"%f %f]\n",
|
||||||
|
M[0], M[4], M[8], M[12], M[1], M[5], M[9], M[13], M[2], M[6],
|
||||||
|
M[10], M[14], M[3], M[7], M[11], M[15]);
|
||||||
|
|
||||||
glUniformMatrix4fv( m_location, 1, GL_FALSE, M ); // send the updated model transformation matrix to the GPU
|
glUniformMatrix4fv(
|
||||||
glDrawArrays( GL_TRIANGLES, 0, NVERTICES ); // draw a triangle between the first vertex and each successive vertex pair in the hard-coded model
|
m_location, 1, GL_FALSE,
|
||||||
glFlush(); // ensure that all OpenGL calls have executed before swapping buffers
|
M); // send the updated model transformation matrix to the GPU
|
||||||
|
glDrawArrays(
|
||||||
|
GL_TRIANGLES, 0,
|
||||||
|
NVERTICES); // draw a triangle between the first vertex and each
|
||||||
|
// successive vertex pair in the hard-coded model
|
||||||
|
glFlush(); // ensure that all OpenGL calls have executed before swapping
|
||||||
|
// buffers
|
||||||
|
|
||||||
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
|
||||||
|
@ -225,9 +270,8 @@ int main(int argc, char** argv) {
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
glfwDestroyWindow(window);
|
glfwDestroyWindow(window);
|
||||||
glfwTerminate(); // destroys any remaining objects, frees resources allocated by GLFW
|
glfwTerminate(); // destroys any remaining objects, frees resources allocated
|
||||||
|
// by GLFW
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
} // end main
|
} // end main
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,22 @@
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "glad/glad.h"
|
||||||
|
#include "GLFW/glfw3.h"
|
||||||
|
|
||||||
// Basic OpenGL program
|
// Basic OpenGL program
|
||||||
// Based on example code from: Interactive Computer Graphics: A Top-Down Approach with Shader-Based OpenGL (6th Edition), by Ed Angel
|
// Based on example code from: Interactive Computer Graphics: A Top-Down
|
||||||
|
// Approach with Shader-Based OpenGL (6th Edition), by Ed Angel
|
||||||
|
|
||||||
#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
|
||||||
static char*
|
static char *readShaderSource(const char *shaderFile) {
|
||||||
readShaderSource(const char* shaderFile)
|
|
||||||
{
|
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
long length, count;
|
long length, count;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
|
@ -20,7 +26,7 @@ readShaderSource(const char* shaderFile)
|
||||||
fp = fopen(shaderFile, "rb");
|
fp = fopen(shaderFile, "rb");
|
||||||
|
|
||||||
// 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);
|
printf("can't open shader source file %s\n", shaderFile);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -28,7 +34,8 @@ readShaderSource(const char* shaderFile)
|
||||||
// determine the file size
|
// determine the file size
|
||||||
fseek(fp, 0, SEEK_END); // move position indicator to the end of the file;
|
fseek(fp, 0, SEEK_END); // move position indicator to the end of the file;
|
||||||
length = ftell(fp); // return the value of the current position
|
length = ftell(fp); // return the value of the current position
|
||||||
if (DEBUG_ON) fprintf(stdout, "length in bytes of shader file: %ld\n", length);
|
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
|
// allocate a buffer with the indicated number of bytes, plus one
|
||||||
buffer = new char[length + 1];
|
buffer = new char[length + 1];
|
||||||
|
@ -36,10 +43,11 @@ readShaderSource(const char* shaderFile)
|
||||||
// read the appropriate number of bytes from the file
|
// read the appropriate number of bytes from the file
|
||||||
fseek(fp, 0, SEEK_SET); // move position indicator to the start of 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
|
count = fread(buffer, 1, length, fp); // read all of the bytes
|
||||||
if (DEBUG_ON) fprintf(stdout, "count of bytes successfully read: %ld\n", count);
|
if (DEBUG_ON)
|
||||||
|
fprintf(stdout, "count of bytes successfully read: %ld\n", count);
|
||||||
|
|
||||||
// append a NULL character to indicate the end of the string
|
// append a NULL character to indicate the end of the string
|
||||||
buffer[count] = '\0'; //because on some systems, count != length
|
buffer[count] = '\0'; // because on some systems, count != length
|
||||||
|
|
||||||
// close the file
|
// close the file
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
@ -49,14 +57,13 @@ readShaderSource(const char* shaderFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a GLSL program object from vertex and fragment shader files
|
// Create a GLSL program object from vertex and fragment shader files
|
||||||
GLuint
|
GLuint InitShader(const char *vShaderFileName, const char *fShaderFileName) {
|
||||||
InitShader(const char* vShaderFileName, const char* fShaderFileName)
|
|
||||||
{
|
|
||||||
GLuint vertex_shader, fragment_shader;
|
GLuint vertex_shader, fragment_shader;
|
||||||
GLuint program;
|
GLuint program;
|
||||||
|
|
||||||
// check GLSL version
|
// check GLSL version
|
||||||
if (DEBUG_ON) printf("GLSL version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
|
if (DEBUG_ON)
|
||||||
|
printf("GLSL version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
|
||||||
|
|
||||||
// Create shader handlers
|
// Create shader handlers
|
||||||
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
@ -67,15 +74,15 @@ InitShader(const char* vShaderFileName, const char* fShaderFileName)
|
||||||
std::string fs_text = readShaderSource(fShaderFileName);
|
std::string fs_text = readShaderSource(fShaderFileName);
|
||||||
|
|
||||||
// error check
|
// error check
|
||||||
if ( vs_text == "" ) {
|
if (vs_text == "") {
|
||||||
printf("Failed to read from vertex shader file %s\n", vShaderFileName);
|
printf("Failed to read from vertex shader file %s\n", vShaderFileName);
|
||||||
exit( 1 );
|
exit(1);
|
||||||
} else if (DEBUG_ON) {
|
} else if (DEBUG_ON) {
|
||||||
printf("read shader code:\n%s\n", vs_text.c_str());
|
printf("read shader code:\n%s\n", vs_text.c_str());
|
||||||
}
|
}
|
||||||
if ( fs_text == "" ) {
|
if (fs_text == "") {
|
||||||
printf("Failed to read from fragment shader file %s\n", fShaderFileName);
|
printf("Failed to read from fragment shader file %s\n", fShaderFileName);
|
||||||
exit( 1 );
|
exit(1);
|
||||||
} else if (DEBUG_ON) {
|
} else if (DEBUG_ON) {
|
||||||
printf("read shader code:\n%s\n", fs_text.c_str());
|
printf("read shader code:\n%s\n", fs_text.c_str());
|
||||||
}
|
}
|
||||||
|
@ -92,30 +99,30 @@ InitShader(const char* vShaderFileName, const char* fShaderFileName)
|
||||||
|
|
||||||
// Check for errors in compiling shaders
|
// Check for errors in compiling shaders
|
||||||
GLint compiled;
|
GLint compiled;
|
||||||
glGetShaderiv( vertex_shader, GL_COMPILE_STATUS, &compiled );
|
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &compiled);
|
||||||
if ( !compiled ) {
|
if (!compiled) {
|
||||||
printf("vertex_shader failed to compile\n");
|
printf("vertex_shader failed to compile\n");
|
||||||
if (DEBUG_ON) {
|
if (DEBUG_ON) {
|
||||||
GLint logMaxSize, logLength;
|
GLint logMaxSize, logLength;
|
||||||
glGetShaderiv( vertex_shader, GL_INFO_LOG_LENGTH, &logMaxSize );
|
glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, &logMaxSize);
|
||||||
printf("printing error message of %d bytes\n", logMaxSize);
|
printf("printing error message of %d bytes\n", logMaxSize);
|
||||||
char* logMsg = new char[logMaxSize];
|
char *logMsg = new char[logMaxSize];
|
||||||
glGetShaderInfoLog( vertex_shader, logMaxSize, &logLength, logMsg );
|
glGetShaderInfoLog(vertex_shader, logMaxSize, &logLength, logMsg);
|
||||||
printf("%d bytes retrieved\n", logLength);
|
printf("%d bytes retrieved\n", logLength);
|
||||||
printf("error message: %s\n", logMsg);
|
printf("error message: %s\n", logMsg);
|
||||||
delete[] logMsg;
|
delete[] logMsg;
|
||||||
}
|
}
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
glGetShaderiv( fragment_shader, GL_COMPILE_STATUS, &compiled );
|
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &compiled);
|
||||||
if ( !compiled ) {
|
if (!compiled) {
|
||||||
printf("fragment_shader failed to compile\n");
|
printf("fragment_shader failed to compile\n");
|
||||||
if (DEBUG_ON) {
|
if (DEBUG_ON) {
|
||||||
GLint logMaxSize, logLength;
|
GLint logMaxSize, logLength;
|
||||||
glGetShaderiv( fragment_shader, GL_INFO_LOG_LENGTH, &logMaxSize );
|
glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &logMaxSize);
|
||||||
printf("printing error message of %d bytes\n", logMaxSize);
|
printf("printing error message of %d bytes\n", logMaxSize);
|
||||||
char* logMsg = new char[logMaxSize];
|
char *logMsg = new char[logMaxSize];
|
||||||
glGetShaderInfoLog( fragment_shader, logMaxSize, &logLength, logMsg );
|
glGetShaderInfoLog(fragment_shader, logMaxSize, &logLength, logMsg);
|
||||||
printf("%d bytes retrieved\n", logLength);
|
printf("%d bytes retrieved\n", logLength);
|
||||||
printf("error message: %s\n", logMsg);
|
printf("error message: %s\n", logMsg);
|
||||||
delete[] logMsg;
|
delete[] logMsg;
|
||||||
|
@ -137,5 +144,4 @@ InitShader(const char* vShaderFileName, const char* fShaderFileName)
|
||||||
return program;
|
return program;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue