glVertexAttribPointer

glVertexAttribPointer — define an array of generic vertex attribute data

https://www.opengl.org/sdk/docs/man/html/glVertexAttribPointer.xhtml

void glVertexAttribPointer( GLuint index,
GLint size,
GLenum type,
GLboolean normalized,
GLsizei stride,
const GLvoid * pointer);

Parameters

index
Specifies the index of the generic vertex attribute to be modified.

size
Specifies the number of components per generic vertex attribute. Must be 1, 2, 3, 4. Additionally, the symbolic constant GL_BGRA is accepted by glVertexAttribPointer. The initial value is 4.

type
Specifies the data type of each component in the array. The symbolic constants GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, and GL_UNSIGNED_INT are accepted by glVertexAttribPointer and glVertexAttribIPointer. Additionally GL_HALF_FLOAT, GL_FLOAT, GL_DOUBLE, GL_FIXED, GL_INT_2_10_10_10_REV, GL_UNSIGNED_INT_2_10_10_10_REV and GL_UNSIGNED_INT_10F_11F_11F_REV are accepted by glVertexAttribPointer. GL_DOUBLE is also accepted by glVertexAttribLPointer and is the only token accepted by the type parameter for that function. The initial value is GL_FLOAT.

normalized
For glVertexAttribPointer, specifies whether fixed-point data values should be normalized (GL_TRUE) or converted directly as fixed-point values (GL_FALSE) when they are accessed.

stride
Specifies the byte offset between consecutive generic vertex attributes. If stride is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0.

pointer
Specifies a offset of the first component of the first generic vertex attribute in the array in the data store of the buffer currently bound to the GL_ARRAY_BUFFER target. The initial value is 0.

 

 

Lab0

MY FIRST OPENGL SHADER

lab0-ShaderTriangle

lab0-ShaderTriangle-uniform (fragment shader에 color값을 uniform variable을 사용하여 보내기)

////////////////////////////////////////////////////////////////////////////////////////////////////

simple.vs

////////////////////////////////////////////////////////////////////////////////////////////////////

#version 150

in vec4 vPosition;

void
main()
{
gl_Position = vPosition;
}

 

 

////////////////////////////////////////////////////////////////////////////////////////////////////

simple.fs

////////////////////////////////////////////////////////////////////////////////////////////////////

#version 150

out vec4  fColor;

void
main()
{
fColor = vec4( 1.0, 0.0, 0.0, 1.0 );
}

////////////////////////////////////////////////////////////////////////////////////////////////////

triangle.cpp

////////////////////////////////////////////////////////////////////////////////////////////////////

#include <cmath>
#include <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/freeglut_ext.h>

 

const float vertexPositions[] = {
-0.75f, -0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
0.75f, 0.75f, 0.0f, 1.0f,
};

 

GLuint program;
GLuint vao;
GLuint buffer;

 

// Create a NULL-terminated string by reading the provided file
static char* readShaderSource(const char* shaderFile)
{
FILE* fp = fopen(shaderFile, “r”);

 

if ( fp == NULL ) { return NULL; }

 

fseek(fp, 0L, SEEK_END);
long size = ftell(fp);

 

fseek(fp, 0L, SEEK_SET);
char* buf = new char[size + 1];
fread(buf, 1, size, fp);

 

buf[size] = ”;
fclose(fp);

 

return buf;
}

 

// Create a GLSL program object from vertex and fragment shader files
GLuint InitShader(const char* vShaderFile, const char* fShaderFile)
{
struct Shader {
const char* filename;
GLenum type;
GLchar* source;
} shaders[2] = {
{ vShaderFile, GL_VERTEX_SHADER, NULL },
{ fShaderFile, GL_FRAGMENT_SHADER, NULL }
};

 

GLuint program = glCreateProgram();

for ( int i = 0; i < 2; ++i ) {
Shader& s = shaders[i];
s.source = readShaderSource( s.filename );
if ( shaders[i].source == NULL ) {
std::cerr << “Failed to read ” << s.filename << std::endl;
exit( EXIT_FAILURE );
}

 

GLuint shader = glCreateShader( s.type );

 

glShaderSource( shader, 1, (const GLchar**) &s.source, NULL );
glCompileShader( shader );

 

GLint compiled;
glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
if ( !compiled ) {
std::cerr << s.filename << ” failed to compile:” << std::endl;
GLint logSize;
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize );
char* logMsg = new char[logSize];
glGetShaderInfoLog( shader, logSize, NULL, logMsg );
std::cerr << logMsg << std::endl;
delete [] logMsg;

 

exit( EXIT_FAILURE );
}

 

delete [] s.source;

 

glAttachShader( program, shader );
}

 

/* link and error check */
glLinkProgram(program);

 

GLint linked;
glGetProgramiv( program, GL_LINK_STATUS, &linked );
if ( !linked ) {
std::cerr << “Shader program failed to link” << std::endl;
GLint logSize;
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
char* logMsg = new char[logSize];
glGetProgramInfoLog( program, logSize, NULL, logMsg );
std::cerr << logMsg << std::endl;
delete [] logMsg;

 

exit( EXIT_FAILURE );
}

 

/* use program object */
glUseProgram(program);

 

return program;
}

 

////////////////////////////////////////////////////////////////////////////////////////////////////

 

void init( void )
{
// Load shaders and use the resulting shader program
program = InitShader( “simple.vs”, “simple.fs” );
glUseProgram(program);

 

// VAO
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

 

// VBO
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);

 

glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
}

 

void display( void )
{
glClear( GL_COLOR_BUFFER_BIT ); // clear the window

 

glDrawArrays(GL_TRIANGLES, 0, 3); // draw triangle

 

glFlush();
}

 

void keyboard( unsigned char key, int x, int y )
{
switch ( key ) {
case 27:
exit( EXIT_SUCCESS );
break;
}
}

 

int main( int argc, char **argv )
{

glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGBA );
glutInitWindowSize( 512, 512 );
glutCreateWindow(argv[0]);

glewInit();
init();

 

glutDisplayFunc( display );
glutKeyboardFunc( keyboard );

 

glutMainLoop();
return 0;
}

OPENGL PROGRAMMABLE PIPELINE

http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Chapter-1:-The-Graphics-Pipeline.html

 

Modern OpenGL (OpenGL 3.x) 프로그래밍은 다음과 같은 단계로 진행한다.
1.Shader 프로그램을 만든다.
2.Vertex 자료를 Vertex Buffer Object (VBO)와 Vertex Array Object (VAO)를 만들고 이 자료를 쉐이더에 로딩한다.
3.이 자료의 위치와 쉐이더의 변수와 “연결(Connect)” 한다.
4.렌더링을 수행한다.

MODERN OPENGL INSTALLATION FOR WINDOWS 8 VISUAL STUDIO 2015

Windows8.1 or 10 64-bit 운영체제에서 Visual Studio 2015과 OpenGL 3.x 이상을 지원하는 그래픽카드 드라이버가 설치되어 있어야 합니다.

1. freeglut는 아래 파일을 다운 받아 unzip해 주어야 합니다. 
http://freeglut.sourceforge.net/

압축을 풀어보면 bin,include, lib 세 개의 폴더가 있습니다.
C:\………\freeglut-MSVC-3.0.0-2.mp\freeglut\include\GL
freeglut.h freeglut_ext.h freeglut_std.h glut.h

C:\………\freeglut-MSVC-3.0.0-2.mp\freeglut\lib
freeglut.lib

C:\………\freeglut-MSVC-3.0.0-2.mp\freeglut\bin
freeglut.dll

2. glew는 아래 파일을 다운 받아 unzip해 주어야 합니다. 
http://glew.sourceforge.net/

압축을 풀어보면 include, lib, bin, doc 네 개의 폴더가 있습니다.
C:\………\glew-2.0.0-win32\glew-2.0.0\include\GL
glew.h glxew.h wglew.h

C:\………\glew-2.0.0-win32\glew-2.0.0\lib\Release\Win32
glew32.lib glew32s.lib
C:\………\glew-2.0.0-win32\glew-2.0.0\lib\Release\x64
glew32.lib glew32s.lib

C:\………\glew-2.0.0-win32\glew-2.0.0\lib\Release MX\Win32
glew32mx.lib glew32mxs.lib
C:\………\glew-2.0.0-win32\glew-2.0.0\lib\Release MX\x64
glew32mx.lib glew32mxs.lib

C:\………\glew-2.0.0-win32\glew-2.0.0\bin\Release\Win32
glew32.dllc glewinfo.exe visualinfo.exe
C:\………\glew-2.0.0-win32\glew-2.0.0\bin\Release\x64
glew32.dllc glewinfo.exe visualinfo.exe

C:\………\glew-2.0.0-win32\glew-2.0.0\bin\Release MX\Win32
glew32mx.dll
C:\………\glew-2.0.0-win32\glew-2.0.0\bin\Release MX\x64
glew32mx.dll

3. glm는 아래 파일을 다운 받아 unzip해 주어야 합니다.

OpenGL Mathematics (GLM)

https://github.com/g-truc/glm/releases

C:\………..\glm-0.9.7.6\glm 안에 glm 디렉토리 전체

4.  VisualStudio2015의 include와 lib 디렉토리에 파일을 복사한다.

본인이 설치한 VisualStudio2015의 Include & Lib 디렉토리를 찾으려면,
VS2015을 실행하여 Project에 Properties를 보면
VC++ Directories에서 Include와 Library Directories 를 Edit누른후
Macros를 누르면
$(VC_IncludePath)가 내가 테스트해본 컴퓨터에서는 C:\Program Files
(x86)\Microsoft Visual Studio 14.0\VC\include
$(VC_LibraryPath_x86)가 내가 테스트해본 컴퓨터에서는 Program Files
(x86)\Microsoft Visual Studio 14.0\
VC\lib

 

5. Windows\system32 폴더에 *.dll을 넣는다.

C:\Windows\system32 폴더에 
freeglut.dll를 넣습니다.
glew32.dll 를 넣습니다.
** 64-bit 운영체제 (Window8 기준)
C:\Windows\SysWOW64 폴더에도 
freeglut.dll를 넣습니다.
glew32.dll 를 넣습니다.

OPENGLGLUT-GETTING-STARTED

Windows7 운영체제에서 Visual Studio 2015과 OpenGL 3.x 이상을 지원하는 그래픽카드 드라이버가 설치되어 있어야 합니다.

1. freeglut는 아래 파일을 다운 받아 unzip해 주어야 합니다. 
http://freeglut.sourceforge.net/

압축을 풀어보면 include, lib, system32 세 개의 폴더가 있습니다.
C:\………\freeglut-MSVC-3.0.0-2.mp\freeglut\include\GL
freeglut.h freeglut_ext.h freeglut_std.h glut.h

C:\………\freeglut-MSVC-3.0.0-2.mp\freeglut\lib
freeglut.lib

C:\………\freeglut-MSVC-3.0.0-2.mp\freeglut\bin
freeglut.dll

2. glew는 아래 파일을 다운 받아 unzip해 주어야 합니다. 
http://glew.sourceforge.net/

압축을 풀어보면 include, lib, system32 세 개의 폴더가 있습니다.
C:\………\glew-2.0.0-win32\glew-2.0.0\include\GL
glew.h glxew.h wglew.h

C:\………\glew-2.0.0-win32\glew-2.0.0\lib\Release\Win32
glew32.lib glew32s.lib
C:\………\glew-2.0.0-win32\glew-2.0.0\lib\Release MX\Win32
glew32mx.lib glew32mxs.lib

C:\………\glew-2.0.0-win32\glew-2.0.0\bin\Release\Win32
glew32.dll
C:\………\glew-2.0.0-win32\glew-2.0.0\bin\Release MX\Win32
glew32mx.dll

3. glm는 아래 파일을 다운 받아 unzip해 주어야 합니다. 

https://github.com/g-truc/glm/releases

C:\………..\glm-0.9.7.6\glm 안에 glm 디렉토리 전체를 복사

4. Microsoft SDKs가 있는 디렉토리의 Include와 Lib 폴더에 *.h와 *.lib을 넣는다.

C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\gl 폴더에 freeglut.h freeglut_ext.h freeglut_std.h glut.h를 넣습니다.
C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\gl 폴더에 glew.h glxew.h wglew.h를 넣습니다.
C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include 폴더에 glm 폴더 전체를 복사해 넣습니다.C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib폴더에 freeglut.lib를 넣습니다.
C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib폴더에 glew32.lib glew32mx.lib glew32mxs.lib glew32s.lib를 넣습니다.

5. Windows\system32 폴더에 *.dll을 넣는다.

C:\Windows\system32 폴더에 freeglut.dll를 넣습니다.
C:\Windows\system32 폴더에 glew32.dll glew32mx.dll를 넣습니다.

 

** 64-bit 운영체제 (Window7 기준)
C:\Windows\SysWOW64 폴더에도 freeglut.dll를 넣습니다.
C:\Windows\SysWOW64 폴더에도 glew32.dll glew32mx.dll를 넣습니다.

6. 세팅이 모두 끝나면, 프로젝트를 Win32 Console로 생성(File->New->Project, 템플렛에서 Win32->Win32 Console Application을 선택하고 <your project name>을 넣는다)하고, 아래의 소스를 넣어 컴파일, 실행해 봅니다.

/* minimal program to open & clear a window */
#include <cmath>
#include <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/freeglut_ext.h>

 

void init(void)
{
}

 

void display( void )
{
glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
glClear( GL_COLOR_BUFFER_BIT ); // clear the window

 

glFlush();
}

 

void keyboard( unsigned char key, int x, int y )
{
switch ( key ) {
case 27:
exit( EXIT_SUCCESS );
break;
}
}

 

int main( int argc, char **argv )
{ 
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGBA );
glutInitWindowSize( 512, 512 );
glutCreateWindow( “Your First OpenGL” );glewInit();
init();

 

glutDisplayFunc( display );
glutKeyboardFunc( keyboard );

 

glutMainLoop();
return 0;
}

7. 이때, opengl32.lib glu32.lib glut32.lib 라이브러리 링크를 걸어줍니다.

Project->Properties(ALT+F7)->Configuration Properties탭->Linker탭->Input탭에 Additional Dependencies에 “glew32.lib;freeglut.lib”를 넣는다.

8. 컴파일/링크(Build)(F7)후 실행(Debug)(F5)시켜서 하얀색화면의 윈도우가 뜨면 완성된 것입니다. 

MODERN OPENGL TUTORIALS

Modern” OpenGL is about OpenGL 2.1+, OpenGL ES 2.0+ and WebGL, with a programmable pipeline and shaders.

Modern OpenGL Programminghttp://en.wikibooks.org/wiki/OpenGL_Programming#Modern_OpenGL

Modern OpenGL Step-by-Step 
http://ogldev.atspace.co.uk/

Learning Modern 3D Graphics Programming
http://www.arcsynthesis.org/gltut/index.html

OpenGLBook.com
http://openglbook.com/

AmazingKing Modern OpenGL Tutorials
http://www.theamazingking.com/ogl.php