-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrawCircle.cpp
100 lines (75 loc) · 2.37 KB
/
DrawCircle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "pch.h"
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <vector>
#include "toys.h"
#define PI 3.14159265358979323846264338327950288
using namespace glm;
using namespace std;
void render(GLFWwindow* window);
void init();
int main(void)
{
if (!glfwInit()) {
exit(EXIT_FAILURE);
}
#ifdef __APPLE__
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
GLFWwindow* window = glfwCreateWindow(640, 480, "Starbead", NULL, NULL);
glfwMakeContextCurrent(window);
glewInit();
init();
while (!glfwWindowShouldClose(window)) {
render(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
}
GLuint vertexBuffer = 0;
GLuint vertexArray = 0;
GLuint elementArray = 0;
vector<vec3> vertices;
vector<u32vec3> triangles;
Program prog;
float theta = 0.0;
void init() {
prog.loadShaders("shader.vert", "shader.frag");
vertices.push_back(vec3(0, 0, 0));
for (int i = 0; i < 81; i++) {
vertices.push_back(vec3(0.45*cos(theta), 0.6*sin(theta), 0));
theta = theta + 0.025*PI;
}
for (int i = 0; i < 81; i++) {
triangles.push_back(u32vec3(0, i + 1, i + 2));
}
glGenVertexArrays(1, &vertexArray);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(vec3), vertices.data(), GL_STATIC_DRAW);
glBindVertexArray(vertexArray);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, nullptr);
glGenBuffers(1, &elementArray);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArray);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangles.size() * sizeof(u32vec3), triangles.data(), GL_STATIC_DRAW);
}
void render(GLFWwindow* window) {
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(prog.programID);
glBindVertexArray(vertexArray);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArray);
glDrawElements(GL_TRIANGLES, triangles.size() * 3, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
}