Skip to content

Commit

Permalink
Render from texture completed
Browse files Browse the repository at this point in the history
  • Loading branch information
yghannam committed Apr 16, 2013
1 parent 1e4ee5e commit 82abdea
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 41 deletions.
27 changes: 0 additions & 27 deletions TermProjectC/TermProjectC/TermProjectC.vcxproj (1).filters

This file was deleted.

18 changes: 10 additions & 8 deletions TermProjectC/TermProjectC/kernels.cl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ __kernel void clearRaster(
__kernel void getPixelColor(
const int numShapes,
__global float4 *shapeData,
__global uchar4 *raster){
__write_only image2d_t raster){

if(DEBUG)
{
Expand Down Expand Up @@ -260,7 +260,7 @@ __kernel void getPixelColor(
worldRay.o = eye;
worldRay.d = transformRayDirection(localDirection, U, V, W);

uchar4 color = (uchar4)(0, 128, 0, 255);
float4 color = (float)(0.f, 128.f, 0.f, 255.f);

float3 lightDir = (float3)(-1.f, -1.f, -1.f);

Expand All @@ -279,14 +279,16 @@ __kernel void getPixelColor(
//printf("reflect: %2.2f\n", intersection.reflect);

float3 Intensity = clamp(dot(intersection.N, normalize(lightDir)), 0.f, 1.f) + clamp(dot(intersection.N, normalize(-lightDir)), 0.f, 1.f);

uchar red = (uchar) round(255*Intensity.x);
uchar green = (uchar) round(255*Intensity.y);
uchar blue = (uchar) round(255*Intensity.z);
color = (uchar4)(red, green, blue, 255);
/*
float red = round(255.f*Intensity.x);
float green = round(255.f*Intensity.y);
float blue = round(255.f*Intensity.z);*/
color = (float4)(Intensity, 1.f);
}

raster[row * columns + col] = color;
//printf("Color %v4d\n", color);
write_imagef(raster, (int2)(col, row), color);
//raster[row * columns + col] = color;
}

__kernel void moveShapes(
Expand Down
95 changes: 89 additions & 6 deletions TermProjectC/TermProjectC/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,30 @@ void InitializeCL()
float shapeData[] = {1.5, 0.0, 0.0, 1.0, -1.5, 0.0, 0.0, 1.0, 0, 1.5, 0.0, 1.0};
cl_mem shape_buf = clCreateBuffer(context, CL_MEM_READ_WRITE, numShapes*4*4, NULL, &err);
CheckError(err, "Create Shape Buffer");
pixel_buf = clCreateBuffer(context, CL_MEM_READ_WRITE, width*height*4, NULL, &err);

glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

// Set parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);

pixel_buf = clCreateFromGLTexture(context, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, tex, &err); //clCreateBuffer(context, CL_MEM_READ_WRITE, width*height*4, NULL, &err);
CheckError(err, "Create Pixel Buffer");

cl_image_format info;
err = clGetImageInfo(pixel_buf, CL_IMAGE_FORMAT, sizeof(cl_image_format), &info, NULL);
CheckError(err, "Get Image Info");

err = clSetKernelArg(colorKernel, 0, sizeof(cl_int), &numShapes);
CheckError(err, "Set Color Kernel Arg 0");
err = clSetKernelArg(colorKernel, 1, sizeof(cl_mem*), (void*) &shape_buf);
CheckError(err, "Set Color Kernel Arg 1");
err = clSetKernelArg(colorKernel, 2, sizeof(cl_mem*), (void*) &pixel_buf);
err = clSetKernelArg(colorKernel, 2, sizeof(cl_mem), &pixel_buf);
CheckError(err, "Set Color Kernel Arg 2");

err = clSetKernelArg(moveKernel, 0, sizeof(cl_int), &numShapes);
Expand Down Expand Up @@ -243,6 +259,19 @@ void InitWindow(int argc, char* argv[])
GLenum error = glewInit();
if(error != GLEW_OK)
printf("GLEW not initialized with Error %s\n", glewGetErrorString(error));

glClearColor(0.0, 0.0, 0.0, 1.0);
glDisable(GL_DEPTH_TEST);

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(
60.0,
(GLfloat)width / (GLfloat)height,
0.1,
10.0);
}

void ResizeFunction(int Width, int Height)
Expand All @@ -255,6 +284,11 @@ void ResizeFunction(int Width, int Height)

void RenderFunction(void)
{
glFinish();

err = clEnqueueAcquireGLObjects(cQ, 1, &pixel_buf, 0, NULL, NULL);
CheckError(err, "Acquire GL Objects");

err = clEnqueueNDRangeKernel(cQ, colorKernel, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL);
CheckError(err, "Enqueue Color Kernel");

Expand All @@ -265,12 +299,61 @@ void RenderFunction(void)

clFinish(cQ);

err = clEnqueueReadBuffer(cQ, pixel_buf, 1, 0, width*height*4, pixels, 0, NULL, NULL);
/*err = clEnqueueReadBuffer(cQ, pixel_buf, 1, 0, width*height*4, pixels, 0, NULL, NULL);
CheckError(err, "Read Pixel Buffer");
glClear(GL_COLOR_BUFFER_BIT);
glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glFlush();
clFinish(cQ);
*/
err = clEnqueueReleaseGLObjects(cQ, 1, &pixel_buf, 0, NULL, NULL);
CheckError(err, "Release GL Objects");

//glClear(GL_COLOR_BUFFER_BIT);

// Bind texture
glBindTexture(GL_TEXTURE_2D, tex);

// Display image using texture
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

glMatrixMode( GL_MODELVIEW);
glLoadIdentity();

glViewport(0, 0, width, height);

glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0);
glVertex3f(-1.0, -1.0, 0.5);

glTexCoord2f(1.0, 0.0);
glVertex3f(1.0, -1.0, 0.5);

glTexCoord2f(1.0, 1.0);
glVertex3f(1.0, 1.0, 0.5);

glTexCoord2f(0.0, 1.0);
glVertex3f(-1.0, 1.0, 0.5);

glEnd();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

glDisable(GL_TEXTURE_2D);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

//glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
//glFlush();
//SwapBuffers();
glutSwapBuffers();
glutPostRedisplay();
}

0 comments on commit 82abdea

Please sign in to comment.