您的位置:首页 > 运维架构

OpenGL学习 Our First OpenGL Program

2014-08-12 16:37 525 查看
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 420 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, 1, vertex_shader_source, NULL);
31   glCompileShader(vertex_shader);
32   // Create and compile fragment shader
  fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  glShaderSource(fragment_shader, 1, 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, 1, fs_source, NULL);
glCompileShader(fs);

GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, vs_source, NULL);
glCompileShader(vs);

glAttachShader(program, vs);
glAttachShader(program, fs);

glLinkProgram(program);

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
}

virtual void render(double currentTime)
{
static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, green);

glUseProgram(program);
glDrawArrays(GL_LINES, 0, 3);
}

virtual void shutdown()
{
glDeleteVertexArrays(1, &vao);
glDeleteProgram(program);
}

private:
GLuint          program;
GLuint          vao;
};

DECLARE_MAIN(singlepoint_app)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: