OpenGL 3:画圆
这次使用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:画圆的更多相关文章
- 【openGL】画圆
#include "stdafx.h" #include <GL/glut.h> #include <stdlib.h> #include <math ...
- 【转】【OPenGL】OPenGL 画图板-- 中点算法画圆
为了能以任意点为圆心画圆,我们可以把圆心先设为视点(相当于于将其平移到坐标原点),然后通过中点法扫描转换后,再恢复原来的视点(相当于将圆心平移回原来的位置). 圆心位于原点的圆有四条对称轴x=0,y= ...
- 《图形学》实验六:中点Bresenham算法画圆
开发环境: VC++6.0,OpenGL 实验内容: 使用中点Bresenham算法画圆. 实验结果: 代码: #include <gl/glut.h> #define WIDTH 500 ...
- 【openGL】画五角星
#include "stdafx.h" #include <GL/glut.h> #include <stdlib.h> #include <math ...
- WebGIS中基于AGS的画圆查询简析以及通过Polygon来构造圆的算法
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 某个项目需求中需要在前端进行画圆查询,将圆范围上的多边形要素 ...
- ArcGIS JS 学习笔记2 实现仿百度的拖拽画圆
一.前言 吐槽一下,百度在国内除了百度地图是良心产品外,其他的真的不敢恭维.在上一篇笔记里,我已经实现了自定义的地图测量模块.在百度地图里面(其他地图)都有一个周边搜索的功能,拖拽画一个圆,然后以圆半 ...
- canvas入门(画圆)
1.想在H5上画一个canvas,必须在页面上你需要的地方添加canvas标签, <canvas id="myCanvas"></canvas> 接着需 ...
- javascript画直线和画圆的方法(非HTML5的方法)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【液晶模块系列基础视频】4.1.X-GUI图形界面库-画线画圆等函数简介
[液晶模块系列基础视频]4.1.X-GUI图形界面库-画线画圆等函数简介 ============================== 技术论坛:http://www.eeschool.org 博客地 ...
随机推荐
- 怎样查看Eclipse是32位还是64位
首先进入到Eclipse的安装目录,如下图: 查找到文件名为"eclipse.ini" 文件,使用文本编辑工具,或记事本打开,如下图: 如图中的红框所示,如果是win32.x8 ...
- 利用Modbus协议读取电能表的数据
1.电脑要有485转232的转换器2.你要看懂DLT_645—1997规约的通讯协议,现在大多电能表厂都会遵行这个通讯协议,DLT_645—1997规约不是最新的通讯协议.就看电表的使用什么通讯协议. ...
- spring3.0.5的aop使用
spring3.0.5开始支持jpa2.0了,但是最近笔者在使用他的的时候发现了3.0.5的包与2.5.5相比,有所精简.其他外部的包,我们需要自己下载. AOP必须的spring包 org.spri ...
- spring aop expression简单说明
<aop:config> <aop:pointcut id="userDAO" expression="execution(public * cn.da ...
- u-boot 源码修改 bootcmd,IP ,BOOTARGS等参数
uboot1.1.6\include\configs\smdk6410.h #define CONFIG_BOOTCOMMAND"nand read 0xc0008000 0x200000 ...
- Generate Parentheses java实现
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 20、内存溢出(Out of Memory)
内存引用(释放强引用) Object obj=new Object(); obj = null; 内存引用(使用软引用) 软引用是主要用于内存敏感的高速缓存.在jvm报告内存不足之前会清 除所 ...
- Jemeter对Oracle数据库性能测试方法
下载Oracle的jdbc数据库驱动包,注意Oracle数据库的版本,这里使用的是:Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 ...
- Leave Morningstar
Today, closed 4++ years developer life (2009.11.17-2014.02.28) in MorningStar Shenzhen Office Austra ...
- 【原】Storm环境搭建
2.Storm环境搭建 单机 ... 集群 ... 搭建Storm开发环境 搭建Storm开发环境主要概括为以下两步: 1.下载Storm发行稳定版,然后解压,最后把解压后的bin/文件所在目录添加到 ...