main.cpp /******************************************************************************| | CPSC 4050/6050 Computer Garphics Assignment 5, Daljit Singh Dhillon, 2020 | | Modified from: | | | | DPA 8090 Example Code, Eric Patterson, 2017 | | | | This is mostly plain C but uses a few things from C++ so needs C++ compiler. | | Some OpenGL setup code here and obj_parser, math_funcs, and gl_utils | | are from Angton Gerdelan and "Anton's OpenGL 4 Tutorials." | | http://antongerdelan.net/opengl/ | | Email: anton at antongerdelan dot net | | Copyright Dr Anton Gerdelan, Trinity College Dublin, Ireland. | |******************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <math.h> #include <time.h> #include <GL/glew.h> // Include GLEW (or new version of GL if on Windows). #include <GLFW/glfw3.h> // GLFW helper library. #include "maths_funcs.h" // Anton's maths functions. #include "gl_utils.h" // Anton's opengl functions and small utilities like logs #include "obj_parser.h" // Anton's little Wavefront .obj mesh loader #include "stb_image.h" // Sean Barrett's image loader with Anton's load_texture() #include <iostream> using namespace std; #define _USE_MATH_DEFINES #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444 // The view and proj matrices below make-up the camera position, orientation, fov, etc. // The model matrices moves your surface of revolution to the world coordinate system extern mat4 view_mat; extern mat4 proj_mat; extern mat4 model_mat; // the vector below indicates camra placement. //It looks at (0,0,0) with (0,1,0) as the up-vector vec3 cam_pos (0.0f, 10.0f, 120.0f); // Below are the declarations for three functions from stub.cpp void loadSurfaceOfRevolution(float highestPointY); void loadUniforms(GLuint shader_programme); void drawSurfaceOfRevolution(); void keyboardFunction(GLFWwindow* window, int key, int scancode, int action, int mods); // Forward Declaration // this function loads a .jpg or .png file into a previously activated texture map bool load_texture (const char* file_name, GLuint* tex); /******************************************************************************| | argc: number of input command-line parameters | | argv: array of input command-line character strings | | EXPECTED INPUT | | | | argv[0]: automatically asigned the program name cg05 | | argv[1]: Vertex Shader Filename (first argument at command-line) | | argv[2]: Fragment Shader Filename (second argument at command-line) | |******************************************************************************/ int main (int argc, char *argv[]) { if (argc < 3) { printf("\nEnter atleast 2 filenames to run this program: (1) vtx_shader, (2) frag_shader\n"); printf("\nQuiting now...\n"); return 0; } /*--------------------------------START OPENGL--------------------------------*/ assert (restart_gl_log ()); assert (start_gl ()); // Start glfw window with GL context within. glfwSetKeyCallback(g_window, keyboardFunction); /*------------------------------CREATE GEOMETRY-------------------------------*/ float highestPoint; cout << "Enter the max height for the natural form: "; cin >> highestPoint; loadSurfaceOfRevolution(highestPoint); /*-------------------------------CREATE SHADERS-------------------------------*/ // The vertex shader program generally acts to transform vertices. // The fragment shader is where we'll do the actual "shading." GLuint shader_programme = create_programme_from_files ( argv[1], argv[2] ); glUseProgram (shader_programme); /*-------------------------------CREATE CAMERA--------------------------------*/ // This sets up the matrix transformations that act as the camera lens // and sensor would to transform vertices from view space. float near = 0.1f; // clipping planes float far = 1000.0f; float fovy = 70.0f; // vertical field of view, horiz calculated for aspect float aspect = (float)g_gl_width / (float)g_gl_height; // aspect ratio proj_mat = perspective (fovy, aspect, near, far); mat4 T = translate ( identity_mat4 (), vec3 (-cam_pos.v[0], -cam_pos.v[1], -cam_pos.v[2]) ); // would usually use inverse camera orientation with position to construct // view matrix, but for simplicity since camera axis-aligned, not needed here view_mat = T; /*---------------------------SET RENDERING DEFAULTS---------------------------*/ // The model matrix stores the position and orientation transformations for your SoR model_mat = identity_mat4(); // Setup basic GL display attributes. glEnable (GL_DEPTH_TEST); // enable depth-testing glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer" //glEnable (GL_CULL_FACE); // cull face //glCullFace (GL_BACK); // cull back face //glFrontFace (GL_CW); // set counter-clock-wise vertex order to mean the front glClearColor (0.5, 0.5, 0.5, 1.0); // non-black background to help spot mistakes glViewport (0, 0, g_gl_width, g_gl_height); // make sure correct aspect ratio /*-------------------------------RENDERING LOOP-------------------------------*/ while (!glfwWindowShouldClose (g_window)) { // update timers static double previous_seconds = glfwGetTime (); double current_seconds = glfwGetTime (); double elapsed_seconds = current_seconds - previous_seconds; previous_seconds = current_seconds; _update_fps_counter (g_window); // clear graphics context glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // setup shader use glUseProgram (shader_programme); // update and draw mesh, rotating the SoR // set a pace independent of outright rendering speed double rotAmt = -15 * elapsed_seconds; model_mat = rotate_y_deg(identity_mat4(), rotAmt) * model_mat; // load uniform variables for shaders // YOU HAVE TO IMPLEMENT THIS FUNCTION IN stub.cpp loadUniforms(shader_programme); // The following function will actually draw your previously dispatched/loaded Surface of Revolution // YOU HAVE TO IMPLEMENT THIS FUNCTION IN stub.cpp drawSurfaceOfRevolution(); // update other events like input handling glfwPollEvents (); aspect = (float)g_gl_width / (float)g_gl_height; // aspect ratio proj_mat = perspective (fovy, aspect, near, far); // put the stuff we've been drawing onto the display glfwSwapBuffers (g_window); } // close GL context and any other GLFW resources glfwTerminate(); return 0; } stub.cpp /******************************************************************************| | CPSC 6050 Computer Graphics Calla Corder Final Project | |Adpapted from Assignment 6 | |******************************************************************************/ #include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <assert.h> #include <math.h> #include <time.h> #include "maths_funcs.h" // Anton's maths functions. #include "gl_utils.h" // Anton's opengl functions and small utilities like logs #include "stb_image.h" // Sean Barrett's image loader with Anton's load_texture() #include <iostream> using namespace std; #define _USE_MATH_DEFINES #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444 mat4 view_mat; mat4 proj_mat; mat4 model_mat; int size; int side = 128; int iterations = 0; float yValues[129][129] = {}; float diamondSquares[129][129] = {}; float midPoints[129][129] = {}; float multiplications[129][129] = {}; float maxHeight = 50; float lightX = 0.0; float lightY = 0.0; float lightZ = 0.0; float specEx = 4.0; int terrainFlag = 1; int mathFlag = 0; float diamondMax = 0; float midPointMax = 0; float midPointMax2 = 0; float multiplicationMax = 0; float highestPoint = 0; int smoothFlag = 0; frag.glsl #version 410 // Define INPUTS from fragment shader //uniform mat4 view_mat; in vec3 Normal; in vec3 fpos; // These come from the VAO for texture coordinates. varying vec2 textures; varying float height; uniform float lightX; uniform float lightY; uniform float lightZ; uniform int terrainFlag; uniform float maxHeight; out vec4 fragment_color; //RGBA color vec3 lightPos = vec3(lightX, lightY, lightZ); const vec3 specColor = vec3(1.0,1.0,1.0); float specEx = 5.0; void main () { float colorR = 1.0; float colorG = 1.0; float colorB = 1.0; if(terrainFlag == 1) { //general //low points are blue, high points are red colorG = 0.0; colorR = height / maxHeight; //y = 0 is all blue colorB = 1.0 - (height/maxHeight); } if (terrainFlag == 2) { //stalagmite colorR = (height/maxHeight) + .96; colorG = (height/maxHeight) + .4; colorB = height/(maxHeight*2.5); specEx = 9.0; } if (terrainFlag == 3) { //ice colorR = (height/maxHeight)* 0.25; colorG = ((height/maxHeight)*0.8) + 0.05; colorB = (height/maxHeight) + 0.3; specEx = 4.0; } if (terrainFlag == 4) { //grass colorR = ((height/maxHeight)*.21) + .10; colorG = ((height/maxHeight)*.6)+.06; colorB = (height/maxHeight) * .15; specEx = 12.0; } if(terrainFlag == 5) { //silver colorR = ((height/maxHeight)*.80)+.1; colorG = ((height/maxHeight)*.75)+0.06; colorB = (height/maxHeight)*.7; specEx = 0.5; } vec3 diffColor = vec3(colorR, colorG, colorB); vec3 normal = normalize(Normal); vec3 lightDir = normalize(lightPos - fpos); //float amb = max(dot(lightDir ,normal), 0.0); float amb = 0.5; float spec = 0.0; if (amb > 0.0) { vec3 refDir = reflect(-lightDir, normal); vec3 viewDir = normalize(-fpos); float specAngle = max(dot(refDir, viewDir), 0.0); spec = pow(specAngle, specEx); } fragment_color = vec4(amb * diffColor + spec * specColor, 1.0); //fragment_color = vec4(diffColor, 1.0); } vs.glsl #version 410 layout (location = 0) in vec3 vtxPosition; layout (location = 1) in vec3 normal; layout (location = 2) in vec2 texture; layout (location = 3) in float heightValues; uniform mat4 proj_mat, view_mat, model_mat; out vec3 Normal; out vec3 fpos; varying vec2 textures; varying float height; void main () { gl_Position = proj_mat * view_mat * model_mat *vec4(vtxPosition, 1.0); fpos = vec3(model_mat * vec4(vtxPosition, 1.0)); Normal = mat3(transpose(inverse(model_mat))) * normal; textures = texture; height = heightValues; } </plaintext> </body> </html>