Render a sphere / Earth

master
sigonasr2 1 year ago
parent a14516f74f
commit 05e821adc3
  1. BIN
      LoadingModels/earth.jpg
  2. 46
      LoadingModels/main.cpp
  3. 2
      LoadingModels/vertShader.glsl
  4. BIN
      Models/brick1.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 836 KiB

@ -54,17 +54,18 @@ void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed
glGenBuffers(numVBOs, vbo); glGenBuffers(numVBOs, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, pvalues.size()*sizeof(float), vertices.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, pvalues.size()*4, &pvalues[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, tvalues.size()*sizeof(float), tvalues.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, tvalues.size()*4, &tvalues[0],GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
glBufferData(GL_ARRAY_BUFFER, nvalues.size()*sizeof(float), nvalues.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, nvalues.size()*4, &nvalues[0], GL_STATIC_DRAW);
} }
void setupTextures(){ void setupTextures(){
pyrTex=utils::loadTexture("brick1.jpg"); pyrTex=utils::loadTexture("earth.jpg");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,pyrTex); glBindTexture(GL_TEXTURE_2D,pyrTex);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
@ -74,11 +75,24 @@ 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,2.0f,-10.0f}; camera={0.0f,0.0f,-10.0f};
setupVertices(); setupVertices();
setupTextures(); setupTextures();
} }
void DrawSphere(){
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat));
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
glBindBuffer(GL_ARRAY_BUFFER,vbo[0]);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,vbo[1]);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,vbo[2]);
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,0,0);
glDrawArrays(GL_TRIANGLES,0,sphere.getNumIndices());
}
double lastTime=0; double lastTime=0;
void display(GLFWwindow* window, double currentTime) { void display(GLFWwindow* window, double currentTime) {
@ -86,7 +100,7 @@ void display(GLFWwindow* window, double currentTime) {
lastTime=currentTime; lastTime=currentTime;
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
glClearColor(0,0,0.2,1); glClearColor(0.2,0,0.2,1);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(renderingProgram); glUseProgram(renderingProgram);
@ -94,6 +108,7 @@ void display(GLFWwindow* window, double currentTime) {
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
mvLoc=glGetUniformLocation(renderingProgram,"mv_matrix"); mvLoc=glGetUniformLocation(renderingProgram,"mv_matrix");
projLoc=glGetUniformLocation(renderingProgram,"proj_matrix"); projLoc=glGetUniformLocation(renderingProgram,"proj_matrix");
@ -104,23 +119,12 @@ void display(GLFWwindow* window, double currentTime) {
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),{0.f,0.f,0.f});
mMat=glm::translate(mMat,{0.f,0.f,0.f}); mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,0.2f});
mMat=glm::scale(mMat,{5,5,5});
mvMat=vMat*mMat; mvMat=vMat*mMat;
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat)); DrawSphere();
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
glBindBuffer(GL_ARRAY_BUFFER,vbo[0]);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,vbo[1]);
glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,vbo[2]);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glActiveTexture(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,pyrTex);
glDrawArrays(GL_TRIANGLES,0,sphere.getNumIndices());
} }
int main(void) { int main(void) {

@ -1,9 +1,9 @@
#version 430 #version 430
layout (location=0) in vec3 position; layout (location=0) in vec3 position;
layout (location=1) in vec2 texCoords; layout (location=1) in vec2 texCoords;
layout (location=2) in vec3 normals;
uniform mat4 mv_matrix; uniform mat4 mv_matrix;
uniform mat4 proj_matrix; uniform mat4 proj_matrix;
out vec4 varyingColor;
out vec2 uv; out vec2 uv;
mat4 buildTranslate(float x, float y, float z); mat4 buildTranslate(float x, float y, float z);

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 KiB

Loading…
Cancel
Save