Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

SHOKWAV

Member Since 26 May 2011
Offline Last Active Dec 15 2012 01:09 PM
-----

Posts I've Made

In Topic: How to Render Only a Section of a Texture (OpenGL 3.1)?

10 December 2012 - 05:56 AM

Ah well that makes sense. Although I thought UVs were only for supplying corners.

In Topic: Lighting and Shading 101

09 December 2012 - 03:27 PM

Seems that glut is messing with some internal state, no? You really shouldn't be using glut or fixed-functionality.

In Topic: How to Render Only a Section of a Texture (OpenGL 3.1)?

09 December 2012 - 03:25 PM

The uvs are computed from xml files at run-time. But I can't 'skip' a portion of a texture using supplied uvs, right? Also, what I'm using is working, I just moved it to the fragment shader.

In Topic: How to Render Only a Section of a Texture (OpenGL 3.1)?

09 December 2012 - 09:54 AM

I don't see how this could be done CPU wise? But here's what I'm attempting to use to render a section of a texture (frag shader)

#version 140

in vec4 frag_color;

in vec2 frag_tex_coord;

uniform sampler2D tex;

uniform vec2 frag_tex_coord_offset;

void main(){

gl_FragColor = texture(tex, frag_tex_coord + frag_tex_coord_offset);

}


In Topic: glut:can someone recommend a simple png loading lib for vc2010

08 December 2012 - 08:35 PM

Here's what I'm currently using, straight from libpng.

int Memento::_GetTextureInfo(int color_type){
switch(color_type){
case PNG_COLOR_TYPE_GRAY:
  return (1);
  break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
  return (2);
  break;
case PNG_COLOR_TYPE_RGB:
  return (3);
  break;
case PNG_COLOR_TYPE_RGB_ALPHA:
  return (4);
  break;
default:
  return (-1);
  break;
};
return (-1);
}
unsigned Memento::_LoadImageIntoTexture(std::string const& file, unsigned& width, unsigned& height,
  bool clamp_texture){
png_struct* img_read_struct = NULL;
png_info* img_info_struct = NULL;
png_byte img_sig[PNG_SIG_SIZE], *img_pixels = NULL, **img_row_ptrs = NULL;
std::FILE* img_file = NULL;
int img_bit_depth, img_color_type, img_components;
unsigned tex, gl_colors, img_width, img_height;
if(!(img_file = std::fopen(file.c_str(), "rb")))return (PNG_READ_FAILURE);
std::fread(img_sig, PNG_SIG_SIZE, sizeof(png_byte), img_file);
if(png_sig_cmp(img_sig, 0, PNG_SIG_SIZE) != 0){
  std::fclose(img_file);
  return (PNG_READ_FAILURE);
}
std::rewind(img_file);
if(!(img_read_struct = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))){
  std::fclose(img_file);
  return (PNG_READ_FAILURE);
}
if(!(img_info_struct = png_create_info_struct(img_read_struct))){
  png_destroy_read_struct(&img_read_struct, NULL, NULL);
  std::fclose(img_file);
  return (PNG_READ_FAILURE);
}
if(setjmp(png_jmpbuf(img_read_struct))){
  png_destroy_read_struct(&img_read_struct, &img_info_struct, NULL);
  std::fclose(img_file);
  return (PNG_READ_FAILURE);
}
png_init_io(img_read_struct, img_file);
png_read_info(img_read_struct, img_info_struct);
img_bit_depth = png_get_bit_depth(img_read_struct, img_info_struct);
img_color_type = png_get_color_type(img_read_struct, img_info_struct);
if(img_color_type == PNG_COLOR_TYPE_PALETTE)
  png_set_palette_to_rgb(img_read_struct);
if(img_color_type == PNG_COLOR_TYPE_GRAY && img_bit_depth < 8){
  png_set_expand_gray_1_2_4_to_8(img_read_struct);
}
if(png_get_valid(img_read_struct, img_info_struct, PNG_INFO_tRNS))
  png_set_tRNS_to_alpha(img_read_struct);
if(img_bit_depth == 16){
  png_set_strip_16(img_read_struct);
}else if(img_bit_depth < 8){
  png_set_packing(img_read_struct);
}
png_read_update_info(img_read_struct, img_info_struct);
png_get_IHDR(img_read_struct, img_info_struct, &img_width,
   &img_height, &img_bit_depth, &img_color_type, NULL, NULL, NULL);
if((img_components = Memento::_GetTextureInfo(img_color_type)) == -1){
  png_destroy_read_struct(&img_read_struct, &img_info_struct, NULL);
  std::fclose(img_file);
  return (PNG_READ_FAILURE);
}
img_pixels = (png_byte*)malloc(sizeof(png_byte) * (img_width * img_height * img_components));
img_row_ptrs = (png_byte**)malloc(sizeof(png_byte*) * img_height);
for(unsigned i = 0; i < img_height; ++i)
  img_row_ptrs[i] = (png_byte*)(img_pixels + (i * img_width * img_components));
png_read_image(img_read_struct, img_row_ptrs);
png_read_end(img_read_struct, NULL);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if(clamp_texture){
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
}
	(img_components == 4)? (gl_colors = GL_RGBA):(0);
	(img_components == 3)? (gl_colors = GL_RGB):(0);
	(img_components == 2)? (gl_colors = GL_LUMINANCE_ALPHA):(0);
	(img_components == 1)? (gl_colors = GL_LUMINANCE):(0);
	glTexImage2D(GL_TEXTURE_2D, 0, img_components, img_width,
	  img_height, 0, gl_colors, GL_UNSIGNED_BYTE, img_pixels);
	glBindTexture(GL_TEXTURE_2D, 0);
	png_destroy_read_struct(&img_read_struct, &img_info_struct, NULL);
	std::fclose(img_file);
	free(img_row_ptrs);
	free(img_pixels);
	width = img_width;
	height = img_height;
	return (tex);
}

PARTNERS