步驟
1-初始化:
GLFW窗口,GLAD
2-計算球體頂點:
通過數學方法計算球體的每個頂點座標
3-數據處理:
通過球體頂點座標構造三角形網絡,生成並綁定VAO&VBO&EBO(準備再GPU中進行處理),設置頂點屬性指針(本質上就是告訴OpenGL如何處理數據)
4-着色器:
給出頂點和片段着色器,然後鏈接爲着色器程序,渲染時使用着色器程序
5-渲染:
使用畫線模式畫圓,開啓面剔除,剔除背面,使用線框模式畫球,清空緩衝,交換緩衝區檢查觸發事件
6-結束:
釋放資源
結果
填充模式和線框模式效果對比
開啓面剔除和線框模式效果對比
只需要展示一個面,否則會有重合,此處剔除背面爲例
代碼
此處只給出main.cpp,具體工程參考本人的github
main.cpp
/*
步驟:
1-初始化: GLFW窗口,GLAD
2-計算球體頂點:通過數學方法計算球體的每個頂點座標
3-數據處理: 通過球體頂點座標構造三角形網絡,生成並綁定VAO&VBO&EBO(準備再GPU中進行處理),設置頂點屬性指針(本質上就是告訴OpenGL如何處理數據)
4-着色器:給出頂點和片段着色器,然後鏈接爲着色器程序,渲染時使用着色器程序
5-渲染:使用畫線模式畫圓,開啓面剔除,剔除背面,使用線框模式畫球,清空緩衝,交換緩衝區檢查觸發事件
6-結束:釋放資源
*/
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "Shader.h"
#include <iostream>
#include <math.h>
#include <vector>
const unsigned int screen_width = 780;
const unsigned int screen_height = 780;
const GLfloat PI= 3.14159265358979323846f;
//將球橫縱劃分成50*50的網格
const int Y_SEGMENTS = 50;
const int X_SEGMENTS = 50;
int main()
{
/*1-初始化*/
//初始化GLFW
glfwInit();//初始化GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//opengl版本號3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);// 次版本號3
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//使用核心模式(無序向後兼容性)
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);//如果使用的是Mac OS X系統,需加上這行
glfwWindowHint(GLFW_RESIZABLE, false);//不可改變窗口大小
//創建窗口(寬、高、窗口名稱)
auto window = glfwCreateWindow(screen_width, screen_height,"Sphere",nullptr,nullptr);
//檢測窗口是否創建成功
if (window==nullptr)
{
std::cout << "Failed to Create OpenGL Context" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);//將窗口的上下文設置爲當前進程的主上下文
//初始化GLAD,加載OpenGL指針地址的函數
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout<<"Failed to initialize GLAD"<<std::endl;
return -1;
}
//指定當前視口尺寸(前兩個參數爲左下角位置,後兩個參數是渲染窗口寬、高)
glViewport(0, 0, screen_width, screen_height);
std::vector<float> sphereVertices;
std::vector<int> sphereIndices;
/*2-計算球體頂點*/
//生成球的頂點
for (int y=0;y<=Y_SEGMENTS;y++)
{
for (int x=0;x<=X_SEGMENTS;x++)
{
float xSegment = (float)x / (float)X_SEGMENTS;
float ySegment = (float)y / (float)Y_SEGMENTS;
float xPos = std::cos(xSegment * 2.0f * PI) * std::sin(ySegment * PI);
float yPos = std::cos(ySegment * PI);
float zPos = std::sin(xSegment * 2.0f * PI) * std::sin(ySegment * PI);
sphereVertices.push_back(xPos);
sphereVertices.push_back(yPos);
sphereVertices.push_back(zPos);
}
}
//生成球的Indices
for (int i=0;i<Y_SEGMENTS;i++)
{
for (int j=0;j<X_SEGMENTS;j++)
{
sphereIndices.push_back(i * (X_SEGMENTS + 1) + j);
sphereIndices.push_back((i + 1) * (X_SEGMENTS + 1) + j);
sphereIndices.push_back((i + 1) * (X_SEGMENTS + 1) + j+1);
sphereIndices.push_back(i* (X_SEGMENTS + 1) + j);
sphereIndices.push_back((i + 1) * (X_SEGMENTS + 1) + j + 1);
sphereIndices.push_back(i * (X_SEGMENTS + 1) + j + 1);
}
}
/*3-數據處理*/
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
//生成並綁定球體的VAO和VBO
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
//將頂點數據綁定至當前默認的緩衝中
glBufferData(GL_ARRAY_BUFFER, sphereVertices.size() * sizeof(float), &sphereVertices[0], GL_STATIC_DRAW);
GLuint element_buffer_object;//EBO
glGenBuffers(1, &element_buffer_object);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer_object);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sphereIndices.size() * sizeof(int), &sphereIndices[0], GL_STATIC_DRAW);
//設置頂點屬性指針
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
//解綁VAO和VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
/*4-着色器*/
Shader shader("task3.vs", "task3.fs");
/*5-渲染*/
//渲染循環
while (!glfwWindowShouldClose(window))
{
//清空顏色緩衝
glClearColor(0.0f, 0.34f, 0.57f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
shader.Use();
//繪製球
//開啓面剔除(只需要展示一個面,否則會有重合)
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glBindVertexArray(VAO);
//使用線框模式繪製
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawElements(GL_TRIANGLES, X_SEGMENTS * Y_SEGMENTS * 6, GL_UNSIGNED_INT, 0);
//點陣模式繪製
//glPointSize(5);
//glDrawElements(GL_POINTS, X_SEGMENTS * Y_SEGMENTS * 6, GL_UNSIGNED_INT, 0);
//交換緩衝並且檢查是否有觸發事件(比如鍵盤輸入、鼠標移動)
glfwSwapBuffers(window);
glfwPollEvents();
}
/*6-結束*/
//刪除VAO和VBO,EBO
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &element_buffer_object);
//清理所有的資源並正確退出程序
glfwTerminate();
return 0;
}