
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "glm/glm.hpp"
#include <iostream>
#include "utils.h"
using namespace std;
#define numVAOs 1
#define numVBOs 3
void setupVertices(void);
GLuint renderingProgram;
GLuint vao[numVAOs];
GLuint vbo[numVBOs];
float x = 0.0f; // location of triangle on x axis
float inc = 0.01f; // offset for moving the triangle
float theta = 0;

void init(GLFWwindow* window) {
	renderingProgram = createShaderProgram(); //load, compile, create, attach and link shader programmes done here
	setupVertices();
}
void display(GLFWwindow* window, double currentTime) {
	glClearColor(1.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glUseProgram(renderingProgram); //specify linked program to use
	 
	glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); //three vertices of triangle 1 primitive
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(0); //point to VBO 

	glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(1);
	glDrawArrays(GL_TRIANGLES, 0, 3);  //draw triangle primitive

	glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(0);
	glDrawArrays(GL_TRIANGLES, 0, 3);
}
int main(void) {
	if (!glfwInit()) { exit(EXIT_FAILURE); }
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	GLFWwindow* window = glfwCreateWindow(600, 600, "Triangle glfw window", NULL, NULL);
	glfwMakeContextCurrent(window);
	if (glewInit() != GLEW_OK) { exit(EXIT_FAILURE); }
	glfwSwapInterval(1);
	init(window);
	while (!glfwWindowShouldClose(window)) {
		display(window, glfwGetTime());
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	glfwDestroyWindow(window);
	glfwTerminate();
	exit(EXIT_SUCCESS);
}


void setupVertices(void) { 
	// 9 vertices, for first triangle
	float tri1VertexPos[9] = {
	0.0, -0.25, 0.0,
	-0.75,-0.25,0.0,
	0.0, 0.25, 0.0};
	float tri1VertexColor[9] = {
	1.0,0.0,0.0,
	0.0,1.0,0.0,
	0.0,0.0,1.0};
	// 9 vertices, for 2nd triangle
	float tri2VertexPos[9] = {
	0.55 , -0.25, 0.0,
	0.0,-0.25,0.0,
	0.55, 0.25, 0.0 };

	//initialse VAO and VBOs
	glGenVertexArrays(numVAOs, vao); //we will use one VAO
	glBindVertexArray(vao[0]);
	glGenBuffers(numVBOs, vbo);//we have three VBO's
	glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);//first VBO
	glBufferData(GL_ARRAY_BUFFER, sizeof(tri1VertexPos), tri1VertexPos, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);//first VBO
	glBufferData(GL_ARRAY_BUFFER, sizeof(tri1VertexColor), tri1VertexColor, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);//3nd VBO
	glBufferData(GL_ARRAY_BUFFER, sizeof(tri2VertexPos), tri2VertexPos, GL_STATIC_DRAW);
}
