OpenGL学习 Our First OpenGL Program
This shows you how to create the main window with the book’s application framework and how to render simple graphics into it.
In shaders,we use #version430 core to tell the shader compiler that we intend to use version 4.3 of the shading language.The keyword core to indicate that we only intend to use features from the core profile of OpenGL.
The main function is where the shader starts executing.
gl_Position is part of the plumbing that connects the shader to the rest of OpenGL.All variables that start with gl_ are part of OpenGL and connect shaders to each other or to the various parts of fixed functionality in OpenGL.In the vertex shader,gl_Position represents the output position of the vertex.
#version core
void main(void)
{
gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
}
Using the keyword "out" to declares "color" as a output variable.In fragment shaders, the value of out put variables will be sent to window or screen.
If we want to draw anything when our pipeline does not contain a vertex shader,the results will be undefined and almost certainly not what you were hoping for.So we should have both a vertex and a feagment shader at least.
Next we will compile and link them so that we can run the OpenGL application.
GLuint compile_shaders(void)
{
3 GLuint vertex_shader;
GLuint fragment_shader;
GLuint program;
// Source code for vertex shader
7 static const GLchar * vertex_shader_source[] =
{
"#version 430 core \n"
" \n"
"void main(void) \n"
"{ \n"
" gl_Position = vec4(0.0, 0.0, 0.5, 1.0); \n"
"}\n"
};
// Source code for fragment shader
static const GLchar * fragment_shader_source[] =
{
"#version 430 core \n"
" \n"
"out vec4 color; \n"
" \n"
"void main(void) \n"
"{ \n"
" color = vec4(0.0, 0.8, 1.0, 1.0); \n"
"} \n"
};
// Create and compile vertex shader
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, , vertex_shader_source, NULL);
31 glCompileShader(vertex_shader);
32 // Create and compile fragment shader
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, , fragment_shader_source, NULL);
glCompileShader(fragment_shader);
// Create program, attach shaders to it, and link it
37 program = glCreateProgram();
glAttachShader(program, vertex_shader);
39 glAttachShader(program, fragment_shader);
glLinkProgram(program);
// Delete the shaders as the program has them now
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
return program;
}
glCreateShader() creates an empty shader object, ready to accept source code and be compiled.
glShaderSource() hands shader source code to the shader object so that it can keep a copy of it.
glCompileShader() compiles whatever source code is contained in the shader object.
glCreateProgram() creates a program object to which you can attach shader objects.
glAttachShader() attaches a shader object to a program object.
glLinkProgram() links all of the shader objects attached to a program object together.
glDeleteShader() deletes a shader object. Once a shader has been linked into a program object, the program contains the binary code and the shader is no longer needed.
Fortunately, GLSL includes a special input to the vertex shader called gl_VertexID, which is the index of the vertex that is being processed at the time. The gl_VertexID input starts counting from the value given by the first parameter of glDrawArrays() and counts upwards one vertex at a time for count vertices (the third parameter of glDrawArrays()).
Example code:
#include <sb6.h> class singlepoint_app : public sb6::application
{
void init()
{
static const char title[] = "OpenGL SuperBible - Single Triangle"; sb6::application::init(); memcpy(info.title, title, sizeof(title));
} virtual void startup()
{
static const char * vs_source[] =
{
"#version 420 core \n"
" \n"
"void main(void) \n"
"{ \n"
" const vec4 vertices[] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0), \n"
" vec4(-0.25, -0.25, 0.5, 1.0), \n"
" vec4( 0.0, 0.0, 0.5, 1.0)); \n"
" \n"
" gl_Position = vertices[gl_VertexID]; \n"
"} \n"
}; static const char * fs_source[] =
{
"#version 420 core \n"
" \n"
"out vec4 color; \n"
" \n"
"void main(void) \n"
"{ \n"
" color = vec4(0.0, 0.8, 1.0, 1.0); \n"
"} \n"
}; program = glCreateProgram();
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, , fs_source, NULL);
glCompileShader(fs); GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, , vs_source, NULL);
glCompileShader(vs); glAttachShader(program, vs);
glAttachShader(program, fs); glLinkProgram(program); glGenVertexArrays(, &vao);
glBindVertexArray(vao);
} virtual void render(double currentTime)
{
static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, , green); glUseProgram(program);
glDrawArrays(GL_LINES, , );
} virtual void shutdown()
{
glDeleteVertexArrays(, &vao);
glDeleteProgram(program);
} private:
GLuint program;
GLuint vao;
}; DECLARE_MAIN(singlepoint_app)
OpenGL学习 Our First OpenGL Program的更多相关文章
- OPENGL学习之路(0)--安装
此次实验目的: 安装并且配置环境. 1 下载 https://www.opengl.org/ https://www.opengl.org/wiki/Getting_Started#Downloadi ...
- OpenGL 学习笔记 01 环境配置
以下教程仅适用于Mac下的Xcode编程环境!其他的我也不会搞. 推荐教程:opengl-tutorial 本项目Github网址 OpenGL太可怕了...必需得把学的记下来,不然绝壁 ...
- OpenGL学习之路(一)
1 引子 虽然是计算机科班出身,但从小对几何方面的东西就不太感冒,空间想象能力也较差,所以从本科到研究生,基本没接触过<计算机图形学>.为什么说基本没学过呢?因为好奇(尤其是惊叹于三维游戏 ...
- OpenGL学习之路(三)
1 引子 这些天公司一次次的软件发布节点忙的博主不可开交,另外还有其它的一些事也占用了很多时间.现在坐在电脑前,在很安静的环境下,与大家分享自己的OpenGL学习笔记和理解心得,感到格外舒服.这让我回 ...
- OpenGL学习之路(四)
1 引子 上次读书笔记主要是学习了应用三维坐标变换矩阵对二维的图形进行变换,并附带介绍了GLSL语言的编译.链接相关的知识,之后介绍了GLSL中变量的修饰符,着重介绍了uniform修饰符,来向着色器 ...
- OpenGL学习之路(五)
1 引子 不知不觉我们已经进入到读书笔记(五)了,我们先对前四次读书笔记做一个总结.前四次读书笔记主要是学习了如何使用OpenGL来绘制几何图形(包括二维几何体和三维几何体),并学习了平移.旋转.缩放 ...
- OpenGL学习进程(3)第一课:初始化窗体
本节是OpenGL学习的第一个课时,下面介绍如何初始化一个窗体: (1)显示一个有蓝色背景的窗体: #include <GL/glut.h> #include <st ...
- OpenGL学习笔记3——缓冲区对象
在GL中特别提出了缓冲区对象这一概念,是针对提高绘图效率的一个手段.由于GL的架构是基于客户——服务器模型建立的,因此默认所有的绘图数据均是存储在本地客户端,通过GL内核渲染处理以后再将数据发往GPU ...
- OpenGL学习进程(12)第九课:矩阵乘法实现3D变换
本节是OpenGL学习的第九个课时,下面将详细介绍OpenGL的多种3D变换和如何操作矩阵堆栈. (1)3D变换: OpenGL中绘制3D世界的空间变换包括:模型变换.视图变换.投影变换和视口 ...
随机推荐
- P2896 [USACO08FEB]一起吃饭Eating Together
传送门 可以考虑DP 设 f [ i ] [ 1/2/3 ] [ 0/1 ] 表示当前考虑到第 i 头牛,打算让当前位置的编号变成 1/2/3,并且打算让整段序列上升/下降 0/1 然后就对每种情况慢 ...
- 江西财经大学第一届程序设计竞赛 B
链接:https://www.nowcoder.com/acm/contest/115/B来源:牛客网 题目描述 给出一个出生日期,比如:1999-09-09, 问:从出生那一天开始起,到今天2018 ...
- restframework CBV试图的4种方式
day100 上节回顾 1 APIView流程 2 序列化组件(restframework) 1 # 自定义序列化组件 class BookSerializers(serializers.Serial ...
- oracle中所有表的字段和注释
select t1.owner ,t1.table_name ,t1.column_id ,t1.column_name ,t1.data_type ,t2.comments from all_tab ...
- 解决Yii2 添加css后页面刷新也无反应的情况
'assetManager' => [ // uncomment the following line if you want to auto update your assets (unix ...
- hadoop - hbase 单机版的安装部署
1. 下载安装包. http://www.apache.org/dyn/closer.lua/hbase/ 选择版本,单机进入以下页面 2. tar xfz hbase-1.2.6-bin.tar.g ...
- web前端开发道路
https://github.com/z-jingjie/developer-roadmap-zh-CN
- 使用PyCharm实现远程编写并调试代码
使用PyCharm实现远程编写并调试代码 版权声明:本文为博主原创文章,转载请注明出https://www.cnblogs.com/wenqiangit/p/9771947.html 因为工作中使用的 ...
- OfficeControl插件的用法
项目中需要用到文档在线编辑的功能,网上找到这篇文章: http://hi.baidu.com/hurtingwings/item/bf83b6343305a94e3075a19e
- c++ primer 中讲的顶层const 和 底层 const 理解
c++ primer 中讲的 顶层const 和 底层 const 以前没搞懂的顶层const和底层const,这次看了后感觉明白了. 首先,const是一个限定符,被它修饰的变量的值不能改 ...