added moon

This commit is contained in:
xkamikoo 2021-01-16 21:20:03 +01:00
parent a403ea75d6
commit 0ac976fa5c
2 changed files with 28 additions and 12 deletions

View File

@ -26,13 +26,13 @@ GLuint programTex;
GLuint programSun;
Core::Shader_Loader shaderLoader;
Core::RenderContext armContext;
std::vector<Core::Node> arm;
int ballIndex;
GLuint sunTexture;
GLuint earthTexture;
GLuint moonTexture;
obj::Model sphereModel;
Core::RenderContext sphereContext;
@ -43,6 +43,13 @@ glm::vec3 cameraDir;
glm::mat4 cameraMatrix, perspectiveMatrix;
struct Light {
glm::vec3 pos;
glm::vec3 color;
};
std::vector<Light> lights;
void keyboard(unsigned char key, int x, int y)
{
float angleSpeed = 0.1f;
@ -108,11 +115,18 @@ glm::mat4 drawPlanet(float time, glm::vec3 orbit, glm::vec3 translation, glm::ve
return planetModelMatrix;
}
glm::mat4 drawMoon(glm::mat4 planetModelMatrix, float time, glm::vec3 orbit, glm::vec3 translation, glm::vec3 rotation, glm::vec3 scale)
{
glm::mat4 moonModelMatrix = glm::mat4(planetModelMatrix);
moonModelMatrix = glm::rotate(moonModelMatrix, time, orbit);
moonModelMatrix = glm::translate(moonModelMatrix, translation);
moonModelMatrix = glm::rotate(moonModelMatrix, time, rotation);
moonModelMatrix = glm::scale(moonModelMatrix, scale);
return moonModelMatrix;
}
void renderScene()
{
// Aktualizacja macierzy widoku i rzutowania. Macierze sa przechowywane w zmiennych globalnych, bo uzywa ich funkcja drawObject.
// (Bardziej elegancko byloby przekazac je jako argumenty do funkcji, ale robimy tak dla uproszczenia kodu.
// Jest to mozliwe dzieki temu, ze macierze widoku i rzutowania sa takie same dla wszystkich obiektow!)
cameraMatrix = createCameraMatrix();
perspectiveMatrix = Core::createPerspectiveMatrix();
float time = glutGet(GLUT_ELAPSED_TIME) / 1000.f;
@ -120,27 +134,28 @@ void renderScene()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glUseProgram(program);
glm::vec3 lightPos = glm::vec3(2, 0, 2);
//glUniform3f(glGetUniformLocation(program, "light_dir"), 2, 1, 0);
glUniform3f(glGetUniformLocation(program, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glUseProgram(programSun);
glUniform3f(glGetUniformLocation(programSun, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glm::vec3 sunPos = glm::vec3(0.0f, 0.0f, 0.0f);
glm::mat4 sunModelMatrix = glm::mat4(1.0f);
sunModelMatrix = glm::translate(sunModelMatrix, glm::vec3(0, 0, 0));
sunModelMatrix = glm::translate(sunModelMatrix, sunPos);
sunModelMatrix = glm::rotate(sunModelMatrix, time/10.0f, glm::vec3(0.0f, 1.0f, 1.0f));
drawObjectTexture(programSun, sphereContext, sunModelMatrix, glm::vec3(0.8f, 0.5f, 0.1f), sunTexture);
glUseProgram(programTex);
glm::vec3 lightPos = sunPos;
glUniform3f(glGetUniformLocation(programTex, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
glUniform3f(glGetUniformLocation(programTex, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glm::mat4 earth = drawPlanet(time / 5.0f, glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(-3.5f, 0.0f, -3.5f), glm::vec3(0.5f, 0.5f, 0.5f));
glm::mat4 moon = drawMoon(earth, time/2.0f, glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0, 1, 1), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.2f, 0.2f, 0.2f));
earth = glm::rotate(earth, time/3.0f, glm::vec3(0.0f, 0.0f, 1.0f));
drawObjectTexture(programTex, sphereContext, earth, glm::vec3(0.8f, 0.8f, 0.8f), earthTexture);
drawObjectTexture(programTex, sphereContext, moon, glm::vec3(0.9f, 1.0f, 0.9f), moonTexture);
glUseProgram(0);
glutSwapBuffers();
@ -157,6 +172,7 @@ void init()
sphereContext.initFromOBJ(sphereModel);
sunTexture = Core::LoadTexture("textures/sun.png");
earthTexture = Core::LoadTexture("textures/earth2.png");
moonTexture = Core::LoadTexture("textures/moon.png");
}
void shutdown()

BIN
textures/moon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 MiB