国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

OpenGL太陽(yáng)系行星系統(tǒng)簡(jiǎn)單實(shí)現(xiàn)

這篇具有很好參考價(jià)值的文章主要介紹了OpenGL太陽(yáng)系行星系統(tǒng)簡(jiǎn)單實(shí)現(xiàn)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

一、項(xiàng)目簡(jiǎn)介

使用OpenGL(glfw, glad)模擬太陽(yáng)系(地球、月球、太陽(yáng))系統(tǒng),涉及三維圖形變換、坐標(biāo)系統(tǒng),光照模型(待添加)、鍵盤鼠標(biāo)事件處理等內(nèi)容,在main函數(shù)中封裝了絕大部分的OpenGL接口,極大的減少了代碼量,操作簡(jiǎn)便。

二、代碼特點(diǎn)

Shader.h Camera.h與Texture.h封裝著色器、攝像機(jī)與紋理類,其中紋理類實(shí)例化即完成生成紋理與綁定、設(shè)置紋理環(huán)繞方式與過(guò)濾等工作,減少大量代碼量。

VertexArray,VertexBuffer與IndexBuffer封裝VAO, VBO, EBO,類實(shí)例化時(shí)即自動(dòng)生成頂點(diǎn)數(shù)組對(duì)象、頂點(diǎn)緩沖對(duì)象、元素緩沖對(duì)象,并提供綁定與解綁等接口。VertexBufferLayout類可自由設(shè)置頂點(diǎn)屬性。

Sphere類獲得繪制球體所需的所有頂點(diǎn)坐標(biāo)以及索引緩沖數(shù)組,用于實(shí)例化VBO和EBO對(duì)象,可手動(dòng)調(diào)節(jié)設(shè)置球體繪制的精細(xì)程度。

通過(guò)Renderer類的Draw方法實(shí)現(xiàn)球體繪制。

三、源碼實(shí)現(xiàn)及詳細(xì)說(shuō)明

GitHub源碼地址:wonderful2643/SolarSystem (github.com)

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>

#include "Shader.h"
#include "Sphere.h"
#include "Config.h"
#include "Camera.h"
#include "Renderer.h"
#include "Texture.h"

#include "VertexArray.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"
#include "VertexBufferLayout.h"

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);

// camera
Camera camera(glm::vec3(0.0f, 5.0f, 35.0f));
float lastX = WindowWidth / 2.0f;
float lastY = WindowHeight / 2.0f;
bool firstMouse = true;

// timing
float deltaTime = 0.0f;    // time between current frame and last frame
float lastFrame = 0.0f;

int main()
{
  glfwInit();
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  GLFWwindow* window = glfwCreateWindow(WindowWidth, WindowHeight, "Solar System", nullptr, nullptr);

  if (!window)
  {
    std::cout << "Failed to create GLFW window" << std::endl;
    glfwTerminate();
    return -1;
  }

  glfwMakeContextCurrent(window);
  glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  glfwSetCursorPosCallback(window, mouse_callback);
  glfwSetScrollCallback(window, scroll_callback);

  //注釋該行,調(diào)試出bug鍵盤鼠標(biāo)不會(huì)卡死
  //glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  glfwSwapInterval(1);

  if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  {
    std::cout << "Failed to initialize GLAD" << std::endl;
    return -1;
  }

  std::cout << glGetString(GL_VERSION) << std::endl;
  
  //獲得球體所有頂點(diǎn)坐標(biāo)以及EBO數(shù)組
  Sphere mySphere(40);
  std::vector<float> sphereVertices = mySphere.getVertices();
  std::vector<unsigned int> sphereIndices = mySphere.getIndices();

  {
    //VAO
    VertexArray va;
    //VBO
    VertexBuffer vb(&sphereVertices[0], sphereVertices.size() * sizeof(float));

    VertexBufferLayout layout;
    //頂點(diǎn)屬性布局:前三位為球體上點(diǎn)的x, y, z坐標(biāo),后兩位為2D紋理坐標(biāo)
    layout.Push<float>(3);
    layout.Push<float>(2);
    va.AddBuffer(layout);

    IndexBuffer ib(&sphereIndices[0], sphereIndices.size());//放在最后

    glEnable(GL_DEPTH_TEST);

    Shader shader("res/shader/task3.vs", "res/shader/task3.fs");
    shader.Bind();
    shader.setInt("u_Texture", 0);

    Texture textureSun("res/textures/sun.jpg");
    Texture textureEarth("res/textures/earth.jpg");
    Texture textureMoon("res/textures/moon.jpg");

    vb.Unbind();
    va.Unbind();
    ib.Unbind();
    shader.Unbind();

    Renderer render;

    glm::mat4 view = camera.GetViewMatrix();
    glm::mat4 proj = glm::perspective(glm::radians(camera.Zoom), (float)WindowWidth / (float)WindowHeight, 0.1f, 100.0f);

    // 渲染循環(huán)
    while (!glfwWindowShouldClose(window))
    {
      float currentFrame = static_cast<float>(glfwGetTime());
      deltaTime = currentFrame - lastFrame;
      lastFrame = currentFrame;

      processInput(window);

      glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

      va.Bind();
      {
        // Sun
        glm::mat4 model = glm::mat4(1.0f);
        model = glm::translate(model, glm::vec3(0.0f, 1.0f, 0.0f));
        model = glm::rotate(glm::mat4(1.0f), -(float)glfwGetTime() / 5, glm::vec3(0.0f, 1.0f, 0.0f));
        model = scale(model, glm::vec3(sunScale, sunScale, sunScale));
        glm::mat4 mvp = proj * view * model;

        textureSun.Bind();
        shader.Bind();
        shader.setMat4("u_MVP", mvp);

        render.Draw(mySphere.getNumIndices());
      }
      
      {
        // Earth
        glm::mat4 model = glm::mat4(1.0f);
        // 公轉(zhuǎn)
        model = glm::rotate(model, (float)glfwGetTime() / 1.5f, glm::vec3(0.0f, 1.0f, 0.0f));
        model = glm::translate(model, glm::vec3(SunEarthDistance, .0f, .0f));
        // 抵消公轉(zhuǎn)對(duì)自身傾斜方向的影響,保證公轉(zhuǎn)后 仍然向右傾斜
        model = glm::rotate(model, -(float)glfwGetTime(), glm::vec3(0.0f, 1.0f, 0.0f));
        model = glm::rotate(model, -glm::radians(ErothAxialAngle), glm::vec3(0.0, 0.0, 1.0));
        // 自轉(zhuǎn)
        model = glm::rotate(model, -(float)glfwGetTime(), glm::vec3(0.0f, 1.0f, 0.0f));
        glm::mat4 mvp = proj * view * model;

        textureEarth.Bind();
        shader.Bind();
        shader.setMat4("u_MVP", mvp);

        render.Draw(mySphere.getNumIndices());
      }

      {
        // Moon
        glm::mat4 model = glm::mat4(1.0f);
        // 地日公轉(zhuǎn)
        model = glm::rotate(model, (float)glfwGetTime() / 1.5f, glm::vec3(0.0f, 1.0f, 0.0f));
        model = glm::translate(model, glm::vec3(SunEarthDistance, .0f, .0f));
        // 月球公轉(zhuǎn)
        model = glm::rotate(model, (float)glfwGetTime() * 2.0f, glm::vec3(0.0f, 1.0f, 0.0f));
        model = glm::translate(model, glm::vec3(MoonEarthDistance, 0.0, 0.0));
        // 月球自轉(zhuǎn)
        model = glm::rotate(model, -(float)glfwGetTime(), glm::vec3(0.0f, 1.0f, 0.0f));
        model = scale(model, glm::vec3(moonScale, moonScale, moonScale));
        glm::mat4 mvp = proj * view * model;

        textureMoon.Bind();
        shader.Bind();
        shader.setMat4("u_MVP", mvp);

        render.Draw(mySphere.getNumIndices());
      }

      glfwSwapBuffers(window);
      glfwPollEvents();
    }
  }

  glfwTerminate();
  return 0;
}

void processInput(GLFWwindow* window)
{
  if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
    glfwSetWindowShouldClose(window, true);

  if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
    camera.ProcessKeyboard(FORWARD, deltaTime);
  if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
    camera.ProcessKeyboard(BACKWARD, deltaTime);
  if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
    camera.ProcessKeyboard(LEFT, deltaTime);
  if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
    camera.ProcessKeyboard(RIGHT, deltaTime);
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
  glViewport(0, 0, width, height);
}

void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
{
  float xpos = static_cast<float>(xposIn);
  float ypos = static_cast<float>(yposIn);

  if (firstMouse)
  {
    lastX = xpos;
    lastY = ypos;
    firstMouse = false;
  }

  float xoffset = xpos - lastX;
  float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top

  lastX = xpos;
  lastY = ypos;

  camera.ProcessMouseMovement(xoffset, yoffset);
}

// glfw: whenever the mouse scroll wheel scrolls, this callback is called
// ----------------------------------------------------------------------
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
  camera.ProcessMouseScroll(static_cast<float>(yoffset));
}

四、效果展示及項(xiàng)目框架

glfw繪制地球,c++,opencv,Powered by 金山文檔

參考鏈接

C++ 實(shí)現(xiàn)太陽(yáng)系行星系統(tǒng)(OpenGL)

OpenGL畫球面(6) - 邗影 - 博客園 (cnblogs.com)

openGL編程學(xué)習(xí)(3):太陽(yáng)、地球、月亮(含自轉(zhuǎn)和公轉(zhuǎn))和航天飛機(jī)_LynnJute的博客-CSDN博客_glfw實(shí)現(xiàn)日地月

OpenGL入門三——變換進(jìn)階

LearnOpenGL CN

【【譯】TheCherno-OpenGL系列教程】文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-763990.html

到了這里,關(guān)于OpenGL太陽(yáng)系行星系統(tǒng)簡(jiǎn)單實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 基于 H5 與 WebGL 的3D太陽(yáng)系立體展示

    前言 近年來(lái)隨著引力波的發(fā)現(xiàn)、黑洞照片的拍攝、火星上存在水的證據(jù)發(fā)現(xiàn)等科學(xué)上的突破,以及文學(xué)影視作品中諸如《三體》、《流浪地球》、《星際穿越》等的傳播普及,宇宙空間中那些原本遙不可及的事物離我們?cè)絹?lái)越近,人們對(duì)未知文明的關(guān)注和對(duì)宇宙空間的好奇達(dá)

    2024年02月20日
    瀏覽(19)
  • WxGL應(yīng)用實(shí)例:繪制高精度的3D太陽(yáng)系模型

    WxGL應(yīng)用實(shí)例:繪制高精度的3D太陽(yáng)系模型

    天何所沓?十二焉分?日月安屬?列星安陳?—— 屈原 遠(yuǎn)古時(shí)期的人類就對(duì)神秘幽邃的星空充滿了好奇與敬畏。仰望星空,宇宙浩瀚。比宇宙更壯闊的,是人類對(duì)宇宙的不懈追問(wèn)和沉淀在基因中的探索精神。 本文嘗試用WxGL來(lái)回答“日月安屬、列星安陳”這個(gè)古老的問(wèn)題。太

    2023年04月21日
    瀏覽(94)
  • 【計(jì)算機(jī)圖形學(xué)】【實(shí)驗(yàn)報(bào)告】太陽(yáng)系繪制、B樣條曲線繪制(附代碼)

    【計(jì)算機(jī)圖形學(xué)】【實(shí)驗(yàn)報(bào)告】太陽(yáng)系繪制、B樣條曲線繪制(附代碼)

    實(shí) 驗(yàn) 報(bào) 告 一、實(shí)驗(yàn)?zāi)康?掌握三維圖形的顯示原理和方法,掌握三維觀察的原理和方法; 掌握OpenGL中矩陣堆棧函數(shù)的使用,會(huì)使用堆棧函數(shù)進(jìn)行復(fù)雜場(chǎng)景的組裝。 掌握OpenGL中三維觀察變換常用的函數(shù)的使用方法,了解三維模型的貼圖方法; 掌握自由曲線的生成方法,熟練

    2024年02月10日
    瀏覽(31)
  • Visual Studio 2022 搭建GLFW OpenGL開發(fā)環(huán)境

    Visual Studio 2022 搭建GLFW OpenGL開發(fā)環(huán)境

    最近工作需要 需要寫一個(gè)全景的視頻播放器 網(wǎng)上搜了下大概解決方案是 ffmpeg+opengl b站有很多視頻? 按照視頻 搭建了OpenGL的開發(fā)環(huán)境 先去GLFW的網(wǎng)站下載 windows平臺(tái)的庫(kù)文件 為什么使用GLFW? 因?yàn)镚LFW是跨平臺(tái)的? ?我下的是64位版本解壓后有目錄如下 ? ?包含了動(dòng)態(tài)庫(kù)和靜態(tài)庫(kù)

    2024年02月03日
    瀏覽(23)
  • Ubuntu20.04搭建OpenGL環(huán)境(glfw+glad)

    Ubuntu20.04搭建OpenGL環(huán)境(glfw+glad)

    本文在VMware安裝Ubuntu20.04桌面版的環(huán)境下搭建OpenGL, 按照本文搭建完成后可以執(zhí)行LearnOpenGL網(wǎng)站上的demo 。 關(guān)于VMware可自行到VMware Workstation Pro | CN下載 關(guān)于Ubuntu20.04桌面版可自行到官網(wǎng)或Index of /ubuntu-releases/20.04.6/ | 清華大學(xué)開源軟件鏡像站 | Tsinghua Open Source Mirror下載 這里窗口

    2024年02月12日
    瀏覽(29)
  • Visual studio2022 利用glfw+glad配置OpenGL環(huán)境

    Visual studio2022 利用glfw+glad配置OpenGL環(huán)境

    鏈接:https://visualstudio.microsoft.com/zh-hans/ 搜索時(shí)最常見的是glut,但是了解到glut最后更新時(shí)間是1998年,比我還小兩歲…目前沒人維護(hù)了,也被棄用了… 目前,最常用的就是freeglut+glew和glfw+glad兩種組合。據(jù)說(shuō)前者比較經(jīng)典,后者比較新潮,所以作為潮人,我選擇了后者(具體可

    2024年02月04日
    瀏覽(28)
  • 【GIS開發(fā)】基于C++繪制三維數(shù)字地球Earth(OpenGL、glfw、glut)

    【GIS開發(fā)】基于C++繪制三維數(shù)字地球Earth(OpenGL、glfw、glut)

    ??三維數(shù)字地球系列相關(guān)文章如下??: 1 【小沐學(xué)GIS】基于C++繪制三維數(shù)字地球Earth(OpenGL、glfw、glut)第一期 2 【小沐學(xué)GIS】基于C++繪制三維數(shù)字地球Earth(OpenGL、glfw、glut)第二期 3 【小沐學(xué)GIS】基于OpenSceneGraph(OSG)繪制三維數(shù)字地球Earth 4 【小沐學(xué)GIS】基于C++繪制太陽(yáng)系

    2023年04月17日
    瀏覽(55)
  • OpenGL —— 2.5、繪制第一個(gè)三角形(附源碼,glfw+glad)(更新:紋理貼圖)

    OpenGL —— 2.5、繪制第一個(gè)三角形(附源碼,glfw+glad)(更新:紋理貼圖)

    源碼效果 ? C++源碼 ? ?????紋理圖片 ????? ?????需下載stb_image.h這個(gè)解碼圖片的庫(kù),該庫(kù)只有一個(gè)頭文件。 ? ????? 具體代碼: ?????????? vertexShader.glsl ? ?????????? fragmentShader.glsl ? ?????????? main.c ? ???

    2024年02月11日
    瀏覽(22)
  • Android OpenGL ES實(shí)現(xiàn)簡(jiǎn)單綠幕摳圖

    Android OpenGL ES實(shí)現(xiàn)簡(jiǎn)單綠幕摳圖

    目錄 正文 OES Filter BlendShader Filter 最后的效果 缺陷 實(shí)現(xiàn)綠幕摳圖,其實(shí)想法很簡(jiǎn)單。 這里簡(jiǎn)單粗暴的使用著色器替換。 OES Filter 直接實(shí)現(xiàn)在相機(jī)預(yù)覽上的Shader ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #extension GL_OES_EGL_image_external : require precision mediump float ; ???????

    2024年02月13日
    瀏覽(22)
  • 太陽(yáng)輻射環(huán)境模擬系統(tǒng)系統(tǒng)

    太陽(yáng)輻射環(huán)境模擬系統(tǒng)系統(tǒng)

    太陽(yáng)輻射環(huán)境模擬系統(tǒng)是一種高度專業(yè)化的設(shè)備,用于模擬太陽(yáng)光的全譜段輻射,包括紫外線、可見光和紅外線。這種系統(tǒng)的核心功能是在實(shí)驗(yàn)室條件下復(fù)制太陽(yáng)的輻射條件,以評(píng)估材料、產(chǎn)品或設(shè)備在實(shí)際太陽(yáng)輻射影響下的性能和耐久性。 ? 光伏電池測(cè)試:評(píng)估太陽(yáng)能電池

    2024年03月10日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包