Skip to content

adonis的第一次作业 #47

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: hw01
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 67 additions & 29 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,62 @@
#include <iostream>
#include <cstring>
#include <cstdlib>
//using namespace std;

static void render() {
//��������ɫ��Բ�����꣬��ʼ������
static void render(float r, float g, float b, float x, float y, float start, float end) {
glBegin(GL_TRIANGLES);
constexpr int n = 1000;
constexpr float pi = 3.1415926535897f;
float radius = 0.4f;//��뾶
float inner_radius = 0.15f;//�ڰ뾶
float limit = n * end;
for (float i = n * start; i < limit; i++) {
float angle = i / (float)n * pi * 2;
float angle_next = (i + 1) / (float)n * pi * 2;
glColor3f(r, g, b);
glVertex3f(radius * sinf(angle) + x, radius * cosf(angle) + y, 0.0f);
glVertex3f(radius * sinf(angle_next) + x, radius * cosf(angle_next) + y, 0.0f);
glVertex3f(inner_radius * sinf(angle) + x, inner_radius * cosf(angle) + y, 0.0f);

glVertex3f(inner_radius * sinf(angle_next) + x, inner_radius * cosf(angle_next) + y, 0.0f);
glVertex3f(inner_radius * sinf(angle) + x, inner_radius * cosf(angle) + y, 0.0f);
glVertex3f(radius * sinf(angle_next) + x, radius * cosf(angle_next) + y, 0.0f);
}
CHECK_GL(glEnd());
}

//--Բͼ��--������ڷ�~�������ε���������3nʱ����ӵ��¸�ѭ����eg:123 412 341 234 ...
static void render_round(){
glBegin(GL_TRIANGLES);
constexpr int n = 1000;//�޸�n��ò�ͬͼ��
constexpr float pi = 3.1415926535897f;
float radius = 0.5f;
float inner_radius = 0.25f;
static int x = 0;
x++;
if (x > n)
x -= n;
for (int i = 0; i < x; i++) {
float angle = i / (float)n * pi * 2;
float angle_next = (i + 1) / (float)n * pi * 2;
glColor3f(1.0f, 1.0f, 1.0f);//��
glVertex3f(0.0f, 0.0f, 0.0f);
glColor3f(1.0f, 0.0f, 1.0f);//��
glVertex3f(radius * sinf(angle), radius * cosf(angle), 0.0f);
glColor3f(1.0f, 1.0f, 0.0f);//��
glVertex3f(radius * sinf(angle_next), radius * cosf(angle_next), 0.0f);
glColor3f(0.0f, 1.0f, 1.0f);//��
glVertex3f(inner_radius * sinf(angle), inner_radius * cosf(angle), 0.0f);
glVertex3f(inner_radius * sinf(angle_next), inner_radius * cosf(angle_next), 0.0f);
/* glVertex3f(inner_radius * sinf(angle), inner_radius * cosf(angle), 0.0f);
glVertex3f(radius * sinf(angle_next), radius * cosf(angle_next), 0.0f); */
}
CHECK_GL(glEnd());
}
//---��ɫ����----
static void render_triangle()
{
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
Expand All @@ -16,32 +70,11 @@ static void render() {
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
CHECK_GL(glEnd());
/* glBegin(GL_TRIANGLES); */
/* constexpr int n = 100; */
/* constexpr float pi = 3.1415926535897f; */
/* float radius = 0.5f; */
/* float inner_radius = 0.25f; */
/* static int x = 0; */
/* x++; */
/* if (x > n) */
/* x -= n; */
/* for (int i = 0; i < x; i++) { */
/* float angle = i / (float)n * pi * 2; */
/* float angle_next = (i + 1) / (float)n * pi * 2; */
/* glVertex3f(0.0f, 0.0f, 0.0f); */
/* glVertex3f(radius * sinf(angle), radius * cosf(angle), 0.0f); */
/* glVertex3f(radius * sinf(angle_next), radius * cosf(angle_next), 0.0f); */
/* glVertex3f(inner_radius * sinf(angle), inner_radius * cosf(angle), 0.0f); */
/* glVertex3f(inner_radius * sinf(angle_next), inner_radius * cosf(angle_next), 0.0f); */
/* glVertex3f(inner_radius * sinf(angle), inner_radius * cosf(angle), 0.0f); */
/* glVertex3f(radius * sinf(angle_next), radius * cosf(angle_next), 0.0f); */
/* } */
/* CHECK_GL(glEnd()); */
}

int main() {
if (!glfwInit()) {
const char *errmsg;
if (!glfwInit()) {//��ʼ��
const char* errmsg;
glfwGetError(&errmsg);
if (!errmsg) errmsg = "(no error)";
std::cerr << "failed to initialize GLFW: " << errmsg << '\n';
Expand All @@ -61,18 +94,20 @@ int main() {
}

// Create window
GLFWwindow *window = glfwCreateWindow(640, 640, "Example", NULL, NULL);
GLFWwindow* window = glfwCreateWindow(640, 640, "Example", NULL, NULL);
if (!window) {
const char *errmsg;
const char* errmsg;
glfwGetError(&errmsg);
if (!errmsg) errmsg = "(no error)";
std::cerr << "GLFW failed to create window: " << errmsg << '\n';
std::cerr << "==============================================\n";
if (!strcmp(errmsg, "X11: The DISPLAY environment variable is missing")) {
std::cerr << "You seems not running with graphic display\n";
} else if (!strcmp(errmsg, "WGL: The driver does not appear to support OpenGL")) {
}
else if (!strcmp(errmsg, "WGL: The driver does not appear to support OpenGL")) {
std::cerr << "Please consider install an OpenGL driver, or use the mesa driver\n";
} else if (!strcmp(errmsg, "WGL: Failed to create OpenGL context")) {
}
else if (!strcmp(errmsg, "WGL: Failed to create OpenGL context")) {
std::cerr << "Your driver seems not supporting the required OpenGL version\n";
}
std::cerr << "- If you have a physical graphic card (e.g. NVIDIA), install it from your graphic card vendor official website: http://www.nvidia.com/Download/index.aspx\n";
Expand Down Expand Up @@ -108,7 +143,10 @@ int main() {
while (!glfwWindowShouldClose(window)) {
// render graphics
CHECK_GL(glClear(GL_COLOR_BUFFER_BIT));
render();
render_round();//��Բ
render(1.0f, 0.0f, 0.0f, 0.0f, 0.4f, (float)7 / 12, (float)17 / 12);
render(0.0f, 1.0f, 0.0f, -0.46f, -0.4f, 0.25f, (float)13 / 12);
render(0.0f, 0.0f, 1.0f, 0.46f, -0.4f, (float)1/12, (float)11/12);
// refresh screen
glfwSwapBuffers(window);
glfwPollEvents();
Expand Down