Add in Square Pyramid render.

master
sigonasr2 1 year ago
parent ac412093de
commit 4d952f035d
  1. 48
      Models/main.cpp
  2. 16
      Models/vertShader.glsl

@ -16,14 +16,14 @@ GLuint vao[numVAOs];
GLuint vbo[numVBOs]; GLuint vbo[numVBOs];
glm::vec3 camera; glm::vec3 camera;
glm::vec3 cubeLoc; glm::vec3 cubeLoc,pyramidLoc;
GLuint vLoc,projLoc; GLuint mvLoc,projLoc;
int width, height; int width, height;
float aspect; float aspect;
glm::mat4 pMat, vMat; glm::mat4 mMat, pMat, vMat, mvMat;
void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed at origin void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed at origin
float vertexPositions[108] = { float cubePositions[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,
@ -37,23 +37,34 @@ void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed
-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
}; };
float pyramidPositions[54]={
-1.0f, -1.0f, 1.0f, 1.0f, - 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-1.0f, -1.0f, - 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.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); glGenVertexArrays(1, vao);
glBindVertexArray(vao[0]); glBindVertexArray(vao[0]);
glGenBuffers(numVBOs, vbo); glGenBuffers(numVBOs, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(cubePositions), cubePositions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(pyramidPositions), pyramidPositions, GL_STATIC_DRAW);
} }
void init(GLFWwindow* window) { void init(GLFWwindow* window) {
renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl"); renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl");
camera={0.0f,0.0f,-200.0f}; camera={0.0f,0.0f,-8.0f};
cubeLoc={0.0f,-2.0f,0.0f}; cubeLoc={0.0f,-2.0f,0.0f};
pyramidLoc={4.0f,4.0f,-4.0f};
setupVertices(); setupVertices();
} }
double lastTime=0; double lastTime=0;
GLuint tfLoc;
void display(GLFWwindow* window, double currentTime) { void display(GLFWwindow* window, double currentTime) {
double elapsedTime=currentTime-lastTime; double elapsedTime=currentTime-lastTime;
@ -64,8 +75,10 @@ void display(GLFWwindow* window, double currentTime) {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(renderingProgram); glUseProgram(renderingProgram);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
vLoc=glGetUniformLocation(renderingProgram,"v_matrix"); mvLoc=glGetUniformLocation(renderingProgram,"mv_matrix");
projLoc=glGetUniformLocation(renderingProgram,"proj_matrix"); projLoc=glGetUniformLocation(renderingProgram,"proj_matrix");
glfwGetFramebufferSize(window,&width,&height); glfwGetFramebufferSize(window,&width,&height);
@ -73,26 +86,31 @@ void display(GLFWwindow* window, double currentTime) {
pMat=glm::perspective(utils::degToRad(60),aspect,0.1f,1000.f); 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}); vMat=glm::translate(glm::mat4(1.0f),glm::vec3{camera.x,camera.y,camera.z});
mMat=glm::translate(glm::mat4(1.0f),cubeLoc);
mvMat=vMat*mMat;
glUniformMatrix4fv(vLoc,1,GL_FALSE,glm::value_ptr(vMat)); glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat));
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
tfLoc=glGetUniformLocation(renderingProgram,"tf");
glUniform1f(tfLoc,(float)currentTime);
glBindBuffer(GL_ARRAY_BUFFER,vbo[0]); glBindBuffer(GL_ARRAY_BUFFER,vbo[0]);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES,0,36);
glEnable(GL_DEPTH_TEST); mMat=glm::translate(glm::mat4(1.0f),pyramidLoc);
glDepthFunc(GL_LEQUAL); mvMat=vMat*mMat;
glDrawArraysInstanced(GL_TRIANGLES,0,36,100000); glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat));
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
glBindBuffer(GL_ARRAY_BUFFER,vbo[1]);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glDrawArrays(GL_TRIANGLES,0,18);
} }
int main(void) { int main(void) {
if (!glfwInit()) { exit(EXIT_FAILURE); } if (!glfwInit()) { exit(EXIT_FAILURE); }
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
GLFWwindow* window = glfwCreateWindow(1200, 1200, "Hello Cubes!", NULL, NULL); GLFWwindow* window = glfwCreateWindow(1920, 1080, "Hello Cubes!", NULL, NULL);
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) { exit(EXIT_FAILURE); } if (glewInit() != GLEW_OK) { exit(EXIT_FAILURE); }
glfwSwapInterval(1); glfwSwapInterval(1);

@ -1,8 +1,7 @@
#version 430 #version 430
layout (location=0) in vec3 position; layout (location=0) in vec3 position;
uniform mat4 v_matrix; uniform mat4 mv_matrix;
uniform mat4 proj_matrix; uniform mat4 proj_matrix;
uniform float tf;
out vec4 varyingColor; out vec4 varyingColor;
mat4 buildTranslate(float x, float y, float z); mat4 buildTranslate(float x, float y, float z);
@ -12,19 +11,6 @@ mat4 buildRotateZ(float rad);
void main(void) void main(void)
{ {
float i = gl_InstanceID + tf;
float a = sin(203.0*i/8000.0)*403.0;
float b = sin(301.0*i/4001.0)*401.0;
float c = sin(400.0*i/6003.0)*405.0;
mat4 localRotX = buildRotateX(1*i);
mat4 localRotY = buildRotateY(1*i);
mat4 localRotZ = buildRotateZ(1*i);
mat4 localTrans = buildTranslate(a,b,c);
mat4 newM_matrix = localTrans*localRotX*localRotY*localRotZ;
mat4 mv_matrix = v_matrix*newM_matrix;
gl_Position=proj_matrix*mv_matrix*vec4(position,1.0); gl_Position=proj_matrix*mv_matrix*vec4(position,1.0);
varyingColor=vec4(position,1.0)*0.5+vec4(0.5,0.5,0.5,0.5); varyingColor=vec4(position,1.0)*0.5+vec4(0.5,0.5,0.5,0.5);
} }

Loading…
Cancel
Save