设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

开始在OS X开发OpenGL应用

2014-6-16 12:19| 发布者: joejoe0332| 查看: 4906| 评论: 0|原作者: 海王, LeoXu, 何传友, 地狱星星|来自: oschina

摘要: 有一段时间了,我一直想着进入某些视频游戏开发领域. 对于OpenGL我也很感兴趣,并且因此我也想到了为什么不深入去研究研究OpenGL,再去做一款视频游戏呢?我已经有了许多我想要去探索的游戏的点子,但第一都是得要婴 ...


着色器是一个GL的应用程序的重要组成部分。它们是运行在显卡上并最终决定什么被渲染到屏幕上的程序。请注意,在我们的代码中,我们使用版本410 core。这必须配合我们正在使用的OpenGL版本,它的核心配置文件是4.1版,因此选用410 core。

01// Vertex shader source code. This draws the vertices in our window. We have 3 vertices since we're drawing an triangle.
02// Each vertex is represented by a vector of size 4 (x, y, z, w) coordinates.
03static const char * vs_source[] =
04{
05    "#version 410 core                                                 \n"
06    "                                                                  \n"
07    "void main(void)                                                   \n"
08    "{                                                                 \n"
09    "    const vec4 vertices[] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0),  \n"
10    "                                   vec4(-0.25, -0.25, 0.5, 1.0),  \n"
11    "                                   vec4( 0.25,  0.25, 0.5, 1.0)); \n"
12    "                                                                  \n"
13    "    gl_Position = vertices[gl_VertexID];                          \n"
14    "}                                                                 \n"
15};
16 
17// Fragment shader source code. This determines the colors in the fragment generated in the shader pipeline. In this case, it colors the inside of our triangle specified by our vertex shader.
18static const char * fs_source[] =
19{
20    "#version 410 core                                                 \n"
21    "                                                                  \n"
22    "out vec4 color;                                                   \n"
23    "                                                                  \n"
24    "void main(void)                                                   \n"
25    "{                                                                 \n"
26    "    color = vec4(0.0, 0.8, 1.0, 1.0);                             \n"
27    "}                                                                 \n"
28};


在渲染我们的着色器之后,我们需要实时的编译他们并绑定他们到我们的GL程序以便于显卡使用他们。

01// This next section we'll generate the OpenGL program and attach the shaders to it so that we can render our triangle.
02program = glCreateProgram();
03 
04// We create a shader with our fragment shader source code and compile it.
05GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
06glShaderSource(fs, 1, fs_source, NULL);
07glCompileShader(fs);
08 
09// We create a shader with our vertex shader source code and compile it.
10GLuint vs = glCreateShader(GL_VERTEX_SHADER);
11glShaderSource(vs, 1, vs_source, NULL);
12glCompileShader(vs);
13 
14// We'll attach our two compiled shaders to the OpenGL program.
15glAttachShader(program, vs);
16glAttachShader(program, fs);
17 
18glLinkProgram(program);
19 
20// Generate vertex arrays for our program. More explanation on this will come in the future.
21glGenVertexArrays(1, &vao);
22glBindVertexArray(vao);
23 
24// We'll specify that we want to use this program that we've attached the shaders to.
25glUseProgram(program);


一旦我们告诉显卡利用绑定的着色器来使用我们的程序,那么我们开始绘制东西吧。接下来的代码块是我们的渲染循环。我们不断地画三角形到银幕上,直到该程序存在。

01//As long as our window remains  open(ESC IS not  pressed),we'll continue to render things.
02while(!glfwWindowshouldclose(window))
03{
04  //set up our  green background color
05static  const  GLfloat green[]={0.25f,0.0f,1.0f};
06//clear the  entire  buffer with  our  green  color(sets  the  background to  be  green).
07  glDrawBufferfv{GL_TRIANGLES,0,3);
08//SWAP THE  BUFFERS  SO  THAT  WHAT  WE  DREW WILL APPEAR on the sreen.
09glfwSwapBuffers(window);
10glfwpollEvent();
11}


这是大量的好的代码,如果你没有阅读它或者充分地理解它。我已经评论过它并试图解释,但是某些概念我仍要企图掌握它。有两种主要的外卖,我想在这儿开车回家。


1.有大量设置的代码。

2.也有着色器的源代码。保持他们作为静态的“const char,是粪便。


对于我的下一篇文章,我将详细从我们的c++代码里面来如何移动着色器源代码,以至于它不在那儿纠结,更容易编辑。

着色器是非常重要的图形并且完全不实用,如果他们被卡在我们的c++代码里面。对于以后的帖子,我将表明我已经做了在其他类和模块里面,重构了大量的已经设置的代码。


同样,如果你喜欢看见我最近的源代码,那么它就在Github上面


后会有期!



酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部