Don't like ads? PRO users don't see any ads ;-)
Guest

TexGen - main.cpp

By: a guest on Jun 10th, 2012  |  syntax: C++  |  size: 7.14 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <cstdint>
  5. #include "SimpleImage.h"
  6. #include <vector>
  7. #include <math.h>
  8. //Load a block of data from a file
  9. //(this is the counterpart to SaveBlock from Assignment 1)
  10. bool LoadBlock(const char * Path, uint16_t * Block, size_t Count) {
  11.     if(!Block) return false;
  12.     FILE * filePointer = NULL;
  13.     errno_t error = fopen_s(&filePointer, Path, "rb");
  14.     if(error) return false;
  15.     fread(Block, sizeof(uint16_t), Count, filePointer);
  16.     fclose(filePointer);
  17.     return true;
  18. }
  19.  
  20. void calcAlphas(float height, float slope, float& alpha1, float& alpha2, float& alpha3) {
  21.         alpha1 = (1-height) * slope;
  22.         alpha2 = height;
  23.         alpha3 = height * slope;
  24. }
  25.  
  26. int main(int argc, char * argv[])
  27. {
  28.         //simple commandline validity check
  29.         if(argc<9 || strcmp(argv[1],"-r")       != 0 || strcmp(argv[3],"-i")        != 0
  30.               || strcmp(argv[5],"-o_color") != 0 || strcmp(argv[7],"-o_normal") != 0)
  31.         {
  32.                 puts("Syntax: TextureGenerator -r <Power of 2 resolution> -i <Input height field filename> "
  33.                          "-o_color <Output color filename> -o_normal <Output normal filename>\n");
  34.                 system("pause");
  35.                 return -1;
  36.         }
  37.  
  38.         //parse commandline arguments
  39.         int resolution = atoi(argv[2]);
  40.         char* filename_in         = argv[4];
  41.         char* filename_out_color  = argv[6];
  42.         char* filename_out_normal = argv[8];
  43.  
  44.         //Allocate raw height field
  45.         std::vector<uint16_t> height_raw;
  46.         height_raw.resize(resolution*resolution);
  47.  
  48.         //Load raw height field (the output from TerrainGenerator)
  49.         //Hint: &(height_raw[0]) obtains a pointer to the internal raw C++ array of the vector
  50.         bool success = LoadBlock(filename_in, &height_raw[0], resolution*resolution);
  51.                
  52.         //print warning if write failed
  53.         if (!success)
  54.         {
  55.                 puts("SaveBlock failed\n");
  56.                 system("pause");
  57.                 return -1;
  58.         }
  59.  
  60.         //convert raw heightfield to floats in range [0;1]
  61.         std::vector<float> height(resolution*resolution);
  62.         for (int i=0; i<resolution*resolution; i++)
  63.         {
  64.                 height[i] = (float)height_raw[i] / (float)UINT16_MAX;
  65.         }
  66.  
  67.         //Load input textures
  68.         //TODO: use textures of your liking from /external/textures or
  69.         //      use your own textures (do not commit textures from external
  70.         //      to your own repository)
  71.         SimpleImage tex_flat_low  ("../../external/textures/gras15.jpg");
  72.         SimpleImage tex_steep_low ("../../external/textures/ground02.jpg");
  73.         SimpleImage tex_flat_high ("../../external/textures/rock1.jpg");
  74.         SimpleImage tex_steep_high("../../external/textures/rock5.jpg");
  75.  
  76.         //Create output images
  77.         SimpleImage img_color(resolution, resolution);
  78.         SimpleImage img_normal(resolution, resolution);
  79.  
  80.         float df_du;
  81.         float df_dv;
  82.  
  83.         //Iterate over height field
  84.         for (int y=0; y<resolution; y++)
  85.         {
  86.                 for (int x=0; x<resolution; x++)
  87.                 {
  88.                         // TODO: Assignment 2.1.2
  89. /*                              if(x!=resolution-1 && x!=0){
  90.                                 df_du = (height[(x+1)*resolution+y] - height[(x-1)*resolution+y])/2;}          
  91.                         else if(x==0){
  92.                                 df_du =  (height[(x+1)*resolution+y] - height[(x)*resolution+y])/2;}
  93.                         else if(x==resolution-1){
  94.                                 df_du =  (height[(x)*resolution+y] - height[(x-1)*resolution+y])/2;}
  95.  
  96.                         if(y!=resolution-1 && y!= 0){
  97.                                 df_dv =  (height[x*resolution+(y+1)] - height[x*resolution+(y-1)])/2;}
  98.                         else if(y==0){
  99.                                 df_dv = (height[x*resolution+(y+1)] - height[x*resolution+y])/2;}
  100.                         else if(y==resolution-1){
  101.                                 df_dv = (height[x*resolution+y] - height[x*resolution+(y-1)])/2;}
  102. */
  103.                         float spz   = 1/((2^16)-1);
  104.                         float spxy  = 1/((4096)-1);
  105.                         float scale = spz/(2 * spxy);
  106.  
  107.                         if(x!=resolution-1 && x!=0){
  108.                                 df_du = (scale*(height[(x-1)*resolution+y] - height[(x+1)*resolution+y]));}            
  109.                         else if(x==0){
  110.                                 df_du =  (scale*(height[(x)*resolution+y] - height[(x+1)*resolution+y]));}
  111.                         else if(x==resolution-1){
  112.                                 df_du =  (scale*(height[(x-1)*resolution+y] - height[(x)*resolution+y]));}
  113.  
  114.                         if(y!=resolution-1 && y!= 0){
  115.                                 df_dv =  (scale*(height[x*resolution+(y-1)] - height[x*resolution+(y+1)]));}
  116.                         else if(y==0){
  117.                                 df_dv = (scale*(height[x*resolution+(y)] - height[x*resolution+(y+1)]));}
  118.                         else if(y==resolution-1){
  119.                                 df_dv = (scale*(height[x*resolution+(y-1)] - height[x*resolution+(y)]));}
  120.  
  121.  
  122.                         //df_du = (scale*(height[(x-1)*resolution+y] - height[(x+1)*resolution+y]));
  123.                     //df_dv = (scale*(height[x*resolution+(y-1)] - height[x*resolution+(y+1)]));
  124.                        
  125.                         float n[3] ={df_du, df_dv, -1};
  126.  
  127.                         float normal_x = n[0];
  128.                         float normal_y = n[1];
  129.                         float normal_z = n[2];
  130.  
  131.                     float length = sqrt(normal_x*normal_x+normal_y*normal_y+normal_z*normal_z);
  132.  
  133.                         normal_x = normal_x/length;
  134.                         normal_y = normal_y/length;
  135.                         normal_z = normal_z/length;
  136.  
  137.                         //default normal pointing upward
  138.                         /*float normal_x = 0.0f;
  139.                         float normal_y = 0.0f;
  140.                         float normal_z = 1.0f;*/
  141.  
  142.                         //map floats in range [-1;1] to bytes in range [0;255]
  143.                         BYTE normal_r = (BYTE)((normal_x+1.0f)/2.0f * 255.0f);
  144.                         BYTE normal_g = (BYTE)((normal_y+1.0f)/2.0f * 255.0f);
  145.                         BYTE normal_b = (BYTE)((normal_z+1.0f)/2.0f * 255.0f);
  146.  
  147.                         //set all normals in normal image to default normal
  148.                         img_normal.setPixel(x, y, normal_r, normal_g, normal_b);
  149.  
  150.                         // TODO: Assignment 2.1.3
  151.  
  152.                         //Texture Array
  153.                         SimpleImage seamlessTexture[4] = {tex_flat_low,tex_steep_low,tex_flat_high,tex_steep_high};
  154.  
  155.                         //Blend Weights
  156.            
  157.             float h = height[x*resolution+y] ;
  158.             float s = 1-normal_z;
  159.                         float a1,a2,a3;
  160.             calcAlphas(h,s,a1,a2,a3);
  161.            
  162.                         // TODO: Assignment 2.1.4
  163.  
  164.                        
  165.  
  166.                         //for a start, we simply use one of our textures for the color image
  167.                         BYTE r, b, g;
  168.                     BYTE r0,r1,r2,r3;
  169.                         BYTE g0,g1,g2,g3;
  170.                         BYTE b0,b1,b2,b3;
  171.  
  172.  
  173.  
  174.                         int x_seemless0 = x % seamlessTexture[0].getWidth();  //seemless texturing
  175.                         int y_seemless0 = y % seamlessTexture[0].getHeight(); //seemless texturing
  176.  
  177.                         int x_seemless1 = x % seamlessTexture[1].getWidth();  //seemless texturing
  178.                         int y_seemless1 = y % seamlessTexture[1].getHeight(); //seemless texturing
  179.  
  180.                         int x_seemless2 = x % seamlessTexture[2].getWidth();  //seemless texturing
  181.                         int y_seemless2 = y % seamlessTexture[2].getHeight(); //seemless texturing
  182.  
  183.                         int x_seemless3 = x % seamlessTexture[3].getWidth();  //seemless texturing
  184.                         int y_seemless3 = y % seamlessTexture[3].getHeight(); //seemless texturing
  185.  
  186.                         seamlessTexture[0].getPixel(x_seemless0,y_seemless0,r0,g0,b0);
  187.                         seamlessTexture[1].getPixel(x_seemless1,y_seemless1,r1,g1,b1);
  188.                         seamlessTexture[2].getPixel(x_seemless2,y_seemless2,r2,g2,b2);
  189.                         seamlessTexture[3].getPixel(x_seemless3,y_seemless3,r3,g3,b3);
  190.                        
  191.  
  192.                         r = (BYTE)(a3*r3+(1-a3)*(a2*r2+(1-a2)*(a1*r1+(1-a1)*r0)));
  193.                         g = (BYTE)(a3*g3+(1-a3)*(a2*g2+(1-a2)*(a1*g1+(1-a1)*g0)));
  194.                         b = (BYTE)(a3*b3+(1-a3)*(a2*b2+(1-a2)*(a1*b1+(1-a1)*b0)));
  195.  
  196.                        
  197.  
  198.                         img_color.setPixel(x, y, r, g, b);
  199.  
  200.                 }
  201.         }
  202.  
  203.  
  204.         //write color image
  205.         success = img_color.save(filename_out_color);
  206.        
  207.         //print warning if write failed
  208.         if (!success)
  209.         {
  210.                 puts("img_color.save failed\n");
  211.                 system("pause");
  212.                 return -1;
  213.         }
  214.        
  215.         //write normal image
  216.         success = img_normal.save(filename_out_normal);
  217.  
  218.         //print warning if write failed
  219.         if (!success)
  220.         {
  221.                 puts("img_normal.save failed\n");
  222.                 system("pause");
  223.                 return -1;
  224.         }
  225.  
  226.        
  227.        
  228.         return 0;
  229. }