#include<iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include<cmath>
#define pi 3.14159265
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
float ang2rad(float ang) {
return (ang * pi) / 180;
}
float findBiggerRadius(float smallR) {
float a, b, c, x1, x2, d;
float num1 = sin(ang2rad(36)) / sin(ang2rad(126));
a = (1 - (pow(num1, 2)));
b = (2 * num1 * smallR * cos(ang2rad(126)));
c = -(smallR * smallR);
//scanf("%f %f %f",&a,&b,&c);
if (a != 0)
{
d = sqrt(b * b - 4 * a * c);
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);
if (x1 < x2)
return x2;
else
return x1;
}
return -1;
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//将主版本号(Major)和次版本号(Minor)都设为3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//告诉GLFW我们使用的是核心模式(Core-profile)
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))//我们给GLAD传入了用来加载系统相关的OpenGL函数指针地址的函数。GLFW给我们的是glfwGetProcAddress,它根据我们编译的系统定义了正确的函数。
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 500, 500);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
//glUseProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
float v_center = 0.0f;
float radius_small = 0.3f;
float radius_big = findBiggerRadius(radius_small);
float v[35];
float degree = 54.0f;
int i = 0;
while (i < 15) {
v[i++] = v_center + radius_small * cos(ang2rad(degree));
v[i++] = v_center + radius_small * sin(ang2rad(degree));
v[i++] = 1.0f;
degree += 72.0f;
}
degree = 18.0f;
while (i < 30) {
v[i++] = v_center + radius_big * cos(ang2rad(degree));
v[i++] = v_center + radius_big * sin(ang2rad(degree));
v[i++] = 1.0f;
degree += 72.0f;
}
v[i++] = v_center;
v[i++] = v_center;
v[i++] = 1.0f;
/*
unsigned int indices[] = {
// 注意索引从0开始!
// 此例的索引(0,1,2,3)就是顶点数组vertices的下标,
// 这样可以由下标代表顶点组合成矩形
0, 1, 3, // 第一个三角形
1, 2, 3 // 第二个三角形
};
*/
unsigned int indices[] = {
// 注意索引从0开始!
// 此例的索引(0,1,2,3)就是顶点数组vertices的下标,
// 这样可以由下标代表顶点组合成矩形
0, 6, 10,
6, 1, 10,
1, 7, 10,
2, 7, 10,
2, 8, 10,
3, 8, 10,
3, 9, 10,
4, 9, 10,
4, 5, 10,
0, 5, 10
};
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO); {
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(v), v, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_TRUE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
//VAO解绑前不能解绑EBO,如:glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
while (!glfwWindowShouldClose(window))//我们需要在程序中添加一个while循环,我们可以把它称之为渲染循环(Render Loop),它能在我们让GLFW退出前一直保持运行
{
// 输入
processInput(window);
//渲染指令QAQ
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
//glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawElements(GL_TRIANGLES, 30, GL_UNSIGNED_INT, 0);
//检查并调用事件,交换缓冲
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
对其代码进行修改使其从一颗五角星变成一大四小的五颗