|
|
@ -20,16 +20,36 @@ GLuint vao[numVAOs]; |
|
|
|
GLuint vbo[numVBOs]; |
|
|
|
GLuint vbo[numVBOs]; |
|
|
|
|
|
|
|
|
|
|
|
glm::vec3 camera; |
|
|
|
glm::vec3 camera; |
|
|
|
GLuint mvLoc,projLoc; |
|
|
|
GLuint globalAmbLoc,ambLoc,diffLoc,specLoc,lPosLoc,mAmbLoc,mDiffLoc,mSpecLoc,mShinLoc,mvLoc,projLoc,nLoc; |
|
|
|
int width, height; |
|
|
|
int width, height; |
|
|
|
float aspect; |
|
|
|
float aspect; |
|
|
|
glm::mat4 mMat, pMat, vMat, mvMat; |
|
|
|
glm::mat4 mMat, pMat, vMat, mvMat,invTrMat; |
|
|
|
|
|
|
|
glm::vec3 currentLightPos,lightPosV; |
|
|
|
|
|
|
|
std::array<float,3>lightPos; |
|
|
|
|
|
|
|
glm::vec3 initialLightLoc={0.0f,2.0f,4.0f}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float globalAmbient[4] = { 0.7f, 0.7f, 0.7f, 1.0f }; |
|
|
|
|
|
|
|
float lightAmbient[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; |
|
|
|
|
|
|
|
float lightDiffuse[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; |
|
|
|
|
|
|
|
float lightSpecular[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float* matAmb = utils::silverAmbient(); |
|
|
|
|
|
|
|
float* matDif = utils::silverDiffuse(); |
|
|
|
|
|
|
|
float* matSpe = utils::silverSpecular(); |
|
|
|
|
|
|
|
float matShi = utils::silverShininess(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float* matAmb2 = utils::jadeAmbient(); |
|
|
|
|
|
|
|
float* matDif2 = utils::jadeDiffuse(); |
|
|
|
|
|
|
|
float* matSpe2 = utils::jadeSpecular(); |
|
|
|
|
|
|
|
float matShi2 = utils::jadeShininess(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::stack<glm::mat4>transforms; |
|
|
|
std::stack<glm::mat4>transforms; |
|
|
|
|
|
|
|
|
|
|
|
GLuint pyrTex; |
|
|
|
GLuint pyrTex; |
|
|
|
|
|
|
|
|
|
|
|
Sphere sphere(48); |
|
|
|
Sphere sphere(48); |
|
|
|
Torus torus(1.9,0.4,48); |
|
|
|
Torus torus(1.0,0.5,48); |
|
|
|
ImportedModel shuttle("shuttle.obj"); |
|
|
|
ImportedModel shuttle("shuttle.obj"); |
|
|
|
|
|
|
|
|
|
|
|
void setupVertices(void) { |
|
|
|
void setupVertices(void) { |
|
|
@ -117,13 +137,13 @@ void setupVertices(void) { |
|
|
|
nvalues.push_back(normals[i].z); |
|
|
|
nvalues.push_back(normals[i].z); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo[7]); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo[8]); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, pvalues.size()*sizeof(float), pvalues.data(), GL_STATIC_DRAW); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, pvalues.size()*sizeof(float), pvalues.data(), GL_STATIC_DRAW); |
|
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo[8]); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo[9]); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, tvalues.size()*sizeof(float), tvalues.data(),GL_STATIC_DRAW); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, tvalues.size()*sizeof(float), tvalues.data(),GL_STATIC_DRAW); |
|
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo[9]); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo[10]); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, nvalues.size()*sizeof(float), nvalues.data(), GL_STATIC_DRAW); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, nvalues.size()*sizeof(float), nvalues.data(), GL_STATIC_DRAW); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -139,7 +159,7 @@ void setupTextures(){ |
|
|
|
|
|
|
|
|
|
|
|
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,5.0f,-10.0f}; |
|
|
|
camera={0.0f,0.0f,-10.0f}; |
|
|
|
setupVertices(); |
|
|
|
setupVertices(); |
|
|
|
setupTextures(); |
|
|
|
setupTextures(); |
|
|
|
} |
|
|
|
} |
|
|
@ -147,6 +167,7 @@ void init(GLFWwindow* window) { |
|
|
|
void DrawSphere(){ |
|
|
|
void DrawSphere(){ |
|
|
|
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat)); |
|
|
|
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)); |
|
|
|
|
|
|
|
glUniformMatrix4fv(nLoc, 1, GL_FALSE, glm::value_ptr(invTrMat)); |
|
|
|
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); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[1]); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[1]); |
|
|
@ -161,6 +182,7 @@ void DrawSphere(){ |
|
|
|
void DrawTorus(){ |
|
|
|
void DrawTorus(){ |
|
|
|
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat)); |
|
|
|
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)); |
|
|
|
|
|
|
|
glUniformMatrix4fv(nLoc, 1, GL_FALSE, glm::value_ptr(invTrMat)); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[4]); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[4]); |
|
|
|
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); |
|
|
|
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[5]); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[5]); |
|
|
@ -175,16 +197,48 @@ void DrawTorus(){ |
|
|
|
void DrawShuttle(){ |
|
|
|
void DrawShuttle(){ |
|
|
|
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat)); |
|
|
|
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)); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[7]); |
|
|
|
glUniformMatrix4fv(nLoc, 1, GL_FALSE, glm::value_ptr(invTrMat)); |
|
|
|
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); |
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[8]); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[8]); |
|
|
|
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0); |
|
|
|
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[9]); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[9]); |
|
|
|
|
|
|
|
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0); |
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[10]); |
|
|
|
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,0,0); |
|
|
|
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,0,0); |
|
|
|
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES,0,shuttle.getNumVertices()); |
|
|
|
glDrawArrays(GL_TRIANGLES,0,shuttle.getNumVertices()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void installLights(glm::mat4 vMat){ |
|
|
|
|
|
|
|
currentLightPos = glm::vec3(initialLightLoc.x, initialLightLoc.y, initialLightLoc.z); |
|
|
|
|
|
|
|
// convert light's position to view space, and save it in a float array
|
|
|
|
|
|
|
|
lightPosV = glm::vec3(vMat * glm::vec4(currentLightPos, 1.0)); |
|
|
|
|
|
|
|
lightPos[0] = lightPosV.x; |
|
|
|
|
|
|
|
lightPos[1] = lightPosV.y; |
|
|
|
|
|
|
|
lightPos[2] = lightPosV.z; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get the locations of the light and material fields in the shader
|
|
|
|
|
|
|
|
globalAmbLoc = glGetUniformLocation(renderingProgram, "globalAmbient"); |
|
|
|
|
|
|
|
ambLoc = glGetUniformLocation(renderingProgram, "light.ambient"); |
|
|
|
|
|
|
|
diffLoc = glGetUniformLocation(renderingProgram, "light.diffuse"); |
|
|
|
|
|
|
|
specLoc = glGetUniformLocation(renderingProgram, "light.specular"); |
|
|
|
|
|
|
|
lPosLoc = glGetUniformLocation(renderingProgram, "light.position"); |
|
|
|
|
|
|
|
mAmbLoc = glGetUniformLocation(renderingProgram, "material.ambient"); |
|
|
|
|
|
|
|
mDiffLoc = glGetUniformLocation(renderingProgram, "material.diffuse"); |
|
|
|
|
|
|
|
mSpecLoc = glGetUniformLocation(renderingProgram, "material.specular"); |
|
|
|
|
|
|
|
mShinLoc = glGetUniformLocation(renderingProgram, "material.shininess"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// set the uniform light and material values in the shader
|
|
|
|
|
|
|
|
glProgramUniform4fv(renderingProgram, globalAmbLoc, 1, globalAmbient); |
|
|
|
|
|
|
|
glProgramUniform4fv(renderingProgram, ambLoc, 1, lightAmbient); |
|
|
|
|
|
|
|
glProgramUniform4fv(renderingProgram, diffLoc, 1, lightDiffuse); |
|
|
|
|
|
|
|
glProgramUniform4fv(renderingProgram, specLoc, 1, lightSpecular); |
|
|
|
|
|
|
|
glProgramUniform3fv(renderingProgram, lPosLoc, 1, lightPos.data()); |
|
|
|
|
|
|
|
glProgramUniform4fv(renderingProgram, mAmbLoc, 1, matAmb); |
|
|
|
|
|
|
|
glProgramUniform4fv(renderingProgram, mDiffLoc, 1, matDif); |
|
|
|
|
|
|
|
glProgramUniform4fv(renderingProgram, mSpecLoc, 1, matSpe); |
|
|
|
|
|
|
|
glProgramUniform1f(renderingProgram, mShinLoc, matShi); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
double lastTime=0; |
|
|
|
double lastTime=0; |
|
|
|
|
|
|
|
|
|
|
|
void display(GLFWwindow* window, double currentTime) { |
|
|
|
void display(GLFWwindow* window, double currentTime) { |
|
|
@ -198,35 +252,53 @@ void display(GLFWwindow* window, double currentTime) { |
|
|
|
glUseProgram(renderingProgram); |
|
|
|
glUseProgram(renderingProgram); |
|
|
|
glEnable(GL_DEPTH_TEST); |
|
|
|
glEnable(GL_DEPTH_TEST); |
|
|
|
glDepthFunc(GL_LEQUAL); |
|
|
|
glDepthFunc(GL_LEQUAL); |
|
|
|
|
|
|
|
glEnable(GL_CULL_FACE); |
|
|
|
|
|
|
|
glFrontFace(GL_CCW); |
|
|
|
|
|
|
|
glEnable(GL_BLEND); |
|
|
|
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glEnableVertexAttribArray(0); |
|
|
|
glEnableVertexAttribArray(0); |
|
|
|
glEnableVertexAttribArray(1); |
|
|
|
glEnableVertexAttribArray(1); |
|
|
|
glEnableVertexAttribArray(2); |
|
|
|
glEnableVertexAttribArray(2); |
|
|
|
|
|
|
|
|
|
|
|
mvLoc=glGetUniformLocation(renderingProgram,"mv_matrix"); |
|
|
|
mvLoc=glGetUniformLocation(renderingProgram,"mv_matrix"); |
|
|
|
projLoc=glGetUniformLocation(renderingProgram,"proj_matrix"); |
|
|
|
projLoc=glGetUniformLocation(renderingProgram,"proj_matrix"); |
|
|
|
|
|
|
|
nLoc = glGetUniformLocation(renderingProgram, "norm_matrix"); |
|
|
|
|
|
|
|
|
|
|
|
glfwGetFramebufferSize(window,&width,&height); |
|
|
|
glfwGetFramebufferSize(window,&width,&height); |
|
|
|
aspect=(float)width/(float)height; |
|
|
|
aspect=(float)width/(float)height; |
|
|
|
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}); |
|
|
|
vMat=glm::lookAt(glm::vec3{camera.x,camera.y,camera.z},{0,-3,0},{0,1,0}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mMat=glm::translate(glm::mat4(1.0f),{0.f,0.f,0.f}); |
|
|
|
installLights(vMat); |
|
|
|
mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,-0.2f}); |
|
|
|
|
|
|
|
mMat=glm::scale(mMat,{8,8,8}); |
|
|
|
//vMat=glm::lookAt(glm::vec3{camera.x,camera.y,camera.z},{0,-3,0},{0,1,0});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mMat=glm::translate(glm::mat4(1.0f),{0.f,-2.f,0.f}); |
|
|
|
|
|
|
|
mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,0.2f}); |
|
|
|
|
|
|
|
mMat=glm::scale(mMat,{7,7,7}); |
|
|
|
mvMat=vMat*mMat; |
|
|
|
mvMat=vMat*mMat; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
invTrMat=glm::transpose(glm::inverse(mvMat)); |
|
|
|
|
|
|
|
|
|
|
|
//DrawSphere();
|
|
|
|
//DrawSphere();
|
|
|
|
|
|
|
|
|
|
|
|
DrawShuttle(); |
|
|
|
DrawShuttle(); |
|
|
|
|
|
|
|
|
|
|
|
mMat=glm::translate(glm::mat4(1.0f),{0.f,-3.f,0.f}); |
|
|
|
|
|
|
|
mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,0.4f}); |
|
|
|
glProgramUniform4fv(renderingProgram, mAmbLoc, 1, matAmb2); |
|
|
|
mMat=glm::scale(mMat,{3,0.1,3}); |
|
|
|
glProgramUniform4fv(renderingProgram, mDiffLoc, 1, matDif2); |
|
|
|
|
|
|
|
glProgramUniform4fv(renderingProgram, mSpecLoc, 1, matSpe2); |
|
|
|
|
|
|
|
glProgramUniform1f(renderingProgram, mShinLoc, matShi2); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mMat=glm::translate(glm::mat4(1.0f),{4.f,2.f,0.f}); |
|
|
|
|
|
|
|
mMat=glm::rotate(mMat,float(currentTime/4),{1.f,0.f,0.f}); |
|
|
|
|
|
|
|
mMat=glm::scale(mMat,{3,3,3}); |
|
|
|
mvMat=vMat*mMat; |
|
|
|
mvMat=vMat*mMat; |
|
|
|
|
|
|
|
|
|
|
|
//DrawTorus();
|
|
|
|
invTrMat=glm::transpose(glm::inverse(mvMat)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DrawTorus(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int main(void) { |
|
|
|
int main(void) { |
|
|
|