我正在尝试渲染多边形以使行星的轨道可见。
目前,我的卫星和行星正在渲染和绕太阳旋转的单个点。
我的多边形无法渲染。
我没有尝试显示任何可渲染的多边形。
这是我用来计算并尝试渲染多边形的Planet类的方法:
// render this planets orbit circle
void Planet::renderOrbit(glm::vec2 currentPlanetPosition)
{
// draw a line strip
glBegin(GL_LINE_STRIP);
// loop round from 0 to 2*PI and draw around the radius of the orbit using trigonometry
for (float angle = 0.0f; angle < 6.283185307f; angle += 0.05f)
{
glVertex3f(sin(angle) * orbitDistance, cos(angle) * orbitDistance, 0.0f);
}
glVertex3f(0.0f, orbitDistance, 0.0f);
glEnd();
//render the moons' orbit
glPushMatrix();
// translate to the center of this planet to draw the moon orbit around it
glTranslatef(currentPlanetPosition[0], currentPlanetPosition[1], 0.0f);
// draw moon orbits
glPopMatrix();
}
这是我用来绘制我的星球的代码。
void Planet::drawPlanet(glm::vec2 planetPosition, GLfloat UniverseSpeed) {
shader.Use();
//Drawing
glm::mat4 model = glm::mat4(1.0f);
glm::vec2 currentPlanetPosition = calculatePos(planetPosition, UniverseSpeed);
//Relative Position
model = glm::translate(model, glm::vec3(currentPlanetPosition.x, 0.0f,currentPlanetPosition.y));
//Rotation
model = glm::rotate(model, glm::radians((GLfloat)glfwGetTime() * rotationSpeed * UniverseSpeed), glm::vec3(0.0f, 1.0f, 0.0f));
//Scaling
model = glm::scale(model, glm::vec3(planetaryScale, planetaryScale, planetaryScale));
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
planetModel.Draw(shader);
for (Planet moon : listOfPlanets) {
moon.drawPlanet(currentPlanetPosition, UniverseSpeed);
moon.renderOrbit(currentPlanetPosition);
}
}
main.cpp
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
// GLEW
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
// GL includes
#include "Shader.h"
#include "Camera.h"
#include "Model.h"
#include "Planet.h"
// GLM Mathemtics
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// Other Libs
#include "SOIL2/SOIL2.h"
#include <math.h>
#include <list>
// Properties
const GLuint WIDTH = 1200, HEIGHT = 900;
int SCREEN_WIDTH, SCREEN_HEIGHT;
// Function prototypes
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mode);
void MouseCallback(GLFWwindow* window, double xPos, double yPos);
void DoMovement();
// Camera
Camera camera(glm::vec3(0.0f, 100.0f, 0.0f));
bool keys[1024];
GLfloat lastX = 400, lastY = 300;
bool firstMouse = true;
GLfloat deltaTime = 0.0f;
GLfloat lastFrame = 0.0f;
GLfloat UniverseSpeed = 1.0f;
int main()
{
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
if (nullptr == window)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
glfwGetFramebufferSize(window, &SCREEN_WIDTH, &SCREEN_HEIGHT);
// Set the required callback functions
glfwSetKeyCallback(window, KeyCallback);
glfwSetCursorPosCallback(window, MouseCallback);
// GLFW Options
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
if (GLEW_OK != glewInit())
{
std::cout << "Failed to initialize GLEW" << std::endl;
return EXIT_FAILURE;
}
// Define the viewport dimensions
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
// OpenGL options
glEnable(GL_DEPTH_TEST);
// Setup and compile our shaders
Shader shader("./res/shaders/modelLoading.vs", "./res/shaders/modelLoading.frag");
// Load models
//Model ourModel( "./res/models/nanosuit/nanosuit.obj");
//planets
Model earthModel("./res/models/planets/earth/earth.obj");
Model jupiterModel("./res/models/planets/jupiter/jupiter1.obj");
Model marsModel("./res/models/planets/mars/Mars_normal-strong.obj");
Model mercuryModel("./res/models/planets/mercury/mercury.obj");
Model neptuneModel("./res/models/planets/neptune/neptune.obj");
Model saturnModel("./res/models/planets/saturn/saturn3.obj");
Model uranusModel("./res/models/planets/uranus/uranus.obj");
Model venusModel("./res/models/planets/venus/venus.obj");
//sun
Model sunModel("./res/models/planets/Sun/sun2.obj");
//moons
Model moonModel("./res/models/planets/moon/moon1.obj");
//Model sunMoons[] = {earthModel, jupiterModel, marsModel,mercuryModel, moonModel, neptuneModel, saturnModel, sunModel, uranusModel, venusModel};
//view distance 300
glm::mat4 projection = glm::perspective(camera.Zoom, (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 0.1f, 300.0f);
std::list<Planet> noMoons;
std::list<Planet> earthMoons;
std::list<Planet> planets;
////Luna
//radius = 10.0f
//Angle 0.005f
Planet earthMoon = Planet(shader, moonModel, 10.0f, 0.005f, 8.0f * UniverseSpeed, 10000.0f * UniverseSpeed, 0.005f, noMoons);
earthMoons.push_back(earthMoon);
////Mercury
//radius = 70.0f
//Angle 0.008f
Planet mercury = Planet(shader, mercuryModel, 70.0f, 0.01f, 8.0f * UniverseSpeed, 1000.0f * UniverseSpeed, 0.008f, noMoons);
////Venus
//radius = 80.0f
//angle = 0.007f
Planet venus =Planet(shader, venusModel, 80.0f, 0.01f, 8.0f * UniverseSpeed, 1000.0f * UniverseSpeed, 0.007f, noMoons);
////earth
//radius = 90.0f
//angle = 0.006f
Planet earth =Planet(shader, earthModel, 90.0f, 0.01f, 8.0f * UniverseSpeed, 1000.0f * UniverseSpeed, 0.006f, earthMoons);
////Mars
//radius = 100.0f
//angle = 0.005f
Planet mars = Planet(shader, marsModel, 100.0f, 0.01f, 8.0f * UniverseSpeed, 1000.0f * UniverseSpeed, 0.005f, earthMoons);
//// JUPITER
//radius = 120.0f
//angle = 0.0045f
Planet jupiter = Planet(shader, jupiterModel, 120.0f, 0.01f, 8.0f * UniverseSpeed, 1000.0f * UniverseSpeed, 0.0045f, earthMoons);
////Uranus
//radius = 190.0f
//angle = 0.0035f
Planet uranus = Planet(shader, uranusModel, 190.0f, 0.01f, 8.0f * UniverseSpeed, 1000.0f * UniverseSpeed, 0.0035f, noMoons);
////SATURN
//radius = 160.0f
//angle = 0.0040f
Planet saturn = Planet(shader, saturnModel, 160.0f, 0.01f, 8.0f * UniverseSpeed, 1000.0f * UniverseSpeed, 0.004f, noMoons);
//radius = 220.0f
//angle = 0.003f
Planet neptune = Planet(shader, neptuneModel, 220.0f, 0.01f, 8.0f * UniverseSpeed, 1000.0f * UniverseSpeed, 0.003f, noMoons);
////Sun
//radius = 0.0f
//Angle 0.006f
planets.push_back(mercury);
planets.push_back(venus);
planets.push_back(earth);
planets.push_back(mars);
planets.push_back(jupiter);
planets.push_back(uranus);
planets.push_back(saturn);
planets.push_back(neptune);
Planet sun= Planet(shader, sunModel, 0.0f, 0.01f, 16.0f* UniverseSpeed, 0.0f, 0.006f, planets);
// Game loop
while (!glfwWindowShouldClose(window))
{
// Set frame time
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// Check and call events
glfwPollEvents();
DoMovement();
// Clear the colorbuffer
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.Use();
glm::mat4 view = camera.GetViewMatrix();
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
sun.drawPlanet(glm::vec2(0.0f, 0.0f), UniverseSpeed);
// Swap the buffers
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
// Moves/alters the camera positions based on user input
void DoMovement()
{
// Camera controls
if (keys[GLFW_KEY_W] || keys[GLFW_KEY_UP]) {
camera.ProcessKeyboard(FORWARD, deltaTime);
}
if (keys[GLFW_KEY_S] || keys[GLFW_KEY_DOWN]) {
camera.ProcessKeyboard(BACKWARD, deltaTime);
}
if (keys[GLFW_KEY_A] || keys[GLFW_KEY_LEFT]) {
camera.ProcessKeyboard(LEFT, deltaTime);
}
if (keys[GLFW_KEY_D] || keys[GLFW_KEY_RIGHT]) {
camera.ProcessKeyboard(RIGHT, deltaTime);
}
}
// Is called whenever a key is pressed/released via GLFW
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (GLFW_KEY_ESCAPE == key && GLFW_PRESS == action) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (GLFW_KEY_KP_ADD== key ) {
UniverseSpeed = UniverseSpeed * 1.25;
std::cout << UniverseSpeed << std::endl;
}
else if(GLFW_KEY_KP_SUBTRACT == key){
UniverseSpeed = UniverseSpeed / 1.25;
std::cout << UniverseSpeed << std::endl;
}
if (key >= 0 && key < 1024) {
if (action == GLFW_PRESS) {
keys[key] = true;
}
else if (action == GLFW_RELEASE) {
keys[key] = false;
}
}
}
void MouseCallback(GLFWwindow* window, double xPos, double yPos) {
if (firstMouse) {
lastX = xPos;
lastY = yPos;
firstMouse = false;
}
GLfloat xOffset = xPos - lastX;
GLfloat yOffset = lastY - yPos; // Reversed since y-coordinates go from bottom to left
lastX = xPos;
lastY = yPos;
camera.ProcessMouseMovement(xOffset, yOffset);
}
我怀疑多边形的书写方式正确,但是由于某种原因,它最终并没有被渲染。
我在其他项目中将多边形渲染为2D图像,但是从未在3D项目中像这样的轨道上渲染2D对象。
我怀疑我对如何将两个词组合在一起缺乏了解。