Commit 7006df5f3b680618a1597310683f8c334ca527fb

Authored by David Mayerich
1 parent 0174d823

reformat of directory structure

rts/biology/fibernet.h 0 → 100644
rts/cuda/glbind.h 0 → 100644
  1 +#ifndef RTS_GL_BIND_H
  2 +#define RTS_GL_BIND_H
  3 +
  4 +#include <GL/glew.h>
  5 +#include <GL/gl.h>
  6 +
  7 +#include <stdio.h>
  8 +#include <cstring>
  9 +
  10 +#include <cudaHandleError.h>
  11 +#include "cuda_gl_interop.h"
  12 +#include "rts/gl/error.h"
  13 +
  14 +
  15 +static void rtsInitGLEW()
  16 +{
  17 + //Initialize the GLEW toolkit
  18 +
  19 + GLenum err = glewInit();
  20 + if(GLEW_OK != err)
  21 + {
  22 + printf("Error starting GLEW.");
  23 + }
  24 + fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  25 +}
  26 +
  27 +static void rts_cudaSetDevice(int major = 1, int minor = 3)
  28 +{
  29 + cudaDeviceProp prop;
  30 + int dev;
  31 +
  32 + //find a CUDA device that can handle an offscreen buffer
  33 + int num_gpu;
  34 + HANDLE_ERROR(cudaGetDeviceCount(&num_gpu));
  35 + printf("Number of CUDA devices detected: %d\n", num_gpu);
  36 + memset(&prop, 0, sizeof(cudaDeviceProp));
  37 + prop.major=major;
  38 + prop.minor=minor;
  39 + HANDLE_ERROR(cudaChooseDevice(&dev, &prop));
  40 + HANDLE_ERROR(cudaGetDeviceProperties(&prop, dev));
  41 + HANDLE_ERROR(cudaGLSetGLDevice(dev));
  42 +}
  43 +
  44 +static void* rts_cudaMapResource(cudaGraphicsResource* cudaBufferResource)
  45 +{
  46 + //this function takes a predefined CUDA resource and maps it to a pointer
  47 + void* buffer;
  48 + HANDLE_ERROR(cudaGraphicsMapResources(1, &cudaBufferResource, NULL));
  49 + size_t size;
  50 + HANDLE_ERROR(cudaGraphicsResourceGetMappedPointer( (void**)&buffer, &size, cudaBufferResource));
  51 + return buffer;
  52 +}
  53 +static void rts_cudaUnmapResource(cudaGraphicsResource* resource)
  54 +{
  55 + //this function unmaps the CUDA resource so it can be used by OpenGL
  56 + HANDLE_ERROR(cudaGraphicsUnmapResources(1, &resource, NULL));
  57 +}
  58 +
  59 +static void rts_cudaCreateRenderBuffer(GLuint &glBufferName, cudaGraphicsResource* &cudaBufferResource, int resX, int resY)
  60 +{
  61 + //delete the previous buffer name and resource
  62 + if(cudaBufferResource != 0)
  63 + HANDLE_ERROR(cudaGraphicsUnregisterResource(cudaBufferResource));
  64 + if(glBufferName != 0)
  65 + glDeleteBuffers(1, &glBufferName);
  66 +
  67 + //generate an OpenGL offscreen buffer
  68 + glGenBuffers(1, &glBufferName);
  69 +
  70 + //bind the buffer - directs all calls to this buffer
  71 + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, glBufferName);
  72 + glBufferData(GL_PIXEL_UNPACK_BUFFER, resX * resY * sizeof(uchar3), NULL, GL_DYNAMIC_DRAW_ARB);
  73 + CHECK_OPENGL_ERROR
  74 + HANDLE_ERROR(cudaGraphicsGLRegisterBuffer(&cudaBufferResource, glBufferName, cudaGraphicsMapFlagsNone));
  75 +}
  76 +
  77 +#endif
... ...
rts/cuda/memory.h 0 → 100644
  1 +#include <cuda.h>
  2 +
... ...
rts/cuda/threads.h 0 → 100644
  1 +#include "cuda_runtime.h"
  2 +#include "device_launch_parameters.h"
  3 +#include "rts/cuda/callable.h"
  4 +
  5 +#ifndef CUDA_THREADS_H
  6 +#define CUDA_THREADS_H
  7 +
  8 +#define MAX_GRID 65535
  9 +
  10 +__device__ unsigned int ThreadIndex1D()
  11 +{
  12 + return blockIdx.y * gridDim.x * blockDim.x + blockIdx.x * blockDim.x + threadIdx.x;
  13 +}
  14 +
  15 +dim3 GenGrid1D(unsigned int N, unsigned int blocksize = 128)
  16 +{
  17 + dim3 dimgrid;
  18 +
  19 + dimgrid.x = (N + blocksize - 1)/blocksize;
  20 + dimgrid.y = 1;
  21 + dimgrid.z = 1;
  22 +
  23 + if(dimgrid.x > MAX_GRID)
  24 + {
  25 + dimgrid.y = (dimgrid.x + MAX_GRID - 1) / MAX_GRID;
  26 + dimgrid.x = MAX_GRID;
  27 + }
  28 +
  29 + return dimgrid;
  30 +
  31 +}
  32 +
  33 +
  34 +#endif
... ...
rts/envi/envi.h
... ... @@ -173,7 +173,7 @@ class EnviFile
173 173 exit(1);
174 174 }
175 175  
176   - float r, v0, v1;
  176 + float v0, v1;
177 177 for(int n=0; n<N; n++)
178 178 {
179 179 v0 = ((float*)A)[n];
... ...
rts/envi/envi_header.h
... ... @@ -72,7 +72,7 @@ struct EnviHeader
72 72 std::string trim(std::string line)
73 73 {
74 74 //trims whitespace from the beginning and end of line
75   - int start_i, end_i;
  75 + unsigned int start_i, end_i;
76 76 for(start_i=0; start_i < line.length(); start_i++)
77 77 if(line[start_i] != 32)
78 78 {
... ... @@ -188,16 +188,12 @@ struct EnviHeader
188 188 {
189 189 //this function returns a sequence of comma-delimited strings
190 190 std::vector<double> result;
191   -
192   - double fentry;
193   -
194 191 std::string entry;
195 192 size_t i;
196 193 do
197 194 {
198 195 i = sequence.find_first_of(',');
199 196 entry = sequence.substr(0, i);
200   - fentry = atof(entry.c_str());
201 197 sequence = sequence.substr(i+1);
202 198 result.push_back(atof(entry.c_str()));
203 199 //std::cout<<entry<<" ";
... ... @@ -356,7 +352,7 @@ struct EnviHeader
356 352 if(band_names.size() > 0)
357 353 {
358 354 outfile<<"band names = {"<<std::endl;
359   - for(int i=0; i<band_names.size(); i++)
  355 + for(unsigned int i=0; i<band_names.size(); i++)
360 356 {
361 357 outfile<<band_names[i];
362 358 if(i < band_names.size() - 1)
... ... @@ -365,7 +361,7 @@ struct EnviHeader
365 361 outfile<<"}"<<std::endl;
366 362 }
367 363 outfile<<"wavelength = {"<<std::endl;
368   - for(int i=0; i<wavelength.size()-1; i++)
  364 + for(unsigned int i=0; i<wavelength.size()-1; i++)
369 365 outfile<<wavelength[i]<<", ";
370 366 outfile<<wavelength.back()<<"}"<<std::endl;
371 367  
... ...
rts/gl/error.h 0 → 100755
  1 +#ifndef RTS_OPENGL_ERROR
  2 +#define RTS_OPENGL_ERROR
  3 +
  4 +#include <stdio.h>
  5 +#include <GL/gl.h>
  6 +#include <GL/glu.h>
  7 +
  8 +#define CHECK_OPENGL_ERROR \
  9 +{ GLenum error; \
  10 + while ( (error = glGetError()) != GL_NO_ERROR) { \
  11 + printf( "OpenGL ERROR: %s\nCHECK POINT: %s (line %d)\n", gluErrorString(error), __FILE__, __LINE__ ); \
  12 + } \
  13 +}
  14 +
  15 +#endif
0 16 \ No newline at end of file
... ...
rts/gl/rtsSourceCode.h 0 → 100755
  1 +#ifndef RTSSOURCECODE_H
  2 +#define RTSSOURCECODE_H
  3 +
  4 +#include <string>
  5 +#include <fstream>
  6 +#include <vector>
  7 +#include <iostream>
  8 +
  9 +using namespace std;
  10 +
  11 +///This class defines generic source code that can be loaded from text files. It is primarily used by the rts_glShaderProgram class for GLSL programming.
  12 +
  13 +class rtsSourceCode
  14 +{
  15 +public:
  16 + vector<string> source; //the actual source code
  17 + void clear() ///<Clears any current source code from the class.
  18 + {
  19 + source.clear();
  20 + }
  21 + void LoadSource(const char* filename) ///<Loads source code from a specified file.
  22 + {
  23 + ifstream infile; //create an input file
  24 + infile.open(filename); //load the specified file
  25 +
  26 + if(!infile.is_open()) //if the file is not open, exit
  27 + {
  28 + return;
  29 + }
  30 + source.clear(); //remove any previous code
  31 +
  32 + while(!infile.eof())
  33 + {
  34 + string current_line;
  35 + getline(infile, current_line);
  36 + current_line += '\n';
  37 + source.push_back(current_line);
  38 + }
  39 + }
  40 + rtsSourceCode(const char* filename) ///<Constructor creates the class and loads source code from the specified file.
  41 + {
  42 + LoadSource(filename);
  43 + }
  44 + rtsSourceCode(){} ///<Constructor creates a blank class.
  45 + rtsSourceCode& operator+=(const rtsSourceCode& rhs)
  46 + {
  47 + int lines = rhs.source.size();
  48 + for(int l=0; l<lines; l++)
  49 + source.push_back(rhs.source[l]);
  50 + return *this;
  51 + }
  52 + rtsSourceCode& operator+=(const string& rhs)
  53 + {
  54 + source.push_back(rhs);
  55 + return *this;
  56 + }
  57 + void ConsoleOut() ///<Sends the source code to the standard output.
  58 + {
  59 + unsigned int lines = source.size();
  60 + for(unsigned int l = 0; l<lines; l++)
  61 + cout<<l<<": "<<source[l];
  62 + }
  63 +};
  64 +
  65 +#endif
0 66 \ No newline at end of file
... ...
rts/gl/rts_glShaderObject.h 0 → 100755
  1 +#ifndef RTS_GLSHADERS
  2 +#define RTS_GLSHADERS
  3 +
  4 +#include <GL/glew.h>
  5 +//#include "windows.h"
  6 +#include <GL/gl.h>
  7 +#include "rtsSourceCode.h"
  8 +
  9 +class rts_glShaderObject
  10 +{
  11 +private:
  12 + void init()
  13 + {
  14 + id = 0;
  15 + compiled = false;
  16 + type = GL_FRAGMENT_SHADER;
  17 + }
  18 +public:
  19 + bool compiled;
  20 + GLenum type;
  21 + rtsSourceCode source;
  22 + GLuint id;
  23 + string log;
  24 +
  25 + rts_glShaderObject(GLenum type, const char* filename)
  26 + {
  27 + init(); //initialize the shader
  28 + SetType(type); //set the shader type
  29 + LoadSource(filename); //load the source code
  30 + }
  31 + rts_glShaderObject(GLenum type, rtsSourceCode sourceCode)
  32 + {
  33 + init(); //initialize the shader
  34 + SetType(type); //set the shader type
  35 + source = sourceCode;
  36 + }
  37 + rts_glShaderObject()
  38 + {
  39 + init();
  40 + }
  41 + rts_glShaderObject(GLenum type)
  42 + {
  43 + init();
  44 + SetType(type);
  45 + }
  46 + void LoadSource(const char* filename)
  47 + {
  48 + source = rtsSourceCode(filename); //get the shader source code
  49 +
  50 + }
  51 + void SetType(GLenum type)
  52 + {
  53 + if(id != 0) //if a shader currently exists, delete it
  54 + {
  55 + glDeleteShader(id);
  56 + id = 0;
  57 + }
  58 + type = type;
  59 + id = glCreateShader(type); //create a shader object
  60 + if(id == 0) //if a shader was not created, log an error
  61 + {
  62 + log = "Error getting shader ID from OpenGL";
  63 + return;
  64 + }
  65 + }
  66 + void UploadSource()
  67 + {
  68 + //create the structure for the shader source code
  69 + GLsizei count = source.source.size();
  70 + GLchar** code_string = new GLchar*[count];
  71 + GLint* length = new GLint[count];
  72 + for(int l = 0; l<count; l++) //for each line of code
  73 + {
  74 + length[l] = source.source[l].size();
  75 + code_string[l] = new GLchar[length[l]]; //copy the string into a new structure
  76 + source.source[l].copy(code_string[l], (unsigned int)length[l]);
  77 +
  78 + }
  79 + glShaderSource(id, count, (const GLchar**)code_string, length); //attach the shader source
  80 + }
  81 + void Compile()
  82 + {
  83 + /*
  84 + This function compiles the shader source code, records any errors to a log, and sets the compiled flag.
  85 + */
  86 + //send the source code to the GPU
  87 + UploadSource();
  88 +
  89 + //compile the shader
  90 + glCompileShader(id); //compile the shader
  91 + GLint compile_status;
  92 + glGetShaderiv(id, GL_COMPILE_STATUS, &compile_status); //get the compile status
  93 + if(compile_status != GL_TRUE) //if there was an error
  94 + {
  95 + GLchar buffer[1000]; //create a log buffer
  96 + GLsizei length;
  97 + glGetShaderInfoLog(id, 1000, &length, buffer); //get the log
  98 + log = buffer;
  99 + compiled = false;
  100 + }
  101 + else
  102 + compiled = true;
  103 +
  104 + }
  105 + void PrintLog()
  106 + {
  107 + cout<<log;
  108 + if(log.size() != 0) cout<<endl;
  109 + }
  110 + void Clean(){if(id != 0) glDeleteShader(id);}
  111 +};
  112 +
  113 +
  114 +
  115 +#endif
... ...
rts/gl/rts_glShaderProgram.h 0 → 100755
  1 +#ifndef RTS_GLSHADERPROGRAM_H
  2 +#define RTS_GLSHADERPROGRAM_H
  3 +
  4 +/*********************************************************
  5 +//create a shader program
  6 + rts_glShaderProgram myProgram;
  7 +//initialize
  8 + myProgram.Init();
  9 +//Attach shaders
  10 + myProgram.AttachShader(GL_FRAGMENT_SHADER, "filename.glsl");
  11 +//Compile and link
  12 + myProgram.Compile();
  13 + myProgram.Link();
  14 + myProgram.PrintLog();
  15 +//attach uniform variables
  16 + myProgram.AttachTextureMap("texture", texture);
  17 + myProgram.AttachGlobalUniform("light_intensity", &intensity);
  18 +
  19 +//use the program
  20 + myProgram.BeginProgram();
  21 + //render
  22 + myProgram.EndProgram();
  23 +**********************************************************/
  24 +
  25 +
  26 +#include "rts_glShaderObject.h"
  27 +#include "rts_glShaderUniform.h"
  28 +#include "rts_glTextureMap.h"
  29 +#include <algorithm>
  30 +
  31 +using namespace std;
  32 +
  33 +class rts_glShaderProgram
  34 +{
  35 +private:
  36 + void get_uniforms()
  37 + {
  38 + GLint num_uniforms;
  39 + glGetProgramiv(id, GL_ACTIVE_UNIFORMS, &num_uniforms); //get the number of uniform variables
  40 + GLint max_name_length;
  41 + glGetProgramiv(id, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_name_length); //get the maximum uniform name length
  42 + GLchar* name_buffer = new GLchar[max_name_length]; //create a buffer to store the name
  43 + GLsizei length; //I'm not using these yet
  44 + GLint size;
  45 + GLenum type; //variable's data type
  46 + GLint location; //GPU location of the variable
  47 + for(int i=0; i<num_uniforms; i++) //create an rts_glShaderUniform structure for each variable
  48 + {
  49 + glGetActiveUniform(id, i, max_name_length, &length, &size, &type, name_buffer); //get the uniform information
  50 + location = glGetUniformLocation(id, name_buffer); //get the GPU location of the variable
  51 + //create the rts_glShaderUniform structure
  52 + rts_glShaderUniform current;
  53 + current.location = location;
  54 + current.name = name_buffer;
  55 + current.type = type;
  56 + current.p_value = NULL;
  57 +
  58 +
  59 + uniform_list.push_back(current);
  60 + }
  61 +
  62 + }
  63 + int get_index(const char* name)
  64 + {
  65 + unsigned int size = uniform_list.size();
  66 + for(unsigned int i=0; i<size; i++)
  67 + {
  68 + if(uniform_list[i].name == name)
  69 + return i;
  70 + }
  71 + return -1;
  72 + }
  73 + string log;
  74 +public:
  75 + GLuint id;
  76 + bool linked;
  77 + vector<rts_glShaderObject> shader_list; //list of opengl shaders
  78 + vector<rts_glShaderUniform> uniform_list; //list of active uniform variables
  79 + vector<rts_glTextureMap> texture_list; //list of texture maps
  80 +
  81 + rts_glShaderProgram()
  82 + {
  83 + linked = false;
  84 + id = 0;
  85 + }
  86 + void AttachShader(rts_glShaderObject shader)
  87 + {
  88 + if(id == 0)
  89 + {
  90 + Init();
  91 + }
  92 + if(shader.id == 0) //if the shader is invalid
  93 + {
  94 + log = "Shader is invalid";
  95 + return;
  96 + }
  97 +
  98 + //attach the shader to the program
  99 + glAttachShader(id, shader.id); //attach the shader to the program in OpenGL
  100 + CHECK_OPENGL_ERROR
  101 + shader_list.push_back(shader); //push the shader onto our list for later access
  102 + }
  103 + //type = GL_FRAGMENT_SHADER or GL_VERTEX_SHADER
  104 + void AttachShader(GLenum type, const char* filename)
  105 + {
  106 + rts_glShaderObject shader(type, filename);
  107 + AttachShader(shader);
  108 + }
  109 + void AttachShader(GLenum type, rtsSourceCode source)
  110 + {
  111 + rts_glShaderObject shader(type, source);
  112 + AttachShader(shader);
  113 + }
  114 + void PrintLog()
  115 + {
  116 + cout<<log;
  117 +
  118 + if(log.size() != 0) cout<<endl;
  119 + }
  120 + void Compile()
  121 + {
  122 + if(shader_list.size() == 0)
  123 + {
  124 + log = "No shaders to compile";
  125 + return;
  126 + }
  127 +
  128 + vector<rts_glShaderObject>::iterator iter;
  129 + for(iter = shader_list.begin(); iter != shader_list.end(); iter++)
  130 + {
  131 + (*iter).Compile();
  132 + //(*iter).PrintLog();
  133 + }
  134 + }
  135 + void Link()
  136 + {
  137 + glLinkProgram(id); //link the current shader program
  138 + GLint link_status; //test to see if the link went alright
  139 + glGetProgramiv(id, GL_LINK_STATUS, &link_status);
  140 + if(link_status != GL_TRUE)
  141 + {
  142 + linked = false;
  143 + }
  144 + else
  145 + linked = true;
  146 +
  147 + GLsizei length;
  148 + GLchar buffer[1000];
  149 + glGetProgramInfoLog(id, 1000, &length, buffer);
  150 + log = buffer;
  151 +
  152 + get_uniforms(); //create the list of active uniform variables
  153 + }
  154 + void BeginProgram()
  155 + {
  156 + CHECK_OPENGL_ERROR
  157 + if(id == 0) //if the program is invalid, return
  158 + {
  159 + log = "Invalid program, cannot use.";
  160 + return;
  161 + }
  162 + if(!linked)
  163 + {
  164 + cout<<"Shader Program used without being linked."<<endl;
  165 + //exit(1);
  166 + }
  167 +
  168 + //set up all of the texture maps
  169 + int num_textures = texture_list.size();
  170 +
  171 + for(int t=0; t<num_textures; t++)
  172 + {
  173 + glActiveTexture(GL_TEXTURE0 + t);
  174 + CHECK_OPENGL_ERROR
  175 + //glEnable(texture_list[t].texture_type);
  176 + //CHECK_OPENGL_ERROR
  177 + glBindTexture(texture_list[t].texture_type, texture_list[t].name);
  178 + CHECK_OPENGL_ERROR
  179 + }
  180 +
  181 + glUseProgram(id);
  182 + CHECK_OPENGL_ERROR
  183 + }
  184 + void EndProgram()
  185 + {
  186 + CHECK_OPENGL_ERROR
  187 + //return standard functionality
  188 + int num_textures = texture_list.size();
  189 +
  190 + //disable all texture units
  191 + for(int t=0; t<num_textures; t++)
  192 + {
  193 + glActiveTexture(GL_TEXTURE0 + t);
  194 + glDisable(texture_list[t].texture_type);
  195 + CHECK_OPENGL_ERROR
  196 + }
  197 + //make sure that the single default texture unit is active
  198 + if(num_textures > 0)
  199 + glActiveTexture(GL_TEXTURE0);
  200 + CHECK_OPENGL_ERROR
  201 +
  202 + //return to OpenGL default shading
  203 + glUseProgram(0);
  204 + CHECK_OPENGL_ERROR
  205 + }
  206 + void PrintUniforms()
  207 + {
  208 + cout<<"Shader Uniforms: "<<endl;
  209 + unsigned int i;
  210 + for(i=0; i<uniform_list.size(); i++)
  211 + {
  212 + cout<<i<<": "<<uniform_list[i].name<<" "<<uniform_list[i].location<<endl;
  213 + }
  214 + }
  215 + void AttachGlobalUniform(unsigned int index, void* param) //attaches a global variable to the indexed uniform parameter
  216 + {
  217 + uniform_list[index].p_value = param;
  218 + }
  219 + void AttachGlobalUniform(const char* name, void* param)
  220 + {
  221 + //find the index of the shader
  222 + int index = get_index(name);
  223 + if(index != -1)
  224 + AttachGlobalUniform(index, param);
  225 + else
  226 + {
  227 + string strError = "Error finding uniform variable: ";
  228 + strError += name;
  229 + cout<<strError<<endl;
  230 + }
  231 + }
  232 + void AttachTextureMap(unsigned int index, rts_glTextureMap texture) //attaches a texture map to the program
  233 + {
  234 + //if there is not a texture map assigned to the variable
  235 + if(uniform_list[index].p_value == NULL)
  236 + {
  237 + uniform_list[index].p_value = new unsigned int[1];
  238 + ((unsigned int*)uniform_list[index].p_value)[0] = texture_list.size(); //set the parameter value to the index of the texture
  239 + texture_list.push_back(texture); //add the texture to the texture list
  240 + }
  241 + //if there is a texture map assigned, replace it
  242 + else
  243 + {
  244 + texture_list[((unsigned int*)(uniform_list[index].p_value))[0]] = texture;
  245 + }
  246 +
  247 + }
  248 + void AttachTextureMap(const char* name, rts_glTextureMap texture)
  249 + {
  250 + int index = get_index(name);
  251 + if(index != -1) //make sure that the uniform index is valid
  252 + AttachTextureMap(index, texture);
  253 + else
  254 + cout<<"Error finding texture index. Try linking."<<endl;
  255 + }
  256 + void UpdateGlobalUniforms() //sends updated uniform information to the GPU
  257 + {
  258 + CHECK_OPENGL_ERROR
  259 + BeginProgram();
  260 + CHECK_OPENGL_ERROR
  261 + unsigned int num = uniform_list.size();
  262 + for(unsigned int i=0; i<num; i++)
  263 + uniform_list[i].submit_to_gpu();
  264 + EndProgram();
  265 + }
  266 + void Init() //Initialize the shader program
  267 + {
  268 + CHECK_OPENGL_ERROR
  269 + if(id != 0)
  270 + Clean();
  271 + id = glCreateProgram();
  272 + if(id == 0)
  273 + log = "Error getting program ID from OpenGL";
  274 + CHECK_OPENGL_ERROR
  275 + }
  276 + void Clean()
  277 + {
  278 + if(id != 0)
  279 + glDeleteProgram(id);
  280 + id = 0;
  281 +
  282 + //these are allocated outside the object and can just be cleared
  283 + uniform_list.clear();
  284 + texture_list.clear();
  285 +
  286 + //delete each shader from OpenGL
  287 + int num_shad = shader_list.size();
  288 + for(int i=0; i<num_shad; i++)
  289 + shader_list[i].Clean();
  290 + //clear the list
  291 + shader_list.clear();
  292 + }
  293 +};
  294 +
  295 +#endif
... ...
rts/gl/rts_glShaderUniform.h 0 → 100755
  1 +#ifndef RTS_GLSHADERUNIFORM_H
  2 +#define RTS_GLSHADERUNIFORM_H
  3 +
  4 +#include "CHECK_OPENGL_ERROR.h"
  5 +#include <GL/glew.h>
  6 +#include <string>
  7 +
  8 +using namespace std;
  9 +
  10 +enum rtsUniformEnum {RTS_FLOAT, RTS_INT, RTS_BOOL, RTS_FLOAT_MATRIX};
  11 +
  12 +///This class stores a single uniform variable for GLSL and is designed to be used by the rts_glShaderProgram class.
  13 +struct rts_glShaderUniform
  14 +{
  15 +public:
  16 + string name; //the name of the variable
  17 + GLint location; //the location in the program
  18 + void* p_value; //pointer to the global data representing the value in main memory
  19 + GLenum type; //variable type (float, int, vec2, etc.)
  20 + //rtsUniformEnum rts_type; //type of variable in rts format
  21 + //unsigned int num; //the number of values required by the variable (1 for float, 2 for vec2, etc.)
  22 + string log;
  23 +
  24 + //void convert_type(GLenum gl_type); //converts the OpenGL data type to something useful for rts
  25 + void submit_to_gpu()
  26 + {
  27 + if(location < 0)
  28 + return;
  29 + if(p_value == NULL)
  30 + {
  31 + cout<<"Error in uniform address: "<<name<<endl;
  32 + return;
  33 + }
  34 +
  35 +
  36 + CHECK_OPENGL_ERROR
  37 + switch(type)
  38 + {
  39 + case GL_FLOAT:
  40 + glUniform1fv(location, 1, (float*)p_value);
  41 + break;
  42 + case GL_FLOAT_VEC2:
  43 + glUniform2fv(location, 1, (float*)p_value);
  44 + break;
  45 + case GL_FLOAT_VEC3:
  46 + glUniform3fv(location, 1, (float*)p_value);
  47 + break;
  48 + case GL_FLOAT_VEC4:
  49 + glUniform4fv(location, 1, (float*)p_value);
  50 + break;
  51 + case GL_INT:
  52 + glUniform1iv(location, 1, (int*)p_value);
  53 + break;
  54 + case GL_INT_VEC2:
  55 + glUniform2iv(location, 1, (int*)p_value);
  56 + break;
  57 + case GL_INT_VEC3:
  58 + glUniform3iv(location, 1, (int*)p_value);
  59 + break;
  60 + case GL_INT_VEC4:
  61 + glUniform4iv(location, 1, (int*)p_value);
  62 + break;
  63 + case GL_BOOL:
  64 + glUniform1iv(location, 1, (int*)p_value);
  65 + break;
  66 + case GL_BOOL_VEC2:
  67 + glUniform2iv(location, 1, (int*)p_value);
  68 + break;
  69 + case GL_BOOL_VEC3:
  70 + glUniform3iv(location, 1, (int*)p_value);
  71 + break;
  72 + case GL_BOOL_VEC4:
  73 + glUniform4iv(location, 1, (int*)p_value);
  74 + break;
  75 + case GL_FLOAT_MAT2:
  76 + glUniformMatrix2fv(location, 1, GL_FALSE, (float*)p_value);
  77 + break;
  78 + case GL_FLOAT_MAT3:
  79 + glUniformMatrix3fv(location, 1, GL_FALSE, (float*)p_value);
  80 + break;
  81 + case GL_FLOAT_MAT4:
  82 + glUniformMatrix4fv(location, 1, GL_FALSE, (float*)p_value);
  83 + break;
  84 + case GL_SAMPLER_1D:
  85 + case GL_SAMPLER_2D:
  86 + case GL_SAMPLER_3D:
  87 + case GL_SAMPLER_CUBE:
  88 + case GL_SAMPLER_1D_SHADOW:
  89 + case GL_SAMPLER_2D_SHADOW:
  90 + default:
  91 + glUniform1iv(location, 1, (int*)p_value);
  92 + break;
  93 + }
  94 + CHECK_OPENGL_ERROR
  95 + }
  96 + rts_glShaderUniform()
  97 + {
  98 + location = -1;
  99 + p_value = NULL;
  100 + }
  101 +};
  102 +
  103 +
  104 +
  105 +#endif
0 106 \ No newline at end of file
... ...
rts/gl/rts_glUtilities.h 0 → 100755
  1 +#ifndef RTS_GLUTILITIES_H
  2 +#define RTS_GLUTILITIES_H
  3 +
  4 +
  5 +#define CHECK_OPENGL_ERROR \
  6 +{ GLenum error; \
  7 + while ( (error = glGetError()) != GL_NO_ERROR) { \
  8 + printf( "OpenGL ERROR: %s\nCHECK POINT: %s (line %d)\n", gluErrorString(error), __FILE__, __LINE__ ); \
  9 + } \
  10 +}
  11 +
  12 +#endif
0 13 \ No newline at end of file
... ...
rts/gl/texture.h 0 → 100755
  1 +#ifndef RTS_GLTEXTUREMAP_H
  2 +#define RTS_GLTEXTUREMAP_H
  3 +
  4 +//#include <GL/glew.h>
  5 +#include "rts/math/vector.h"
  6 +#include "rts/gl/error.h"
  7 +#include <stdlib.h>
  8 +
  9 +namespace rts{
  10 +
  11 +///This class stores an OpenGL texture map and is used by rts_glShaderProgram.
  12 +class glTexture
  13 +{
  14 +private:
  15 + void get_type() //guesses the texture type based on the size
  16 + {
  17 + if(size[1] == 0)
  18 + texture_type = GL_TEXTURE_1D;
  19 + else if(size[2] == 0)
  20 + texture_type = GL_TEXTURE_2D;
  21 + else
  22 + texture_type = GL_TEXTURE_3D;
  23 + }
  24 + void set_wrapping() //set the texture wrapping based on the dimensions
  25 + {
  26 + CHECK_OPENGL_ERROR
  27 + switch(texture_type)
  28 + {
  29 + case GL_TEXTURE_3D:
  30 + glTexParameteri(texture_type, GL_TEXTURE_WRAP_R_EXT, GL_REPEAT);
  31 + case GL_TEXTURE_2D:
  32 + glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  33 + case GL_TEXTURE_1D:
  34 + glTexParameteri(texture_type, GL_TEXTURE_WRAP_S, GL_REPEAT);
  35 + break;
  36 + case GL_TEXTURE_RECTANGLE_ARB:
  37 + glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  38 + glTexParameteri(texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  39 + break;
  40 +
  41 + default:
  42 + break;
  43 + }
  44 + CHECK_OPENGL_ERROR
  45 + }
  46 + //void set_bits(GLvoid* bits);
  47 +public:
  48 + vector<GLsizei, 3> size; //vector representing the size of the texture
  49 + GLuint name; //texture name assigned by OpenGL
  50 + GLenum texture_type; //1D, 2D, 3D
  51 + GLint internal_format; //number of components (ex. 4 for RGBA)
  52 + GLenum pixel_format; //type of data (RGBA, LUMINANCE)
  53 + GLenum data_type; //data type of the bits (float, int, etc.)
  54 +
  55 + //constructor
  56 + glTexture()
  57 + {
  58 + name = 0;
  59 + }
  60 + glTexture(GLvoid *bits,
  61 + GLenum type = GL_TEXTURE_2D,
  62 + GLsizei width = 256,
  63 + GLsizei height = 256,
  64 + GLsizei depth = 0,
  65 + GLint internalformat = 1,
  66 + GLenum format = GL_LUMINANCE,
  67 + GLenum datatype = GL_UNSIGNED_BYTE,
  68 + GLint interpolation = GL_LINEAR)
  69 + {
  70 + init(bits, type, width, height, depth, internalformat, format, datatype, interpolation);
  71 + }
  72 +
  73 + void begin()
  74 + {
  75 + glEnable(texture_type);
  76 + CHECK_OPENGL_ERROR
  77 + glBindTexture(texture_type, name);
  78 + CHECK_OPENGL_ERROR
  79 + }
  80 + void end()
  81 + {
  82 + glDisable(texture_type);
  83 + CHECK_OPENGL_ERROR
  84 + }
  85 +
  86 + ///Creates an OpenGL texture map. This function requires basic information about the texture map as well as a pointer to the bit data describing the texture.
  87 + void init(GLvoid *bits,
  88 + GLenum type = GL_TEXTURE_2D,
  89 + GLsizei width = 256,
  90 + GLsizei height = 256,
  91 + GLsizei depth = 0,
  92 + GLint internalformat = 1,
  93 + GLenum format = GL_LUMINANCE,
  94 + GLenum datatype = GL_UNSIGNED_BYTE,
  95 + GLint interpolation = GL_LINEAR)
  96 + {
  97 + CHECK_OPENGL_ERROR
  98 + if(name != 0)
  99 + glDeleteTextures(1, &name);
  100 +
  101 +
  102 + CHECK_OPENGL_ERROR
  103 + if(datatype == GL_FLOAT)
  104 + {
  105 + glPixelStorei(GL_PACK_ALIGNMENT, 4);
  106 + glPixelStorei(GL_UNPACK_ALIGNMENT, 4); //I honestly don't know what this does but it fixes problems
  107 + }
  108 + else if(datatype == GL_UNSIGNED_BYTE)
  109 + {
  110 + //glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  111 + //glPixelStorei(GL_PACK_ALIGNMENT, 1);
  112 + }
  113 + else if(datatype == GL_UNSIGNED_SHORT)
  114 + {
  115 + //glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
  116 + //glPixelStorei(GL_PACK_ALIGNMENT, 2);
  117 + }
  118 + CHECK_OPENGL_ERROR
  119 + glGenTextures(1, &name); //get the texture name from OpenGL
  120 + //cout<<"OpenGL Name: "<<name<<endl;
  121 + CHECK_OPENGL_ERROR
  122 + size = vector<GLsizei, 3>(width, height, depth); //assign the texture size
  123 + //get_type(); //guess the type based on the size
  124 + texture_type = type; //set the type of texture
  125 + glEnable(texture_type); //enable the texture map
  126 + CHECK_OPENGL_ERROR
  127 + glBindTexture(texture_type, name); //bind the texture for editing
  128 + CHECK_OPENGL_ERROR
  129 + set_wrapping(); //set the texture wrapping parameters
  130 + CHECK_OPENGL_ERROR
  131 + glTexParameteri(texture_type, GL_TEXTURE_MAG_FILTER, interpolation); //set filtering
  132 + CHECK_OPENGL_ERROR
  133 + glTexParameteri(texture_type, GL_TEXTURE_MIN_FILTER, interpolation);
  134 + CHECK_OPENGL_ERROR
  135 + internal_format = internalformat; //set the number of components per pixel
  136 + pixel_format = format; //set the pixel format
  137 + data_type = datatype; //set the data type
  138 + SetBits(bits); //send the bits to the OpenGL driver
  139 + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //replace the specified vertex color
  140 + CHECK_OPENGL_ERROR
  141 + glDisable(texture_type);
  142 +
  143 + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  144 + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  145 + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  146 + }
  147 + void Clean()
  148 + {
  149 + if(name != 0)
  150 + {
  151 + glDeleteTextures(1, &name);
  152 + CHECK_OPENGL_ERROR
  153 + name = 0;
  154 + }
  155 + }
  156 + void SetBits(GLvoid *bits)
  157 + {
  158 + glEnable(texture_type); //enable the texture map
  159 + CHECK_OPENGL_ERROR
  160 + glBindTexture(texture_type, name);
  161 + CHECK_OPENGL_ERROR
  162 +
  163 + switch(texture_type)
  164 + {
  165 + case GL_TEXTURE_3D:
  166 + glTexImage3D(texture_type, 0, internal_format, size[0], size[1], size[2], 0, pixel_format, data_type, bits);
  167 + CHECK_OPENGL_ERROR
  168 + break;
  169 + case GL_TEXTURE_2D:
  170 + case GL_TEXTURE_RECTANGLE_ARB:
  171 + glTexImage2D(texture_type, 0, internal_format, size[0], size[1], 0, pixel_format, data_type, bits);
  172 + CHECK_OPENGL_ERROR
  173 + break;
  174 + case GL_TEXTURE_1D:
  175 + glTexImage1D(texture_type, 0, internal_format, size[0], 0, pixel_format, data_type, bits);
  176 + CHECK_OPENGL_ERROR
  177 + break;
  178 + default:
  179 + //glTexImage2D(texture_type, 0, internal_format, size.x, size.y, 0, pixel_format, data_type, bits);
  180 + break;
  181 + }
  182 + CHECK_OPENGL_ERROR
  183 + }
  184 + void ResetBits(GLvoid *bits)
  185 + {
  186 + glEnable(texture_type); //enable the texture map
  187 + CHECK_OPENGL_ERROR
  188 + glBindTexture(texture_type, name);
  189 + CHECK_OPENGL_ERROR
  190 +
  191 + switch(texture_type)
  192 + {
  193 + case GL_TEXTURE_3D:
  194 + //glTexImage3D(texture_type, 0, internal_format, size.x, size.y, size.z, 0, pixel_format, data_type, bits);
  195 + break;
  196 + case GL_TEXTURE_2D:
  197 + case GL_TEXTURE_RECTANGLE_ARB:
  198 + glTexSubImage2D(texture_type, 0, 0, 0, size[0], size[1], pixel_format, data_type, bits);
  199 + CHECK_OPENGL_ERROR
  200 + break;
  201 + case GL_TEXTURE_1D:
  202 + //glTexImage1D(texture_type, 0, internal_format, size.x, 0, pixel_format, data_type, bits);
  203 + break;
  204 + default:
  205 + //glTexImage2D(texture_type, 0, internal_format, size.x, size.y, 0, pixel_format, data_type, bits);
  206 + break;
  207 + }
  208 + glDisable(texture_type);
  209 + CHECK_OPENGL_ERROR
  210 + }
  211 + void* GetBits(GLenum format, GLenum type)
  212 + {
  213 + //returns the texture data
  214 +
  215 + int components;
  216 + switch(format)
  217 + {
  218 + case GL_RED:
  219 + case GL_GREEN:
  220 + case GL_BLUE:
  221 + case GL_ALPHA:
  222 + case GL_LUMINANCE:
  223 + components = 1;
  224 + break;
  225 + case GL_LUMINANCE_ALPHA:
  226 + components = 2;
  227 + break;
  228 + case GL_RGB:
  229 + case GL_BGR:
  230 + components = 3;
  231 + break;
  232 + case GL_RGBA:
  233 + case GL_BGRA:
  234 + components = 4;
  235 + break;
  236 + }
  237 +
  238 + int type_size;
  239 + switch(type)
  240 + {
  241 + case GL_UNSIGNED_BYTE:
  242 + case GL_BYTE:
  243 + type_size = sizeof(char);
  244 + break;
  245 + case GL_UNSIGNED_SHORT:
  246 + case GL_SHORT:
  247 + type_size = sizeof(short);
  248 + break;
  249 + case GL_UNSIGNED_INT:
  250 + case GL_INT:
  251 + type_size = sizeof(int);
  252 + break;
  253 + case GL_FLOAT:
  254 + type_size = sizeof(float);
  255 + break;
  256 + }
  257 +
  258 + //allocate memory for the texture
  259 + void* result = malloc(components*type_size * size[0] * size[1]);
  260 +
  261 + begin();
  262 + glGetTexImage(texture_type, 0, format, type, result);
  263 +
  264 + CHECK_OPENGL_ERROR
  265 + end();
  266 +
  267 +
  268 + return result;
  269 +
  270 + }
  271 +};
  272 +
  273 +}
  274 +
  275 +#define RTS_UNKNOWN 0
  276 +
  277 +#endif
... ...
rts/math/complex.h
... ... @@ -15,12 +15,12 @@ namespace rts
15 15 {
16 16  
17 17 template <class T>
18   -struct rtsComplex
  18 +struct complex
19 19 {
20 20 T r, i;
21 21  
22 22 //default constructor
23   - CUDA_CALLABLE rtsComplex()
  23 + CUDA_CALLABLE complex()
24 24 {
25 25 r = 0.0;
26 26 i = 0.0;
... ... @@ -49,16 +49,16 @@ struct rtsComplex
49 49 }
50 50  
51 51 //constructor when given real and imaginary values
52   - CUDA_CALLABLE rtsComplex(T r, T i)
  52 + CUDA_CALLABLE complex(T r, T i)
53 53 {
54 54 this->r = r;
55 55 this->i = i;
56 56 }
57 57  
58 58 //return the current value multiplied by i
59   - CUDA_CALLABLE rtsComplex<T> imul()
  59 + CUDA_CALLABLE complex<T> imul()
60 60 {
61   - rtsComplex<T> result;
  61 + complex<T> result;
62 62 result.r = -i;
63 63 result.i = r;
64 64  
... ... @@ -68,70 +68,70 @@ struct rtsComplex
68 68 //ARITHMETIC OPERATORS--------------------
69 69  
70 70 //binary + operator (returns the result of adding two complex values)
71   - CUDA_CALLABLE rtsComplex<T> operator+ (const rtsComplex<T> rhs)
  71 + CUDA_CALLABLE complex<T> operator+ (const complex<T> rhs)
72 72 {
73   - rtsComplex<T> result;
  73 + complex<T> result;
74 74 result.r = r + rhs.r;
75 75 result.i = i + rhs.i;
76 76 return result;
77 77 }
78 78  
79   - CUDA_CALLABLE rtsComplex<T> operator+ (const T rhs)
  79 + CUDA_CALLABLE complex<T> operator+ (const T rhs)
80 80 {
81   - rtsComplex<T> result;
  81 + complex<T> result;
82 82 result.r = r + rhs;
83 83 result.i = i;
84 84 return result;
85 85 }
86 86  
87 87 //binary - operator (returns the result of adding two complex values)
88   - CUDA_CALLABLE rtsComplex<T> operator- (const rtsComplex<T> rhs)
  88 + CUDA_CALLABLE complex<T> operator- (const complex<T> rhs)
89 89 {
90   - rtsComplex<T> result;
  90 + complex<T> result;
91 91 result.r = r - rhs.r;
92 92 result.i = i - rhs.i;
93 93 return result;
94 94 }
95 95  
96 96 //binary - operator (returns the result of adding two complex values)
97   - CUDA_CALLABLE rtsComplex<T> operator- (const T rhs)
  97 + CUDA_CALLABLE complex<T> operator- (const T rhs)
98 98 {
99   - rtsComplex<T> result;
  99 + complex<T> result;
100 100 result.r = r - rhs;
101 101 result.i = i;
102 102 return result;
103 103 }
104 104  
105 105 //binary MULTIPLICATION operators (returns the result of multiplying complex values)
106   - CUDA_CALLABLE rtsComplex<T> operator* (const rtsComplex<T> rhs)
  106 + CUDA_CALLABLE complex<T> operator* (const complex<T> rhs)
107 107 {
108   - rtsComplex<T> result;
  108 + complex<T> result;
109 109 result.r = r * rhs.r - i * rhs.i;
110 110 result.i = r * rhs.i + i * rhs.r;
111 111 return result;
112 112 }
113   - CUDA_CALLABLE rtsComplex<T> operator* (const T rhs)
  113 + CUDA_CALLABLE complex<T> operator* (const T rhs)
114 114 {
115   - return rtsComplex<T>(r * rhs, i * rhs);
  115 + return complex<T>(r * rhs, i * rhs);
116 116 }
117 117  
118 118 //binary DIVISION operators (returns the result of dividing complex values)
119   - CUDA_CALLABLE rtsComplex<T> operator/ (const rtsComplex<T> rhs)
  119 + CUDA_CALLABLE complex<T> operator/ (const complex<T> rhs)
120 120 {
121   - rtsComplex<T> result;
  121 + complex<T> result;
122 122 T denom = rhs.r * rhs.r + rhs.i * rhs.i;
123 123 result.r = (r * rhs.r + i * rhs.i) / denom;
124 124 result.i = (- r * rhs.i + i * rhs.r) / denom;
125 125  
126 126 return result;
127 127 }
128   - CUDA_CALLABLE rtsComplex<T> operator/ (const T rhs)
  128 + CUDA_CALLABLE complex<T> operator/ (const T rhs)
129 129 {
130   - return rtsComplex<T>(r / rhs, i / rhs);
  130 + return complex<T>(r / rhs, i / rhs);
131 131 }
132 132  
133 133 //ASSIGNMENT operators-----------------------------------
134   - CUDA_CALLABLE rtsComplex<T> & operator=(const rtsComplex<T> &rhs)
  134 + CUDA_CALLABLE complex<T> & operator=(const complex<T> &rhs)
135 135 {
136 136 //check for self-assignment
137 137 if(this != &rhs)
... ... @@ -141,7 +141,7 @@ struct rtsComplex
141 141 }
142 142 return *this;
143 143 }
144   - CUDA_CALLABLE rtsComplex<T> & operator=(const T &rhs)
  144 + CUDA_CALLABLE complex<T> & operator=(const T &rhs)
145 145 {
146 146 this->r = rhs;
147 147 this->i = 0;
... ... @@ -150,34 +150,34 @@ struct rtsComplex
150 150 }
151 151  
152 152 //arithmetic assignment operators
153   - CUDA_CALLABLE rtsComplex<T> operator+=(const rtsComplex<T> &rhs)
  153 + CUDA_CALLABLE complex<T> operator+=(const complex<T> &rhs)
154 154 {
155 155 *this = *this + rhs;
156 156 return *this;
157 157 }
158   - CUDA_CALLABLE rtsComplex<T> operator+=(const T &rhs)
  158 + CUDA_CALLABLE complex<T> operator+=(const T &rhs)
159 159 {
160 160 *this = *this + rhs;
161 161 return *this;
162 162 }
163 163  
164   - CUDA_CALLABLE rtsComplex<T> operator*=(const rtsComplex<T> &rhs)
  164 + CUDA_CALLABLE complex<T> operator*=(const complex<T> &rhs)
165 165 {
166 166 *this = *this * rhs;
167 167 return *this;
168 168 }
169   - CUDA_CALLABLE rtsComplex<T> operator*=(const T &rhs)
  169 + CUDA_CALLABLE complex<T> operator*=(const T &rhs)
170 170 {
171 171 *this = *this * rhs;
172 172 return *this;
173 173 }
174 174 //divide and assign
175   - CUDA_CALLABLE rtsComplex<T> operator/=(const rtsComplex<T> &rhs)
  175 + CUDA_CALLABLE complex<T> operator/=(const complex<T> &rhs)
176 176 {
177 177 *this = *this / rhs;
178 178 return *this;
179 179 }
180   - CUDA_CALLABLE rtsComplex<T> operator/=(const T &rhs)
  180 + CUDA_CALLABLE complex<T> operator/=(const T &rhs)
181 181 {
182 182 *this = *this / rhs;
183 183 return *this;
... ... @@ -189,9 +189,9 @@ struct rtsComplex
189 189 return std::sqrt(r * r + i * i);
190 190 }
191 191  
192   - CUDA_CALLABLE rtsComplex<T> log()
  192 + CUDA_CALLABLE complex<T> log()
193 193 {
194   - rtsComplex<T> result;
  194 + complex<T> result;
195 195 result.r = std::log(std::sqrt(r * r + i * i));
196 196 result.i = std::atan2(i, r);
197 197  
... ... @@ -199,9 +199,9 @@ struct rtsComplex
199 199 return result;
200 200 }
201 201  
202   - CUDA_CALLABLE rtsComplex<T> exp()
  202 + CUDA_CALLABLE complex<T> exp()
203 203 {
204   - rtsComplex<T> result;
  204 + complex<T> result;
205 205  
206 206 T e_r = std::exp(r);
207 207 result.r = e_r * std::cos(i);
... ... @@ -216,18 +216,18 @@ struct rtsComplex
216 216 return pow((double)y);
217 217 }*/
218 218  
219   - CUDA_CALLABLE rtsComplex<T> pow(T y)
  219 + CUDA_CALLABLE complex<T> pow(T y)
220 220 {
221   - rtsComplex<T> result;
  221 + complex<T> result;
222 222  
223 223 result = log() * y;
224 224  
225 225 return result.exp();
226 226 }
227 227  
228   - CUDA_CALLABLE rtsComplex<T> sqrt()
  228 + CUDA_CALLABLE complex<T> sqrt()
229 229 {
230   - rtsComplex<T> result;
  230 + complex<T> result;
231 231  
232 232 //convert to polar coordinates
233 233 T a = std::sqrt(r*r + i*i);
... ... @@ -253,7 +253,7 @@ struct rtsComplex
253 253 }
254 254  
255 255 //COMPARISON operators
256   - CUDA_CALLABLE bool operator==(rtsComplex<T> rhs)
  256 + CUDA_CALLABLE bool operator==(complex<T> rhs)
257 257 {
258 258 if(r == rhs.r && i == rhs.i)
259 259 return true;
... ... @@ -267,72 +267,44 @@ struct rtsComplex
267 267 return false;
268 268 }
269 269  
270   - /*//FRIEND functions
271   - //unary minus operator (for negating the complex number)
272   - template<class A> CUDA_CALLABLE friend complex<A> operator-(const complex<A> &rhs);
273   -
274   - //multiplication by T values when the complex number isn't on the left hand side
275   - template<class A> CUDA_CALLABLE friend complex<A> operator*(const A a, const complex<A> b);
276   -
277   - //division by T values when the complex number isn't on the left hand side
278   - template<class A> CUDA_CALLABLE friend complex<A> operator/(const A a, const complex<A> b);
279   -
280   - //POW function
281   - //template<class A> CUDA_CALLABLE friend complex<A> pow(const complex<A> x, T y);
282   - template<class A> CUDA_CALLABLE friend complex<A> pow(const complex<A> x, int y);
283   -
284   - //log function
285   - template<class A> CUDA_CALLABLE friend complex<A> log(complex<A> x);
286   -
287   - //exp function
288   - template<class A> CUDA_CALLABLE friend complex<A> exp(complex<A> x);
289   -
290   - //sqrt function
291   - template<class A> CUDA_CALLABLE friend complex<A> sqrt(complex<A> x);
292   -
293   - //trigonometric functions
294   - template<class A> CUDA_CALLABLE friend complex<A> sin(complex<A> x);
295   -
296   - template<class A> CUDA_CALLABLE friend complex<A> cos(complex<A> x);*/
297   -
298 270 };
299 271  
300 272 } //end RTS namespace
301 273  
302 274 //addition
303 275 template<typename T>
304   -CUDA_CALLABLE static rts::rtsComplex<T> operator+(const double a, const rts::rtsComplex<T> b)
  276 +CUDA_CALLABLE static rts::complex<T> operator+(const double a, const rts::complex<T> b)
305 277 {
306   - return rts::rtsComplex<T>(a + b.r, b.i);
  278 + return rts::complex<T>(a + b.r, b.i);
307 279 }
308 280  
309 281 //subtraction with a real value
310 282 template<typename T>
311   -CUDA_CALLABLE static rts::rtsComplex<T> operator-(const double a, const rts::rtsComplex<T> b)
  283 +CUDA_CALLABLE static rts::complex<T> operator-(const double a, const rts::complex<T> b)
312 284 {
313   - return rts::rtsComplex<T>(a - b.r, -b.i);
  285 + return rts::complex<T>(a - b.r, -b.i);
314 286 }
315 287  
316 288 //minus sign
317 289 template<typename T>
318   -CUDA_CALLABLE static rts::rtsComplex<T> operator-(const rts::rtsComplex<T> &rhs)
  290 +CUDA_CALLABLE static rts::complex<T> operator-(const rts::complex<T> &rhs)
319 291 {
320   - return rts::rtsComplex<T>(-rhs.r, -rhs.i);
  292 + return rts::complex<T>(-rhs.r, -rhs.i);
321 293 }
322 294  
323 295 //multiply a T value by a complex value
324 296 template<typename T>
325   -CUDA_CALLABLE static rts::rtsComplex<T> operator*(const double a, const rts::rtsComplex<T> b)
  297 +CUDA_CALLABLE static rts::complex<T> operator*(const double a, const rts::complex<T> b)
326 298 {
327   - return rts::rtsComplex<T>(a * b.r, a * b.i);
  299 + return rts::complex<T>((T)a * b.r, (T)a * b.i);
328 300 }
329 301  
330 302 //divide a T value by a complex value
331 303 template<typename T>
332   -CUDA_CALLABLE static rts::rtsComplex<T> operator/(const double a, const rts::rtsComplex<T> b)
  304 +CUDA_CALLABLE static rts::complex<T> operator/(const double a, const rts::complex<T> b)
333 305 {
334 306 //return complex<T>(a * b.r, a * b.i);
335   - rts::rtsComplex<T> result;
  307 + rts::complex<T> result;
336 308  
337 309 T denom = b.r * b.r + b.i * b.i;
338 310  
... ... @@ -350,41 +322,41 @@ CUDA_CALLABLE static complex&lt;T&gt; pow(complex&lt;T&gt; x, int y)
350 322 }*/
351 323  
352 324 template<typename T>
353   -CUDA_CALLABLE static rts::rtsComplex<T> pow(rts::rtsComplex<T> x, T y)
  325 +CUDA_CALLABLE static rts::complex<T> pow(rts::complex<T> x, T y)
354 326 {
355 327 return x.pow(y);
356 328 }
357 329  
358 330 //log function
359 331 template<typename T>
360   -CUDA_CALLABLE static rts::rtsComplex<T> log(rts::rtsComplex<T> x)
  332 +CUDA_CALLABLE static rts::complex<T> log(rts::complex<T> x)
361 333 {
362 334 return x.log();
363 335 }
364 336  
365 337 //exp function
366 338 template<typename T>
367   -CUDA_CALLABLE static rts::rtsComplex<T> exp(rts::rtsComplex<T> x)
  339 +CUDA_CALLABLE static rts::complex<T> exp(rts::complex<T> x)
368 340 {
369 341 return x.exp();
370 342 }
371 343  
372 344 //sqrt function
373 345 template<typename T>
374   -CUDA_CALLABLE static rts::rtsComplex<T> sqrt(rts::rtsComplex<T> x)
  346 +CUDA_CALLABLE static rts::complex<T> sqrt(rts::complex<T> x)
375 347 {
376 348 return x.sqrt();
377 349 }
378 350  
379 351  
380 352 template <typename T>
381   -CUDA_CALLABLE static T abs(rts::rtsComplex<T> a)
  353 +CUDA_CALLABLE static T abs(rts::complex<T> a)
382 354 {
383 355 return a.abs();
384 356 }
385 357  
386 358 template <typename T>
387   -CUDA_CALLABLE static T real(rts::rtsComplex<T> a)
  359 +CUDA_CALLABLE static T real(rts::complex<T> a)
388 360 {
389 361 return a.r;
390 362 }
... ... @@ -396,16 +368,16 @@ CUDA_CALLABLE static float real(float a)
396 368 }
397 369  
398 370 template <typename T>
399   -CUDA_CALLABLE static T imag(rts::rtsComplex<T> a)
  371 +CUDA_CALLABLE static T imag(rts::complex<T> a)
400 372 {
401 373 return a.i;
402 374 }
403 375  
404 376 //trigonometric functions
405 377 template<class A>
406   -CUDA_CALLABLE rts::rtsComplex<A> sin(const rts::rtsComplex<A> x)
  378 +CUDA_CALLABLE rts::complex<A> sin(const rts::complex<A> x)
407 379 {
408   - rts::rtsComplex<A> result;
  380 + rts::complex<A> result;
409 381 result.r = std::sin(x.r) * std::cosh(x.i);
410 382 result.i = std::cos(x.r) * std::sinh(x.i);
411 383  
... ... @@ -413,9 +385,9 @@ CUDA_CALLABLE rts::rtsComplex&lt;A&gt; sin(const rts::rtsComplex&lt;A&gt; x)
413 385 }
414 386  
415 387 template<class A>
416   -CUDA_CALLABLE rts::rtsComplex<A> cos(const rts::rtsComplex<A> x)
  388 +CUDA_CALLABLE rts::complex<A> cos(const rts::complex<A> x)
417 389 {
418   - rts::rtsComplex<A> result;
  390 + rts::complex<A> result;
419 391 result.r = std::cos(x.r) * std::cosh(x.i);
420 392 result.i = -(std::sin(x.r) * std::sinh(x.i));
421 393  
... ... @@ -424,12 +396,16 @@ CUDA_CALLABLE rts::rtsComplex&lt;A&gt; cos(const rts::rtsComplex&lt;A&gt; x)
424 396  
425 397  
426 398 template<class A>
427   -std::ostream& operator<<(std::ostream& os, rts::rtsComplex<A> x)
  399 +std::ostream& operator<<(std::ostream& os, rts::complex<A> x)
428 400 {
429 401 os<<x.toStr();
430 402 return os;
431 403 }
432 404  
  405 +#if __GNUC__ > 3 && __GNUC_MINOR__ > 7
  406 +template<class T> using rtsComplex = rts::complex<T>;
  407 +#endif
  408 +
433 409  
434 410  
435 411 #endif
... ...
rts/math/function.h 0 → 100644
  1 +#ifndef RTS_FUNCTION_H
  2 +#define RTS_FUNCTION_H
  3 +
  4 +#include <string>
  5 +
  6 +namespace rts{
  7 +
  8 +template <class X, class Y>
  9 +class function
  10 +{
  11 + //datapoint class for storing function points
  12 + struct dataPoint
  13 + {
  14 + X x;
  15 + Y y;
  16 + };
  17 +
  18 + //function data
  19 + std::vector<dataPoint> f;
  20 +
  21 + //comparison function for searching lambda
  22 + static bool findCeiling(dataPoint a, dataPoint b)
  23 + {
  24 + return (a.x > b.x);
  25 + }
  26 +
  27 +
  28 +public:
  29 + Y linear(X x)
  30 + {
  31 + //declare an iterator
  32 + typename std::vector< dataPoint >::iterator it;
  33 +
  34 + dataPoint s;
  35 + s.x = x;
  36 +
  37 + it = search(f.begin(), f.end(), &s, &s + 1, &function<X, Y>::findCeiling);
  38 +
  39 + //if the wavelength is past the end of the list, return the back
  40 + if(it == f.end())
  41 + return f.back().y;
  42 + //if the wavelength is before the beginning of the list, return the front
  43 + else if(it == f.begin())
  44 + return f.front().y;
  45 + //otherwise interpolate
  46 + else
  47 + {
  48 + X xMax = (*it).x;
  49 + X xMin = (*(it - 1)).x;
  50 + //std::cout<<lMin<<"----------"<<lMax<<std::endl;
  51 +
  52 + X a = (x - xMin) / (xMax - xMin);
  53 + Y riMin = (*(it - 1)).y;
  54 + Y riMax = (*it).y;
  55 + Y interp;
  56 + interp = riMin * a + riMax * (1.0 - a);
  57 + return interp;
  58 + }
  59 + }
  60 +
  61 + void insert(X x, Y y)
  62 + {
  63 + //declare an iterator
  64 + typename std::vector< dataPoint >::iterator it;
  65 +
  66 + dataPoint s;
  67 + s.x = x;
  68 + s.y = y;
  69 +
  70 + it = search(f.begin(), f.end(), &s, &s + 1, &function<X, Y>::findCeiling);
  71 +
  72 + //if the function value is past the end of the vector, add it to the back
  73 + if(it == f.end())
  74 + return f.push_back(s);
  75 + //otherwise add the value at the iterator position
  76 + else
  77 + {
  78 + f.insert(it, s);
  79 + }
  80 +
  81 + }
  82 +
  83 + X getX(unsigned int i)
  84 + {
  85 + return f[i].x;
  86 + }
  87 +
  88 + Y getY(unsigned int i)
  89 + {
  90 + return f[i].y;
  91 + }
  92 +
  93 + unsigned int getN()
  94 + {
  95 + return f.size();
  96 + }
  97 +
  98 + dataPoint operator[](int i)
  99 + {
  100 + return f[i];
  101 + }
  102 +
  103 + function<X, Y> operator+(Y r)
  104 + {
  105 + function<X, Y> result;
  106 +
  107 + //add r to every point in f
  108 + for(int i=0; i<f.size(); i++)
  109 + {
  110 + result.f.push_back(f[i]);
  111 + result.f[i].y += r;
  112 + }
  113 +
  114 + return result;
  115 + }
  116 +
  117 +
  118 +};
  119 +
  120 +} //end namespace rts
  121 +
  122 +
  123 +#endif
... ...
rts/math/matrix.h
... ... @@ -9,12 +9,12 @@ namespace rts
9 9 {
10 10  
11 11 template <class T, int N>
12   -struct rtsMatrix
  12 +struct matrix
13 13 {
14 14 //the matrix will be stored in column-major order (compatible with OpenGL)
15 15 T M[N*N];
16 16  
17   - rtsMatrix()
  17 + matrix()
18 18 {
19 19 for(int r=0; r<N; r++)
20 20 for(int c=0; c<N; c++)
... ... @@ -29,7 +29,7 @@ struct rtsMatrix
29 29 return M[col * N + row];
30 30 }
31 31  
32   - rtsMatrix<T, N> operator=(T rhs)
  32 + matrix<T, N> operator=(T rhs)
33 33 {
34 34 int Nsq = N*N;
35 35 for(int i=0; i<Nsq; i++)
... ... @@ -38,7 +38,7 @@ struct rtsMatrix
38 38 return *this;
39 39 }
40 40  
41   - /*rtsMatrix<T, N> operator=(rtsMatrix<T, N> rhs)
  41 + /*matrix<T, N> operator=(matrix<T, N> rhs)
42 42 {
43 43 for(int i=0; i<N; i++)
44 44 M[i] = rhs.M[i];
... ... @@ -46,9 +46,9 @@ struct rtsMatrix
46 46 return *this;
47 47 }*/
48 48  
49   - rtsVector<T, N> operator*(rtsVector<T, N> rhs)
  49 + vector<T, N> operator*(vector<T, N> rhs)
50 50 {
51   - rtsVector<T, N> result;
  51 + vector<T, N> result;
52 52  
53 53 for(int r=0; r<N; r++)
54 54 for(int c=0; c<N; c++)
... ... @@ -82,10 +82,14 @@ struct rtsMatrix
82 82 } //end namespace rts
83 83  
84 84 template <typename T, int N>
85   -std::ostream& operator<<(std::ostream& os, rts::rtsMatrix<T, N> M)
  85 +std::ostream& operator<<(std::ostream& os, rts::matrix<T, N> M)
86 86 {
87 87 os<<M.toStr();
88 88 return os;
89 89 }
90 90  
  91 +#if __GNUC__ > 3 && __GNUC_MINOR__ > 7
  92 +template<class T, int N> using rtsMatrix = rts::matrix<T, N>;
  93 +#endif
  94 +
91 95 #endif
... ...
rts/math/point.h
1 1 #ifndef RTS_rtsPoint_H
2 2 #define RTS_rtsPoint_H
3 3  
4   -#include "rts/math/vector.h"
  4 +#include "rts/math/vector.h"
5 5 #include <string.h>
6 6 #include "rts/cuda/callable.h"
7 7  
... ... @@ -9,17 +9,17 @@ namespace rts
9 9 {
10 10  
11 11 template <class T, int N>
12   -struct rtsPoint
  12 +struct point
13 13 {
14 14 T p[N];
15 15  
16   - CUDA_CALLABLE rtsPoint()
17   - {
  16 + CUDA_CALLABLE point()
  17 + {
18 18  
19 19 }
20 20  
21 21 //efficiency constructor, makes construction easier for 1D-4D vectors
22   - CUDA_CALLABLE rtsPoint(T x, T y = (T)0.0, T z = (T)0.0, T w = (T)0.0)
  22 + CUDA_CALLABLE point(T x, T y = (T)0.0, T z = (T)0.0, T w = (T)0.0)
23 23 {
24 24 if(N >= 1)
25 25 p[0] = x;
... ... @@ -29,51 +29,51 @@ struct rtsPoint
29 29 p[2] = z;
30 30 if(N >= 4)
31 31 p[3] = w;
32   - }
33   -
34   - //arithmetic operators
35   - CUDA_CALLABLE rts::rtsPoint<T, N> operator+(rts::rtsVector<T, N> v)
36   - {
37   - rts::rtsPoint<T, N> r;
38   -
39   - //calculate the position of the resulting rtsPoint
40   - for(int i=0; i<N; i++)
41   - r.p[i] = p[i] + v.v[i];
42   -
43   - return r;
44   - }
45   - CUDA_CALLABLE rts::rtsPoint<T, N> operator-(rts::rtsVector<T, N> v)
46   - {
47   - rts::rtsPoint<T, N> r;
48   -
49   - //calculate the position of the resulting rtsPoint
50   - for(int i=0; i<N; i++)
51   - r.p[i] = p[i] - v.v[i];
52   -
53   - return r;
54   - }
55   - CUDA_CALLABLE rts::rtsVector<T, N> operator-(rts::rtsPoint<T, N> rhs)
56   - {
57   - rts::rtsVector<T, N> r;
58   -
59   - //calculate the position of the resulting rtsPoint
60   - for(int i=0; i<N; i++)
61   - r.v[i] = p[i] - rhs.p[i];
62   -
63   - return r;
64   - }
65   - CUDA_CALLABLE rts::rtsPoint<T, N> operator*(T rhs)
66   - {
67   - rts::rtsPoint<T, N> r;
68   -
69   - //calculate the position of the resulting rtsPoint
70   - for(int i=0; i<N; i++)
71   - r.p[i] = p[i] * rhs;
72   -
73   - return r;
74 32 }
75 33  
76   - CUDA_CALLABLE rtsPoint(const T(&data)[N])
  34 + //arithmetic operators
  35 + CUDA_CALLABLE rts::point<T, N> operator+(vector<T, N> v)
  36 + {
  37 + rts::point<T, N> r;
  38 +
  39 + //calculate the position of the resulting point
  40 + for(int i=0; i<N; i++)
  41 + r.p[i] = p[i] + v.v[i];
  42 +
  43 + return r;
  44 + }
  45 + CUDA_CALLABLE rts::point<T, N> operator-(vector<T, N> v)
  46 + {
  47 + rts::point<T, N> r;
  48 +
  49 + //calculate the position of the resulting point
  50 + for(int i=0; i<N; i++)
  51 + r.p[i] = p[i] - v.v[i];
  52 +
  53 + return r;
  54 + }
  55 + CUDA_CALLABLE vector<T, N> operator-(point<T, N> rhs)
  56 + {
  57 + vector<T, N> r;
  58 +
  59 + //calculate the position of the resulting point
  60 + for(int i=0; i<N; i++)
  61 + r.v[i] = p[i] - rhs.p[i];
  62