Jump to content

  • Log In with Google      Sign In   
  • Create Account


Jossos

Member Since 19 Dec 2012
Offline Last Active Aug 26 2014 09:54 AM

Topics I've Started

Multitexturing not working

01 July 2014 - 11:03 AM

Ok, so I'm trying to get this shader to load 4 textures and blend them together (for tiling reasons).

#version 330

in vec2 TexCoord0;

uniform sampler2D gTex0;
uniform sampler2D gTex1;
uniform sampler2D gTex2;
uniform sampler2D gTex3;

out vec4 FragColor;

void main()
{
    vec4 color1 = texture2D(gTex0, TexCoord0);
    vec4 color2 = texture2D(gTex1, TexCoord0);
    vec4 color3 = texture2D(gTex2, TexCoord0);
    vec4 color4 = texture2D(gTex3, TexCoord0);

    color1 = color1 * (1 - TexCoord0.x) * TexCoord0.y;
    color2 = color2 * (1 - TexCoord0.x) * (1 - TexCoord0.y);
    color3 = color3 * TexCoord0.x * (1 - TexCoord0.y);
    color4 = color4 * TexCoord0.x * TexCoord0.y;

    FragColor = color1 + color2 + color3 + color4;
}

If i do 'FragColor = color1' (or any of the colors, like FragColor = color3 etc) it draws the texture, fading from its full color to black as it should do. Each of these textures work on their own, but when trying to use 2 or more (such as "FragColor = color1 + color2;"), it just renders black for some reason.

I can switch the color1/color2/color3/color4 in the shader without touching any other code and it renders that texture, so  im guessing that it is loading in all of the textures correctly.

 

If you need to see more code let me know, but it feels like the error is within the fragment shader itself.

 

Thanks


A couple of small problems

28 January 2014 - 04:37 AM

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.


Get blank screen in modelview, don't know why

27 October 2013 - 05:12 AM

I'm not understanding why I can't get anything rendered to the screen. I have code very similar to this in another program which renders fine, but this program just gives me a blank screen.

 

Here's my initialization code:

void GRAPHICS::InitGraphics(SDL_Surface* screen, bool fullscreen, int width, int height)
{
	SDL_Init(SDL_INIT_EVERYTHING);

	glClearColor(0.0, 0.0, 0.0, 1.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(35.0, width / height, 0.2, 500.0);

	//SetScreenAttributes(screen, fullscreen, width, height);
	SDL_SetVideoMode(width, height, 32, SDL_OPENGL);

	glMatrixMode(GL_MODELVIEW);

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

And here's my render function:

void GAME::Render()
{
		// ** Clear the screen and translations for a new render ** //
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	glMatrixMode(GL_PROJECTION);
	glOrtho(0, header.scrWidth, header.scrHeight, 0.0, -1.0, 1.0);
	glColor3f(1.0f, 1.0f, 1.0f);

	// 2D drawings here

		// ** Put all Drawing data here ** //

	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	gluPerspective(35.0, (float)header.scrWidth / (float)header.scrHeight, 0.2, 500.0);

	glColor3f(1.0f, 1.0f, 1.0f);

	glBegin(GL_TRIANGLES);
		glVertex3f(0.0f, 0.0f, 0.0f);
		glVertex3f(0.5f, 0.0f, 0.0f);
		glVertex3f(0.0f, 0.5f, 0.0f);
	glEnd();

	SDL_GL_SwapBuffers();
}

I'm sure the fault lies somewhere in one of these functions. I have other code, but for now a simple triangle on the screen would be nice, but I can't even get that. the clear color is black by the way.

 

Help is greatly appreciated!


Need help - loading textures

24 September 2013 - 03:08 AM

Hi, I use opengl with SDL. this function works in my other programs. I have the function load a file in the same directory as the other files and it works.

 

Now my problem is that I use this in another program, and for some reason, it returns some absurdly large number. I coped over working files to see if it was the actual image files, and no it still doesn't work. I watch the value of id in this code. In my older program, it holds the value 1 before it returns. In my new program, it returns something like 3465094356 something stupid like that.

// Load a Texture and return the ID
unsigned int GRAPHICS::LoadTexture(const char *filename)
{
	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;
	}

	unsigned int id;
	glGenTextures(1, &id);
	glBindTexture(GL_TEXTURE_2D, id);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img->w, img->h, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, img2->pixels);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	SDL_FreeSurface(img);
	SDL_FreeSurface(img2);

	return id;
}

I don't understand at all why it would behave differently in each program.

 

Help is appreciated


3D Movement and trigonometry

13 June 2013 - 06:25 PM

Hey, I'm not so good at trigonometry and require some assistance. I want a 3D scene to move when I drag with the middle mouse button. The scene can be rotated freely in the x and y directions (2 floats xRot and yRot), a functionality I already have working. What I want is the scene to translate(move) right, left, up down(I do some calculations of the mouse position), but I want the scene to move according to its current rotation. It's hard to describe, but I think It's understandable.

 

edit: What I want is similar to the camera-move function you find in most modelling programs. It shifts the scene in a direction with your mouse.

 

Help is greatly appreciated!


PARTNERS