这次使用OpenGL画圆,而且中间画一个实心的五角星。

1. 画实心五角:

由于之前使用Polygen画会出现故障,或许是各个GPU硬件也会不一样的,所以使用Polygen画实心五角星并不可靠;

所以这里直接使用三角形画出五角星,不须要Polygen。

2 画圆

由于GLEW里面没有现成的圆形,所以仅仅能使用人工定顶点,然后画圆的方法;

当中的数学原理能够參考这里:http://slabode.exofire.net/circle_draw.shtml

最后得到效果:

非常美丽,非常标准的五角星外加一个圆形。

注:本程序仅仅使用Vertex Buffer,没使用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:画圆的更多相关文章

  1. 【openGL】画圆

    #include "stdafx.h" #include <GL/glut.h> #include <stdlib.h> #include <math ...

  2. 【转】【OPenGL】OPenGL 画图板-- 中点算法画圆

    为了能以任意点为圆心画圆,我们可以把圆心先设为视点(相当于于将其平移到坐标原点),然后通过中点法扫描转换后,再恢复原来的视点(相当于将圆心平移回原来的位置). 圆心位于原点的圆有四条对称轴x=0,y= ...

  3. 《图形学》实验六:中点Bresenham算法画圆

    开发环境: VC++6.0,OpenGL 实验内容: 使用中点Bresenham算法画圆. 实验结果: 代码: #include <gl/glut.h> #define WIDTH 500 ...

  4. 【openGL】画五角星

    #include "stdafx.h" #include <GL/glut.h> #include <stdlib.h> #include <math ...

  5. WebGIS中基于AGS的画圆查询简析以及通过Polygon来构造圆的算法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 某个项目需求中需要在前端进行画圆查询,将圆范围上的多边形要素 ...

  6. ArcGIS JS 学习笔记2 实现仿百度的拖拽画圆

    一.前言 吐槽一下,百度在国内除了百度地图是良心产品外,其他的真的不敢恭维.在上一篇笔记里,我已经实现了自定义的地图测量模块.在百度地图里面(其他地图)都有一个周边搜索的功能,拖拽画一个圆,然后以圆半 ...

  7. canvas入门(画圆)

    1.想在H5上画一个canvas,必须在页面上你需要的地方添加canvas标签, <canvas id="myCanvas"></canvas>   接着需 ...

  8. javascript画直线和画圆的方法(非HTML5的方法)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 【液晶模块系列基础视频】4.1.X-GUI图形界面库-画线画圆等函数简介

    [液晶模块系列基础视频]4.1.X-GUI图形界面库-画线画圆等函数简介 ============================== 技术论坛:http://www.eeschool.org 博客地 ...

随机推荐

  1. VS2010使用EventHandler发邮件

    转:http://blog.csdn.net/alfred_72/article/details/9980279 因为不知道VS2010 Sharepoint 有EventReciver这个添加项,走 ...

  2. ruby函数回调的实现方法

    以前一直困惑ruby不像python,c可以将函数随意传递,然后在需要的时候才去执行.其实本质原因是ruby的函数不是对象. 通过查阅资料发现可以使用如下方法: def func(a, b) puts ...

  3. [Everyday Mathematics]20150209

    设 $f$ 在区间 $I$ 上三阶可导, $f'\neq 0$, 则可定义 $f$ 的 Schwarz 导数: $$\bex S(f,x)=\frac{f'''(x)}{f'(x)}-\frac{3} ...

  4. IIS配置网站(WebServices),局域网都能访问

    IIS配置网站(WebServices),局域网都能访问 前言 上篇说到在本机创建一个WebServices,用控制台应用程序调用WebServices的SayHello方法. http://www. ...

  5. iOS学习网站及大牛网址(实时更新)

    iOS学习网站及大牛网址(实时更新) 学习网站 https://github.com/Tim9Liu9/TimLiu-iOS  自己总结的iOS.mac开源项目及库 https://github.co ...

  6. PHP中的替代语法

    今天看了一下wordpress的代码,里面有些少见的php替代语法, <?php else : ?> <div class="entry-content"> ...

  7. [LeetCode] Add Digits (a New question added)

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  8. sqlserver 中的GUID 全局唯一标识 -摘自网络

    --简单实用全局唯一标识 DECLARE @myid uniqueidentifierSET @myid = NEWID()PRINT 'Value of @myid is: '+ CONVERT(v ...

  9. homework6-更加简单的题目

    又把时间搞错了 以为这次要写客户端程序的博客 没想到这次是“怎么吃” 言归正传 cnblog上面有很多技术博客 http://perhaps.cnblogs.com/archive/2005/08/0 ...

  10. HDU 4911 Inversion (逆序数 归并排序)

    Inversion 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/A Description bobo has a sequen ...