You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.4 KiB
111 lines
3.4 KiB
#include <GL/glew.h>
|
|
#include <GLFW/glfw3.h>
|
|
#include "utils.h"
|
|
#include <algorithm>
|
|
#include <vector>
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <array>
|
|
|
|
#define numVAOs 1
|
|
#define numVBOs 2
|
|
|
|
GLuint renderingProgram;
|
|
GLuint vao[numVAOs];
|
|
GLuint vbo[numVBOs];
|
|
|
|
glm::vec3 camera;
|
|
glm::vec3 cubeLoc;
|
|
GLuint vLoc,projLoc;
|
|
int width, height;
|
|
float aspect;
|
|
glm::mat4 pMat, vMat;
|
|
|
|
void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed at origin
|
|
float vertexPositions[108] = {
|
|
-1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
|
|
1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
|
|
1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
|
|
1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
|
|
1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
|
|
-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
|
|
-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
|
|
-1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
|
|
-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
|
|
1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f,
|
|
-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
|
|
1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f
|
|
};
|
|
glGenVertexArrays(1, vao);
|
|
glBindVertexArray(vao[0]);
|
|
glGenBuffers(numVBOs, vbo);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
|
|
}
|
|
|
|
void init(GLFWwindow* window) {
|
|
renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl");
|
|
camera={0.0f,0.0f,-200.0f};
|
|
cubeLoc={0.0f,-2.0f,0.0f};
|
|
setupVertices();
|
|
}
|
|
|
|
double lastTime=0;
|
|
GLuint tfLoc;
|
|
|
|
void display(GLFWwindow* window, double currentTime) {
|
|
double elapsedTime=currentTime-lastTime;
|
|
lastTime=currentTime;
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
|
glClearColor(0,0,0.2,1);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glUseProgram(renderingProgram);
|
|
|
|
vLoc=glGetUniformLocation(renderingProgram,"v_matrix");
|
|
projLoc=glGetUniformLocation(renderingProgram,"proj_matrix");
|
|
|
|
glfwGetFramebufferSize(window,&width,&height);
|
|
aspect=(float)width/(float)height;
|
|
pMat=glm::perspective(utils::degToRad(60),aspect,0.1f,1000.f);
|
|
|
|
vMat=glm::translate(glm::mat4(1.0f),glm::vec3{camera.x,camera.y,camera.z});
|
|
|
|
glUniformMatrix4fv(vLoc,1,GL_FALSE,glm::value_ptr(vMat));
|
|
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
|
|
tfLoc=glGetUniformLocation(renderingProgram,"tf");
|
|
glUniform1f(tfLoc,(float)currentTime);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[0]);
|
|
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
glDepthFunc(GL_LEQUAL);
|
|
glDrawArraysInstanced(GL_TRIANGLES,0,36,100000);
|
|
}
|
|
|
|
int main(void) {
|
|
if (!glfwInit()) { exit(EXIT_FAILURE); }
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
GLFWwindow* window = glfwCreateWindow(1200, 1200, "Hello Cubes!", 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);
|
|
} |