• Content count

  • Joined

  • Last visited

Community Reputation

138 Neutral


  • Rank
  1. GUI nightmares

    Eh, your code actually looks pretty slick, considering you're doing GUI programming. Also, the "established libraries" for GUIs are notoriously bulky, so hand-rolling a solution is actually a viable option in this case.
  2. Hello, I need to learn the proper usage for glClearBufferData for an application I'm creating. I've looked at the official documentation... which isn't very descriptive, mainly what I don't understand are the internalformat, format, and type parameters. void glClearBufferData( GLenum target, GLenum internalformat, GLenum format, GLenum type, const void * data); I have a buffer with a target and some data, and all the data are in float form. I want to clear it all out to 0. Question is, how? Which one do I specify that it's in floats? Internalformat? What about the format and type? I just want to clear out all the memory. I know to specify NULL for data... but what about format and type?
  3. Ah well that makes sense. Although I thought UVs were only for supplying corners.
  4. OpenGL Lighting and Shading 101

    Seems that glut is messing with some internal state, no? You really shouldn't be using glut or fixed-functionality.
  5. 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.
  6. 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)[CODE] #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); } [/CODE]
  7. Here's what I'm currently using, straight from libpng. [CODE] 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); } [/CODE]
  8. Hello, I have a problem of which I couldn't seem to find any advice of on Google. I'm using a tileset creator (Tiled) for my game, which takes images and splits them up into tilesets for rendering. I figure this is very efficient and I would like to adopt this in my rendering. However, how can I actually just render a PORTION of a texture in OpenGL? Changing the texture coordinates does nothing more than just move the actual corners of the image. What I want to do is choose what area of the texture to render from, rather than having to split up the image into a bunch of smaller images and load them, which sounds extremely inefficient. Is this a GLSL or an OpenGL problem? Help!
  9. I love you. That fixed everything, and I can finally more forward with the program. THANK.YOU. In case anyone was wondering, this is what I was trying to describe:
  10. Guys, please help... I'm really desperate to get this done... please, can anyone explain this unfixable, sudden problem? It ONLY does this when I reset the rotated polygon and try to rotate it from there...
  11. It's... really hard to explain, it's rotating very strangely. It's supposed to rotate only on the Z axis, and was doing just that a while ago... header: [CODE]#ifndef POLY2D_H #define POLY2D_H #include "Global.h" #include "Dim2D.h" class Poly2D{ Dim2D<float>* center; float rotation; short (*vertices)[2]; float (*rotVertices)[2]; //rotated vertices const ushort vertLength; void setCenter(); public: Poly2D(Dim2D<short>* list[], const ushort len); ~Poly2D(); void translate(Dim2D<short>*& amount); void rotateBy(const float angle); void draw(); }; #endif[/CODE] definition file: [CODE]#include "Poly2D.h" Poly2D::Poly2D(Dim2D<short>* list[], const ushort len):center(new Dim2D<float>()), vertices(new short[len][2]), rotVertices(new float[len][2]), vertLength(len), rotation(0.0f){ for(ushort i = 0; i < len; i++){ vertices[i][X] = list[i] -> x; vertices[i][Y] = list[i] -> y; rotVertices[i][X] = (float)list[i] -> x; rotVertices[i][Y] = (float)list[i] -> y; } setCenter(); } Poly2D::~Poly2D(){ } void Poly2D::setCenter(){ float cx = 0.0f, cy = 0.0f; for(ushort i = 0; i < vertLength; i++){ cx += rotVertices[i][X]; cy += rotVertices[i][Y]; } cx = cx / (float)vertLength; cy = cy / (float)vertLength; center -> x = cx; center -> y = cy; } void Poly2D::translate(Dim2D<short>*& amount){ //todo: update this for(ushort i = 0; i < vertLength; i++){ vertices[i][X] += (float)amount -> x; vertices[i][Y] += (float)amount -> y; } setCenter(); } void Poly2D::rotateBy(const float angle){ rotation += angle; const float sinA = sinf(rotation), cosA = cosf(rotation); for(ushort i = 0; i < vertLength; i++){ //reset vertices rotVertices[i][X] = vertices[i][X]; rotVertices[i][Y] = vertices[i][Y]; float &x = rotVertices[i][X], &y = rotVertices[i][Y]; x -= center -> x; y -= center -> y; x = (x * cosA) - (y * sinA); y = (x * sinA) + (y * cosA); x += center -> x; y += center -> y; } } void Poly2D::draw(){ glEnableClientState(GL_VERTEX_ARRAY); glLoadIdentity(); glPushMatrix(); glVertexPointer(2, GL_FLOAT, 0, rotVertices); glDrawArrays(GL_POLYGON, 0, vertLength); glPopMatrix(); glDisableClientState(GL_VERTEX_ARRAY); }[/CODE]
  12. Strange... I took your advice, and I created another array to hold a copy of the vertex data to apply the rotations to... now it's wanting to rotate on like every axis? [CODE]void Poly2D::rotateBy(const float angle){ //accumulated rotation... what's the efficient way to clamp this? rotation += angle; const float sinA = sinf(rotation), cosA = cosf(rotation); for(ushort i = 0; i < vertLength; i++){ //this is just a copy of the vertex data rotVertices[i][X] = vertices[i][X]; rotVertices[i][Y] = vertices[i][Y]; float &x = rotVertices[i][X], &y = rotVertices[i][Y]; x -= center -> x; y -= center -> y; x = (x * cosA) - (y * sinA); y = (x * sinA) + (y * cosA); x += center -> x; y += center -> y; } }[/CODE]
  13. [quote name='jefferytitan' timestamp='1339564827' post='4948721'] This is common in graphics due to compounding rounding issues. I would suggest keeping the original polygon co-ordinates untouched, and do rendering based on a copy which is the original polygon rotated by the accumulated rotations. Also consider letting the GPU do the work, as it's pretty good at applying a transformation to a sequence of primitives. [/quote] Thanks, I figured that was the issue, I'll resolve it quite easily now. Also I'm not very good at things like GLSL, lol.
  14. I'm making my own polygon class for a game I'm making. However, whenever I try to apply rotation to 'em, it shrinks the entire polygon. After around 1000 rotations the shrinking becomes apparent, and visually you can notice them shrinking rapidly. Is there any way to minimize this? I'm assuming it's some precision errors? Here's my code below, I'm hoping it's enough to see my problem. (X and Y are macros btw) [CODE]void Poly2D::rotateBy(const float angle){ const float sinA = sinf(angle), cosA = cosf(angle); for(ushort i = 0; i < vertLength; i++){ float &x = vertices[i][X], &y = vertices[i][Y]; //move to origin for rotating x -= center -> x; y -= center -> y; x = (x * cosA) - (y * sinA); y = (x * sinA) + (y * cosA); //move back x += center -> x; y += center -> y; std::cout << x << ", " << y << std::endl; } }[/CODE]
  15. [quote name='ReaperSMS' timestamp='1332610919' post='4924931'] You're already telling it to use 2 shorts per vertex, glDrawArrays expects a vertex count, not a component count. [/quote] Thanks, guys! Fixed it, and it runs perfectly now. I do have one last question: can I just enable and disable the vertex and textrue arrays when I startup and shutdown my program, or should I enable and disable them every time I draw? I'm trying to conserve speed.