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世界的空间变换包括:模型变换.视图变换.投影变换和视口 ...
随机推荐
- Go语言基础练习题系列2
1.练习1 生成一个随机数,让一个用户去猜这个数是多少? 代码示例如下: package main import ( "fmt" "math/rand" //m ...
- kohana reading session data error(session_start)引起的错误
在项目中碰到一个很奇怪的问题,同一个网站目录下两个项目,手机端访问从A项目切换到B项目,总是报错,session_start抛错,后来排查了很久,终于发现是存储session的时候直接存储的对象,导致 ...
- iPhone10.3.X越狱后SSH连接不上的问题(已解决)
iPhone10.3.X越狱后SSH连接不上的问题 G0blin RC2,iPhone5s10.3.3 Jailbreak 最近研究了好几天,试了好多的方法. ssh 访问越狱iPhone的两种方式 ...
- Impala与Hive的优缺点和异同
定位: HIVE:长时间的批处理查询分析 impala:实时交互式SQL查询 impala优缺点优点: 1. 生成执行计划树,不用多次启动job造成多余开销,并且减少中间结果数据写入磁盘,执行速度快 ...
- Spring配置问题:The prefix "util" for element "util:map" is not bound.
在spring的头部文件中没有引入: xmlns:util=”http://www.springframework.org/schema/util” 原文:https://blog.csdn.net/ ...
- python 和 C# DES加密
C# code: using System; using System.IO; using System.Security.Cryptography; using System.Text; names ...
- 一些自己编写的简单的js
图片在窗口内弹来弹去的效果 <div class="FrontAdv_float01-default" style="position: absolute;z-in ...
- 转载 Some indexes or index [sub]partitions of table VAS.TAB_PUB_CALLLOG have been marked unusable
http://www.xifenfei.com/2011/12/some-indexes-or-index-subpartitions-of-table-vas-tab_pub_calllog-hav ...
- C# 通过 Hessian服务调用java
1.下载Hessiancsharp.dll 2.新建一个c#窗体程序,然后在添加一个类库 3.添加一个接口类,接口中的方法名和参数与java中的服务方法名和参数保持一致 public partial ...
- suau 公约公倍
8619 公约公倍 时间限制:500MS 内存限制:1000K 提交次数:475 通过次数:108 题型: 编程题 语言: G++;GCC Description 给定六个正整数a,b,c,d ...