OpenGL基础代码整理
3-1:画点,连成线
// OPENGL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
void init(void)
{
glClearColor(1.0, 1.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-10.0, 10.0, -10.0, 10.0);
}
void lineSegment(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.4, 0.2);
glBegin(GL_LINES);
glVertex2i(10,10);
glVertex2i(-10,-10);
glVertex2i(-10, 0);
glVertex2i(10, 0);
glVertex2i(0, -10);
glVertex2i(0, 10);
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(800, 400);
glutCreateWindow("第一个OpenGL程序");
init();//设置背景,坐标系
glutDisplayFunc(lineSegment);//
glutMainLoop();
return 0;
}
4-1
// OPENGL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define Pi 3.1415926
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-200.0, 200.0, -200.0, 200.0);
}
//rho = a(1-sint(theta))
//x = a(1 - sin(t))*cos(t)
//y = a(1 - sin(t))*sin(t)
void circleDisplay(void)
{
float t;
glClear(GL_COLOR_BUFFER_BIT);
srand((unsigned)time(NULL));
glColor3f(1.0, 0.0, 0.0);
glLineWidth(10.0);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 1000; i++)
{
t = -1 * Pi / 2+ 2 * i * Pi / 1000;
glVertex2f(100*(1 - sin(t))*cos(t), 100*(1 - sin(t))*sin(t));
}
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50,100);
glutInitWindowSize(800, 800);
glutCreateWindow("第一个OpenGL程序");
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-200.0, 200.0, -200.0, 200.0);
glutDisplayFunc(circleDisplay);
glutMainLoop();
return 0;
}
4-2
// OPENGL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define Pi 3.1415926
float a = 60.0;
void circleDisplay(void)
{
double rr, gg, bb;
float x, y;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glLineWidth(5.0);
//cout << "a=" << a << endl;
glBegin(GL_LINE_STRIP);
for (int i = 0; i <= 10000; i++)
{
x = -5 + 10.0 * i / 10000;
glVertex2f(x, pow(x * x, 1.0 / 3.0) + 0.9 * sqrt(3.3 - x * x) * sin(a * Pi * x));
//glVertex2f(x, x * x);
}
glEnd();
glFlush();
}
void SpecialKeys(int key, int x, int y)
{
if (key == GLUT_KEY_UP)
a = a + 0.25;
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50,100);
glutInitWindowSize(800, 800);
glutCreateWindow("第一个OpenGL程序");
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-3.0, 3.0, -3.0, 3.0);
glutDisplayFunc(circleDisplay);
//glutSpecialFunc(SpecialKeys);
glutMainLoop();
return 0;
}
4-3:point
// OPENGL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define Pi 3.1415926
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-200.0, 200.0, -200.0, 200.0);
}
void circleDisplay(void)
{
double rr, gg, bb;
float x, y;
glClear(GL_COLOR_BUFFER_BIT);
srand((unsigned)time(NULL));
glPointSize(10.0);
glBegin(GL_POINTS);
for (int i = 0; i < 1000; i++)
{
rr = rand() / double(RAND_MAX);
gg = rand() / double(RAND_MAX);
bb = rand() / double(RAND_MAX);
x = (rand() % (400 + 1)) - 200;
y = (rand() % (400 + 1)) - 200;
glColor3d(rr, gg, bb);
glVertex2f(x, y);
}
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50,100);
glutInitWindowSize(800, 800);
glutCreateWindow("第一个OpenGL程序");
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-200.0, 200.0, -200.0, 200.0);
glutDisplayFunc(circleDisplay);
glutMainLoop();
return 0;
}
5-1:bitmap显示字体
// OPENGL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define Pi 3.1415926
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-200.0, 200.0, -200.0, 200.0);
}
void circleDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glRasterPos2i(0,0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'A');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'B');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'C');
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'D');
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50,100);
glutInitWindowSize(800, 800);
glutCreateWindow("第一个OpenGL程序");
init();
glutDisplayFunc(circleDisplay);
glutMainLoop();
return 0;
}
6-1:blending
// OPENGL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define Pi 3.1415926
int windowWidth = 600, windowHeight = 600;
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_BLEND);
//glBlendFunc(GL_ONE, GL_ZERO);// sFactor, dFactor
//glBlendFunc(GL_ZERO, GL_ONE);// sFactor, dFactor
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendFunc(GL_ONE, GL_ONE);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1.0, 0.0, 0.0, 0.9);
glRecti(-50, -50, 100, 100);
glColor4f(0.0, 1.0, 0.0, 0.9);
glRecti(-100, -100, 50, 50);
glFlush();
}
void windowReshape(int w, int h)
{
float aspectRatio;
windowWidth = w;
windowHeight = h;
if (windowHeight == 0) windowHeight = 1;
aspectRatio = (float)windowWidth / (float)windowHeight;
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (windowWidth >= windowHeight)
gluOrtho2D(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0);
else gluOrtho2D(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("第一个OpenGL程序");
init();
glutReshapeFunc(windowReshape);
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
6-2:reshape
// OPENGL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define Pi 3.1415926
int windowWidth = 600, windowHeight = 600;
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glRectf(-50.0,-50.0, 50.0, 50.0);
glFlush();
}
void windowReshape(int w, int h)
{
float aspectRatio;
windowWidth = w;
windowHeight = h;
aspectRatio = (float)windowWidth / (float)windowHeight;
//if (windowHeight == 0) windowHeight = 1;
//aspectRatio = (float)windowWidth / (float)windowHeight;
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (windowWidth >= windowHeight)
gluOrtho2D(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0);
else gluOrtho2D(-100.0, 100.0, -100.0 / aspectRatio, 100.0/aspectRatio);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("第一个OpenGL程序");
init();
glutReshapeFunc(windowReshape);
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
6-3:smooth
// OPENGL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define Pi 3.1415926
int windowWidth = 600, windowHeight = 600;
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
//glEnable(GL_BLEND);
//glBlendFunc(GL_ONE, GL_ZERO);// sFactor, dFactor
//glBlendFunc(GL_ZERO, GL_ONE);// sFactor, dFactor
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendFunc(GL_ONE, GL_ONE);
}
void myDisplay(void)
{
double rr, gg, bb;
float t, x, y;
srand((unsigned)time(NULL));
glClear(GL_COLOR_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
glBegin(GL_POLYGON);
glVertex2f(0.0, 0.0);
for (int i = 0; i <= 20; i++)
{
rr = rand() / double(RAND_MAX);
gg = rand() / double(RAND_MAX);
bb = rand() / double(RAND_MAX);
t = Pi *i / 20;
x = 60.0*cos(t);
y = 60.0*sin(t);
glColor3d(rr, gg, bb);
glVertex2f(x, y);
}
glEnd();
glFlush();
}
void windowReshape(int w, int h)
{
float aspectRatio;
windowWidth = w;
windowHeight = h;
if (windowHeight == 0) windowHeight = 1;
aspectRatio = (float)windowWidth / (float)windowHeight;
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (windowWidth >= windowHeight)
gluOrtho2D(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0);
else gluOrtho2D(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("第一个OpenGL程序");
init();
glutReshapeFunc(windowReshape);
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
7-1:antialiasing 消除锯齿
#include "stdafx.h"
#define Pi 3.1415926
int windowWidth = 600, windowHeight = 600;
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glLineWidth(200.0);
glBegin(GL_LINES);
glVertex2i(-5, -1);
glVertex2i(5, 1);
glEnd();
glFlush();
}
void windowReshape(int w, int h)
{
float aspectRatio;
windowWidth = w;
windowHeight = h;
aspectRatio = (float)windowWidth / (float)windowHeight;
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (windowWidth >= windowHeight)
gluOrtho2D(-10.0*aspectRatio, 10.0*aspectRatio, -10.0, 10.0);
else gluOrtho2D(-10.0, 10.0, -10.0 / aspectRatio, 10.0 / aspectRatio);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("第一个OpenGL程序");
init();
glutReshapeFunc(windowReshape);
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
7-2:fill area
#include "stdafx.h"
#define Pi 3.1415926
int windowWidth = 600, windowHeight = 600;
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0, 0.0, 0.0);
//glBegin(GL_TRIANGLES);
//glVertex2i(-50, -50);
//glVertex2i(50, -50);
//glVertex2i(0, 50);
//glEnd();
//glColor3f(1.0, 0.0, 0.0);
//glBegin(GL_POLYGON);
//glVertex2i(-50, 0); glVertex2i(-25, -50);
//glVertex2i(25, -50); glVertex2i(50, 0);
//glVertex2i(25, 50); glVertex2i(-25, 50);
//glEnd();
//glShadeModel(GL_SMOOTH);//GL_FLAT
//glBegin(GL_TRIANGLES);
//glColor3f(1.0, 0.0, 0.0); glVertex2i(-50, -50);
//glColor3f(0.0, 1.0, 0.0); glVertex2i(50, -50);
//glColor3f(0.0, 0.0, 1.0); glVertex2i(0, 50);
//glEnd();
//glShadeModel(GL_SMOOTH);//GL_FLAT
//glBegin(GL_POLYGON);
//glColor3ub(255, 0, 0); glVertex2i(-50, 0);
//glColor3ub(255, 97, 0); glVertex2i(-25, -50);
//glColor3ub(255, 255, 0); glVertex2i(25, -50);
//glColor3ub(0, 255, 0); glVertex2i(50, 0);
//glColor3ub(0, 255, 255); glVertex2i(25, 50);
//glColor3ub(0, 0, 255); glVertex2i(-25, 50);
//glEnd();
//glShadeModel(GL_SMOOTH);//GL_FLAT
//glBegin(GL_POLYGON);
//glColor3ub(255, 0, 0); glVertex2i(-50, 0);
//glColor3ub(255, 255, 0); glVertex2i(-25, -50);
//glColor3ub(255, 255, 0); glVertex2i(25, -50);
//glColor3ub(255, 0, 0); glVertex2i(50, 0);
//glEnd();
glFlush();
}
void windowReshape(int w, int h)
{
float aspectRatio;
windowWidth = w;
windowHeight = h;
aspectRatio = (float)windowWidth / (float)windowHeight;
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (windowWidth >= windowHeight)
gluOrtho2D(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0);
else gluOrtho2D(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("第一个OpenGL程序");
init();
glutReshapeFunc(windowReshape);
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
7-3:line-type
// OPENGL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define Pi 3.1415926
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-10.0, 10.0, -10.0, 10.0);
}
void circleDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_LINE_STIPPLE);
glLineWidth(10.0f);
glColor3f(0.0, 0.0, 0.0);
glLineStipple(1, 0x0101);
glBegin(GL_LINES);
glVertex2f(-5.0,-5.0);
glVertex2f(5.0, 5.0);
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(50,100);
glutInitWindowSize(800, 800);
glutCreateWindow("第一个OpenGL程序");
init();
glutDisplayFunc(circleDisplay);
glutMainLoop();
return 0;
}
OpenGL基础代码整理的更多相关文章
- Mysql基础代码(不断完善中)
Mysql基础代码,不断完善中~ /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限 ...
- OpenGL基础图形编程
一.OpenGL与3D图形世界1.1.OpenGL使人们进入三维图形世界 我们生活在一个充满三维物体的三维世界中,为了使计算机能精确地再现这些物体,我们必须能在三维空间描绘这些物体.我们又生活在一个充 ...
- opengl基础学习专题 (二) 点直线和多边形
题外话 随着学习的增长,越来越觉得自己很水.关于上一篇博文中推荐用一个 学习opengl的 基于VS2015的 simplec框架.存在 一些问题. 1.这个框架基于VS 的Debug 模式下,没有考 ...
- 【转】OpenGL基础图形编程(一)
原文:http://blog.chinaunix.net/uid-20638550-id-1909183.html 分类: 一.OpenGL与3D图形世界 1.1.OpenGL使人们进入三维图形世界 ...
- material design 的android开源代码整理
material design 的android开源代码整理 1 android (material design 效果的代码库) 地址请点击:MaterialDesignLibrary 效果: 2 ...
- MFC+OpenGL基础绘制<转>
转载地址:https://blog.csdn.net/u013232740/article/details/47904115 ------------------------------------- ...
- opengl基础学习专题 (一 )编程环境搭建
题外话: 第一次在博客园上同大家分享博文.水的的地方,错别字的地方.环境交流.批评.知道了马上改. 以前在百度空间中写技术分享博文,后来百度啥也没说就把整个空间封了.当时感觉 还是有点寒心.只想黑一下 ...
- [译]Vulkan教程(04)基础代码
[译]Vulkan教程(04)基础代码 General structure 通用结构 In the previous chapter you've created a Vulkan project w ...
- Photon Server 实现注册与登录(二) --- 服务端代码整理
一.有的代码前端和后端都会用到.比如一些请求的Code.使用需要新建项目存放公共代码. 新建项目Common存放公共代码: EventCode :存放服务端自动发送信息给客户端的code Operat ...
随机推荐
- xadmin进行全局配置(修改模块名为中文以及其他自定义的操作步骤)
1.实现自定义配置和收缩: 在apps->users->adminx.py中操作如下图内容 2.改成中文 操作如下图所示: 图1: 图2: run重启,刷新页面即可实现如下图: 接下来 ...
- Python—包管理工具与上传工具
https://blog.csdn.net/libbyandhelen/article/details/78808959 https://www.cnblogs.com/nineep/p/947529 ...
- 渗透测试学习 二十九、kali安装,信息搜集,服务器扫描
kali安装,信息搜集,服务器扫描 kali介绍 Kali Linux是基于Debian的Linux发行版, 设计用于数字取证操作系统.由Offensive Security Ltd维护和资助.最先由 ...
- Win2003下IIS以FastCGI模式运行PHP
由于PHP5.3 的改进,原有的IIS 通过isapi 方式解析PHP脚本已经不被支持,PHP从5.3.0 以后的版本开始使用微软的 fastcgi 模式,这是一个更先进的方式,运行速度更快,更稳定. ...
- leetcode——动态规划
立志要熟练动态规划,加油! 最长回文子串给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 思路:设dp[l][r]表示s[l……r]是否回文,枚举右边界r,然后 ...
- MYSQL主从复制--传统方式
目录 0.MYSQL主从复制概述 1.主从复制的作用 2.主从复制存在的问题 3.主从复制问题的解决办法 4.主从复制的原理(面试必问) 1.MYSQL主从复制-传统方式 1.环境准备 2.Maste ...
- 图解JVM垃圾内存回收算法
图解JVM垃圾内存回收算法 这篇文章主要介绍了图解JVM垃圾内存回收算法,由于年轻代堆空间的垃圾回收会很频繁,因此其垃圾回收算法会更加重视回收效率,下面博主和大家来一起学习一下吧 前言 首先,我们要讲 ...
- 2019-2020-1 20199305《Linux内核原理与分析》第二周作业
C程序的反汇编 (一)实验截图 复制所需要的C程序到"剪切板" 在虚拟机环境下粘贴过来 接下来进行反汇编,通过输入gcc -S -o main.s main.c -m32得到32位 ...
- ArcGIS10.2配置PostgreSQL9.2标准教程
ArcGIS 支持Oracle.DB2.PostgreSQL.SQLite关系型数据库升级为企业地理数据,Oracle太庞大,SQLite太小,DB2多在IBM上用,只有PostgreSQL最适合,它 ...
- 基于Docker的Consul服务发现集群搭建
在去年的.NET Core微服务系列文章中,初步学习了一下Consul服务发现,总结了两篇文章.本次基于Docker部署的方式,以一个Demo示例来搭建一个Consul的示例集群,最后给出一个HA的架 ...