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.

1 #version 420 core
2 void main(void)
3 {
4     gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
5 }              

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.

 1 GLuint compile_shaders(void)
 2 {
 3   GLuint vertex_shader;
 4   GLuint fragment_shader;
 5   GLuint program;
 6   // Source code for vertex shader
 7   static const GLchar * vertex_shader_source[] =
 8   {
 9     "#version 430 core \n"
10     " \n"
11      "void main(void) \n"
12     "{ \n"
13     " gl_Position = vec4(0.0, 0.0, 0.5, 1.0); \n"
14     "}\n"
15   };
16   // Source code for fragment shader
17   static const GLchar * fragment_shader_source[] =
18   {
19     "#version 430 core \n"
20     " \n"
21     "out vec4 color; \n"
22     " \n"
23     "void main(void) \n"
24     "{ \n"
25     " color = vec4(0.0, 0.8, 1.0, 1.0); \n"
26     "} \n"
27   };
28   // Create and compile vertex shader
29   vertex_shader = glCreateShader(GL_VERTEX_SHADER);
30   glShaderSource(vertex_shader, 1, vertex_shader_source, NULL);
31   glCompileShader(vertex_shader);
32   // Create and compile fragment shader
33   fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
34   glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
35   glCompileShader(fragment_shader);
36   // Create program, attach shaders to it, and link it
37   program = glCreateProgram();
38   glAttachShader(program, vertex_shader);
39   glAttachShader(program, fragment_shader);
40   glLinkProgram(program);
41   // Delete the shaders as the program has them now
42   glDeleteShader(vertex_shader);
43   glDeleteShader(fragment_shader);
44   return program;
45 }

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:

 1 #include <sb6.h>
 2
 3 class singlepoint_app : public sb6::application
 4 {
 5     void init()
 6     {
 7         static const char title[] = "OpenGL SuperBible - Single Triangle";
 8
 9         sb6::application::init();
10
11         memcpy(info.title, title, sizeof(title));
12     }
13
14     virtual void startup()
15     {
16         static const char * vs_source[] =
17         {
18             "#version 420 core                                                 \n"
19             "                                                                  \n"
20             "void main(void)                                                   \n"
21             "{                                                                 \n"
22             "    const vec4 vertices[] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0),  \n"
23             "                                   vec4(-0.25, -0.25, 0.5, 1.0),  \n"
24             "                                   vec4( 0.0,  0.0, 0.5, 1.0)); \n"
25             "                                                                  \n"
26             "    gl_Position = vertices[gl_VertexID];                          \n"
27             "}                                                                 \n"
28         };
29
30         static const char * fs_source[] =
31         {
32             "#version 420 core                                                 \n"
33             "                                                                  \n"
34             "out vec4 color;                                                   \n"
35             "                                                                  \n"
36             "void main(void)                                                   \n"
37             "{                                                                 \n"
38             "    color = vec4(0.0, 0.8, 1.0, 1.0);                             \n"
39             "}                                                                 \n"
40         };
41
42         program = glCreateProgram();
43         GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
44         glShaderSource(fs, 1, fs_source, NULL);
45         glCompileShader(fs);
46
47         GLuint vs = glCreateShader(GL_VERTEX_SHADER);
48         glShaderSource(vs, 1, vs_source, NULL);
49         glCompileShader(vs);
50
51         glAttachShader(program, vs);
52         glAttachShader(program, fs);
53
54         glLinkProgram(program);
55
56         glGenVertexArrays(1, &vao);
57         glBindVertexArray(vao);
58     }
59
60     virtual void render(double currentTime)
61     {
62         static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
63         glClearBufferfv(GL_COLOR, 0, green);
64
65         glUseProgram(program);
66         glDrawArrays(GL_LINES, 0, 3);
67     }
68
69     virtual void shutdown()
70     {
71         glDeleteVertexArrays(1, &vao);
72         glDeleteProgram(program);
73     }
74
75 private:
76     GLuint          program;
77     GLuint          vao;
78 };
79
80 DECLARE_MAIN(singlepoint_app)

OpenGL学习 Our First OpenGL Program

时间: 2024-08-10 21:29:09

OpenGL学习 Our First OpenGL Program的相关文章

【opengl 学习笔记01】HelloWorld示例

<<OpenGL Programming Guide>>这本书是看了忘,忘了又看,赶脚还是把笔记做一做心里比较踏实,哈哈. 我的主题是,好记性不如烂笔头. ================================================================ 1. 下载glut库 glut库地址为:www.opengl.org/resources/libraries/glut/glutdlls37beta.zip glut全称为:OpenGL Utilit

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

    本节是OpenGL学习的第一个课时,下面介绍如何初始化一个窗体:     (1)显示一个有蓝色背景的窗体: #include <GL/glut.h> #include <stdlib.h> void display(void) { /* clear all pixels */ glClear (GL_COLOR_BUFFER_BIT); glFlush (); } int main(int argc, char** argv) { glutInit(&argc, a

OpenGL学习之路(四)

1 引子 上次读书笔记主要是学习了应用三维坐标变换矩阵对二维的图形进行变换,并附带介绍了GLSL语言的编译.链接相关的知识,之后介绍了GLSL中变量的修饰符,着重介绍了uniform修饰符,来向着色器程序传入输入参数. 这次读书笔记的内容相对有趣一些,主要是和园友们分享讨论三维坐标变换矩阵在三维几何体上的应用,以及介绍一下如何实现三维图形与用户操作的交互.这一次笔记在三维编程中也是非常重要的——我们最后开发的三维程序最终就是要和目标用户进行交互的. 之前一直没有在博客上放过gif格式的动画图片,

OpenGL学习之路(五)

1 引子 不知不觉我们已经进入到读书笔记(五)了,我们先对前四次读书笔记做一个总结.前四次读书笔记主要是学习了如何使用OpenGL来绘制几何图形(包括二维几何体和三维几何体),并学习了平移.旋转.缩放坐标变换矩阵的理论推导和实践应用. 这一次读书笔记,我们一起来学习OpenGL中常用的坐标系以及坐标变换.在OpenGL中有几个坐标系,初学者常常被它们搞得晕头转向:为什么需要这些坐标系?各个坐标系有什么作用?……本文就来学习一下这些OpenGL中常用坐标系. 之后来看看投影矩阵的推导,投影变换矩阵

OpenGL学习笔记: (1)mac下OpenGL环境搭建

1,OpenGL是什么 OpenGL(全写Open Graphics Library)是个定义了一个跨编程语言.跨平台的编程接口规格的专业的图形程序接口.它用于三维图像(二维的亦可),是一个功能强大,调用方便的底层图形库. 2,OpenGL能做什么 OpenGL能用来开发跨平台的渲染引擎,在Android.OSX.iOS.Windows.PS等平台均可使用 OpenGL(ES). 3,OpenGL不能做什么 OpenGL不能做物理模拟,OpenGL不能做网络通信,一句话,除了渲染以外的事情,Op

[转]OpenGL学习入门之VS2010环境配置

OpenGL开发环境简介 基于OpenGL标准开发的应用程序运行时需有动态链接库OpenGL32.DLL.Glu32.DLL,这两个文件在安装Windows NT时已自动装载到C:\WINDOWS\SYSTEM32目录下(这里假定用户将Windows NT安装在C盘上).OpenGL的图形库函数封装在动态链接库OpenGL32.DLL中,开发基于OpenGL的应用程序,必须先了解OpenGL的库函数.OpenGL函数命令方式十分有规律,每个库函数均有前缀gl.glu.aux,分别表示该函数属于O

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

此次实验目的: 安装并且配置环境. 1 下载 https://www.opengl.org/ https://www.opengl.org/wiki/Getting_Started#Downloading_OpenGL 在SDK中找到https://www.opengl.org/sdk/libs/ GLUS 下载,解压之后会得到一下文件 2安装 我是用的是MS Studio 2015 这是我的MS STUDIO的安装目录:C:\Program Files (x86)\Microsoft Visu

OpenGL学习之路(一)

1 引子 虽然是计算机科班出身,但从小对几何方面的东西就不太感冒,空间想象能力也较差,所以从本科到研究生,基本没接触过<计算机图形学>.为什么说基本没学过呢?因为好奇(尤其是惊叹于三维游戏的逼真,如魔兽世界.极品飞车),在研究生阶段还专门选修计算机图形学,但也只是听了几堂课,知道了有帧缓存.齐次坐标等零零散散的概念,之后读了一篇论文并上台作报告(压根没读懂).总之,当时只是觉得计算机图形学或三维渲染很牛,甚至问我什么是渲染都不知道,更不知道如何将3维几何体显示到2维屏幕上.令我现在想来非常可笑

OpenGl学习进程(7)第五课:点、边和图形(二)边

本节是OpenGL学习的第五个课时,下面介绍OpenGL边的相关知识: (1)边的概念: 数学上的直线没有宽度,但OpenGL的直线则是有宽度的.同时,OpenGL的直线必须是有限长度,而不是像数学概念那样是无限的.可以认为,OpenGL的“直线”概念与数学上的“线段”接近,它可以由两个端点来确定.     (2)如何绘制边: 1)OpenGL支持绘制三种类型的边: GL_LINES :指定两个顶点,在它们之间绘制一条直线.如果为GL_LINES指定了奇数个顶点,那么最后一个顶点会被忽略. GL