OpenGL - Assignment
OpenGL - Assignment
Q#1: Explore OpenGL. How to use it along with C language? Write a program to
draw a triangle using OpenGL and share screenshot and code?
Answer: Open GL
Open Graphics Library (OpenGL) is a cross-language, cross-platform application
programming interface (API) for rendering 2D and 3D vector graphics. The API is
typically used to interact with a graphics processing unit (GPU), to achieve
hardware-accelerated rendering.
Using It with C Language:
OpenGL is a C library, not a C++ one. The only thing why almost all programs use
C++ for OpenGL is there is a higher and simpler level manipulating it through
some wrappers, libraries or frameworks
Draw Triangle Code:
// Triangle.js
// Vertex shader program
var VSHADER_SOURCE =
'attribute vec4 a_Position;\n' +
'void main() {\n' +
' gl_Position = a_Position;\n' +
'}\n';
// Initialize shaders
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
// Draw a triangle
gl.drawArrays (gl.TRIANGLES, 0, n);
}
function initVertexBuffers(gl) {
var vertices = new Float32Array([
0.0, 0.5, -0.5, -0.5, 0.5, -0.5
]);
var n = 3; // The number of vertices
return n;
}
Output:
-------END------