A couple of small problems

Started by
7 comments, last by Yaoyi 10 years, 2 months ago

Hi,

I'm having a few issues with my application, and have a few queries. If you can help with any of them it would be greatly appreciated (These were all problems I gathered from not being able to find a solution on google).

I will list them here tl;dr version - maybe you can see what you might know:

1. Mipmapping isn't working

2. blending textures/quads to make a smooth transition between separate textures - not sure how to approach this

3. Being able layer 2 textures, or one texture with transparent parts showing a unique color (need multiple possible colors for a single object)

4. Importing Maya/3dsMax animation - More looking for guidance - will not attempt this in the next few months, but maybe after.

(Not OpenGL related, but I don't want to make too many topics - let me know if I should put these in a seperate thread)

5. How to determine if a chunk is in camera view - i.e. what to not render.

6. RTS drag selection - I know how to do one selection (with unit represented as a sphere and raysphering with the mouse), but how to drag a box like in all rts games, and have all units within that selected? I'm sure the math is really simple but I can't think of it.

Ok here are the questions in full:

__________________________________________________________________

1. Firstly, when I load a texture with mipmapping it just gives me a plain white texture (with value of -1). If I don't use mipmapping (enter mipmap as false), the texture renders fine, but looks like crap a long distance away.

My code is here:


int GRAPHICS::LoadTexture(const char *filename, bool mipmap, bool repeat)
{
	glEnable(GL_TEXTURE_2D);
	unsigned int id;
	glGenTextures(1, &id);
	SDL_Surface* img = IMG_Load(filename);

	if (img == NULL) 
	{
		OutputDebugString("\"");
		OutputDebugString(filename);
		OutputDebugString("\" failed to load.\n");
		return -1;
	}

	SDL_PixelFormat form = {NULL, 32, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff, 0, 255};
	SDL_Surface *img2 = SDL_ConvertSurface(img, &form, SDL_SWSURFACE);

	if (img2 == NULL)
	{
		OutputDebugString("\"");
		OutputDebugString(filename);
		OutputDebugString("\" failed to load. img2 == -1\n");
		return -1;
	}

	glBindTexture(GL_TEXTURE_2D, id);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	// Mipmapping
	if (mipmap)
		gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, img2->w, img2->h, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, img2->pixels);
	else
		glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,img2->w,img2->h,0,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8,img2->pixels);

	// Min Filter
	if (mipmap)
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
	else
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

	// Repeat
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeat ? GL_REPEAT : GL_CLAMP );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeat ? GL_REPEAT : GL_CLAMP );

	SDL_FreeSurface(img);
	SDL_FreeSurface(img2);

	return id;
}

Here's my Init function if you want to know that:


void GRAPHICS::Init(int width, int height, bool fullscreen)
{
	SDL_Init(SDL_INIT_EVERYTHING);
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	Refresh(width, height, 45.0f, fullscreen);
	glMatrixMode(GL_MODELVIEW);
	glEnable(GL_DEPTH_TEST);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_NORMALIZE);
	glEnable(GL_COLOR_MATERIAL);

	glLoadIdentity();
}

And the refresh function:


void GRAPHICS::Refresh(int width, int height, float fov, bool fullscreen)
{
	if (fullscreen)
		SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE | SDL_OPENGL | SDL_FULLSCREEN);
	else
		SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE | SDL_OPENGL);
	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(fov, ((float)width*2) / (float)height, 0.1, 500.0);
}

2. Secondly, I need to have my textures blend together. I can't really describe it with words so I will post an image:

Jos_Game_Scr_Shot.jpg

Left is my game so far, you can see the tiles don't blend, on the right is empire earth, the kind of thing I'm wanting to do. It's early enough that I can restructure my tile system. but I am not wanting to make a unique texture for blending every combination of tiles (coz there's a tonne if I want many variants of the tiles)

3. Thirdly, I want part of a texture to change color. I'm wanting to make an RTS - meaning many players can use the same units, but each unit needs to have part of their texture be a different colour. I do not know how to do this. I want to only need to load each object once. Look at right image above, see the blue on the little man, you can make this color anything in that game. That is what I am wanting.

What I was thinking with this is that If I could somehow just apply first a color (like blue or red), and on top of that apply the normal texture, and that texture's transparent parts will show through the color underneath. (For example, I could do glColor3f(1.0f, 0.0f, 0.0f) then glCallList of the object)

4. Fourthly, this is not so urgent, as I don't think I will implement this anytime soon, but is there a way to export animation the same way I export obj files? obj files are so good because they display each vertex, and I just read this data in. That's how I got the trees and the white dude in my scene, but I can't bring in any animation unless I export each frame as an object - something I'm not wanting to do.

I use Maya/3dsMax, any guidance is appreciated.

5. In my game, I have the world made of chunks, and each chunk contains 4x4 reference to tiles(I think it's better than just tiles on their own, maybe not feel free to give your opinion - I'm sure they might be helpful) - Is there a way to determine if one of these areas is not in view? At the moment I just draw everything, even if I can only see a few tiles.

6. RTS drag selection - Most info given in tl;dr - I already have a way to get opengl positions from the mouse (close and far to cast ray in right direction), I'm just not sure of how to draw a box (which would be 2D for the player) and have it know everything inside. I mean, the camera can rotate, then might start dragging from a spot, and drag across many screens, making the start point actually off screen. So many problems, not sure how to solve.

Any help/or suggestions with any of these things will be greatly appreciated. I will tend to this thread when I'm not working. I am not a good programmer, so some of these might be really obvious, and I'm sorry if that is so.

Advertisement

Maybe I can aswer to question #1:

gluBuild2DMipmaps will create the mipmaps, not send the actual texture data to the gpu. In the if(mipmap) you have to do both glTexImage2D and gluBuild2DMipmaps.

Am I wrong?

gluBuild2DMipmaps calls glTexImage2D itself as it needs to upload each level.

The only thing that comes to mind is the glut function doesnt like GL_UNSIGNED_INT_8_8_8_8

Looking at my old code I dont otherwise see why the OP's code doesnt work.

I also notice that I have switched to using glGenerateMipmap perhaps the OP could try that?

As for Q5: Google for occlusion culling

1. Do you mean you're returning -1 (error loading img or img2), you get -1 as a texture ID, or you have 0xFF in every byte of data in the resulting image? Or something else? Have you checked for errors with glGetError()?

2. Multitexturing. At least it looks like that's what the Empire Earth screenshot you provided is doing.

3. You can use different uv coordinates if it's viable and easy to accomplish, then just point to a different subtexture for the different player color. You can also point to an altogether different texture. Those two should be very obvious how they work. You can also pass in a uniform to the shader and do the same modify in the shader. I'm sure there are other approaches as well, but those come to mind at the moment. Passing in a "playerNumber" and doing calculations in the shader usually reduces vertex count and texture count and binding changes and such over doing the same in source code. (p.s. don't use glColor. Video cards can handle textures, and textures give you much better flexibility.)

4. You need to export animations in whatever format that is accepted by the library you use in your code to import the animations. If you hand-coded the obj importing code, you might try just using a library. You can obviously hand-code more complicated import code, but you could also spend that time coding something unique and valuable.

5. If you're using a grid, it's very easy to know which tiles are visible. You can use projection and camera matrices to know which coordinates are on the screen, and model matrix will tell you where the center of each grid square is. If your data structures are set up reasonably, you can skip the model matrix and just calculate the range of tiles visible since they're all predictable sizes and you should know which tile is at the origin (model matrix with no transform).

6. If you are able to calculate a 3D coordinate from the click already, just record both the start and stop coordinates. Maybe you're getting tripped up if you're recording the coordinates in screen space?

I know the answers are brief, but the questions could have really long answers if I went into too much detail for each. If some answer didn't make sense or you're still getting tripped up on something, that's why you're allowed to post more than once =)

Stuff

Thanks so much, really awesome answers. Ill respond to each part.

1. The funciton returns -1, meaning the texture didn't load properly along the way

2. Multitexturing - will look into that

3. Hmm I would really like to just be able to put a texture over a color and have transparencies show through. that would be the cleanest way but stuff I don't understand always seems simple from afar.

4. Well then I probably will use a library in future. Might get my hands dirty with the 3ds max library if it's royalty free.

5. Thanks empirical2, I will search that. To richardurich: That stuff seems a bit confusing to me. What terms should I search for to found out how to do all this?

6. Might look into this later.

Thanks for answers really appreciate it.

1. If it's returning -1 from IMG_Load() failing or SDL_ConvertSurface() failing, you're definitely leaving out some relevant code.

3. You can definitely color the texture and use transparency, but you can also texture the polygon with your color (allowing patterns and such instead of just flat colors) and then use transparency in the exact same way for a 2nd texture. Maybe once you check out multitexturing it will make more sense what options are available. If you already know how to do the coloring thing, just do that until later. It's easy enough to change after the fact since you'd just add a texture. I was just thinking it would be easier to stick with a single thing (textures), especially since you already need to learn multitexturing for the blending you want in 2.

5. For grids, let's take a simple example where every tile is 10x10 pixels, and your screen is 800x600 (0,0 corner and 800,600 opposite corner). So you know you're displaying 80x60 tiles. Now you use the projection matrix to determine where the origin gets transformed. Let's say you discover that is at 1200,-100 in pixel terms (off-screen). And let's assume tile[200][200] is the one at the origin, and that tile array is 400x400 tiles total. So 1200 (x of origin tile) / 10 (pixel width per tile) = 120. So tile[200-120]

  • is at x=0. For Y, -100/10=-10. That means tile[80][210] starts at 0,0 pixel coordinate. Since you display 80x60 tiles, you'll be displaying tile[80][210] through tile[160][270]. (note: the math described is conflating 1-base/0-base counting constantly and ignoring a few things, but hopefully you get the basic idea)
  • If you don't understand how model, view, projection matrices work, poke around for tutorials on that until you find one that clicks. If you don't understand how the math works for isometric, there are tutorials on that too. If my example just plain didn't make sense, hopefully someone else can help out with the explanation since I know I suck at explaining math.

    I'm having a lot of trouble with #2. I need the texture to blend with the 8 surrounding texture, and I have no idea how to do this. I don't think multitexturing can this many textures. Really needing help with this one

    I'm having a lot of trouble with #2. I need the texture to blend with the 8 surrounding texture, and I have no idea how to do this. I don't think multitexturing can this many textures. Really needing help with this one

    You can definitely account for 8 surrounding textures in your blending. It's still just one texture, just 8 pieces of data telling you which part of the texture was used in the surrounding tiles.

    But let's look at this in a simpler and more explicit way. Let's say you have only 2 types of terrain: grass (G) and dirt (D). Let's say you have the following 9 bytes of data and are working on rendering the middle tile:

    GGG

    GDD

    DDD

    We'll assume 100x100 pixel tiles so they're big enough to see the effects. Let's use a static dropoff instead of quadratic so the math is easier: (sizeOfInfluence - distanceFromEdge)/sizeOfInfluence. Here's simple code:

    
    sizeOfInfluence = 25;//blend for 25 pixels out of the 100, so the left 25% of the tile will have bleed-in from the left texture,
                         //middle 50% is 100% our own tile, right 25% bleeds with right texture, etc.
    for (edge=0;edge<4;edge++){
      if ((distanceFromEdge[edge] >= 0) & (distanceFromEdge[edge] < sizeOfInfluence)){//only blend if we're in the area of influence
        blendIntensity = (sizeOfInfluence - distanceFromEdge[edge]) / sizeOfInfluence;
        blendAmount += blendIntensity * texture[edge];
      }
    }

    If you implement that, you'll notice the corners don't look nice. The upper-left is adding G+G at 100% each, so we're not really getting green any more. We can fix that by figuring out total blend intensity. Basically, this would be the alpha channel if we were making an overlay for blending the relies on transparency.

    
    sizeOfInfluence = 25;
    for (edge=0;edge<4;edge++){
      if ((distanceFromEdge[edge] >= 0) & (distanceFromEdge[edge] < sizeOfInfluence)){
        blendIntensity = (sizeOfInfluence - distanceFromEdge[edge]) / sizeOfInfluence;
        totalIntensity += blendIntensity;
        blendAmount += blendIntensity * texture[edge];
      }
    }
    if (totalIntensity > 100%){
      blendAmount = blendAmount / (totalIntensity / 100%);
      totalIntensity = 100%;
    }
    finalColor = blendAmount + texture[currentTile] * (100% - totalIntensity);
    

    Now we're looking better, but the upper-right corner is wrong since we basically want a horizontal line of grass and want to ignore the dirt on the right side. In the Empire Earth screenshot, it looks like they solve this by also including the corner tile in the calculation so

    GD

    DD

    winds up with grass on the diagonal D even though it doesn't directly border grass, and they seem to only blend on grass (not dirt). You can also trail off before the edge, which the lower-left corner should show the general concept. Remember all of this is using a linear approach, so you can get much better effects using more complicated approaches.

    Warning! I wrote the above code in this reply while trying to use algorithms and terminology I think are more friendly to learning. I didn't copy/paste it from real code nor type it into a compiler. It will have bugs and things I left out. Hopefully the concepts you need to learn still got through.

    I'm having a lot of trouble with #2. I need the texture to blend with the 8 surrounding texture, and I have no idea how to do this. I don't think multitexturing can this many textures. Really needing help with this one

    A possible implementation for #2 feature.

    Need to render twice.

    1: At first time rendering, output depth to a texture.

    2: In fragment shader of second time rendering, check the depth to see if it belong to an edge. (like a jump of depth )

    3: If this pixel belong to an edge, do a low-pass filter with texture from its own tile and neighboring tile.

    This topic is closed to new replies.

    Advertisement