Skip to main content
gamedev.net
🔒 Locked

Weird Procedural Texturing problem

Started by RenegadeKoolAid 0 replies 250+ views
Original Post
RenegadeKoolAid
RenegadeKoolAid
Im making a simple program to create a 2048 x 2048 texture out of a few other textures based on rgb colors in the base texture. For example, red in the base texture will be grass in the output texture, green will be sidewalk, etc. I have an all red base texture, so the output should be just the grass texture tiled all over, and it is but it alternates between red, green and blue stripes. Heres what it looks like: output.jpg Heres my code:

#include <windows.h>
#include <iostream>  
#include <cmath>
using namespace std;    

char RGBA_TEXTURE[]     = "ground.tga";
char GRASS_TEXTURE[]    = "grass3.tga";
char SIDEWALK_TEXTURE[] = "street.tga";
char OTHER_TEXTURE1[]   = "";
char OTHER_TEXTURE2[]   = "";

byte *loadTexture( char *name );
void writeTexture( byte *data, short w, short h );

int main( )
{
	int i = 0;
	byte r, g, b;
	
	byte *rgbArray = loadTexture( RGBA_TEXTURE );
	byte *grass    = loadTexture( GRASS_TEXTURE );
	byte *sidewalk = loadTexture( SIDEWALK_TEXTURE );

	byte *output = new byte[ 2048 * 2048 * 3 ];

	for( i = 0; i < 2048 * 2048 * 3; i += 3 )
	{
		r = (byte)( ( (float)rgbArray[i + 0] / 255.0f ) * ( grass[(i + 0) % (512*512)] ) );
		g = (byte)( ( (float)rgbArray[i + 0] / 255.0f ) * ( grass[(i + 1) % (512*512)] ) );
		b = (byte)( ( (float)rgbArray[i + 0] / 255.0f ) * ( grass[(i + 2) % (512*512)] ) ); 


		if( r > 255 )
			r = 255;
		if( r < 0 )
			r = 0;
		if( g > 255 )
			g = 255;
		if( g < 0 )
			g = 0;
		if( b > 255 )
			b = 255;
		if( b < 0 )
			b = 0;

		output[i + 0] = r;
		output[i + 1] = g;
		output[i + 2] = b;
	}

	writeTexture( output, 2048, 2048 );
	return 1;
}

byte *loadTexture( char *name )
{
	byte  junk[12];
	short width;              
    short height;             
    byte  bits;               
    byte  temp;         
	byte *data;
	int   i = 0;
	
	FILE *file = fopen( name, "rb" );     

	fread(  junk,   1, sizeof( junk   ), file ); 
	fread( &width,  1, sizeof( width  ), file );
	fread( &height, 1, sizeof( height ), file );
	fread( &bits,   1, sizeof( bits   ), file );  
	fread( &temp,   1, sizeof( temp   ), file );
	
	bits /= 8;

	data = new byte[ width * height * bits ];

	fread( data, 1, width * height * bits, file );

        for( i = 0; i < width * height * bits; i += bits ) 
	{								    
		temp        = data;
		data     = data[i + 2];
		data[i + 2] = temp;
	}


	fclose( file );	
	return data;
}

void writeTexture( byte *data, short w, short h )
{
	FILE *file = fopen( "output.tga", "wb" );
	
	byte identSize    = 0;
	byte colorMapType = 0;
	byte imageType    = 2;

	short colorMapStart  = 0;
	short colorMapLength = 0;
	byte  colorMapBits   = 0;

	short xStart = 0;
	short yStart = 0;
	short width  = w;
	short height = h;
	byte  bits   = 24;
	byte  descriptor = 0;

	fwrite( &identSize, sizeof( byte ), 1, file );
	fwrite( &colorMapType, sizeof( byte ), 1, file );
	fwrite( &imageType, sizeof( byte ), 1, file );
	fwrite( &colorMapStart, sizeof( short ), 1, file );
	fwrite( &colorMapLength, sizeof( short ), 1, file );
	fwrite( &colorMapBits, sizeof( byte ), 1, file );
	fwrite( &xStart, sizeof( short ), 1, file );
	fwrite( &yStart, sizeof( short ), 1, file );
	fwrite( &width, sizeof( short ), 1, file );
	fwrite( &height, sizeof( short ), 1, file );
	fwrite( &bits, sizeof( byte ), 1, file );
	fwrite( &descriptor, sizeof( byte ), 1, file );

	fwrite( data, sizeof( byte ), w * h * 3, file );

	fclose( file );
}
Does anyone have an idea why it would be alternating like that? Since the base texture is pure red it should be just putting pixels from the grass texture into the output texture, but obviously its shading them for some reason. Thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.