这一节教程的内容会比较少,我们仅仅是对上一节教程中的代码进行扩展,在窗口中渲染一个三角形出来。

本节我们以下图所示正方形来讲解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个顶点绘制。

编译运行

可以看到窗口中显示一个白色的三角形。

OpenGL编程逐步深入(三)在窗口中显示一个三角形的更多相关文章

  1. OpenGL编程逐步深入(二)在窗口中显示一个点

    准备知识 在本文中我们将会接触到OpenGl的扩展库GLEW( OpenGL Extension Wrangler Library),GLEW可以帮助我们处理OpenGl中繁琐的扩展管理.一旦初始化后 ...

  2. 转 在PowerDesigner的PDM图形窗口中显示数据列的中文注释

    Name是名称(字段描述),Code是字段名称,Comment是注释名称,ER图中显示的是Name.一般设计时,Name跟comment都设计成描述, 而设计时候常把comment写成中文,name保 ...

  3. Frameset框架,在同一个浏览器窗口中显示不止一个页面

    总结一下.通过使用Frameset框架,可以在同一个浏览器窗口中显示不止一个页面. 先举个例子: 1 <frameset rows="100,*" cols="*& ...

  4. mysql数据库导出模型到powerdesigner,PDM图形窗口中显示数据列的中文注释

    1,mysql数据库导出模型到powerdesigner 2,CRL+Shift+X 3,复制以下内容,执行 '******************************************** ...

  5. IDA 在string窗口中显示中文字符串

    打开ida61\cfg中的ida.cfg文件找到 // (cp866 version)AsciiStringChars = "\r\n\a\v\b\t\x1B" " !\ ...

  6. service中显示一个dialog

    dialog是依附于activity存在的.但是app中经常需要使用以下的情况,在service中做一些后台操作,在某个临界条件满足时,显示一个dialog告知用户.这时dialog无法直接从serv ...

  7. 将图片在指定窗口中显示-OpenCV应用学习笔记一

    1.OpenCV模块划分 OpenCV其实就是一堆用C和C++语言来实现计算机视觉算法的源代码文件:例如C接口函数cvCany()实现了Canny边缘提取算法,我们可以直接将这些源代码添加到自己的软件 ...

  8. spyder中让生成的图像单独在窗口中显示

    IPython 支持两种形式的绘图 终端输出图像新窗口输出图像方式 1 能够非常方便的保存输出记录(如将`IPython 终端输出转换成 Html 文件) 方式 2 则可以交互式的放大.拖动图片,并且 ...

  9. 如何在DOS窗口中显示UTF-8字符

    在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容.在默认情况下,命令行窗口中使用的代码页是中文或者美国的,即 ...

随机推荐

  1. 安卓安装提示:Android SDK requires Android Developer Toolkit version 21.1.0 or above. (错误解决方法)

    安卓安装提示:Android SDK requires Android Developer Toolkit version 21.1.0 or above.  (错误解决方法) 主要是因为版本号不正确 ...

  2. 【Android应用开发技术:基础构建】命令行下的Android应用开发

    作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.co ...

  3. poj_3468,线段树成段更新

    参考自http://www.notonlysuccess.com/index.php/segment-tree-complete/ #include<iostream> #include& ...

  4. MongoDB Shell (mongo)

    https://docs.mongodb.com/getting-started/shell/client/ The mongo shell is an interactive JavaScript ...

  5. Android JNI和NDK学习(09)--JNI实例二 传递类对象

    1 应用层代码 NdkParam.java是JNI函数的调用类,它的代码如下:   package com.skywang.ndk; import android.app.Activity; impo ...

  6. 英语影视台词---六、Saving Private Ryan Quotes

    英语影视台词---六.Saving Private Ryan Quotes 一.总结 一句话总结: Saving Private Ryan is a 1998 American epic war fi ...

  7. pandas入门10分钟——serries其实就是data frame的一列数据

    10 Minutes to pandas This is a short introduction to pandas, geared mainly for new users. You can se ...

  8. 程序Yuan,eclipse你,会用吗?

    程序Yuan,用eclipse敲代码时还在使用alt+/吗,还在为格式化代码ctrl+shift+f快捷键不快捷发愁吗? 如果是,那么这篇就适合你,请继续往下看. ①设置代码提示快捷键         ...

  9. 关于Fragment的setUserVisibleHint() 方法和onCreateView()的执行顺序

    1:setUserVisibleHint(boolean isVisibleToUser)的方法就很重要,根据方法名来看当前页面是否可见, 里面的boolean值就是判断当前页面是否可见的变量,所以大 ...

  10. iOS: 零误差或极小误差的定时执行或延迟执行?

    问题如下: 节奏类游戏需要执行很多的跟音乐节拍相关的操作,并且为了保证节奏感,需要让操作跟节拍的关系十分紧密.对两者间隔要求不能超过0.02秒或更低. 目前使用了 GCD 中的 asyncAfter( ...