Ejemplo n.º 1
0
void GL_widget_2::initializeGL () {
	makeCurrent();
	init_common();

	set_scales();
	//init shaders
	glewInit();
	if (glewIsSupported("GL_VERSION_2_0")) {
		std::cout << "OpenGL 2.0 is supported" << std::endl;
		GLint texSize; 
		glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
		std::cout << "Maximum texture size: " << texSize << std::endl;

		char *vs,*fs;

		GLuint f,v;

		v = glCreateShader(GL_VERTEX_SHADER);
		f = glCreateShader(GL_FRAGMENT_SHADER);

		vs = read_text_from_file(":/pencil.vert");
		fs = read_text_from_file(":/pencil.frag");

		const char * vv = vs;
		const char * ff = fs;

		glShaderSource(v, 1, &vv,NULL);
		glShaderSource(f, 1, &ff,NULL);

		free(vs);free(fs);

		glCompileShader(v);
		int success;
		glGetShaderiv(v, GL_COMPILE_STATUS, &success);
		if (success!=GL_TRUE) std::cout << LOG_ERROR << "Vertex shader NOT compiled" << std::endl;
		glCompileShader(f);
		glGetShaderiv(f, GL_COMPILE_STATUS, &success);
		if (success!=GL_TRUE) std::cout << LOG_ERROR << "Fragment shader NOT compiled" << std::endl;

		npr_shader_program = glCreateProgram();

		glAttachShader(npr_shader_program,v);
		glAttachShader(npr_shader_program,f);

		glLinkProgram(npr_shader_program);
		glGetProgramiv(npr_shader_program, GL_LINK_STATUS, &success);
		if (success!=GL_TRUE) std::cout << LOG_ERROR << "Shader program NOT compiled" << std::endl;

		glUseProgram(npr_shader_program);
		GLint noise_uniform = glGetUniformLocation(npr_shader_program, "hatchmap");


		//				glGenTextures(1, &noise_texture);
		glActiveTexture(GL_TEXTURE0+6);

		noise_texture = bindTexture(QPixmap(":/perlin.png"), GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, noise_texture);

		noise_uniform = glGetUniformLocation(npr_shader_program, "hatchmap");
		glUniform1i(noise_uniform,6);

		glActiveTexture(GL_TEXTURE0);
		paper_texture = bindTexture(QPixmap(":/paper.png"), GL_TEXTURE_2D);

		QSettings settings;
		GLint hatchscale_location = glGetUniformLocation(npr_shader_program, "hatchScale");
		glUniform1f(hatchscale_location, settings.value("npr-render-distortion").toDouble());

		GLint edge_location = glGetUniformLocation(npr_shader_program, "edgeSize");
		glUniform1f(edge_location, settings.value("npr-render-edge-detection-width").toDouble());

		//glBindTexture(GL_TEXTURE_2D, paper_texture);
		//glBindTexture( GL_TEXTURE_2D, 0);

		glUseProgram(0);
	} else {
		std::cout << LOG_ERROR << "OpenGL 2.0 is not supported" << std::endl;
	}

	double xratio,yratio,bxmin,bxmax,bymin,bymax, half_width,half_height;
	switch (rendering_effect) {
		case RENDERING_PENCIL:
			// free old pencil buffer if exists
			if (pbuffer) {
				pbuffer->releaseFromDynamicTexture();
				glDeleteTextures(1, &dynamic_texture);
				delete pbuffer;
			}
			// create new pencil buffer and init
			pbuffer = new QGLPixelBuffer(QSize(2048, 2048), format(), this);
			pbuffer->makeCurrent();
			init_common();
		
			// generate texture that has the same size/format as the pbuffer
			dynamic_texture = pbuffer->generateDynamicTexture();

			// bind the dynamic texture to the pbuffer - this is a no-op under X11
			pbuffer->bindToDynamicTexture(dynamic_texture);

			// set projection matrix such that the 2048x2048 covers the view screen
			xratio = 2048.0/width();
			bxmin = ((1-xratio)*xmax + (1+xratio)*xmin)/2;
			bxmax = ((1+xratio)*xmax + (1-xratio)*xmin)/2;
			yratio = 2048.0/height();
			bymin = ((1-yratio)*ymax + (1+yratio)*ymin)/2;
			bymax = ((1+yratio)*ymax + (1-yratio)*ymin)/2;
			glViewport(0,0,2048,2048);
			glMatrixMode(GL_PROJECTION);
		    glLoadIdentity();
			glOrtho(bxmin, bxmax, bymin, bymax, -1, 1);
			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();

			// update the widget projection such that it is a window in the middle of 2048x2048
			makeCurrent();
			glViewport(0, 0, width(), height());
			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			half_width=width()/2;
			half_height=height()/2;
		    glOrtho(1024-half_width, 1024+half_width, 1024-half_height, 1024+half_height, -1, 1);
			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();

			break;
		default:

			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			glOrtho(0, width(), 0, height(), -1, 1);
			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();
			break;
	}


}
Ejemplo n.º 2
0
int main(int argc, char* argv[]) {

  if(argc != 3 && argc != 4){
    fprintf(stderr, "Execute: %s <input file> <alphabet size> [<validation_file>]\n", argv[0]);
    exit(-1);
  }

  unsigned long n; // Size of the input sequence
  symbol* text = read_text_from_file(argv[1], &n); // Input sequence
  unsigned int alphabet = (unsigned int)atoi(argv[2]); // Size of the alphabet

  //printf("n: %lu, alphabet: %u, threads: %d\n", n, alphabet, num_threads);

//  printf("%s,%lu,%u,", argv[1], n, alphabet);

  // Memory usage
#ifdef MALLOC_COUNT
  size_t s_total_memory = malloc_count_total();
  size_t s_current_memory = malloc_count_current();
  malloc_reset_peak();

  // Running time. CLOCK_THREAD_CPUTIME_ID: Running time of the thread that call it (main thread in this case)
#else
  struct timespec stime, etime;
  double t;
  if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &stime)) {
    fprintf(stderr, "clock_gettime failed");
    exit(-1);
  }
#endif

  // Wavelet tree construction
#ifdef NO_RANK_SELECT
  BIT_ARRAY** wtree = wt_create(text, n, alphabet);
#else
  bitRankW32Int** wtree = wt_create(text, n, alphabet);
#endif

#ifdef MALLOC_COUNT
  size_t e_total_memory = malloc_count_total();
  size_t e_current_memory = malloc_count_current();
  printf("%s, %u, %zu, %zu, %zu, %zu, %zu\n", argv[1], alphabet, s_total_memory, e_total_memory, malloc_count_peak(), s_current_memory, e_current_memory);

#else
  if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &etime)) {
    fprintf(stderr, "clock_gettime failed");
    exit(-1);
  }

  t = (etime.tv_sec - stime.tv_sec) + (etime.tv_nsec - stime.tv_nsec) / 1000000000.0;
  printf("%d,%s,%lu,%lf\n", __cilkrts_get_nworkers(), argv[1], n, t);
//  printf("%d,%s,%lu\n", __cilkrts_get_nworkers(), argv[1], n); // Merge
#endif

  free(text);

  // Validation mode
  // Generate the original text using the wavelet tree
  if(argc > 3) {
#ifdef NO_RANK_SELECT
    printf("Compile without -DNO_RANK_SELECT to use the validation mode\n");
#else
    FILE *test_file;
    test_file = fopen(argv[3],"wb");

    if (!test_file) {
      printf("Unable to open file!");
      return EXIT_FAILURE;
    }

    symbol access_return = 0;
    unsigned long i = 0;

    if(n*sizeof(symbol) > 10485760)
      printf("Please, use a file smaller than 10MB to validate the algorithm. Otherwise, the validation could take a while.\n");
    else {
      for(i = 0; i < n; i++) {
	access_return =  wt_access(wtree, i, alphabet);
	fwrite(&access_return, sizeof(symbol), 1, test_file);
      }
    }
    fclose(test_file);
#endif
  }

  return EXIT_SUCCESS;
}