OpenGL编程逐步深入(三)在窗口中显示一个三角形
来源:互联网 发布:手机淘宝页头图片 编辑:程序博客网 时间:2024/05/19 17:47
这一节教程的内容会比较少,我们仅仅是对上一节教程中的代码进行扩展,在窗口中渲染一个三角形出来。
本节我们以下图所示正方形来讲解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个顶点绘制。
编译运行
可以看到窗口中显示一个白色的三角形。
0 0
- OpenGL编程逐步深入(三)在窗口中显示一个三角形
- OpenGL编程逐步深入(二)在窗口中显示一个点
- OpenGL编程逐步深入(一)创建一个窗口
- OpenGL编程逐步深入(四)Shaders
- OpenGL编程逐步深入(五)Uniform 变量
- OpenGL编程逐步深入(六)平移变换
- OpenGL编程逐步深入(七)旋转变换
- OpenGL编程逐步深入(八)伸缩变换
- OpenGL编程逐步深入(九)插值处理
- OpenGL编程逐步深入(十)索引绘制
- OpenGL编程逐步深入(十一)组合变换
- OpenGL编程逐步深入(九)插值处理
- OpenGL入门(三):绘制一个三角形
- 在 OpenGL 的显示窗口中截图
- 在 OpenGL 的显示窗口中截图
- [Modern OpenGL系列(三)]用OpenGL绘制一个三角形
- 在opengl编程中怎样把初始图形合理的显示在窗口
- opengl 教程(3) 在窗口画三角形
- 访问控制器的方法
- Codeforces Round #297 (Div. 2)
- windows到linux上回车字符的问题
- 详解C#中MessageBox消息框
- 面试题
- OpenGL编程逐步深入(三)在窗口中显示一个三角形
- 历届试题 大臣的旅费
- 四层负载均衡和七层负载均衡的区别
- angular-leaflet 小例
- 排队规则网卡青涩瞬间
- Codeforces Round #170 (Div. 2)
- 历届试题 买不到的数目
- html学习笔记(2)
- android在非UI线程中更新UI