윈도우 프로그래밍/OpenGL에 해당하는 글 10

OpenGl SuperBible 6 [예제 2-9] 삼각형 그리기

300x250

전체코드

#include <sb7.h>

class singlepoint_app : public sb7::application
{
    void init()
    {
        static const char title[] = "OpenGL SuperBible - Single Triangle";

        sb7::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.25,  0.25, 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_TRIANGLES, 0, 3);
    }

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

private:
    GLuint          program;
    GLuint          vao;
};

DECLARE_MAIN(singlepoint_app)

 

설명

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.25,  0.25, 0.5, 1.0)); \n"
            "                                                                  \n"
            "    gl_Position = vertices[gl_VertexID];                          \n"
            "}                                                                 \n"
        };

vs_source[] 코드 내의 gl_VertexID는

 

render 함수안 glDrawArrays(GL_TRIANGLES, 0, 3); 호출시

인자값 3에 따라 0,1,2가 gl_VertexID로 입력됨

300x250

댓글()

OpenGl SuperBible 6 [예제 2-5]

300x250

2.2 쉐이더 사용하기

[예제 2-5]

 

#include <sb7.h>

class singlepoint_app : public sb7::application
{
    void init()
    {
        static const char title[] = "OpenGL SuperBible - Single Point";

        sb7::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"
            "    gl_Position = vec4(0.0, 0.0, 0.5, 1.0);   \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"
        };



		/* 쉐이더 */
		// Vertex Shader 위치 정보
		// Fragment Shader 색상
		// Geometry Shader 정점 변환

        program = glCreateProgram(); // 쉐이더 프로그램 생성


        GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); // 1번 쉐이더 생성
        glShaderSource(fs, 1, fs_source, NULL);
        glCompileShader(fs);

        GLuint vs = glCreateShader(GL_VERTEX_SHADER); // 2번 쉐이더 생성
        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 red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
        glClearBufferfv(GL_COLOR, 0, red);

        glUseProgram(program);

        glPointSize(40.0f);

        glDrawArrays(GL_POINTS, 0, 1);
    }

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

private:
    GLuint          program;
    GLuint          vao;
};

DECLARE_MAIN(singlepoint_app)

 

 

쉐이더 종류

 - Vertex Shader 위치 정보
 - Fragment Shader 색상
 - Geometry Shader 정점 변환

 

 

 

300x250

댓글()