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世界的空间变换包括:模型变换.视图变换.投影变换和视口 ...
随机推荐
- 剧本--ansible
剧本不喜欢, 1.1 编写剧本规范:(PYyaml语法格式文件) 剧本中有层级划分 每个层级都要用两个空格进行区分 第一级标题 第二级标题 第三级标题 强调注意:一定使用ansible软件配置剧本时, ...
- 2013 New available ICOM A2 Diagnostic & Programming For BMW ICOM A2+B+C
AUTONUMEN.COM offer best price New ICOM A2 Diagnostic & Programming For BMW ICOM A2+B+C. bmw ico ...
- my06_sysbench install for mysql 并初始化表数据
sysbench安装 ************************************************************** 安装sysbench依赖包 rpm -q autom ...
- 20-----BBS论坛
BBS论坛(二十) 20.1.cms添加轮播图后台逻辑代码完成 (1)apps/models.py from exts import db from datetime import datetime ...
- 【ACM】懒省事的小明
懒省事的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的不同种 ...
- ubuntu 常用安装软件
1. Ubuntu安装chrome. sudo apt-get install chromium-browser w
- Typora中给代码块设置快捷键
Tpyore中大部分的操作都是有快捷键的.但是有那么几个常用的却没有快捷键.就比如代码块,这个常用的操作,还有有序无需列表. 下边教会你怎么设置快捷键,打开设置,Preferences[偏好设置],然 ...
- Google Chrome实用插件
Tempermonkey 打开上面的链接需要KXSW_VPN_FQ,下面提供国内获取方式 下载 Tempermonkey_4.7 [提取码:7019]并解压 打开C:\Users\%USERNAME% ...
- 效率工具(fswatch,rsync)
mac 安装 brew: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/in ...
- 安装weblogic步骤
1 mkdir -p /home/geekc3t/weblogic 创建weblogic安装目录2 groupadd weblogic 创建weblogic组3 useradd -g weblogic ...