Archive for the ‘c++’ Category

Posted by kent at 31 January 2007

Category: c++

Fungerer ved å spørre klassen etter en gitt texture (static GLuint load(const char *file)), deretter returnerer den en ID (som brukes av glBindTexture()) for å fargelegge objekter og sånt. Er ikke testet 100% og forutsetter at filnavnet eksisterer.

Klassen er 100% statisk og er ikke beregnet på å instansieres.

Bruk, eksempel:

	j1->tex =j2->tex =j3->tex =j4->tex = GLTexture::load("earth.raw");
	sol->tex = GLTexture::load("sol.raw");

Klassekode:

// GLTexture.h
#pragma once

#include <gl/GL.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <io.h>
#include <algorithm>
#include <map>

using namespace std;

static map<const char *, GLuint> bilder;	// texture, id
static Array<const char *> filer;		// filnavn

class GLTexture
{
private:
	// raw texture
	static GLuint LoadTextureRAW( const char * filename )
	{
		GLuint texture;
		int width, height;
		unsigned char * data;
		FILE * file;

		file = fopen( filename, "rb" );
		if ( file == NULL ) return 0;

		width = 1024;
		height = 512;
		data = (unsigned char *)malloc( width * height * 3 );

		fread( data, width * height * 3, 1, file );
		fclose( file );

		glGenTextures( 1, &texture );
		glBindTexture( GL_TEXTURE_2D, texture );
		glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
		glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
		glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
		glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
		glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

		gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );

		free( data );

		return texture;
	}

	// bitmap
	static GLuint LoadBitmap(const char *filename)
	{
		int i, j=0; //Index variables
		FILE *l_file; //File pointer
		unsigned char *l_texture; //The pointer to the memory zone in which we will load the texture

		// windows.h gives us these types to work with the Bitmap files
		BITMAPFILEHEADER fileheader;
		BITMAPINFOHEADER infoheader;
		RGBTRIPLE rgb;

		GLuint num_texture; // The counter of the current texture is increased
		glGenTextures( 1, &num_texture );
		if( (l_file = fopen(filename, "rb"))==NULL) return (-1); // Open the file for reading

		fread(&fileheader, sizeof(fileheader), 1, l_file); // Read the fileheader

		fseek(l_file, sizeof(fileheader), SEEK_SET); // Jump the fileheader
		fread(&infoheader, sizeof(infoheader), 1, l_file); // and read the infoheader

		// Now we need to allocate the memory for our image (width * height * color deep)
		l_texture = (byte *) malloc(infoheader.biWidth * infoheader.biHeight * 4);
		// And fill it with zeros
		memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4);

		// At this point we can read every pixel of the image
		for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++)
		{
				// We load an RGB value from the file
				fread(&rgb, sizeof(rgb), 1, l_file);

				// And store it
				l_texture[j+0] = rgb.rgbtRed; // Red component
				l_texture[j+1] = rgb.rgbtGreen; // Green component
				l_texture[j+2] = rgb.rgbtBlue; // Blue component
				l_texture[j+3] = 255; // Alpha value
				j += 4; // Go to the next position
		}

		fclose(l_file); // Closes the file stream

		glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter

		// The next commands sets the texture parameters
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); //The minifying function

		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // We don't combine the color with the original surface color, use only the texture map.

		// Finally we define the 2d texture
		glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

		// And create 2d mipmaps for the minifying function
		gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

		free(l_texture); // Free the memory we used to load the texture

		return (num_texture); // Returns the current texture OpenGL ID
	}

public:
	static char *earth, *spaceship, *sol;

	GLTexture()
	{}

	static GLuint load(const char *file)
	{
		int size = bilder.size();

		for (int i=0; i<size; i++)
		{
			if (!strcmpi(file, filer[i]))
			{
				file = filer[i];
				i=size;
			}

		}

		GLuint id = bilder[file];

		if (id == 0)
		{
			// load texture
			const char *ext = strrchr(file, '.');
			if (ext == NULL) { ext = file; } else { ext++; }

			if (!strcmpi(ext,"raw"))
			{
				id = LoadTextureRAW(file);
			}

			if (!strcmpi(ext,"bmp"))
			{
				id = LoadBitmap(file);
			}

			bilder[file] = id;
			filer.add(file);
		}

		return bilder[file];
	}
};
VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share

Posted by kent at 31 January 2007

Category: c++

	void rem (int i)
	{
		data.erase(data.begin()+i);
	}
VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share

Posted by kent at 4 December 2006

Category: c++, Dev

Stack – Hjelpeobjekter.

  • Variabler i blokker { /* i disse blokkene */ }
  • { int a,b,c; }

Heap – Strategiske objekter.

  • Variabler som skal brukes over tid.
  • Car *car = new Car();

class Point<double, 3> point; Point // fjerdekomponenten er 1
class Vector<double, 3> vector; // fjerdekomponenten er 0



class A
{
public:
// default A()
// standard A(params)
// copyconst A(const A &a)
}

VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share