OpenGL 3: Circle

Source: Internet
Author: User

This time, OpenGL is used to draw a circle, and a solid pentagram is drawn in the middle.

1. Draw solid five corners:

Since the previous use of polygen painting will fail, maybe the GPU hardware will be different, so the use of polygen to draw a solid five-pointed star is not reliable;

Therefore, polygen is not required to draw a star using a triangle.

2 circles

Because there is no circular shape in glew, we can only manually set the vertex and then draw the circle;

When the mathematical principles can be taken here: http://slabode.exofire.net/circle_draw.shtml

The final result is as follows:



Very beautiful, very standard star plus a circle.


Note: This program only uses vertex buffer and does not use index buffer.

#pragma once#include <stdio.h>#include <GL\glew.h>#include <GL\freeglut.h>#include "math_3d.h"#include <cmath>namespace Tutorial2_Tri_pentagram{GLuint VBO;const static float PI = 3.1415926f;const static double toPI = PI/180.0;const static double penR = 0.8;const static double penInR = penR * sin(18.0*toPI) / sin(126.0*toPI);void DrawCircle(Vector3f *vers, int offset,float cx, float cy, float r, int num_segments) {for(int i = 0; i < num_segments; i++) { //get the current angle float theta = 2.0f * PI * float(i) / float(num_segments);float x = r * cosf(theta);//calculate the x component float y = r * sinf(theta);//calculate the y component vers[offset+i] = Vector3f(x + cx, y + cy, 0.0f);//output vertex }}void createGeometry(){Vector3f vers[10];for (int i = 0, k = 0; i < 10; i+=2, k++){vers[i].x = (float)(penR * cos((18.0+72.0*k)*toPI));vers[i].y = (float)(penR * sin((18.0+72.0*k)*toPI));vers[i].z = 0.0f;vers[i+1].x = (float)(penInR * cos((72.0*k+54.0)*toPI));vers[i+1].y = (float)(penInR * sin((72.0*k+54.0)*toPI));vers[i+1].z = 0.0f;}Vector3f vers2[412] = {vers[0], vers[4], vers[5], vers[0], vers[5], vers[6],vers[2], vers[6], vers[7], vers[2], vers[7], vers[8]};DrawCircle(vers2, 12, 0.0f, 0.0f, (float)penR, 400);glGenBuffers(1, &VBO);glBindBuffer(GL_ARRAY_BUFFER, VBO);glBufferData(GL_ARRAY_BUFFER, sizeof(vers2), vers2, GL_STATIC_DRAW);}static void renderScene(){glClear(GL_COLOR_BUFFER_BIT);glEnableVertexAttribArray(0);glBindBuffer(GL_ARRAY_BUFFER, VBO);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);glDrawArrays(GL_TRIANGLES, 0, 12);glDrawArrays(GL_LINE_LOOP, 12, 400);glDisableVertexAttribArray(0);glutSwapBuffers();}void initCallBack(){glutDisplayFunc(renderScene);}int run(int argc, char **argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);glutInitWindowSize(800, 600);glutInitWindowPosition(50, 50);glutCreateWindow("Bill's Another Pentagram");initCallBack();GLenum res = glewInit();if (res != GLEW_OK){fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));return 1;}createGeometry();glClearColor(0.0f, 0.6f, 0.2f, 0.0f);glutMainLoop();return 0;}}



OpenGL 3: Circle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.