CS考试辅导 CM0304 Graphics I Graphics Hardware I.1 Graphics Systems

CM0304 Graphics I Graphics Hardware I.1 Graphics Systems

CMT107 Visual Computing

Copyright By PowCoder代写 加微信 powcoder

I.4 Introduction to OpenGL

Xianfang Sun

School of Computer Science & Informatics
Cardiff University

➢ Introduction to OpenGL

• What is OpenGL

• OpenGL History

• OpenGL Pipeline

• OpenGL Components

• Java OpenGL (Jogl)

– Installation of Jogl on Eclipse

➢OpenGL Programming
• Basic OpenGL Coding Framework

• OpenGL Geometric Primitives

• A Simple OpenGL Program

10:32 AM 2CMT107 Visual Computing

What is OpenGL?

10:32 AM 3CMT107 Visual Computing

➢ OpenGL: Open Graphics Library
• Originally IRIS GL (Integrated Raster Imaging System

Graphics Library) from Silicon Graphics
➢ OpenGL is NOT a language, it is

– a software interface to graphics hardware
– a graphics programming library
– a standard for 3D graphics

➢ At the lowest possible level it still allows device
independence
• OpenGL is partly implemented in software and partly

in hardware depending on the device
• No high-level modelling operations, etc.

OpenGL History

10:32 AM 4CMT107 Visual Computing

OpenGL Release GLSL Release Year Features

1.0 — 1992 Fixed-function Pipeline

2.0 1.10 2004 vertex shaders and fragment shaders

2.1 1.20 2006

Deprecated features;
Geometry shaders from 3.2.

3.3 3.30 2010

4.0, 4.1 4.00, 4.10 2010 Tessellation shaders

4.2 4.20 2011

4.3 4.30 2012 Compute shaders

4.4 4.40 2013

4.5 4.50 2014

4.6 4.60 2017

The OpenGL Pipeline

10:32 AM 5CMT107 Visual Computing

The OpenGL Pipeline (Ver < 2.0) 10:32 AM 6CMT107 Visual Computing The OpenGL Pipeline (Ver = 2.0) 10:32 AM 7CMT107 Visual Computing The OpenGL Pipeline (Ver = 3.2) 10:32 AM 8CMT107 Visual Computing The OpenGL Pipeline (Ver = 4.0) 10:32 AM 9CMT107 Visual Computing OpenGL Components 10:32 AM 10CMT107 Visual Computing ➢ Components of the OpenGL interface: • GL: core OpenGL functions • GLU: graphics utility library (a variety of graphics accessory functions, e.g. gluLookAt) • GLUT: OpenGL Utility Toolkit (interface to windowing system via xlib; alternatives: glib+GTK, QT; helpers for creating common objects, e.g. spheres, the teapot) • GLX: low-level interface to X11 (different interfaces for other platforms: glw for windows) Java OpenGL (JOGL) 10:32 AM 11CMT107 Visual Computing ➢ Java OpenGL (JOGL) is a wrapper library that allows OpenGL to be used in the Java programming. ➢ JOGL 1.1.1 gives full access to the APIs in the OpenGL 2.0 specification and limited access to GLU NURBS, providing rendering of curved lines and surfaces via the traditional ➢ JOGL 2.0 provides full access to the APIs in the OpenGL 1.3 - 3.0, 3.1 - 3.3, ≥ 4.0, ES 1.x and ES 2.x specification as well as nearly all vendor extensions. ➢ Newest version (2.3.2) of JOGL can be downloaded from http://jogamp.org/deployment/jogamp-current/archive/ http://jogamp.org/deployment/jogamp-current/archive/ Installation of Jogl on Intellij 10:32 AM 12CMT107 Visual Computing ➢ Download and install Intellij. ➢ Download and install the latest Jogl api. ➢ Set up Jogl as a user library. ➢ Configure Jogl library in each OpenGL (Jogl) project. ➢ All downloads and install instruction are free available from related official sites. ➢ More detail about installation can be found in the file available from learningcentral. Basic OpenGL Coding Framework 10:32 AM 13CMT107 Visual Computing ➢ Configure OpenGL • Create window, Display mode ➢ OpenGL state initialisation • Set background colour, View positions, …… • Compile and link shader programs ➢ Set up Display Function • Render the scene ➢ Set up Reshape Function • resize the view window and recompute projection ➢ Process Event loop OpenGL Geometric Primitives 10:32 AM 14CMT107 Visual Computing A Simple OpenGL C Program (1) 10:32 AM 15CMT107 Visual Computing #include

#include

// Define: number of Vertex Array Objects,

// number of Vertex Buffer Objects,

// number of Vertices

const GLuint numVAOs = 1, numVBOs = 1;

const GLuint numVertices = 1;

// Specify the ids of points, buffers,

// and the vertex attribute position

// in the vertex shader program.

GLuint idPoint = 0, idBuffer = 0;

GLuint vPosition = 0;

// Declare VAOs and VBOs

GLuint VAOs[numVAOs];

GLuint VBOs[numVBOs];

A Simple OpenGL C Program (2)

10:32 AM 16CMT107 Visual Computing

// Define: Vertex shader program, and Fragment shader program

const GLchar* srcVShader =

“#version 330 core\n”

“layout(location = 0) in vec4 vPosition;”

“void main()”

” gl_Position = vPosition;”

const GLchar* srcFShader =

“#version 330 core\n”

“out vec4 fColor;”

“void main()”

” fColor = vec4(1.0, 0.0, 0.0, 1.0);”

A Simple OpenGL C Program (3)

10:32 AM 17CMT107 Visual Computing

void init(void) // initialisation

//Define vertices coordinates

GLfloat vertices[numVertices][2] = {

{0.0f, 0.0f}

//Generate vertex array objects (VAOs), and

//Bind a VAO, i.e., initialise this VAO.

// A second binding is needed later to use it

glGenVertexArrays(numVAOs, VAOs);

glBindVertexArray(VAOs[idPoint]);

//Generate vertex buffer objects (VBOs), and

//Bind a VBO, i.e., initialise this VBO.

glGenBuffers(numVBOs, VBOs);

glBindBuffer(GL_ARRAY_BUFFER, VBOs[idBuffer]);

//The Data is then pooled into the buffer

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),

vertices, GL_STATIC_DRAW);

//Specify the location and data format of the

//array of vertex attributes for rendering

glVertexAttribPointer(vPosition, 2, GL_FLOAT,

GL_FALSE, 0, (void*)(0));

glEnableVertexAttribArray(vPosition);

A Simple OpenGL C Program (4)

10:32 AM 18CMT107 Visual Computing

//Create a shader program

GLuint program = glCreateProgram();

//Compile and attach vertex shader

//into the program

GLuint shader = glCreateShader(GL_VERTEX_SHADER);

glShaderSource(shader, 1, &srcVShader, NULL);

glCompileShader(shader);

glAttachShader(program, shader);

glDeleteShader(shader);

//Compile and attach fragment shader

//into the program

shader = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(shader, 1, &srcFShader, NULL);

glCompileShader(shader);

glAttachShader(program, shader);

glDeleteShader(shader);

//Link and use the shader program

glLinkProgram(program);

glUseProgram(program);

A Simple OpenGL C Program (5)

10:32 AM 19CMT107 Visual Computing

// display the scene

void display(void)

glClear(GL_COLOR_BUFFER_BIT);

glPointSize(5);

//Bind VAO again to use it

glBindVertexArray(VAOs[idPoint]);

glDrawArrays(GL_POINTS, 0, numVertices);

glutSwapBuffers();

// resize the view window,

// and recompute projection matrices

void reshape(int width, int height){};

A Simple OpenGL C Program (6)

10:32 AM 20CMT107 Visual Computing

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

// Initialise GLUT

glutInit(&argc, argv);

glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );

glutInitWindowSize(512, 512);

// Create display window

glutCreateWindow(argv[0]);

// OpenGL Version and profile

glutInitContextVersion(3, 3);

glutInitContextProfile(GLUT_CORE_PROFILE);

// Deal with OpenGL extensions issues

glewExperimental = GL_TRUE;

if( GLEW_OK != glewInit() )

exit(EXIT_FAILURE);

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutMainLoop(); // Start GLUT event loop

A Simple OpenGL Java Program (1)

10:32 AM 21CMT107 Visual Computing

// Import some packages

import java.nio.FloatBuffer;

import com.jogamp.nativewindow.WindowClosingProtocol;

import com.jogamp.newt.opengl.GLWindow;

import com.jogamp.opengl.GL3;

import com.jogamp.opengl.GLAutoDrawable;

import com.jogamp.opengl.GLCapabilities;

import com.jogamp.opengl.GLEventListener;

import com.jogamp.opengl.GLProfile;

import com.jogamp.opengl.util.FPSAnimator;

// Import GL constant

import static com.jogamp.opengl.GL3.*;

A Simple OpenGL Java Program (2)

10:32 AM 22CMT107 Visual Computing

public class Simple implements GLEventListener {

private GLWindow window; // Declare a canvas

final FPSAnimator animator=new FPSAnimator(60, true);

// Define: number of Vertex Array Objects,

// number of Vertex Buffer Objects,

// number of Vertices

// Specify the ids of points, buffers,

// and the vertex attribute position

// in the vertex shader program

private int idPoint = 0, numVAOs = 1;

private int idBuffer = 0, numVBOs = 1;

private int vPosition = 0;

private final int numVertices = 1;

// Declare VAOs and VBOs

private int[] VAOs = new int[numVAOs];

private int[] VBOs = new int[numVBOs];

A Simple OpenGL Java Program (3)

10:32 AM 23CMT107 Visual Computing

private String[] srcVShader =

{ “#version 330 core \n”

+ “layout(location = 0) in vec4 vPosition;”

+ “void main()”

+ ” gl_Position = vPosition;”

private String[] srcFShader =

{ “#version 330 core\n”

+ “out vec4 fColor;”

+ “void main()”

+ ” fColor = vec4(1.0, 0.0, 0.0, 1.0);”

A Simple OpenGL Java Program (4)

10:32 AM 24CMT107 Visual Computing

public Simple() {

GLProfile glp = GLProfile.get(GLProfile.GL3);

GLCapabilities caps = new GLCapabilities(glp);

window = GLWindow.create(caps);

animator.add(window);

// Listen for openGL events

window.addGLEventListener(this);

window.setDefaultCloseOperation(

WindowClosingProtocol.WindowClosingMode.

DISPOSE_ON_CLOSE); //Exit when click close

window.setSize(500, 500); // set the window size

window.setTitle(“Simple Graphics”); // window title

window.setVisible(true); // Display the frame

animator.start();

A Simple OpenGL Java Program (5)

10:32 AM 25CMT107 Visual Computing

public void init(GLAutoDrawable drawable) {

// Get the GL pipeline object

GL3 gl = drawable.getGL().getGL3();

//Define the vertex coordinates

float[] vertexArray = { 0.0f, 0.0f };

//wrap the vertex array into a FloatBuffer.

FloatBuffer vertices = FloatBuffer.wrap(vertexArray);

// Generate vertex array objects (VAOs), and

// Bind a VAO, i.e., initialise this VAO.

// A second binding is needed later to use it

gl.glGenVertexArrays(numVAOs, VAOs, 0);

gl.glBindVertexArray(VAOs[idPoint]);

// Generate vertex buffer objects (VBOs), and

// Bind a VBO, i.e., initialise this VBO.

// The Data is then pooled into the buffer

gl.glGenBuffers(numVBOs, VBOs, 0);

gl.glBindBuffer(GL_ARRAY_BUFFER, VBOs[idBuffer]);

gl.glBufferData(GL_ARRAY_BUFFER, vertexArray.length *

(Float.SIZE / 8),vertices, GL_STATIC_DRAW);

gl.glVertexAttribPointer(vPosition,2,GL_FLOAT,false,0,0L);

gl.glEnableVertexAttribArray(vPosition);

A Simple OpenGL Java Program (6)

10:32 AM 26CMT107 Visual Computing

// Create a shader program

int program = gl.glCreateProgram();

// Compile and attach vertex shader into the program

int shader = gl.glCreateShader(GL_VERTEX_SHADER);

gl.glShaderSource(shader, 1, srcVShader, null);

gl.glCompileShader(shader);

gl.glAttachShader(program, shader);

gl.glDeleteShader(shader);

// Compile and attach fragment shader into the program

shader = gl.glCreateShader(GL_FRAGMENT_SHADER);

gl.glShaderSource(shader, 1, srcFShader, null);

gl.glCompileShader(shader);

gl.glAttachShader(program, shader);

gl.glDeleteShader(shader);

// Link and use the shader program

gl.glLinkProgram(program);

gl.glUseProgram(program);

A Simple OpenGL Java Program (7)

10:32 AM 27CMT107 Visual Computing

public void display(GLAutoDrawable drawable) {

GL3 gl = drawable.getGL().getGL3();

gl.glClear(GL_COLOR_BUFFER_BIT);

gl.glPointSize(5);

gl.glDrawArrays(GL_POINTS, 0, numVertices);

public void reshape(GLAutoDrawable drawable, int x, int y,

int width, int height) {

public void dispose(GLAutoDrawable drawable) {

System.exit(0);

public static void main(String[] args) {

new Simple();

10:32 AM 28CMT107 Visual Computing

➢What is the underlying model for the OpenGL library?
• What are the components of OpenGL?

➢ Basic OpenGL programming with C++ or Java.
• Describe the OpenGL coding framework.

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com