unity - appspot.combranko-cirovic.appspot.com/cp3830/opengl.notes.pdf · what is opengl? opengl is...
TRANSCRIPT
![Page 1: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/1.jpg)
CP3830Computer Graphics
![Page 2: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/2.jpg)
OpenGL Topics
2D graphics : modelling and transformations
3D graphics : modelling and transformations
Bezier curves and surfaces
Lighting and Textures
![Page 3: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/3.jpg)
Unity3D TopicsScenes
Game Objects
Prefabs
Lights
Cameras
Materials
Mechanim
Particle Systems
Physics
C# Scripting
![Page 4: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/4.jpg)
Evaluation
Assignments: 60%
Project: 40%
![Page 5: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/5.jpg)
What is OpenGL?OpenGL is Graphics API - Application Programming Interface - set of functions we can call in our programs.
It hides details about OS, Windowing System, display etc.
Fundamental difference between fixed and programmable pipeline.
![Page 6: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/6.jpg)
Fixed Pipeline
CPU GPU
VRAM
FixedFunctionality
RAM
![Page 7: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/7.jpg)
Fixed Pipeline
Model Matrix
Projection Matrix
Scale / Translate / Rotate
Parallel / Perspective
Viewport Matrix
Model
Map 3D objects to 2D screen
View Matrix
Position camera
![Page 8: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/8.jpg)
Fixed PipelineModel: glutWireCube (1.0);
Model transformation: glScalef (1.0, 2.0, 1.0);
View transformation: gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
Projection matrix: glMatrixMode (GL_PROJECTION);
Viewport transformation: glViewport (0, 0, (GLsizei) w, (GLsizei) h);
![Page 9: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/9.jpg)
Programmable Pipeline
CPU GPU
VRAM
ProgrammableFunctionality
RAM
![Page 10: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/10.jpg)
Programmable PipelineVertex
Processor
Fragment Processor
Programmable
OutputMerging
Model
Rasterizer
Programmable
![Page 11: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/11.jpg)
Programmable Pipeline
Introduced in OpenGL 2.0
Advantages: faster, more realistic graphics
Disadvantages: You’ll find our shortly
![Page 12: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/12.jpg)
OpenGL 1.0 HierararchyGL library lowest level: vertices, matrix computations etc. e.g glVertex3f(0.0, 1.0, 0.0);
GLU (Utility) library helper functions for shapes, transformations etc. e.g gluCylinder(q, b, t, h, sl, st);
GLUT (Utility Toolkit) library highest level: window and interface management. e.g. glutInitWindowSize(640, 480);
![Page 13: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/13.jpg)
OpenGL API fixed
Specify the position of a “camera”
Specify geometry and appearance of objects
Specify materials and lights (optional)
Given OpenGL’s API, we need to:
OpenGL will compute and render the resulting image.
![Page 14: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/14.jpg)
OpenGL Function Format
glVertex3f(x,y,z);
belongs toGL library
functionname
number of arguments
type
![Page 15: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/15.jpg)
Xcode and OpenGLStart Xcode, new project, OS X Application, Command LineTool :
![Page 16: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/16.jpg)
In options, name project Hello and set type to C :
Xcode and OpenGL
![Page 17: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/17.jpg)
Select Project, in Build Settings set target to 10.8
Xcode and OpenGL
![Page 18: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/18.jpg)
Select Target, Build Phases, Link Binary with Libraries, and add OpenGL.framework and GLUT.framework.
Our first example will simply present black square on white background.
Xcode and OpenGL
![Page 19: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/19.jpg)
Hello
![Page 20: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/20.jpg)
main() function
int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(480, 480); glutInitWindowPosition(100, 100); glutCreateWindow("Hello"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }
#include <GLUT/glut.h>
void init(void); void display(void);
![Page 21: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/21.jpg)
init() functionvoid init(void) { glMatrixMode(GL_PROJECTION); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); }
glMatrixMode - specifies which matrix is the current matrix. Matrix representation of a vertex:
OpenGL uses homogeneous coordinates, we’ll talk about it later.
![Page 22: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/22.jpg)
GL_PROJECTION
Computer monitor is 2D surface and we need to transform 3D scene into 2D image.GL_PROJECTION matrix is used for this transformation.
![Page 23: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/23.jpg)
glOrtho functionArguments define coordinates of the clipping plane ( left, right, bottom, top, near, far ).
glOrtho describes a transformation that produces a parallel projection.
![Page 24: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/24.jpg)
display() functionvoid display(void) { glClearColor(1.0, 1.0, 1.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0, 0.0, 0.0); glBegin(GL_POLYGON); glVertex2f(0.25, 0.25); glVertex2f(0.75, 0.25); glVertex2f(0.75, 0.75); glVertex2f(0.25, 0.75); glEnd();
glFlush(); }
![Page 25: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/25.jpg)
Compile and Run
On Mac, in Xcode, hit run and you are done.
On Linux you’ll have to install FreeGlut/MESA libraries and write Makefile
On Windows - it’s doable, but how, I have no idea.
![Page 26: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/26.jpg)
Shaders businessThe next example uses OpenGL 3.2, courtesy of Tom Dalling (http://www.tomdalling.com)
Well, for this you’ll need third party libraries GLEW, GLFW and GLM. Why?
Because almost all the API functionality is gone. So, you’ll either have to write your own libraries or find them somewhere?!
![Page 27: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/27.jpg)
GLEWThe OpenGL Extension Wrangler (GLEW) is what we will be using to give us access to the OpenGL 3.2 API functions.
Unfortunately, accessing OpenGL functions isn't as simple as #include <GL/gl.h>.
In modern OpenGL, the API functions are determined at run time, not compile time.
![Page 28: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/28.jpg)
GLFW
GLFW will allow us to create a window, and receive mouse and keyboard input in a cross-platform way.
OpenGL does not handle window creation or input, so this must be done somewhere else.
![Page 29: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/29.jpg)
GLMOpenGL Mathematics (GLM) is a mathematicslibrary that handles vectors and matrices, amongst other things.
Older versions of OpenGL provided functions like glRotate, glTranslate and glScale.
In modern OpenGL, the previously mentioned functions do not exist, and we must do all of the math ourselves.
![Page 30: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/30.jpg)
Shaders
Shaders are “little programs”, made from GLSL code, that run on the GPU instead of the CPU.
They are written in OpenGL Shading Language (GLSL), which looks like C or C++.
![Page 31: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/31.jpg)
Vertex Shader
The Vertex Shader is the programmable Shader stage in the rendering pipeline that handles the processing of individual vertices.
Vertex shaders are fed Vertex Attribute data, as specified from a vertex array object by a drawing command.
![Page 32: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/32.jpg)
Vertex ShaderA
B
C
![Page 33: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/33.jpg)
vertex-shader.txt
#version 150
in vec3 vert;
void main() { gl_Position = vec4(vert, 1); }
![Page 34: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/34.jpg)
Triangle AssemblyA
B
C
![Page 35: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/35.jpg)
RasterizationA
B
C
![Page 36: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/36.jpg)
Fragment Shader
The main purpose of a fragment shader is to calculate the color of each pixel that is drawn.
A “fragment” is basically a pixel, so you can think of fragment shaders as “pixel shaders”.
![Page 37: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/37.jpg)
Fragment ShaderA
B
C
![Page 38: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/38.jpg)
fragment-shader.txt
#version 150
out vec4 finalColor;
void main() {
finalColor = vec4(1.0, 1.0, 1.0, 1.0); }
![Page 39: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/39.jpg)
VBOs and VAOsSince shaders run on the GPU and the rest of the C++ code runs on the CPU, we need a way of sending data from the CPU to the GPU.
This is where we need Vertex Buffer Objects (VBOs) and Vertex Array Objects (VAOs).
VBOs and VAOs are used to take data from yourC++ program and send it through to the shaders for rendering.
![Page 40: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/40.jpg)
Vertex Buffer ObjectVBO is used to move data, in our case three vertices, from RAM to VRAM.
glGenBuffers(1, &gVBO); glBindBuffer(GL_ARRAY_BUFFER, gVBO); GLfloat vertexData[] = { 0.0f, 0.8f, 0.0f, -0.8f,-0.8f, 0.0f, 0.8f,-0.8f, 0.0f, };
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
![Page 41: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/41.jpg)
Vertex Array ObjectVAOs are the link between the VBOs and the shader variables.
VAOs describe what type of data is contained within a VBO, and which shader variables the data should be sent to.glEnableVertexAttribArray(gProgram->attrib("vert"));
glVertexAttribPointer(gProgram->attrib("vert"), 3, GL_FLOAT, GL_FALSE, 0, NULL);
![Page 42: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/42.jpg)
AppMain - Initialize GLFW
The glfwSetErrorCallback(OnError) line tells GLFW to call the OnError function whenever an error occurs.
glfwSetErrorCallback(OnError);
if(!glfwInit()) throw std::runtime_error("glfwInit failed");
![Page 43: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/43.jpg)
Create WindowglfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); gWindow = glfwCreateWindow((int)SCREEN_SIZE.x, (int)SCREEN_SIZE.y, "OpenGL Tutorial", NULL, NULL);
if(!gWindow) throw std::runtime_error("glfwCreateWindow failed. Can your hardware handle OpenGL 3.2?”);
glfwMakeContextCurrent(gWindow);
![Page 44: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/44.jpg)
Initialize GLEW
This step is required in order to access OpenGL APIs.
glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK) throw std::runtime_error("glewInit failed");
if(!GLEW_VERSION_3_2) throw std::runtime_error("OpenGL 3.2 API is not available.");
See if OpenGL 3.2 is available.
![Page 45: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/45.jpg)
LoadShaders()
Here we compile and link a vertex shader and a fragment shader using the tdogl::Shader and tdogl::Program classes provided.
std::vector<tdogl::Shader> shaders; shaders.push_back(tdogl::Shader::shaderFromFile( ResourcePath(“vertex-shader.txt"), GL_VERTEX_SHADER));
shaders.push_back(tdogl::Shader::shaderFromFile( ResourcePath(“fragment-shader.txt"), GL_FRAGMENT_SHADER));
gProgram = new tdogl::Program(shaders);
![Page 46: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/46.jpg)
LoadTriangle()
Used to create Vertex Buffer and Vertex Array Objects.
These two variables are global.
GLuint gVAO = 0; GLuint gVBO = 0;
![Page 47: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/47.jpg)
Render()At this point the shaders, VBO, and VAO are ready for use. All we have to do now is draw them inside the Render function.
First we clear the screen so that it is completely black:
glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT);
![Page 48: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/48.jpg)
Render()
Next we tell OpenGL that we want to start using our shaders and our VAO:
glUseProgram(gProgram->object()); glBindVertexArray(gVAO);
![Page 49: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/49.jpg)
Render()At last, we can draw that ever-elusive triangle:
glDrawArrays(GL_TRIANGLES, 0, 3);
This call to glDrawArrays says that we want to draw triangles, starting at vertex zero, and ending after three vertices have been sent to the shader.
The vertices will be pulled out of the VBO and sent to the vertex shader. Each pixel within the triangle will be sent to the fragment shader.
![Page 50: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/50.jpg)
Render()The drawing is finished now, so we unbind the shaders and the VAO just to be safe:
glBindVertexArray(0); glUseProgram(0);
The last thing that needs to be done before we can see the triangle is to swap the frame buffers:
glfwSwapBuffers(gWindow);
![Page 51: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/51.jpg)
Enjoying shaders?
Lots of code for a single triangle. Not counting all the supporting libraries and resources.
For the rest of OpenGL overview, I’ll stick with deprecated functionality to introduce some concepts that we’ll need in Unity.
![Page 52: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/52.jpg)
Transformation
There are three fundamental transformations in OpenGL - Translation, Scaling and Rotation.
![Page 53: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/53.jpg)
Translation
1 2 3 4 5 6 7 8
23456
87
1
Translation is done by vector addition.
Example: Given a triangle with vertex vectors( 2, 2 ), ( 4, 6 ), ( 6, 2 ) and displacement vector T = ( 1, 1 ), the resultant vector set is ( 3, 3 ), ( 5, 7 ) and ( 7, 3 ).
![Page 54: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/54.jpg)
Scaling
1 2 3 4 5 6 7 8
23456
87
1
Scaling is done by vector - scalar multiplication.
Example: Given a triangle with vertex vectors ( 2, 2 ), ( 4, 6 ), ( 6, 2 ) and scalar constant S = 0.5, the resultant vector set is ( 1, 1 ), ( 2, 3 ) and ( 3, 1 ).
![Page 55: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/55.jpg)
Rotation
1 2 3 4 5 6 7 8
23456
87
1
Rotation is accomplished by vector multiplication.
Example: Given a line segment with vertex vectors ( 0, 0 ), ( 4, 0 ), and rotation angle 90 degrees, the resultant vector set is ( 0, 0 ), ( 0, 4 ).
![Page 56: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/56.jpg)
Recall the three types of transformations:
Translation V’ = V + T
Scaling V’ = S・T
Rotation V’ = R・T
How to use matrix multiplication for all transformations?
Homogeneous coordinates
And the answer is: homogenous coordinates
![Page 57: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/57.jpg)
Homogeneous coordinates add an extra coordinate to a point, i.e., point ( x, y ) is represented as( x, y, W )
Two homogeneous coordinates represent the same point if they are multiple of each other. ( 1, 2, 1 ) and ( 2, 4, 2 ) represent the same point.
If we set W to be 1, then the homogenized point would be ( x / W, y / W, 1 )
Homogeneous coordinates
![Page 58: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/58.jpg)
Homogeneous coordinatesNow, translation can be performed with matrix multiplication:
where:
![Page 59: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/59.jpg)
Homogeneous coordinatesTransformations for scaling and rotation remain the same:
Scaling
where:
![Page 60: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/60.jpg)
Homogeneous coordinatesRotation
where:
![Page 61: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/61.jpg)
OpenGL Transformations
glTranslatef(GLfloat x, GLfloat y, GLfloat z);
glScalef(GLfloat x, GLfloat y, GLfloat z);
glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
OpenGL has special functions for modelling transformations:
![Page 62: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/62.jpg)
Order of TransformationsWhen using OpenGL transformation functions, they are applied in reversed order.
For example, if we had: glTranslatef(1.0, 1.0, 0.0); glScalef(0.5, 0.5, 0.0); glRotatef(90.0, 0.0, 0.0, 1.0); square();
Then rotation is applied first to square, than scaling and finally translation.
![Page 63: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/63.jpg)
3D ModellingSo far, we have been using orthogonal projection only. For 3D realistic scenes, first of all, we need perspective.
![Page 64: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/64.jpg)
Perspective
In OpenGL perspective is defined as:gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble znear, GLdouble zfar);
![Page 65: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/65.jpg)
fovy - specifies field of view angle, in degrees, in the y direction
aspect - specifies the aspect ratio that determines the field of view in the x direction ( width / height )
znear - distance from the viewer to the near clipping plane
zfar - distance from the viewer to the far clipping plane
Perspective
![Page 66: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/66.jpg)
CameraSo far, we haven’t explicitly defined the position of the camera. In 3D-modelling we have to do that.
![Page 67: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/67.jpg)
CameraIn OpenGL camera is defined as: gluLookAt(GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble lookatX, GLdouble lookatY, GLdouble lookatZ, GLdouble upX, GLdouble upY, GLdouble upZ,);
![Page 68: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/68.jpg)
Camera
eyeX,eyeY,eyeZ - specify the position of the eye point.
lookatX,lookatY,lookatZ - specify the position of the reference ( “look at” ) point.
upX,upY,upZ - specify the direction of the “up” vector, typically ( 0, 1, 0 ).
![Page 69: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/69.jpg)
3D Objects
The following examples demonstrate built-in 3D primitives such as cube, sphere cone, etc. both wireframe and solid.
Functions main(), init() and display() are identical, except for the actual 3D construct in display, just before flush.
![Page 70: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/70.jpg)
Cube
glutWireCube(GLdouble size);glutSolidCube(GLdouble size);;
Argument is cube side’s length.
![Page 71: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/71.jpg)
Sphere
glutWireSphere( GLdouble radius, GLint nslices, GLint nstacks);
where nslices and nstacks are subdivisions / bands around z-axis ( longitude / latitude ).
![Page 72: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/72.jpg)
ConeglutWireCone( GLdouble base, GLdouble height, GLint nslices, GLint nstacks);
where base is the radius of the base, nslices and nstacks are subdivisions / bands around z-axis.
![Page 73: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/73.jpg)
Teapot
glutWireTeapot( GLdouble size);
where size is the relative size of the teapot.
![Page 74: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/74.jpg)
Torus
glutWireTorus( GLdouble inRadius, GLdouble outRadius, GLint nslices, GLint nstacks);
where in/outRadius are inner / outer radii, nslices and nstacks are subdivisions / bands around z-axis.
![Page 75: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/75.jpg)
Tetrahedron
glutWireTetrahedron();
renders a tetrahedron ( 4-sided ) with a radius of √3.
![Page 76: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/76.jpg)
Dodecahedron
glutWireDodecahedron();
renders a tetrahedron (12-sided) with a radius of √3.
![Page 77: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/77.jpg)
CylinderGLUquadricObj *qobj;qobj = gluNewQuadric();gluQuadricDrawStyle(qobj, GLU_LINE);
gluCylinder(qobj, GLdouble baseRad, GLdouble topRad, GLdouble height, GLdouble nslices, GLdouble nstacks);
where base/topRad are base / top radii, nslices and nstacks are subdivisions / bands around z-axis.
![Page 78: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/78.jpg)
3D TransformationsAll of the transformations we have seen have similar representations in 3 dimensions: translation, scaling and rotation. Example:
![Page 79: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/79.jpg)
Spheres.c - header
#include <GL/glut.h> #include <unistd.h>
static GLfloat alpha = 0.0;
void init(void); void display(void); void rotate(void);
![Page 80: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/80.jpg)
Spheres.c - mainint main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(640, 320); glutInitWindowPosition(100,100); glutCreateWindow("Spheres"); init(); glutDisplayFunc(display); glutIdleFunc(rotate); glutMainLoop(); return 0; }
glutIdleFunc(*func)- continuously called when events are not received.
![Page 81: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/81.jpg)
Spheres.c - init
void init() { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); gluPerspective(45, 1, 1, 10); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(2.3, 1.3, 2, 0, 0.25, 0, 0, 1, 0); }
![Page 82: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/82.jpg)
Spheres.c - displayvoid display(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3d(1, 0, 0); glBegin(GL_LINE_STRIP); glVertex3i(0, 0, 0); glVertex3i(1, 0, 0); glEnd(); glBegin(GL_LINE_STRIP); glVertex3i(0, 0, 0); glVertex3i(0, 1, 0); glEnd(); glBegin(GL_LINE_STRIP); glVertex3i(0, 0, 0); glVertex3i(0, 0, 1); glEnd();
![Page 83: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/83.jpg)
Spheres.c - display glColor3d(1, 1, 0); glPushMatrix(); glRotatef(alpha, 0, 1, 0); glTranslatef(1.0, 0.0, 0.0); glutWireSphere(0.25, 20, 20); glRotatef(alpha, 0, 1, 0); glTranslatef(0.5, 0.0, 0.0); glutWireSphere(0.125, 20, 20); glPopMatrix(); glutSwapBuffers(); }
![Page 84: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/84.jpg)
Spheres.c - rotate
void rotate(void) { alpha = alpha + 1.0; if(alpha > 360.0) alpha = 1.0; usleep(5000); glutPostRedisplay(); }
![Page 85: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/85.jpg)
“Realistic” scenesLet’s switch from Wire to Solid model:
Realistic? For “realism” we need lights and materials.
![Page 86: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/86.jpg)
LightFrom a physical perspective, a surface can either emit light by self emission ( e.g. sun )
reflect light from other sources that illuminate it ( e.g. moon )
![Page 87: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/87.jpg)
Types of LightAmbient - rays come from and scatter into all directions
Diffuse - rays come from one direction, scatter into all directions
Specular - rays come from one direction, scatter into one direction
![Page 88: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/88.jpg)
Effect
Diffuse
Specular
Ambient
![Page 89: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/89.jpg)
Lighting in OpenGL
Create and position one or more light sources glLightfv(GLenum light, GLenum pname, const GLfloat param);
light is specified by symbolic name GL_LIGHT0.At least 8 lights are supported ( GL_LIGHT0 - GL_LIGHT8 )
![Page 90: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/90.jpg)
pname defines properties
param specifies the set of parameters for properties
glLightfv(GLenum light, GLenum pname, const GLfloat param);
Lighting in OpenGL
![Page 91: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/91.jpg)
PropertiesParameter Default value Meaning
GL_AMBIENT ( 0.0, 0.0, 0.0, 1.0 ) ambient light intensity
GL_DIFFUSE ( 1.0, 1.0, 1.0, 1.0 ) diffuse light intensity
GL_SPECULAR ( 1.0, 1.0, 1.0, 1.0 ) specular light intensity
GL_POSITION ( 0.0, 0.0, 1.0, 0.0 ) ( x, y, z, w ) position of the light
GL_SPOT_DIRECTION ( 0.0, 0.0, -1.0 ) ( x, y, z ) direction of the spotlight
![Page 92: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/92.jpg)
Parameter Default value Meaning
GL_SPOT_EXPONENT 0 spotlight exponent
GL_SPOT_CUTOFF 180 spotlight cutoff angle
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat white_light[] = { 1.0, 1.0, 1.0, 0.0 }; glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light); glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
Properties
![Page 93: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/93.jpg)
PropertiesIn order to enable lighting we need: glEnable(GL_LIGHTING) and glEnable(GL_LIGHT0)
In order to specify shading we have to define it asglShadeModel(GL_SMOOTH);
Finally we enable and set depth: glEnable(GL_DEPTH_TEST);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
![Page 94: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/94.jpg)
Material propertiesMaterial properties are set with glMaterialfv(GLenum face, GLenum pname, const GLfloat param);
face can be GL_FRONT, GL_BACK or GL_FRONT_AND_BACK.
pname defines properties
param specifies the set of parameters for properties
![Page 95: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/95.jpg)
Material propertiesParameter Default value Meaning
GL_AMBIENT ( 0.2, 0.2, 0.2, 1.0 ) ambient colour of material
GL_DIFFUSE ( 0.8, 0.8, 0.8, 1.0 ) diffuse colour of material
GL_SPECULAR ( 0.0, 0.0, 0.0, 1.0 ) specular colour of material
GL_EMISSION ( 0.0, 0.0, 0.0, 1.0 ) emissive colour of material
GL_SHININESS 0 specular exponent
![Page 96: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/96.jpg)
Light.c
![Page 97: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/97.jpg)
Light.c - header and init#include <GL/glut.h>
void init(void); void display(void);
void init(void) { glMatrixMode (GL_PROJECTION); glLoadIdentity(); glOrtho (-1.5, 1.5, -1.5, 1.5, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
![Page 98: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/98.jpg)
Light.c - displayvoid display(void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat white_light[] = { 1.0, 1.0, 1.0, 0.0 }; glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_SMOOTH); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
![Page 99: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/99.jpg)
Light.c - display
glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light); glLightfv(GL_LIGHT0, GL_SPECULAR, white_light); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST);
glutSolidSphere (1.0, 48, 48); glFlush (); }
![Page 100: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/100.jpg)
Light.c - mainint main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow ("Light"); init (); glutDisplayFunc(display); glutMainLoop(); return 0; }
![Page 101: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/101.jpg)
MoveLight.c
![Page 102: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/102.jpg)
SolidSpheres.c
![Page 103: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/103.jpg)
SolidRobot.c
![Page 104: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/104.jpg)
Bezier Surface
![Page 105: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/105.jpg)
Bezier Surface
Curve: y = f ( x ), e.g. Circle: y = √(r² - x²) 0 ≤ | x | ≤ r
Surface: z = f ( x, y ), e.g. Spherical surface: z = √(r² - x² - y² )
Explicit representation:
Implicit representation:
Surface: f ( x, y, z ) = 0, e.g. Spherical surface: x² + y² + z² - r² = 0
![Page 106: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/106.jpg)
QuadricsA quadric surface is defined by the general quadric equation: a1x2 + a2y2 + a3z2 + a4xy + a5yz + a6xz + a7x + a8y + a9z = 0
In order to use quadrics objects we typically:
Declare quadric object and create it: GLUquadricObj *qobj = gluNewQuadric();
![Page 107: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/107.jpg)
Quadrics
Use gluQuadricDrawStyle(qobj, GLU_LINE) to determine whether the object is rendered as wireframe or solid
Invoke rendering routine gluCylinder(), gluSphere(), gluDisk() or gluPartialDisk()
![Page 108: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/108.jpg)
Evaluators
Provide a way to specify points on a curve or surface
Allow us to describe any polynomial splines or surfaces of any degree ( B-splines, NURBS - Non-uniform B-splines, Bezier curves and surfaces etc. )
![Page 109: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/109.jpg)
Evaluators
CommandglMap1f(GLenum target, TYPE u1, TYPE u2, GLint offset, GLint order, const TYPE *points);defines one-dimensional evaluator.
![Page 110: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/110.jpg)
EvaluatorsGLenum target specifies what the control point represents. For example, GL_MAP1_VERTEX_3 specifies that x, y and z coordinates will be provided.
TYPE u1, TYPE u2 indicate the range of variable u, typically ( 0, 1 ) in the parametric form of the curve C[u] = [ X( u ) Y( u ) Z( u )]
GLint offset is an offset value between the beginning of one control point and the next.
![Page 111: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/111.jpg)
GLint order is the degree of the polynomial plus one and should agree with the number of points
const TYPE *points is a pointer to the first coordinate of the first control point
Example:glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]);
Evaluators
![Page 112: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/112.jpg)
EvaluatorsIn order to enable evaluator, we write glEnable(GL_MAP1_VERTEX_3)assuming target was GL_MAP1_VERTEX_3
In display(void), we finally render curve using glEvalCoord1f((GLfloat) u) where u is the domain coordinate previously defined in glMap1f
![Page 113: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/113.jpg)
2D EvaluatorsSimilar to one dimension, except that all commands take two parameters, u and v, respectively.
Define evaluator(s) with glMap2{fd}()
Enable evaluator(s) with glEnable()
Invoke evaluator(s) by calling glEvalCoord2d() or with glMapGrid2() and glEvalMesh2()
![Page 114: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/114.jpg)
2D EvaluatorsCommand
glMap2f(GLenum target, TYPE u1, TYPE u2, GLint u_offset, GLint u_order, TYPE v1, TYPE v2, GLint v_offset, GLint v_order, const TYPE *points);
defines two-dimensional evaluator.
Command glEnable(GL_MAP2_VERTEX_3) enables the corresponding evaluator with x, y and z coordinates to be provided.
![Page 115: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/115.jpg)
BezierMesh.c
![Page 116: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/116.jpg)
BezierMesh.c - header#include <GL/glut.h>
GLfloat ctrlpts[4][4][3] = { { {-1.5, -1.5, 4.0}, {-0.5, -1.5, 2.0}, { 0.5, -1.5, -1.0}, { 1.5, -1.5, 2.0} }, { {-1.5, -0.5, 1.0}, {-0.5, -0.5, 3.0}, { 0.5, -0.5, 0.0}, { 1.5, -0.5, -1.0} }, { {-1.5, 0.5, 4.0}, {-0.5, 0.5, 0.0}, { 0.5, 0.5, 3.0}, { 1.5, 0.5, 4.0} }, { {-1.5, 1.5, -2.0}, {-0.5, 1.5, -2.0}, { 0.5, 1.5, 0.0}, { 1.5, 1.5, -1.0} } };
void init(void); void display(void);
![Page 117: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/117.jpg)
BezierMesh.c - mainint main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(480, 480); glutInitWindowPosition(100, 100); glutCreateWindow("Bezier Mesh"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }
![Page 118: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/118.jpg)
BezierMesh.c - displayvoid display(void) { int i, j; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glPushMatrix(); glRotatef(85.0, 1.0, 1.0, 1.0); for(j = 0; j <= 30; j++) { glBegin(GL_LINE_STRIP); for(i = 0; i <= 30; i++) glEvalCoord2f((GLfloat) i / 30.0, (GLfloat) j / 30.0); glEnd();
![Page 119: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/119.jpg)
BezierMesh.c - display
glBegin(GL_LINE_STRIP); for(i = 0; i <= 30; i++) glEvalCoord2f((GLfloat) j / 30.0, (GLfloat) i / 30.0); glEnd(); }
![Page 120: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/120.jpg)
BezierMesh.c - display glPointSize(5.0); glColor3f(1.0, 1.0, 0.0); glBegin(GL_POINTS); for (i = 0; i < 16; i++) { for (j = 0; j < 16; j++) { glVertex3fv(&ctrlpoints[i][j][0]); } } glEnd();
glPopMatrix(); glFlush(); }
![Page 121: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/121.jpg)
Textures
Refers to applying images / patterns to a surface
Individual values in texture array are often called texels, defined by texture coordinates T( s, t ).
![Page 122: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/122.jpg)
Textures
Texture map associates a texel with each point in geometric object that is itself mapped to screen coordinates for display
Linear mapping a point on texture T( s, t ) to a point p( u, v ) is defined as: u = as + bt + c and v = ds + et + f
![Page 123: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/123.jpg)
Required stepsEnable Texture Mapping glEnable(GL_TEXTURE_1D); glEnable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_1D); glDisable(GL_TEXTURE_2D);
Create a texture object GLubyte* texture;
typically by reading a raw byte image ( pnm - portable any map format ).
![Page 124: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/124.jpg)
Build series of pre-filtered two-dimensional texture maps of decreasing resolutions ( mipmaps ) gluBuild2DMipmaps(GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLubyte *texture);
Mipmaps
![Page 125: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/125.jpg)
Mipmaps
GLenum target - specifies the target texture. Must be GL_TEXTURE_2D
GLint internalFormat - specifies internal storage format of the texture image, typically 1
GLsizei width and GLsizei height - specify the width and height of the texture image in pixels.
![Page 126: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/126.jpg)
MipmapsGLenum format - specifies the format of the pixel data, typically GL_RGB
GLenum type - specifies the datatype for data
GLubyte *texture - specifies the pointer to the image data in memory
Example:gluBuild2DMipmaps(GL_TEXTURE_2D, 1, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, texture);
![Page 127: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/127.jpg)
Required steps
Create or use named texture glBindTexture(GL_TEXTURE_2D, 0);
Specify texture environment ( optional, mixing colours and texture colours e.g. )
![Page 128: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/128.jpg)
Required steps
Setup texture parameters ( optional, bilinear filters, clamping, repeating etc. )
Specify texels ( texture coordinates ) glTexCoord2f(float s, float t);
![Page 129: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/129.jpg)
Rotating Earth
![Page 130: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/130.jpg)
Earth.c - header#include <GLUT/glut.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>
GLfloat ambient[3]; GLfloat diffuse[3]; GLfloat specular[3]; GLfloat shininess[] = {50.0f};
GLubyte* texture;
static GLfloat alpha = 0.0;
![Page 131: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/131.jpg)
Earth.c - header
unsigned char* readPnm(char*, int*, int*); void getTexture(void); void init(void); void display(void); void rotate(void);
![Page 132: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/132.jpg)
Earth.c - mainint main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(480, 480); glutInitWindowPosition(100,100); glutCreateWindow("Earth"); glutDisplayFunc(display); getTexture(); glutIdleFunc(rotate); init(); glutMainLoop(); return 0; }
![Page 133: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/133.jpg)
Earth.c - readPnmunsigned char* readPnm(char* filename, int* width, int* height) { FILE* fp; int i, w, h, d; unsigned char* image; char head[70]; fp = fopen(filename, "rb"); if (!fp) { perror(filename); return NULL; }
![Page 134: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/134.jpg)
Earth.c - readPnm
fgets(head, 70, fp);
if (strncmp(head, "P6", 2)) { fprintf(stderr, "%s: Not a raw PNM file\n", filename); return NULL; }
![Page 135: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/135.jpg)
Earth.c - readPnm i = 0; while(i < 3) { fgets(head, 70, fp); if (head[0] == '#') continue; if (i == 0) i += sscanf(head, "%d %d %d", &w, &h, &d); else if (i == 1) i += sscanf(head, "%d %d", &h, &d); else if (i == 2) i += sscanf(head, "%d", &d); }
![Page 136: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/136.jpg)
Earth.c - readPnm
image = (unsigned char*)malloc (sizeof(unsigned char) * w * h * 3); fread(image, sizeof(unsigned char), w * h * 3, fp); fclose(fp); *width = w; *height = h; return image; }
![Page 137: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/137.jpg)
Earth.c - getTexture
void getTexture(void) { GLint w, h; texture = (GLubyte*) readPnm("/Users/branko/ Desktop/Earth/Earth/earth.pnm”, &w, &h); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, w, h, GL_RGB, GL_UNSIGNED_BYTE, texture); free(texture); }
![Page 138: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/138.jpg)
Earth.c - initvoid init(void) { glMatrixMode(GL_PROJECTION); gluPerspective(45, 1, 0.1, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(2.3, 1.3, 2, 0, 0.25, 0, 0, 1, 0); GLfloat lightIntensity[] = {3.0f, 3.0f, 3.0f, 1.0f}; GLfloat lightPosition[] = {2.0f, 6.0f, 3.0f, 0.0f};
![Page 139: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/139.jpg)
Earth.c - init glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntensity); glClearColor(0.0, 0.0, 0.0, 0.0); }
![Page 140: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/140.jpg)
Earth.c - display
void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); glPushMatrix(); glRotatef(alpha, 0, 1, 0);
![Page 141: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/141.jpg)
Earth.c - display
GLUquadricObj *qObj = gluNewQuadric(); gluQuadricNormals(qObj, GLU_SMOOTH); gluQuadricTexture(qObj, GL_TRUE); glRotatef(90, 1, 0, 0); gluSphere(qObj, 1.0f, 128, 128); glPopMatrix(); glutSwapBuffers(); }
![Page 142: Unity - appspot.combranko-cirovic.appspot.com/CP3830/OpenGL.notes.pdf · What is OpenGL? OpenGL is Graphics API - Application Programming Interface - set of functions we can call](https://reader034.vdocuments.us/reader034/viewer/2022052313/5a708f3a7f8b9ab6538c0cad/html5/thumbnails/142.jpg)
Earth.c - rotate
void rotate(void) { alpha = alpha + 1.0; if(alpha > 360.0) alpha = 1.0; usleep(25000); glutPostRedisplay(); }