140 likes | 254 Views
Opengl - practical. initialisation. You can use SDL or GLUT and it is done for you In Windows you need to use WinAPI ; this means : Registering a new window class with RegisterClassEx Changing the display settings with ChangeDisplaySettings Creating the window with CreateWindowEx
E N D
initialisation • Youcan use SDL or GLUT and it is done for you • In Windows you need to use WinAPI; this means: • Registering a new window class with RegisterClassEx • Changing the display settings with ChangeDisplaySettings • Creating the window with CreateWindowEx • Selecting and setting a pixel format with ChoosePixelFormat and SetPixelFormat • Creating an OpenGL context with wglCreateContext • Activating the OpenGL context with wglMakeCurrent • And finally showing the window with ShowWindow • The last step is loading any OpenGL extensions. This can be done manually or by using the GLEW (The OpenGL Extension Wrangler Library)
Setting-up our projection • OpenGL is a state based API • Before changing anything you need to tell it what matrix you want to change • To activate the projection matrix stack use glMatrixMode(GL_PROJECTION); • To compute a projection matrix use gluPerspective • Calling gluPerspective a second time will multiply the resultin matrix with the already existing one. It will not replace! • The modelview matrix stack can be activated with glMatrixMode(GL_MODELVIEW) • After this we can setup the camera with gluLookAt • Finally our window coordinates are configured with glViewport
Rendering an object • Immediate mode is deprecated in OpenGL • This means no more rendering with glBegin and glEnd • Rendering is done with vertex buffer objects (VBOs) • Advantages of VBOs: • Data is uploaded to GPU memory • Rendering is faster • Data can be removed from main memory • More clients can share the same rendering since the GPU has the data • Disadvantages of VBOs: • More difficult to learn
Setting up a vbo • Two types of VBOs: • Vertex elements; • Vertex indices; • Generate buffer with glGenBuffers(1, &mVboId); • Bind buffer so OpenGL knows which one you are using glBindBuffer(GL_ARRAY_BUFFER, mVboId); • GL_ARRAY_BUFFER – for vertex elements • GL_ELEMENT_ARRAY_BUFFER – for vertex indices • Upload the data to the GPU: glBufferData(GL_ARRAY_BUFFER, mDataSize, mVertexData, GL_STATIC_DRAW); • The last parameter indicates the usage of the data. In this case it’s static which means it will remain the same for the duration of the rendering
Rendering a VBO • Before rendering we need to specify the transform of the object • The active matrix stack needs to be modelview • Objects can be translated, rotated, scaled with glTranslate, glRotate, glScale • First we need to bind the buffers with glBindBuffer • Specify what data is in the buffer with glEnableClientState: • Possible values GL_VERTEX_ARRAY, GL_NORMAL_ARRAY, GL_TEXTURE_COORD_ARRAY
Rendering a VBO • The position of each vertex element inside the buffer is defined with: • glVertexPointer • glNormalPointer • glTexCoordPointer • The final step is the actual rendering • To render a non indexed buffer use glDrawArrays • To render an indexed buffer use glDrawElements
TEXTURING • Generate texture • glGenTextures(1, &mTextureId); • Bind texture • glBindTexture(GL_TEXTURE_2D, mTextureId); • Filtering used when a pixel is smaller than a texel • glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); • Filtering used when a pixel is bigger than a texel • glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); • Upload image data • glTexImage2D(GL_TEXTURE_2D, 0, depth, width, height, 0, format, GL_UNSIGNED_BYTE, bits);
GLSL • The OpenGL shading language • Based on the C programming language • Runs on the GPU • In OpenGL a program can contain a vertex, a geometry or a fragment shader • A program can be created with glCreateProgram • To create a shader there are 3 steps: • Create shaderglCreateShader ; can have as parameter GL_VERTEX_SHADER, GL_GEOMETRY_SHADER or GL_FRAGMENT_SHADER • Set shader source glShaderSource • Compile shaderglCompileShader
GLSL • Shadersneedtobeattachedtoprogramsandlinked: • glAttachShader • glLinkProgram • Tochangethevalue of a parameterweneedto: • Get theparameterglGetUniformLocation • Set thevaluewithglUniform • Beforerenderingwehavetospecifywhat program we are using: glUseProgram • Andweneedtobindeveryparameter of the program: glUniform
Resources • http://www.opengl.org/sdk/docs/man4/ • http://www.opengl.org/documentation/glsl/ • http://www.songho.ca/opengl/index.html • http://www.lighthouse3d.com/tutorials/glsl-tutorial/ • http://www.lighthouse3d.com/
Opencl • OpenCL is an API that provides parallel computing • It can run on GPUs and CPUs • The language is based on C99 • The API is very similar to that of OpenGL • To create an OpenCL application we need a context • OpenCL programs are similar to GLSL programs, but they have more freedom • It has inter-operability with OpenGL • Can help in speeding up AI computation, rendering computation etc.
Resources • http://www.khronos.org/opencl/ • http://www.khronos.org/webcl/ • http://developer.nvidia.com/cuda/gpu-computing-sdk • http://developer.download.nvidia.com/compute/cuda/4_2/rel/sdk/website/OpenCL/html/samples.html • http://opencl.codeplex.com/wikipage?title=OpenCL%20Tutorials%20-%201