diff --git a/TermProject/CAP6721-Project-YazenGhannam.js b/TermProject/CAP6721-Project-YazenGhannam.js index dd684b4..6a92dd7 100644 --- a/TermProject/CAP6721-Project-YazenGhannam.js +++ b/TermProject/CAP6721-Project-YazenGhannam.js @@ -5,8 +5,20 @@ CAP 6721 Project */ -var gl = null; +var glCtx = null; +var clCtx = null; var shaderProgram = null; +var platforms = null; +var devices = []; +var canvas = null; +var canvasCtx = null; +var raster = null; +var rv = null; +var device = null; +var clCQ = null; +var clProgram = null; +var clKernels = new Object(); +var clBuffers = new Object(); var numSpheres = 500; var spherePositions = []; var startDraw = false; @@ -21,59 +33,59 @@ function parseShader(shaderFile){ function initShaders(){ var vertexShaderCode = parseShader("vertex.shader"); - var vertexShader = gl.createShader(gl.VERTEX_SHADER); - gl.shaderSource(vertexShader, vertexShaderCode); - gl.compileShader(vertexShader); + var vertexShader = glCtx.createShader(glCtx.VERTEX_SHADER); + glCtx.shaderSource(vertexShader, vertexShaderCode); + glCtx.compileShader(vertexShader); - if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) { + if (!glCtx.getShaderParameter(vertexShader, glCtx.COMPILE_STATUS)) { alert("Vertex Shader compilation failed: " + - gl.getShaderInfoLog(vertexShader)); + glCtx.getShaderInfoLog(vertexShader)); } var fragmentShaderCode = parseShader("fragment.shader"); - var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); - gl.shaderSource(fragmentShader, fragmentShaderCode); - gl.compileShader(fragmentShader); + var fragmentShader = glCtx.createShader(glCtx.FRAGMENT_SHADER); + glCtx.shaderSource(fragmentShader, fragmentShaderCode); + glCtx.compileShader(fragmentShader); - if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) { + if (!glCtx.getShaderParameter(fragmentShader, glCtx.COMPILE_STATUS)) { alert("Fragment Shader compilation failed: " + - gl.getShaderInfoLog(fragmentShader)); + glCtx.getShaderInfoLog(fragmentShader)); } - shaderProgram = gl.createProgram(); - gl.attachShader(shaderProgram, vertexShader); - gl.attachShader(shaderProgram, fragmentShader); - gl.linkProgram(shaderProgram); - if(!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)){ - alert("Failed to link shaders: " + gl.getProgramInfoLog(shaderProgram)); + shaderProgram = glCtx.createProgram(); + glCtx.attachShader(shaderProgram, vertexShader); + glCtx.attachShader(shaderProgram, fragmentShader); + glCtx.linkProgram(shaderProgram); + if(!glCtx.getProgramParameter(shaderProgram, glCtx.LINK_STATUS)){ + alert("Failed to link shaders: " + glCtx.getProgramInfoLog(shaderProgram)); } - shaderProgram.positionBuffer = gl.createBuffer(); - gl.bindBuffer(gl.ARRAY_BUFFER, shaderProgram.positionBuffer); - var positionLocation = gl.getAttribLocation(shaderProgram, "aVertexPosition"); - gl.enableVertexAttribArray(positionLocation); - gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0); - - shaderProgram.normalBuffer = gl.createBuffer(); - gl.bindBuffer(gl.ARRAY_BUFFER, shaderProgram.normalBuffer); - var normalLocation = gl.getAttribLocation(shaderProgram, "aVertexNormal"); - gl.enableVertexAttribArray(normalLocation); - gl.vertexAttribPointer(normalLocation, 3, gl.FLOAT, false, 0, 0); - - shaderProgram.elementBuffer = gl.createBuffer(); - - shaderProgram.pMatrixLocation = gl.getUniformLocation(shaderProgram, "uPMatrix"); - shaderProgram.vMatrixLocation = gl.getUniformLocation(shaderProgram, "uVMatrix"); - shaderProgram.mMatrixLocation = gl.getUniformLocation(shaderProgram, "uMMatrix"); - // shaderProgram.normalMatrixLocation = gl.getUniformLocation(shaderProgram, "normalMatrix"); - // shaderProgram.cameraPositionLocation = gl.getUniformLocation(shaderProgram, "cameraPosition"); - // shaderProgram.nkLocation = gl.getUniformLocation(shaderProgram, "nk"); - // shaderProgram.rgbMatrixLocation = gl.getUniformLocation(shaderProgram, "rgbMatrix"); - // shaderProgram.gammaLocation = gl.getUniformLocation(shaderProgram, "gamma"); - // shaderProgram.cieLocation = gl.getUniformLocation(shaderProgram, "cie"); - // shaderProgram.texSamplerLocation = gl.getUniformLocation(shaderProgram, "texSampler"); - // shaderProgram.colorTemperatureLocation = gl.getUniformLocation(shaderProgram, "colorTemperature"); - // shaderProgram.enableCubeLocation = gl.getUniformLocation(shaderProgram, "enableCube"); - // shaderProgram.enableHDRLocation = gl.getUniformLocation(shaderProgram, "enableHDR"); + shaderProgram.positionBuffer = glCtx.createBuffer(); + glCtx.bindBuffer(glCtx.ARRAY_BUFFER, shaderProgram.positionBuffer); + var positionLocation = glCtx.getAttribLocation(shaderProgram, "aVertexPosition"); + glCtx.enableVertexAttribArray(positionLocation); + glCtx.vertexAttribPointer(positionLocation, 3, glCtx.FLOAT, false, 0, 0); + + shaderProgram.normalBuffer = glCtx.createBuffer(); + glCtx.bindBuffer(glCtx.ARRAY_BUFFER, shaderProgram.normalBuffer); + var normalLocation = glCtx.getAttribLocation(shaderProgram, "aVertexNormal"); + glCtx.enableVertexAttribArray(normalLocation); + glCtx.vertexAttribPointer(normalLocation, 3, glCtx.FLOAT, false, 0, 0); + + shaderProgram.elementBuffer = glCtx.createBuffer(); + + shaderProgram.pMatrixLocation = glCtx.getUniformLocation(shaderProgram, "uPMatrix"); + shaderProgram.vMatrixLocation = glCtx.getUniformLocation(shaderProgram, "uVMatrix"); + shaderProgram.mMatrixLocation = glCtx.getUniformLocation(shaderProgram, "uMMatrix"); + // shaderProgram.normalMatrixLocation = glCtx.getUniformLocation(shaderProgram, "normalMatrix"); + // shaderProgram.cameraPositionLocation = glCtx.getUniformLocation(shaderProgram, "cameraPosition"); + // shaderProgram.nkLocation = glCtx.getUniformLocation(shaderProgram, "nk"); + // shaderProgram.rgbMatrixLocation = glCtx.getUniformLocation(shaderProgram, "rgbMatrix"); + // shaderProgram.gammaLocation = glCtx.getUniformLocation(shaderProgram, "gamma"); + // shaderProgram.cieLocation = glCtx.getUniformLocation(shaderProgram, "cie"); + // shaderProgram.texSamplerLocation = glCtx.getUniformLocation(shaderProgram, "texSampler"); + // shaderProgram.colorTemperatureLocation = glCtx.getUniformLocation(shaderProgram, "colorTemperature"); + // shaderProgram.enableCubeLocation = glCtx.getUniformLocation(shaderProgram, "enableCube"); + // shaderProgram.enableHDRLocation = glCtx.getUniformLocation(shaderProgram, "enableHDR"); } @@ -93,27 +105,27 @@ function setSpherePositions(){ function draw(){ - gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); + glCtx.clear(glCtx.COLOR_BUFFER_BIT | glCtx.DEPTH_BUFFER_BIT); for(var i = 0; i < numSpheres; i++){ var mMatrix = mat4.create(); mat4.translate(mMatrix, mMatrix, spherePositions[i]); //console.log(spherePositions[i]); - gl.uniformMatrix4fv(shaderProgram.mMatrixLocation, false, mMatrix); - gl.drawElements(gl.TRIANGLES, moonVertexIndexBuffer.length, gl.UNSIGNED_SHORT, 0); + glCtx.uniformMatrix4fv(shaderProgram.mMatrixLocation, false, mMatrix); + glCtx.drawElements(glCtx.TRIANGLES, vertexIndexBuffer.length, glCtx.UNSIGNED_SHORT, 0); } } -var moonVertexPositionBuffer = []; -var moonVertexNormalBuffer = []; +var vertexPositionBuffer = []; +var vertexNormalBuffer = []; var moonVertexTextureCoordBuffer; -var moonVertexIndexBuffer = []; +var vertexIndexBuffer = []; function initBuffers() { var latitudeBands = 30; var longitudeBands = 30; - var radius = 2; + var radius = 1; var vertexPositionData = []; var normalData = []; var textureCoordData = []; @@ -133,14 +145,14 @@ function initBuffers() { var u = 1 - (longNumber / longitudeBands); var v = 1 - (latNumber / latitudeBands); - moonVertexNormalBuffer.push(x); - moonVertexNormalBuffer.push(y); - moonVertexNormalBuffer.push(z); + vertexNormalBuffer.push(x); + vertexNormalBuffer.push(y); + vertexNormalBuffer.push(z); textureCoordData.push(u); textureCoordData.push(v); - moonVertexPositionBuffer.push(radius * x); - moonVertexPositionBuffer.push(radius * y); - moonVertexPositionBuffer.push(radius * z); + vertexPositionBuffer.push(radius * x); + vertexPositionBuffer.push(radius * y); + vertexPositionBuffer.push(radius * z); } } var indexData = []; @@ -148,13 +160,13 @@ function initBuffers() { for (var longNumber = 0; longNumber < longitudeBands; longNumber++) { var first = (latNumber * (longitudeBands + 1)) + longNumber; var second = first + longitudeBands + 1; - moonVertexIndexBuffer.push(first); - moonVertexIndexBuffer.push(second); - moonVertexIndexBuffer.push(first + 1); + vertexIndexBuffer.push(first); + vertexIndexBuffer.push(second); + vertexIndexBuffer.push(first + 1); - moonVertexIndexBuffer.push(second); - moonVertexIndexBuffer.push(second + 1); - moonVertexIndexBuffer.push(first + 1); + vertexIndexBuffer.push(second); + vertexIndexBuffer.push(second + 1); + vertexIndexBuffer.push(first + 1); } } @@ -165,56 +177,73 @@ function initBuffers() { mat4.lookAt(vMatrix, [0, 0, -60], [0, 0, 0], [0, 1, 0]); - gl.uniformMatrix4fv(shaderProgram.pMatrixLocation, false, pMatrix); - gl.uniformMatrix4fv(shaderProgram.vMatrixLocation, false, vMatrix); + glCtx.uniformMatrix4fv(shaderProgram.pMatrixLocation, false, pMatrix); + glCtx.uniformMatrix4fv(shaderProgram.vMatrixLocation, false, vMatrix); - //console.log(moonVertexPositionBuffer); + //console.log(vertexPositionBuffer); - gl.bindBuffer(gl.ARRAY_BUFFER, shaderProgram.positionBuffer); - gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(moonVertexPositionBuffer), gl.STATIC_DRAW); + glCtx.bindBuffer(glCtx.ARRAY_BUFFER, shaderProgram.positionBuffer); + glCtx.bufferData(glCtx.ARRAY_BUFFER, new Float32Array(vertexPositionBuffer), glCtx.STATIC_DRAW); - gl.bindBuffer(gl.ARRAY_BUFFER, shaderProgram.normalBuffer); - gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(moonVertexNormalBuffer), gl.STATIC_DRAW); + glCtx.bindBuffer(glCtx.ARRAY_BUFFER, shaderProgram.normalBuffer); + glCtx.bufferData(glCtx.ARRAY_BUFFER, new Float32Array(vertexNormalBuffer), glCtx.STATIC_DRAW); - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, shaderProgram.elementBuffer); - gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(moonVertexIndexBuffer), gl.STATIC_DRAW); + glCtx.bindBuffer(glCtx.ELEMENT_ARRAY_BUFFER, shaderProgram.elementBuffer); + glCtx.bufferData(glCtx.ELEMENT_ARRAY_BUFFER, new Uint16Array(vertexIndexBuffer), glCtx.STATIC_DRAW); } -function initWebGL(canvas){ - var context = null; - +function initWebGL(){ try{ - context = canvas.getContext("webgl") || canvas.getContext("experimental-webgl"); + glCtx = canvas.getContext("webgl") || canvas.getContext("experimental-webgl"); //alert("WebGL initialized successfully"); + glCtx.canvas.width = 500; //document.body.offsetWidth; + glCtx.canvas.height = 500; //document.body.offsetHeight; + glCtx.clearColor(0.0, 0.0, 0.0, 1.0); + glCtx.enable(glCtx.DEPTH_TEST); + glCtx.depthFunc(glCtx.LEQUAL); } catch(e){ alert("Unable to use WebGL"); } - return context; +} + +function initWebCL(){ + platforms = WebCL.getPlatformIDs(); + for ( var i in platforms ){ + devices[i] = platforms[i].getDeviceIDs(WebCL.CL_DEVICE_TYPE_DEFAULT); + } + //console.log(platforms); + //console.log(devices); + device = devices[0][0]; + clCtx = WebCL.createContext([WebCL.CL_CONTEXT_PLATFORM, platforms[0]], devices[0]); + clCQ = clCtx.createCommandQueue(devices[0][0], 0); } function main(){ - var canvas = document.getElementById("myCanvas"); - gl = initWebGL(canvas); - gl.canvas.width=500; //document.body.offsetWidth; - gl.canvas.height=500; //document.body.offsetHeight; - gl.clearColor(0.0, 0.0, 0.0, 1.0); - gl.enable(gl.DEPTH_TEST); - gl.depthFunc(gl.LEQUAL); + canvas = document.getElementById("myCanvas"); + initWebGL(); + initWebCL(); + + //console.log(canvas); + //var imageData = clCtx.createImage2D(WebCL.CL_MEM_READ_WRITE, WebCL.CL_RGBA, 500, 500, 500*4); + console.log(clCtx.getSupportedImageFormats(WebCL.CL_MEM_READ_WRITE, WebCL.CL_MEM_OBJECT_IMAGE2D)); //alert("main()"); initShaders(); - gl.useProgram(shaderProgram); + glCtx.useProgram(shaderProgram); initBuffers(); + var drawTime = new Date().getTime(); var requestAnimationFrame = window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame; (function callback(){ requestAnimationFrame(callback); if(startDraw){ setSpherePositions(); + drawTime = new Date().getTime(); draw(); + console.log(new Date().getTime() - drawTime +"ms"); } })(); //alert("drawScene()"); diff --git a/TermProjectC/TermProjectC.sln b/TermProjectC/TermProjectC.sln new file mode 100644 index 0000000..33bfebc --- /dev/null +++ b/TermProjectC/TermProjectC.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual C++ Express 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TermProjectC", "TermProjectC\TermProjectC.vcxproj", "{860AD7D2-A5C0-4464-8124-FD9E570F80A9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {860AD7D2-A5C0-4464-8124-FD9E570F80A9}.Debug|Win32.ActiveCfg = Debug|Win32 + {860AD7D2-A5C0-4464-8124-FD9E570F80A9}.Debug|Win32.Build.0 = Debug|Win32 + {860AD7D2-A5C0-4464-8124-FD9E570F80A9}.Release|Win32.ActiveCfg = Release|Win32 + {860AD7D2-A5C0-4464-8124-FD9E570F80A9}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/TermProjectC/TermProjectC/TermProjectC.vcxproj b/TermProjectC/TermProjectC/TermProjectC.vcxproj new file mode 100644 index 0000000..184dea3 --- /dev/null +++ b/TermProjectC/TermProjectC/TermProjectC.vcxproj @@ -0,0 +1,88 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {860AD7D2-A5C0-4464-8124-FD9E570F80A9} + Win32Proj + TermProjectC + + + + Application + true + NotSet + + + Application + false + true + NotSet + + + + + + + + + + + + + true + + + false + + + + + + Level4 + Disabled + _CRT_SECURE_NO_WARNINGS;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + CompileAsC + + + Console + true + glew32.lib;freeglut.lib;%(AdditionalDependencies) + + + + + Level4 + + + MaxSpeed + true + true + _CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + CompileAsC + + + Console + true + true + true + glew32.lib;freeglut.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/TermProjectC/TermProjectC/TermProjectC.vcxproj.filters b/TermProjectC/TermProjectC/TermProjectC.vcxproj.filters new file mode 100644 index 0000000..dbcb0ac --- /dev/null +++ b/TermProjectC/TermProjectC/TermProjectC.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/TermProjectC/TermProjectC/main.c b/TermProjectC/TermProjectC/main.c new file mode 100644 index 0000000..7990aef --- /dev/null +++ b/TermProjectC/TermProjectC/main.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#define WINDOW_TITLE_PREFIX "Chapter 1" + +int CurrentWidth = 800, + CurrentHeight = 600, + WindowHandle = 0; + +void Initialize(int, char*[]); +void InitWindow(int, char*[]); +void ResizeFunction(int, int); +void RenderFunction(void); + +int main(int argc, char* argv[]) +{ + Initialize(argc, argv); + + glutMainLoop(); + + exit(EXIT_SUCCESS); +} + +void Initialize(int argc, char* argv[]) +{ + InitWindow(argc, argv); + + fprintf( + stdout, + "INFO: OpenGL Version: %s\n", + glGetString(GL_VERSION) + ); + + glClearColor(1.0f, 0.0f, 0.0f, 0.0f); +} + +void InitWindow(int argc, char* argv[]) +{ + glutInit(&argc, argv); + + glutInitContextVersion(4, 0); + glutInitContextFlags(GLUT_FORWARD_COMPATIBLE); + glutInitContextProfile(GLUT_CORE_PROFILE); + + glutSetOption( + GLUT_ACTION_ON_WINDOW_CLOSE, + GLUT_ACTION_GLUTMAINLOOP_RETURNS + ); + + glutInitWindowSize(CurrentWidth, CurrentHeight); + + glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); + + WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX); + + if(WindowHandle < 1) { + fprintf( + stderr, + "ERROR: Could not create a new rendering window.\n" + ); + exit(EXIT_FAILURE); + } + + glutReshapeFunc(ResizeFunction); + glutDisplayFunc(RenderFunction); +} + +void ResizeFunction(int Width, int Height) +{ + CurrentWidth = Width; + CurrentHeight = Height; + glViewport(0, 0, CurrentWidth, CurrentHeight); +} + +void RenderFunction(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glutSwapBuffers(); + glutPostRedisplay(); +}