OpenGL编程逐步深入(三)在窗口中显示一个三角形
2024-08-31 14:30:18
这一节教程的内容会比较少,我们仅仅是对上一节教程中的代码进行扩展,在窗口中渲染一个三角形出来。
本节我们以下图所示正方形来讲解OpenGl中的坐标系统。当沿着Z轴负方向看时,可见顶点的坐标必须在这个正方形内,这样视口变换才可以將它们映射到窗口中的可见区域。
点(-1.0,-1.0)对应窗口的左下角,(-1.0,1.0)对应窗口的左上角,依此类推。如果指定一个顶点在这个正方形之外,构成的三角形在这个正方形之外的部分会被裁剪掉,将会看不到这部分。
显示三角形代码
/*
Copyright 2010 Etay Meiri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Tutorial 03 - Hello triangle!
*/
#include "stdafx.h"
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
GLuint VBO;
//创建顶点结构体,用于表示OpenGL中的顶点
struct Vector3f
{
float x;
float y;
float z;
Vector3f(){}
Vector3f(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
};
static void RenderSceneCB()
{
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, 3);
glDisableVertexAttribArray(0);
glutSwapBuffers();
}
static void InitializeGlutCallbacks()
{
glutDisplayFunc(RenderSceneCB);
}
static void CreateVertexBuffer()
{
Vector3f Vertices[3];
Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices,GL_STATIC_DRAW);
}
int _tmain(int argc, _TCHAR* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutorial 02");
InitializeGlutCallbacks();
// Must be done after glut is initialized!
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
CreateVertexBuffer();
glutMainLoop();
return 0;
}
代码解读
本节代码在上节代码中稍作修改。
Vector3f Vertices[3];
Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);
扩展数组容量,存放三角形的三个顶点。
glDrawArrays(GL_TRIANGLES, 0, 3);
调用绘图函数时將绘制模式改为GL_TRIANGLES(表示绘制三角形),第三个参数改为3,表示使用3个顶点绘制。
编译运行
可以看到窗口中显示一个白色的三角形。
最新文章
- c数组与字符串
- jsonp
- NLog输出目标及类型
- MVVM架构~Knockoutjs系列之js接收C#数据集合的方式
- StoryBoard和代码结合 按比例快速兼容iPhone6/6 Plus教程
- CXF集成spring做webservice接口
- jupyter巨好玩-调试代码自动变文档
- C++11新特性应用--介绍几个新增的便利算法(不更改容器中元素顺序的算法)
- SSM-Spring-16:Spring中一些名词解释
- SpringBoot系列——利用系统环境变量与配置文件的分支选择实现“智能部署”
- RabbitMQ安装笔记
- ③JSP经典回顾
- Lab 7-1
- poj2699
- 处理springmvc的post和get提交参数乱码问题
- 福大软工1816 &#183; 第三次作业 - 结对项目Salty Fish原型图
- HTTP协议之认证
- 开启swap交换分区
- 【前端安全】JavaScript防XSS攻击
- javascript 节点操作拷贝节点cloneNode()
热门文章
- 紫书 习题 11-7 UVa 10801 (单源最短路变形)
- Linux下库文件的设置 (/usr/bin/ld: cannot find -lxxx 的解决办法)
- 【codeforces 367C】Sereja and the Arrangement of Numbers
- 现代C++
- ASP.NET-让html代码输出为字符串
- 集团公司(嵌入ETL工具)財务报表系统解决方式
- httpd: Could not reliably determine the server&;#39;s fully qualified domain name
- 空暇时候思考之const
- d堆
- CIKM 2013 Paper CQARank: Jointly Model Topics and Expertise in Community Question Answering