这次使用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. css中position:relative的真正理解

    其实话说一直以来也没真正去理解好position:relative的用法的真实意义. 我想很多人实实在在用的多都是position:relative和position:absolute结合起来一起用的 ...

  2. Calling C++ code from C# z

    http://blogs.msdn.com/b/borisj/archive/2006/09/28/769708.aspx I apologize for the long delay for thi ...

  3. Fixing the Great Wall

    题意: 在一条线上,有n个坏的地方要修机器人修,机器人的移动速度V,若坏的地方立即被修花费ci,若没修,每单位时间增加d,出去机器人的开始位置,求修完n个地方的最小花费. 分析: 非常经典,求解“未来 ...

  4. bzoj1036: [ZJOI2008]树的统计Count 树链剖分+线段树

    入门题 + 熟悉代码 /************************************************************** Problem: 1036 User: 96655 ...

  5. [irving] C# Windows Beep 调用声音文件

    方法一:Console.Beep(); 方法二:可以用Console.WriteLine("/a");来代替Beep(). MSDN:http://msdn.microsoft.c ...

  6. kmeans算法的matlab实践

    把图像中所有的像素点进行RGB聚类分析,然后输出看结果 img = imread('qq.png'); %取出R矩阵,并将这个R矩阵拉成一列 imgR = img(:,:,1); imgR = img ...

  7. Centos yum源更新为阿里云

    阿里云镜像网址 http://mirrors.aliyun.com/   更新步骤 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d ...

  8. Aggregation(1):Blending、Bagging、Random Forest

    假设我们有很多机器学习算法(可以是前面学过的任何一个),我们能不能同时使用它们来提高算法的性能?也即:三个臭皮匠赛过诸葛亮. 有这么几种aggregation的方式: 一些性能不太好的机器学习算法(弱 ...

  9. bzoj 1109 [POI2007]堆积木Klo(LIS)

    [题意] n个数的序列,删除一个数后序列左移,求最后满足i==a[i]的最大个数. [思路] 设最终得到a[i]==i的序列为s,则s应满足: i<j,a[i]<a[j],i-a[i]&l ...

  10. Ubuntu中使用pyUSB读取鼠标或键盘的数据程序

    参考 :http://www.orangecoat.com/how-to/read-and-decode-data-from-your-mouse-using-this-pyusb-hack 要注意的 ...