openGL绘制地球

网友投稿 308 2022-09-23

openGL绘制地球

openGL系列文章目录

前言

我们可以系统地使用圆的几何知识来通过算法建立球体模型。我们的策略如下。 (1)在整个球体上,选择表示一系列圆形“水平切片”的精度。见图2 的左侧。 (2)将每个圆形切片的圆周细分为若干个点。见图6.2 的右侧。更多的点和水平切片可 以生成更精确、更平滑的球体模型。在我们的模型中,每个切片将具有相同数量的点。 (3)将顶点分组为三角形。一种方法是逐步遍历顶点,在每一步构建两个三角形。例如, 当我们沿着图6.3 中球体上5 个彩色顶点这一行移动时,对于这5 个顶点中的每一个,我们构建了以相应颜色显示的两个三角形

一、代码

#include "glew/glew.h"#include "glfw/glfw3.h"#include "glm/glm.hpp"#include "glm/gtc/matrix_transform.hpp"#include "glm/gtc/type_ptr.hpp"#include "Sphere.h"#include "Utils.h"#include #include #include using namespace std;static const int screen_width = 1920;static const int screen_height = 1080;int width = 0, height = 0;float aspect = 0.f;float cameraX = 0.f, cameraY = 0.f, cameraZ = 0.f;float sphereLocX = 0.f, spherelocY = 0.f, sphereLocZ = 0.f;GLuint renderingProgram = 0;static const int numberVAOs = 1;static const int numberVBOs = 3;GLuint vao[numberVAOs] = { 0 };GLuint vbo[numberVBOs] = { 0 };glm::mat4 mMat(1.f), vMat(1.f), mvMat(1.f), pMat(1.f);int mvLoc = 0;int projLoc = 0;float rotAmt = 0.f;GLuint earthTextureId = 0;Sphere earth = Sphere(48);void setupVertices(){ vector ind = earth.getIndices(); vector vert = earth.getVertices(); vector tex = earth.getTexCoords(); vector norm = earth.getNormals(); vector tang = earth.getTangents(); vector pValues; vector tValues; vector nValues; int numIndices = earth.getNumIndices(); for (int i=0; i

顶点着色器

#version 460 corelayout(location = 0) in vec3 position;layout(location = 1) in vec2 texCoords;uniform mat4 mv_matrix;uniform mat4 proj_matrix;//uniform sampler2D samp;layout (binding = 0) uniform sampler2D samp;//out vec4 color;out vec2 tc;void main(){ gl_Position = proj_matrix * mv_matrix * vec4(position, 1.f); tc = texCoords;}

片元着色器

#version 460 corein vec2 tc;out vec4 color;uniform mat4 mv_matrix;uniform mat4 proj_matrix;layout(binding = 0) uniform sampler2D samp;void main(){ color = texture(samp, tc);}

二、运行效果

源码下载

​​源码工程下载​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:glGetShaderiv函数详解
下一篇:万能的大熊:从剁手到种草,从花钱到赚钱,这个双11和往年不一样!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~