OpenGL立方体
直接画
#include <windows.h>
#include <GL/glut.h>
#include <stdio.h>
#include <string>
#include <iostream>
// 绘制立方体
// 将立方体的八个顶点保存到一个数组里面
static const float vertex_list[][3] =
{
-0.5f, -0.5f, -0.5f,//0
0.5f, -0.5f, -0.5f,//1
-0.5f, 0.5f, -0.5f,//2
0.5f, 0.5f, -0.5f,//3
-0.5f, -0.5f, 0.5f,//4
0.5f, -0.5f, 0.5f,//5
-0.5f, 0.5f, 0.5f,//6
0.5f, 0.5f, 0.5f,//7
};
// 将要使用的顶点的序号保存到一个数组里面
static const GLint index_list[][3] =
{
0,5,1,
0,4,5,
0,3,1,
0,3,2,
0,6,2,
0,6,4,
5,6,4,
5,6,7,
5,3,1,
5,3,7,
3,6,2,
3,6,7,
};
// 绘制立方体
void DrawCube(void)
{
int i, j;
glBegin(GL_TRIANGLES);
for (i = 0; i<12; ++i) // 12 三角形
{
for (j = 0; j<3; ++j) // 每个三角形3顶点
{
glVertex3fv(vertex_list[index_list[i][j]]);
}
}
glEnd();
}
static float rotate = 0;
static int times = 0;
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
/*glTranslatef(-0.2, 0, 0);*/ // 平移
//glScalef(2, 1, 1); // 缩放
times++;
if (times > 100)
{
times = 0;
}
if (times % 100 == 0)
{
rotate += 2;
}
glRotatef(rotate, 1, 0, 0);
glRotatef(rotate, 0, 1, 0);
glColor3f(0, 0, 1);
DrawCube();
glPopMatrix();
glutSwapBuffers();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("GLDemo");
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutMainLoop();
}
GL_TRIANGLE_STRIP方法
#include <windows.h>
#include <GL/glut.h>
#include <stdio.h>
#include <string>
#include <iostream>
// 绘制立方体
// 将立方体的八个顶点保存到一个数组里面
static const float vertex_list[][3] =
{
-0.5f, -0.5f, -0.5f,//0
0.5f, -0.5f, -0.5f,//1
-0.5f, 0.5f, -0.5f,//2
0.5f, 0.5f, -0.5f,//3
-0.5f, -0.5f, 0.5f,//4
0.5f, -0.5f, 0.5f,//5
-0.5f, 0.5f, 0.5f,//6
0.5f, 0.5f, 0.5f,//7
};
void display(void){
glBegin(GL_TRIANGLE_STRIP);
glVertex3fv(vertex_list[0]);
glVertex3fv(vertex_list[1]);
glVertex3fv(vertex_list[2]);
glVertex3fv(vertex_list[3]);
glVertex3fv(vertex_list[6]);
glVertex3fv(vertex_list[7]);
glVertex3fv(vertex_list[4]);
glVertex3fv(vertex_list[5]);
glVertex3fv(vertex_list[0]);
glVertex3fv(vertex_list[1]);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glVertex3fv(vertex_list[0]);
glVertex3fv(vertex_list[2]);
glVertex3fv(vertex_list[4]);
glVertex3fv(vertex_list[6]);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glVertex3fv(vertex_list[1]);
glVertex3fv(vertex_list[3]);
glVertex3fv(vertex_list[5]);
glVertex3fv(vertex_list[7]);
glEnd();
}
static float rotate = 0;
static int times = 0;
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
/*glTranslatef(-0.2, 0, 0);*/ // 平移
//glScalef(2, 1, 1); // 缩放
if (times > 100)
{
times = 0;
}
if (times % 100 == 0)
{
rotate += 0.3;
}
glRotatef(rotate, 0, 1, 0);
glRotatef(rotate, 0, 1, 1);
/*gluLookAt(0.0, 0.99, 0.10, 0.0, .0, 0.0, 0.0, 1.0, 0.0);*/
glColor3f(0, 0, 1);
display();
glPopMatrix();
glutSwapBuffers();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("GLDemo");
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutMainLoop();
}
OpenGL立方体的更多相关文章
- OpenGL立方体在世界坐标系中_缩放_旋转_平移_顶点片源着色器_光照作用_棋盘纹理贴图
读取bmp等图片格式中的像素还有难度,就先用这个棋盘图象素来弄了 代码打错一个就一直First-chance exception ,貌似还有一个要用q或者Q才能成功退出,不知道缺少哪句,我用窗口红叉退 ...
- 46.Qt 使用OpenGL绘制立方体
main.cpp #include <QApplication> #include <iostream> #include "vowelcube.h" in ...
- Bullet物理引擎在OpenGL中的应用
Bullet物理引擎在OpenGL中的应用 在开发OpenGL的应用之时, 难免要遇到使用物理来模拟OpenGL中的场景内容. 由于OpenGL仅仅是一个关于图形的开发接口, 因此需要通过第三方库来实 ...
- 几个opengl立方体绘制案例
VC6 下载 http://blog.csdn.net/bcbobo21cn/article/details/44200205 opengl环境配置 http://blog.csdn.net/bcbo ...
- 【OpenGL】---认识CubeTexture
一.OpenGL Cube Texture 立方体纹理 立方体纹理是一种特殊的纹理技术,他用6幅二维贴图构成一个以原点为中心的纹理立方体.对于每个片段,纹理坐标(s,t,r)被当做三维向量看待,每个纹 ...
- VLC命令参数(转载)
转载自: http://blog.csdn.net/bytxl/article/details/6613449 http://www.cnblogs.com/MikeZhang/archive/201 ...
- VLC命令行参数详解
VLC命令行参数详解 2012-11-29 14:00 6859人阅读 评论(0) 收藏 举报 Usage: vlc [options] [stream] ...You can specify mul ...
- opengl典型例程立方体投影与地图绘制
立方体投影 http://www.cnblogs.com/opengl/p/3790450.html 地图绘制 http://www.cnblogs.com/opengl/p/3790354.html
- OpenGL的几何变换2之内观察立方体
我想实现的一个场景是:一个立方体,相机的坐标在立方体的中心点,相机不变,立方体旋转,可以站在立方体中心点查看立方体内部. 实际上就是立方体图像,这是在全景图片当作比较简单的方式,画面不会变形和扭曲,但 ...
随机推荐
- SpringCloud:(一)服务注册与发现
最近跟着方志明老师学习SpringCloud,博客地址如下: https://blog.csdn.net/forezp/article/details/81040925 自己也跟着撸了一遍,纸上得来终 ...
- cocos2dx基础篇(9) 滑块控件CCControlSlider
[3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)CCControlEvent 改为强枚举 Control::EventType (4)CCControlEvent ...
- 【windows】windows安全基础
windows安全基础 安全主体 security principal 是可以进行身份验证的实体. 哪个安全主体在要求访问?这个维度可以是用户,计算机和进程.一旦确认以后,系统就会发放SID. 例子: ...
- kafka学习(七)
跨集群数据镜像 跨集群镜像的使用场景 1.区域集群和中心集群 2.冗余,发生紧急情况下使用第二个集群,保存相同的数据. 3.云迁移 多集群架构 跨集群中心通信的一些现实情况 1.高延迟 2.有 ...
- 基于 @Scheduled 注解的 ----定时任务
最常用的方法@Scheduled 注解表示起开定时任务 依赖 <dependencies> <dependency> <groupId>org.springfram ...
- 多线程17-Async Programming Model
); ThreadId = Thread.CurrentThread.ManagedThreadId; ; RunOnT ...
- (4.31)sql server中的xml数据操作
关键词:xml数据转为行列方式显示 常规案例: declare @data xml declare @h int set @data=' <bookstore> <row> & ...
- 如何查看SQL Server某个存储过程的执行历史【转】
db_name(d.database_id) as DBName, s.name as 存储名称, s.type_desc as 存储类型, d.cached_time as SP添加到缓存的时间, ...
- P1311选择客栈
这是2011年提高组D1T2,是一个绿色的模拟题,不出所料,没写出代码来. 首先输入n个客栈的颜色和最低消费,然后根据“同颜色但不是一个客栈”以及“两个客栈之间必须有一个的最低消费<=p&quo ...
- MySQL总结(3)
ORDER BY 与 GROUP BY ORDER BY GROUP BY 排序产生的输出 分组行.但输出可能不是分组的顺序 不一定需要 如果与聚集函数一起使用列(或表达式)则必须使用 任意列都可以使 ...