///////////////////////////////////////////////////////////////////////
//
// MA-101156682-triangles.cpp
//
///////////////////////////////////////////////////////////////////////
//*************************************************************************** // HG-22155-Assignment1.cpp by Abel Moore 101156682 (C) 2018 All Rights Reserved. // // Assignment 1 submission. // // Description: this looks cool // Click run to see the results. // *****************************************************************************
using namespace std;
#include "stdlib.h"
#include "time.h"
#include "vgl.h"
#include "LoadShaders.h"
#include "glm\glm.hpp"
#include "glm\gtc\matrix_transform.hpp"
#include "GL\glut.h"
#include <GL\glew.h>
#include "Simple OpenGL Image Library/src/SOIL.h"
#define X_AXIS glm::vec3(1,0,0)
#define Y_AXIS glm::vec3(0,1,0)
#define Z_AXIS glm::vec3(0,0,1)
GLuint cubeVAO;
GLuint MatrixID;
int width, height;
glm::mat4 MVP;
glm::mat4 View;
glm::mat4 Projection;
glm::vec3 currentCamPos;
glm::vec3 currentCamVel;
int frame = 0, currentTime, timebase = 0;
float deltaTime = 0;
bool keyStates[256] = {}; // Create an array of boolean values of length 256 (0-255)
void init(void) {
//keyStates = false;
//Specifying the name of vertex and fragment shaders.
ShaderInfo shaders[] = {
{ GL_VERTEX_SHADER, "triangles.vert" },
{ GL_FRAGMENT_SHADER, "triangles.frag" },
{ GL_NONE, NULL }
};
//Loading and compiling shaders
GLuint program = LoadShaders(shaders);
glUseProgram(program); //My Pipeline is set up
// Get a handle for our "MVP" uniform
MatrixID = glGetUniformLocation(program, "MVP");
// Projection matrix : 45? Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
Projection = glm::perspective(glm::radians(45.0f), 4.0f / 3.0f, 0.1f, 100.0f);
// Or, for an ortho camera :
//Projection = glm::ortho(-1.0f,1.0f,-1.0f,1.0f,0.0f,100.0f); // In world coordinates
currentCamPos = glm::vec3(3.0f, 3.0f, 4.0f);
currentCamVel = glm::vec3(0.0f);
// Camera matrix
View = glm::lookAt(
currentCamPos, // Camera is at (4,3,3), in World Space
glm::vec3(0, 0, 0), // and looks at the origin
glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
);
//************ Setup Cube VAO ************
glGenVertexArrays(1, &cubeVAO);
glBindVertexArray(cubeVAO);
//************ Setup the cube vertex positions ************
// Setup the cube vertex positions array
float cube_positions[] = {
// front
-0.45, -0.45, 0.45,
0.45, -0.45, 0.45,
0.45, 0.45, 0.45,
-0.45, 0.45, 0.45,
// back
-0.45, -0.45, -0.45,
0.45, -0.45, -0.45,
0.45, 0.45, -0.45,
-0.45, 0.45, -0.45,
};
// Setup the cube positions VBO
GLuint cubePositions_vbo = 0;
glGenBuffers(1, &cubePositions_vbo);
// Bind the cube positions VBO
glBindBuffer(GL_ARRAY_BUFFER, cubePositions_vbo);
// Fill the cube positions VBO with the cube positions array
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_positions), cube_positions, GL_STATIC_DRAW);
// Link the vertex shader vec3 at locaiton 0 to the cube positions VBO (since it is the one currently bound)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
//************ Setup the cube vertex colors ************
// Setup the cube vertex colors array
float cube_colors[] = {
// front colors
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 1.0, 1.0,
// back colors
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 1.0, 1.0,
};
// Setup the cube colors VBO
GLuint cube_colors_vbo = 0;
glGenBuffers(1, &cube_colors_vbo);
// Bind the cube colors VBO
glBindBuffer(GL_ARRAY_BUFFER, cube_colors_vbo);
// Fill the cube colors VBO with the cube colors array
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_colors), cube_colors, GL_STATIC_DRAW);
// Link the vertex shader vec3 at locaiton 1 to the cube colors VBO (since it is the one currently bound)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
unsigned char* image = SOIL_load_image("rubiksTexture.png",
&width, &height, 0, SOIL_LOAD_RGB);
GLuint cube_tex;
glGenTextures(1, &cube_tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, cube_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glUniform1i(glGetUniformLocation(program, "texture0"), 0);
//************ Setup the cube index list ************
// Setup the cube index array
GLushort cube_index_array[] = {
// front
0, 1, 2,
2, 3, 0,
// top
1, 5, 6,
6, 2, 1,
// back
7, 6, 5,
5, 4, 7,
// bottom
4, 0, 3,
3, 7, 4,
// left
4, 5, 1,
1, 0, 4,
// right
3, 2, 6,
6, 7, 3,
};
// Setup the cube IBO
GLuint ibo_cube_elements;
glGenBuffers(1, &ibo_cube_elements);
// Bind the cube IBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_elements);
// Fill the cube IBO with the cube index array
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_index_array), cube_index_array, GL_STATIC_DRAW);
}
void keyDown(unsigned char key, int x, int y) {
keyStates[key] = true; // Set the state of the current key to pressed
}
void keyUp(unsigned char key, int x, int y) {
keyStates[key] = false; // Release the state of the current key to pressed
}
void keyOperations(void) {
float cameraSpeed = 10.0f;
if (keyStates['w']) { // If the 'w' key has been pressed
currentCamPos.z -= cameraSpeed * (deltaTime);
}
if (keyStates['s']) { // If the 's' key has been pressed
currentCamPos.z += cameraSpeed * (deltaTime);
}
if (keyStates['a']) { // If the 'a' key has been pressed
currentCamPos.x -= cameraSpeed * (deltaTime);
}
if (keyStates['d']) { // If the 'd' key has been pressed
currentCamPos.x += cameraSpeed * (deltaTime);
}
if (keyStates['r']) { // If the 'r' key has been pressed
currentCamPos.y += cameraSpeed * (deltaTime);
}
if (keyStates['f']) { // If the 'f' key has been pressed
currentCamPos.y -= cameraSpeed * (deltaTime);
}
View = glm::lookAt(
currentCamPos, // Camera is at (4,3,3), in World Space
glm::vec3(0, 0, 0), // and looks at the origin
glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
);
}
void transformObject(glm::vec3 scale, glm::vec3 rotationAxis, float rotationAngle, glm::vec3 translation) {
glm::mat4 Model;
Model = glm::mat4(1.0f);
Model = glm::translate(Model, translation);
Model = glm::rotate(Model, glm::radians(rotationAngle), rotationAxis);
Model = glm::scale(Model, scale);
MVP = Projection * View * Model;
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
}
//---------------------------------------------------------------------
//
// display
//
void display(void) {
keyOperations();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.5f, 0.9f, 0.0f);
// Bind the cube VAO
glBindVertexArray(cubeVAO);
// Transform and Draw the top cube using the cube VAO
float angle = glutGet(GLUT_ELAPSED_TIME) / 1000.0 * -45; // 45° per second
transformObject(glm::vec3(0.5f), X_AXIS + Y_AXIS + Z_AXIS, angle, glm::vec3(0.0f, -0.45f, 0.0f));
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
glutSwapBuffers();
deltaTime = (glutGet(GLUT_ELAPSED_TIME) - currentTime) / 1000.0f;
currentTime = glutGet(GLUT_ELAPSED_TIME);
}
void idle() {
}
void Timer(int id) {
glutPostRedisplay();
glutTimerFunc(15, Timer, 0);
}
//---------------------------------------------------------------------
//
// main
//
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(512, 512);
glutInitWindowPosition(0, 0);
glutCreateWindow("Aroshabel Moore 101156682");
glewExperimental = true;
glewInit(); //Initializes the glew and prepares the drawing pipeline.
glEnable(GL_CULL_FACE); // cull face
glCullFace(GL_BACK); // cull back face
glFrontFace(GL_CCW); // GL_CCW for counter clock-wise
glEnable(GL_DEPTH_TEST);
init();
glutTimerFunc(15, Timer, 0);
glutDisplayFunc(display);
glutKeyboardFunc(keyDown);
glutKeyboardUpFunc(keyUp);
glutIdleFunc(idle);
glutMainLoop();
}
#version 410 core
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 vertex_colour;
layout(location = 2) in vec2 vertex_texture;
out vec3 myColor;
out vec2 texCoord;
uniform highp mat4 MVP;
void main()
{
myColor = vertex_colour;
texCoord = vertex_texture;
gl_Position = MVP * vec4(vertex_position,1.0f);
}
#version 410 core
in vec3 myColor;
in vec2 texCoord;
out vec4 frag_colour;
uniform sampler2D texture0;
void main() {
frag_colour = vec4(myColor, 1.0);
frag_colour = texture(texture0, texCoord);
}