void init() { int row, col; // An ordinary texture groundTexture = loadTexture("../textures/TropicalFoliage0025_1_S.jpg"); waterTexture = loadTexture("../textures/water_deep.jpg"); fiskautomat = loadTexture("../textures/fiskautomat.jpg"); klingoff = loadModel("../models/various/klingon.obj"); // A heightmap image imagedata = readppm("heightmaps/fft-terrain.ppm", &height, &width); // imagedata = readppm("../../../heightmaps/fft-terrain.ppm", &height, &width); // Print out image contents. // Warning! Only do this for small images or you will get incredible amounts of data! if (width < 16) for (row = 0; row < height; row++) for (col = 0; col < width; col++) { printf("(%d, %d): R = %d, G = %d, B = %d\n", row, col, (int)imagedata[(row*width + col)*3], (int)imagedata[(row*width + col)*3+1], (int)imagedata[(row*width + col)*3+2]); } // Use the imagedata array for heights in the terrain! // GL inits glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); // Enable Gouraud shading glEnable(GL_TEXTURE_2D); }
void main(int argc, char *argv[]) { char header[512]; unsigned char img[640*480*3], newimg[640*480*3]; int bufflen, hdrlen; unsigned row=0, col=0, chan=0, pix; int i, j, k; double alpha=1.25; unsigned char beta=25; if(argc < 2) { printf("Use: brighten inputfile\n"); exit(-1); } // TEST CASE #0 // // Basic PPM file read, scaling and bias test, and write-back // header[0]='\0'; readppm(img, &bufflen, header, &hdrlen, &row, &col, &chan, argv[1]); alpha=1.25, beta =25; scaleImage(img, newimg, row, col, chan, alpha, beta); writeppm(newimg, bufflen, header, hdrlen, "brighter-1.25-25.ppm"); alpha=1.5, beta =50; scaleImage(img, newimg, row, col, chan, alpha, beta); writeppm(newimg, bufflen, header, hdrlen, "brighter-1.5-50.ppm"); // // END TEST CASE #0 }
void init() { // Initialize! // -- Place one-time initialization code here // Load models... modelTree = loadModel("models/tree.obj"); modelApache = loadModel("models/apache/apache_wo_rotors.obj"); modelRotor = loadModel("models/apache/main_rotor.obj"); modelBackRotor = loadModel("models/apache/back_rotor.obj"); // Load textures.. // The texture used for the terrain groundTexture = loadTexture("textures/skybox/negativeY.jpg");//"../textures/TropicalFoliage0025_1_S.jpg"); // Texture for the lake lakeTexture = loadTexture("textures/water.jpg"); // Tree texture textureTree = loadTexture("textures/tree.jpg"); // Helicopter texture helicopterTexture = loadTexture("textures/helicopter.jpg"); // Rotor texture textureRotor = loadTexture("textures/rotor.jpg"); // SkyBox textures textureNegativeZ = loadTexture("textures/skybox/negativeZ.jpg"); texturePositiveZ = loadTexture("textures/skybox/positiveZ.jpg"); textureNegativeY = loadTexture("textures/skybox/negativeY.jpg"); texturePositiveY = loadTexture("textures/skybox/positiveY.jpg"); textureNegativeX = loadTexture("textures/skybox/negativeX.jpg"); texturePositiveX = loadTexture("textures/skybox/positiveX.jpg"); // A heightmap image imagedata = readppm("textures/heightmap/heightmap.ppm", &height, &width); // The ground plane size terrainWidth = terrainHeight = 750; // The number of tiles in rows and cols rows = height-1; cols = width-1; // The width and height of each terrain tile tileWidth = (float)terrainWidth/cols; tileHeight = (float)terrainHeight/rows; // Scaling of the terrain heights heightScale = 0.3; // The size of the SkyBox sides skyBoxSize = 200; }
//////////////////////////////////////////////////////////////////////////////// // main computation function //////////////////////////////////////////////////////////////////////////////// void computeImages() { //read in full size of memory image = readppm("maskros512.ppm", &n, &m); out = (unsigned char*) malloc(n*m*3); cl_mem in_data, out_data; cl_int ciErrNum = CL_SUCCESS; // Create space for data and copy image to device (note that we could also use clEnqueueWriteBuffer to upload) in_data = clCreateBuffer(cxGPUContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, 3*n*m * sizeof(unsigned char), image, &ciErrNum); printCLError(ciErrNum,6); out_data = clCreateBuffer(cxGPUContext, CL_MEM_WRITE_ONLY, 3*n*m * sizeof(unsigned char), NULL, &ciErrNum); printCLError(ciErrNum,7); // set the args values ciErrNum = clSetKernelArg(theKernel, 0, sizeof(cl_mem), (void *) &in_data); ciErrNum |= clSetKernelArg(theKernel, 1, sizeof(cl_mem), (void *) &out_data); ciErrNum |= clSetKernelArg(theKernel, 2, sizeof(cl_uint), (void *) &n); ciErrNum |= clSetKernelArg(theKernel, 3, sizeof(cl_uint), (void *) &m); printCLError(ciErrNum,8); // Computing arrangement //size_t localWorkSize, globalWorkSize; size_t globalWorkSize[3] = {512, 512, 1}; size_t localWorkSize[3] = {16, 16, 1}; //256 threads in each block // 32*32 (1024) blocks in total to have 512*512 threads in total printf("Startup time %lf\n", GetSeconds()); // Compute! cl_event event; ResetMilli(); ciErrNum = clEnqueueNDRangeKernel(commandQueue, theKernel, 2, NULL, &globalWorkSize, &localWorkSize, 0, NULL, &event); printCLError(ciErrNum,9); ciErrNum = clWaitForEvents(1, &event); // Synch printCLError(ciErrNum,10); printf("time %lf\n", GetSeconds()); ciErrNum = clEnqueueReadBuffer(commandQueue, out_data, CL_TRUE, 0, 3*n*m * sizeof(unsigned char), out, 0, NULL, &event); printCLError(ciErrNum,11); clWaitForEvents(1, &event); // Synch printCLError(ciErrNum,10); clReleaseMemObject(in_data); clReleaseMemObject(out_data); return; }
int generate_test_image_set(char *filename, char *groupname, const int size) { int n, m; int x, y, half, px, opx, step; unsigned char *image, *imImage; char file[30]; /* Read image to memory */ image = readppm(filename, &n, &m); if (n != size || m != size) { printf("Image size not correct.\n"); getchar(); return 0; } if (!image) { printf("Some other error."); getchar(); return 0; } step = 1; half = size; while ((half /= 2) > 2) { step *= 2; imImage = (unsigned char *)malloc(sizeof(unsigned char) * half * half * 3); for (y = 0; y < half; ++y) { for (x = 0; x < half; ++x) { px = (y * half + x) * 3; opx = ((y * step * half) + x) * 3 * step; imImage[px] = image[opx]; imImage[px + 1] = image[opx + 1]; imImage[px + 2] = image[opx + 2]; } } sprintf_s(file, 30, "%s%u.ppm", groupname, half); writeppm(file, half, half, imImage); free(imImage); } return 0; }
int main() { unsigned char img[2500*2500], newimg[2500*2500]; int bufflen, hdrlen, caseNumber = 0, radius = 0, transformNumber = 0; unsigned row = 0, col = 0, chan = 0; char programCommand = 'a', topMenu[sizeof(TOPMENU)] = TOPMENU, header[512], fileName[250], pipeString[10], newFileName[50]; char singleMenu[sizeof(SINGLEMENU)] = SINGLEMENU, pipeMenu[sizeof(PIPEMENU)] = PIPEMENU; printf("\nWelcome to EagleEye v1.0\n"); while (1) { printf("\nPlease enter the name of the image file you would like to use (must be in same directory as application: "); scanf(" %s", fileName); readppm(img, &bufflen, header, &hdrlen, &row, &col, &chan, fileName, 0); printf("\n%s", topMenu); scanf(" %c", &programCommand); //Single Transform Operation if(programCommand == '1') { printf("\n%s", singleMenu); scanf(" %c",&programCommand); if (programCommand == '1') luminGray(img, newimg, row, col, chan); else if (programCommand == '2') { printf("\nPlease enter radius for blur: "); scanf(" %d", &radius); blur(img, newimg, row, col, chan, radius); printf("Blur Transform completed.\n"); } else if (programCommand = '3') printf("\nGoing back to previous menu\n"); else { printf("Incorrect input, please try again."); printf("\n%s", singleMenu); scanf(" %c",&programCommand); } programCommand = 'a'; transformNumber++; sprintf(newFileName,"newImage%d.ppm",transformNumber); writeppm(newimg, bufflen, header, hdrlen, newFileName, 0); printf("\nNew image created, file name = %s", newFileName); printf("\nWould you like to perform another operation [y/n]: "); scanf(" %c", &programCommand); if(programCommand == 'y' || programCommand == 'Y') { //resetting all allocated memory back to zero states memset(header, 0, sizeof(header)); memset(img, 0, sizeof(img)); memset(newimg,0,sizeof(newimg)); bufflen = 0; hdrlen = 0; row = 0; col = 0; header[0]='\0'; } else { printf("\nProgram Exited.\n"); break; } } //Pipeline Transform Operation else if(programCommand == '2') { printf("\n%s", pipeMenu); scanf(" %s",&pipeString); pipeline(img, newimg, row, col, chan, pipeString); transformNumber++; sprintf(newFileName,"newImage%d.ppm",transformNumber); writeppm(newimg, bufflen, header, hdrlen, newFileName, 0); printf("\nNew image created, file name = %s",newFileName); printf("\nWould you like to perform another operation [y/n]: "); scanf(" %c", &programCommand); if(programCommand == 'y' || programCommand == 'Y') { //resetting all allocated memory back to zero states memset(header, 0, sizeof(header)); memset(img, 0, sizeof(img)); memset(newimg,0,sizeof(newimg)); bufflen = 0; hdrlen = 0; row = 0; col = 0; header[0]='\0'; } else { printf("\nProgram Exited.\n"); break; } } //Quit option else if (programCommand == '3' ) { printf("\nProgram Exited.\n"); break; } else { printf("\nIncorrect Input"); printf("\n\n%s", topMenu); scanf(" %c", &programCommand); } } return 0; }
int readimage(rawimage * img) { int rc; int xres, yres; unsigned char * imgdata; char * name = img->name; if (strstr(name, ".ppm")) { rc = readppm(name, &xres, &yres, &imgdata); } else if (strstr(name, ".tga")) { rc = readtga(name, &xres, &yres, &imgdata); } else if (strstr(name, ".jpg")) { rc = readjpeg(name, &xres, &yres, &imgdata); } else if (strstr(name, ".gif")) { rc = IMAGEUNSUP; } else if (strstr(name, ".png")) { rc = IMAGEUNSUP; } else if (strstr(name, ".tiff")) { rc = IMAGEUNSUP; } else if (strstr(name, ".rgb")) { rc = IMAGEUNSUP; } else if (strstr(name, ".xpm")) { rc = IMAGEUNSUP; } else { rc = readppm(name, &xres, &yres, &imgdata); } switch (rc) { case IMAGEREADERR: fprintf(stderr, "Short read encountered while loading image %s\n", name); rc = IMAGENOERR; /* remap to non-fatal error */ break; case IMAGEUNSUP: fprintf(stderr, "Cannot read unsupported image format for image %s\n", name); break; } /* If the image load failed, create a tiny white colored image to fake it */ /* this allows a scene to render even when a file can't be loaded */ if (rc != IMAGENOERR) { rc = fakeimage(name, &xres, &yres, &imgdata); } /* If we succeeded in loading the image, return it. */ if (rc == IMAGENOERR) { img->xres = xres; img->yres = yres; img->bpp = 3; img->data = imgdata; } return rc; }
void InitGL(void) { GLvoid *image_data; int i, width, height; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (GLfloat) 640 / (GLfloat) 480, 0.1f, 1000.0f); glClearColor(0.7, 0.7, 1, 1); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glCullFace(GL_BACK); glEnable(GL_COLOR_MATERIAL); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_no_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_no_shininess); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Using GL_REPLACE will effectively disable lighting. // The default mode, GL_MODULATE, will use the texture color to // module the shaded material color, so the end result still // contains the shading. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // generate texture objects for (i = 0; texture_files[i].filename != NULL; i++) ; texture_names = malloc(i * sizeof(GLuint)); glGenTextures(i, texture_names); glCheckError("glGenTextures"); // create textures from image files GLint texture_internal_format; GLenum texture_format, texture_type; for (i = 0; texture_files[i].filename != NULL; i++) { image_data = (GLvoid *) readppm(texture_files[i].filename, &width, &height); if (image_data) { printf("texture %d (OpenGL name = %d): %s, %d x %d\n", i, texture_names[i], texture_files[i].filename, width, height); if (texture_files[i].contains_transparency) { // Convert the RGB image to RGBA, by replacing // red pixels with transparent ones // Allocate a temporary RGBA image unsigned char *temp_image = (unsigned char*) malloc(width*height*4); unsigned char *input = image_data; unsigned char *output = temp_image; // Convert image to RGBA for (int pix = 0; pix < width*height; pix++) { if (*input == 255 && *(input+1) == 0 && *(input+2) == 0) { // Input pixel is pure red, make output pixel // white color and fully transparent *output++ = 255; *output++ = 255; *output++ = 255; *output++ = 0; // alpha } else { // Copy image color, make fully opaque *output++ = *input; *output++ = *(input + 1); *output++ = *(input + 2); *output++ = 255; } input += 3; } // Replace the RGB image data with the generated RGBA data free(image_data); image_data = temp_image; // This also influences some texture properties texture_internal_format = GL_RGBA8; texture_format = GL_RGBA; texture_type = GL_UNSIGNED_BYTE; } else { texture_internal_format = GL_RGB8; texture_format = GL_RGB; texture_type = GL_UNSIGNED_BYTE; } glBindTexture(GL_TEXTURE_2D, texture_names[i]); glCheckError("glBindTexture"); // Use GL_REPEAT to repeat the textures glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Chooses the two mipmaps that most closely match the size of the // pixel being textured and uses the GL_LINEAR criterion (a // weighted average of the texture elements that are closest to the // specified texture coordinates) to produce a texture value from // each mipmap. The final texture value is a weighted average of // those two values. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glCheckError("glTexParameteri"); // Use mipmaps gluBuild2DMipmaps(GL_TEXTURE_2D, texture_internal_format, width, height, texture_format, texture_type, image_data); glCheckError("glTexImage2D"); // Free the image data, as OpenGL will have made its internal copy by now free(image_data); } else { perror(texture_files[i].filename); exit(0); } } }