Jump to content

  • Log In with Google      Sign In   
  • Create Account

Azenris

Member Since 22 Oct 2005
Offline Last Active Apr 22 2014 06:54 AM

Topics I've Started

Writing to a file

18 June 2012 - 10:36 AM

Hi, i have this function where I must be misunderstanding something because it is not as expected and I cannot figure out whats going on. Hoping for some help Posted Image

std::string CreateTempConvertedGfx(const std::string & name, const std::string & filename)
{
	std::string handle = CreateHandle(name);
	std::string tempFileName = "temps/" + handle + ".tmp";

	TargaImage image;

	if (!image.load(filename))
	{
		std::cout << "ERROR: Could not load targa image: " << filename << std::endl;
		while(1);
	}

	int frames = 1;
	int frameDelay = 10;
	int frameWidth = 16;
	int frameHeight = 16;

	ReadGDFFile(filename, frames, frameDelay, frameWidth, frameHeight);
	
	frames = image.getWidth() / frameWidth;

	GRAPHIC_DATA gfxData;

	strcpy(gfxData.m_handle, handle.c_str());

	gfxData.m_frames = frames;
	gfxData.m_frameDelay = frameDelay;
	gfxData.m_frameWidth = frameWidth;
	gfxData.m_frameHeight = frameHeight;

	gfxData.m_textureWidth = image.getWidth();
	gfxData.m_textureHeight = image.getHeight();
	gfxData.m_textureSize = image.getSize();

	std::ofstream dstFile(tempFileName);

	if (dstFile.is_open() == false)
	{
		std::cout << "ERROR: Could not open dst: " << tempFileName << std::endl;
		while(1);
	}

	long startp = dstFile.tellp();

	dstFile.write((char *)&gfxData, sizeof(GRAPHIC_DATA));				   // temp - THIS IS WRITING 61 BYTES WHEN GRAPHIC_DATA is 60 ?!

	// temp
	// these should be equal, and they are for Potion, the one with a gdf file
	// if you remove the gdf file it stops working and any you add one too
	// start to work
	// why does having a gdf file making it work and not having one break it
	long tellpp = dstFile.tellp();
	if (tellpp != sizeof(GRAPHIC_DATA))
	{
		std::cout << "ERROR: " << tempFileName << " [" << tellpp << " / " << sizeof(GRAPHIC_DATA) << "] : " << startp << std::endl;
		while(1);
	}
	// end temp

	dstFile.write((char *)image.getImageData(), image.getSize());

	dstFile.close();

	return tempFileName;
}

The problem is, the line
dstFile.write((char *)&gfxData, sizeof(GRAPHIC_DATA));
After calling this I check the the position with tellp and it ends up 1 position ahead of where it should be (i think).

The weird thing is, this write only goes 1 position past IF I have a .gdf file (just a basic file with some info).


The output ill get is
ERROR: temps/Attribute_Damage.tmp [61/60] : 0

And this error is for all files where ReadGDFFile doesnt open anything. Here is the source to that incase

void ReadGDFFile(const std::string & filename, int & frames, int & frameDelay, int & frameWidth, int & frameHeight)
{
	std::string gdfFilename = filename;
	size_t end = gdfFilename.find_last_of(".");

	if (end == std::string::npos)
	{
		std::cout << "ERROR: Invalid file name" << std::endl;
		while(1);
	}

	gdfFilename = gdfFilename.substr(0, end + 1) + "gdf";

	std::ifstream gdfFile(gdfFilename);

	if (gdfFile.is_open())
	{
		std::string option;

		while (gdfFile.eof() == false)
		{
			gdfFile >> option;

			if (option == "frameDelay")
			{
				gdfFile >> frameDelay;
			}
			else if (option == "frameWidth")
			{
				gdfFile >> frameWidth;
			}
			else if (option == "frameHeight")
			{
				gdfFile >> frameHeight;
			}
			else
			{
				std::cout << "Unknown GDF attribute name: " << option;
				while(1);
			}
		}

		gdfFile.close();
	}
}

const int MAX_GFX_NAME = 32;

#pragma pack(push, 1)

struct GRAPHIC_DATA
{
	char m_handle[MAX_GFX_NAME];
	int m_frames;
	int m_frameDelay;
	int m_frameWidth;
	int m_frameHeight;
	int m_textureWidth;
	int m_textureHeight;
	int m_textureSize;
};

#pragma pack(pop)


The thing is Im not sure how anything changes how much data is written. sizeof(GRAPHIC_DATA) should always be the same.

Please ask if you need more info. I have attached the source.
To test, compile, then drag and drop the "files" folder onto the exe in the debug folder.

Thanks for any help.

Colouring verticies vbos/shaders

05 June 2012 - 09:41 AM

Im just starting out with this trying to get a triangle to show coloured. It currently only shows as white though. Probably something simple I'm overlooking, but I cant seem to see it.
CApplication::CApplication(void)
	: m_appName("<Name> - <Build Version>")
	, shader(GLSLProgram("basic_tex.vert", "basic_tex.frag"))
{
}

bool CApplication::Init(void)
{
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	shader.bindAttrib(0, "a_Vertex");
	shader.bindAttrib(1, "a_Color");

	shader.linkProgram();
	shader.bindShader();

	if (!glGenBuffers || !glBindBuffer || !glBufferData)
		LOG->Send("VBOs not supported by your graphics card");

	// VERTICIES
	glGenBuffers(LAST_BUFFER, m_vbos);

	vertices.push_back(CVertex(-1.0f, -0.5f, -4.0f));
	vertices.push_back(CVertex(1.0f, -0.5f, -4.0f));
	vertices.push_back(CVertex(0.0f,  0.5f, -4.0f));

	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, m_vbos[VERTEX_BUFFER]);
	glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(CVertex), &vertices[0], GL_STATIC_DRAW);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

	// INDEX LIST
	indices.push_back(CIndex(0));
	indices.push_back(CIndex(1));
	indices.push_back(CIndex(2));

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vbos[INDEX_BUFFER]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(CIndex), &indices[0], GL_STATIC_DRAW);

	// COLOURS
	colorBuffer.push_back(CColour(1.0f, 0.0f, 0.0f));
	colorBuffer.push_back(CColour(1.0f, 0.0f, 0.0f));
	colorBuffer.push_back(CColour(1.0f, 0.0f, 0.0f));

	glEnableVertexAttribArray(1);
	glBindBuffer(GL_ARRAY_BUFFER, m_vbos[COLOUR_BUFFER]);
	glBufferData(GL_ARRAY_BUFFER, colorBuffer.size() * sizeof(CColour), &colorBuffer[0], GL_STATIC_DRAW);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vbos[INDEX_BUFFER]);

	return true;
}

void CApplication::Render(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glGetFloatv(GL_PROJECTION, projMatrix);
	glGetFloatv(GL_MODELVIEW, modelMatrix);

	shader.sendUniform("modelview_matrix", modelMatrix);
	shader.sendUniform("projection_matrix", projMatrix);

	glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
}

void CApplication::OnResize(int width, int height)
{
	if (width == 0)
		height = 1;

	glViewport(0, 0, width, height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(52.0f, float(width) / float(height), 1.0f, 100.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

vertshader
uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;

layout(location = 0) in vec3 a_Vertex;
layout(location = 1) in vec3 a_Color;

out vec4 color;

void main(void)
{
	gl_Position = projection_matrix * modelview_matrix * vec4(a_Vertex, 1.0);	
	color = vec4(a_Color, 1.0);
}


frag shader
in vec4 color;
out vec4 outColor;

void main(void)
{
	outColor = color;
}


As mentioned my problem is a white triangle is shown on the screen. Currently I wanted it to show red, and eventually be able to specify each vertex a different colour.

Hope I managed to post across relevant information and placed it in the correct place :P
Is there anything that sticks out as wrong?

BTW if you need more information just ask and I'l put it up

Thankyou for any help ! :)

Getting Obj1 to face Obj2

17 October 2009 - 05:48 AM

Hi, I was wondering how I calculate this angle shown in the examples below example1.bmp (145 KB) example2.bmp (145 KB) I have the position of both objects, 0 starts east. X incs right Y incs down

Forward Delcarations

14 September 2009 - 08:06 AM

Hi, i know how to declare a basic class/struct but is it possible to somehow have what I have below.
// ===============================================================================
// FORWARD DECLARATIONS:
class CScreenManager::MOBILE;

// ===============================================================================
// FUNCTIONS:
bool test_dustbin_func_eo(CScreenManager::MOBILE *pMobile);
bool test_dustbin_func_so(CScreenManager::MOBILE *pMobile);

Is it possible to forward declare the CScreenManager::MOBILE struct? Im not sure how I would go about it, or whether I need to redesign it. Id rather not include the entire screen header into this header. TY for any help

BMP, writing to a struct

10 September 2009 - 11:00 AM

I ahve my struct
typedef struct _tBMP_HEADER
{
    unsigned short type;	// must be 19778
    unsigned long size;		// size of BMP file (in bytes)
    unsigned short reserved1;	// reserved, leave 0
    unsigned short reserved2;	// reserved, leave 0
    unsigned long imageOffset;	// offset to image data (in bytes)
} BMP_HEADER;




Within the loading function I have
FILE *pFile;   // file pointer
errno_t error; // used by fopen_s

error = fopen_s(&pFile, filename, "rb");         // open the file as read binary

if (pFile == NULL || error != 0)                 // check it opened ok
    return BMP_RETURN_FAIL_OPENFILE;             // just end now if it didnt

fread(&m_header, sizeof(BMP_HEADER), 1, pFile);  // read the header and store it




However when debugging and looking at m_header its shown as:
type =        19778
size =        0
reserved1 =   0
reserved2 =   1078
imageOffset = 2621440




Any help appreciated ty :) [Edited by - jpetrie on September 11, 2009 10:26:30 AM]

PARTNERS