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的更多相关文章

  1. OPENGL学习之路(0)--安装

    此次实验目的: 安装并且配置环境. 1 下载 https://www.opengl.org/ https://www.opengl.org/wiki/Getting_Started#Downloadi ...

  2. OpenGL 学习笔记 01 环境配置

    以下教程仅适用于Mac下的Xcode编程环境!其他的我也不会搞. 推荐教程:opengl-tutorial  本项目Github网址       OpenGL太可怕了...必需得把学的记下来,不然绝壁 ...

  3. OpenGL学习之路(一)

    1 引子 虽然是计算机科班出身,但从小对几何方面的东西就不太感冒,空间想象能力也较差,所以从本科到研究生,基本没接触过<计算机图形学>.为什么说基本没学过呢?因为好奇(尤其是惊叹于三维游戏 ...

  4. OpenGL学习之路(三)

    1 引子 这些天公司一次次的软件发布节点忙的博主不可开交,另外还有其它的一些事也占用了很多时间.现在坐在电脑前,在很安静的环境下,与大家分享自己的OpenGL学习笔记和理解心得,感到格外舒服.这让我回 ...

  5. OpenGL学习之路(四)

    1 引子 上次读书笔记主要是学习了应用三维坐标变换矩阵对二维的图形进行变换,并附带介绍了GLSL语言的编译.链接相关的知识,之后介绍了GLSL中变量的修饰符,着重介绍了uniform修饰符,来向着色器 ...

  6. OpenGL学习之路(五)

    1 引子 不知不觉我们已经进入到读书笔记(五)了,我们先对前四次读书笔记做一个总结.前四次读书笔记主要是学习了如何使用OpenGL来绘制几何图形(包括二维几何体和三维几何体),并学习了平移.旋转.缩放 ...

  7. OpenGL学习进程(3)第一课:初始化窗体

        本节是OpenGL学习的第一个课时,下面介绍如何初始化一个窗体:     (1)显示一个有蓝色背景的窗体: #include <GL/glut.h> #include <st ...

  8. OpenGL学习笔记3——缓冲区对象

    在GL中特别提出了缓冲区对象这一概念,是针对提高绘图效率的一个手段.由于GL的架构是基于客户——服务器模型建立的,因此默认所有的绘图数据均是存储在本地客户端,通过GL内核渲染处理以后再将数据发往GPU ...

  9. OpenGL学习进程(12)第九课:矩阵乘法实现3D变换

    本节是OpenGL学习的第九个课时,下面将详细介绍OpenGL的多种3D变换和如何操作矩阵堆栈.     (1)3D变换: OpenGL中绘制3D世界的空间变换包括:模型变换.视图变换.投影变换和视口 ...

随机推荐

  1. 多线程DP (要一起行动才可以)

    题目描述 Description 设有N*N的方格图(N<=10,我们将其中的某些方格中填入正整数,而其他的方格中则放入数字0.如下图所示(见样例): 某人从图的左上角的A 点出发,可以向下行走 ...

  2. 江西财经大学第一届程序设计竞赛 D

    链接:https://www.nowcoder.com/acm/contest/115/D来源:牛客网 题目描述 事情,是这样的. 有这么一天双休日的中午. 我刚把我衣服扔进了洗衣机,然后拿了个小板凳 ...

  3. A reader

    A reader lives a thousand lives before he die... The man who never reads lives only one.

  4. tomcat 普通用户报错方案

    Tomcat Neither the JAVA_HOME nor the JRE_HOME environment variable is defined 一眼就能看出来是jdk的环境有问题,但是用了 ...

  5. Git for Linux and Windows

    1.在liunx中安装 1.1yum安装 [root@node1 ~]# yum install git –y [root@node1 ~]# git version git version 1.8. ...

  6. 支付宝支付集成中:refund_fastpay_by_platform_nopwd接口服务器通知验签不通过

    在做p2p配资平台,也就是公司的项目,遇到了一个问题:refund_fastpay_by_platform_nopwd接口服务器通知验签不通过 下面是实录: 通知服务器的POST过来的数据: 1.si ...

  7. http 协议的简单学习 虽然有点老但是 还不错

    HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...

  8. .NET接收邮件下载邮件附件——openpop.net

    使用OpenPop.Net接收邮件很方便,下面是接收下载邮件附件的代码 OpenPop.Net下载地址 https://sourceforge.net/projects/hpop/ public cl ...

  9. equals()方法 与 == 区别

    equals()方法 与 == 区别 : equals():在字符串中比较内容,则必须使用equals(变量),如果相等则为true,否则为false == :使用==比较的是地址是否一致 class ...

  10. Unity脚本的生命周期 同一脚本/不同脚本/游戏对象激没激活/脚本激没激活,几种情况下的Awake,OnEnable,Start的执行顺序

    可以自己在Unity里面试一下 游戏对象在Hierarchy面板不是激活的,它的脚本没作用,脚本中的函数不会执行; 游戏对象在Hierarchy面板是激活的,脚本没激活,Awake会执行,OnEnab ...