7 Replies - 890 Views - Last Post: 20 December 2010 - 06:31 AM Rate Topic: -----

#1 Tiancailee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 04-December 10

Stuck in opengl

Posted 20 December 2010 - 01:03 AM

Hi i am doing amy assignment so far and i am stuck at texturing. i am supposed to make something like a first person shooter game whereby the player can explore the environment. but i do not know how do i do different texture on different shapes. the code given by my teacher when he was teaching my class texturing is


#include <stdio.h>
#include "freeglut.h"
#include <iostream>

typedef struct {
	GLubyte	* imageData;
	GLuint	bpp;
	GLuint	width;
	GLuint	height;
	GLuint	id;
} TextureImage;

//You need to have the prototype function since you include only "loadTGA.h"
bool loadTGA (TextureImage * texture, char * filename);



bool loadTGA (TextureImage * texture, char * filename) {
	GLubyte	TGAheader[] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};		// uncompressed TGA header
	GLubyte	TGAcompare[12];		// for comparing TGA header
	GLubyte	header[6];			// first 6 useful header
	GLuint	bytesPerPixel;
	GLuint	imageSize;
	GLuint	temp;
	GLuint	type = GL_RGBA;		// default to 32 bits per pixel

	FILE * file = fopen (filename, "rb");
	if (!file) return false;

	if (fread (TGAcompare, 1, sizeof (TGAcompare), file) != sizeof (TGAcompare)) {
		cout << "TGAcompare size inconsistent" << endl;
		fclose (file);
		return false;
	}

	if (memcmp (TGAheader, TGAcompare, sizeof (TGAheader)) != 0) {
		cout << "memcmp TGAheader failed" << endl;
		fclose (file);
		return false;
	}

	if (fread (header, 1, sizeof (header), file) != sizeof (header)) {
		cout << "error reading header" << endl;
		fclose (file);
		return false;
	}

	texture->width = header[1] * 256 + header[0];
	texture->height = header[3] * 256 + header[2];

	if (texture->width <= 0 || texture->height <= 0) {
		cout << "texture width / height error" << endl;
		fclose (file);
		return false;
	}

	if (header[4] != 24 && header[4] != 32) {
		cout << "not 24 / 32 bit format" << endl;
		fclose (file);
		return false;
	}

	texture->bpp = header[4];
	bytesPerPixel = texture->bpp / 8;
	imageSize = texture->width * texture->height * bytesPerPixel;
	texture->imageData = (GLubyte *) malloc (imageSize);

	if (!texture->imageData) {
		cout << "error allocating texture memory" << endl;
		fclose (file);
		return false;
	}

	if (fread(texture->imageData, 1, imageSize, file) != imageSize) {	// does size match?
		cout << "allocated memory does not match image size" << endl;
		free (texture->imageData);
		fclose (file);
		return false;
	}

	for (int i = 0; i < imageSize; i += bytesPerPixel) {	// swap red and blue bytes
		temp = texture->imageData[i];
		texture->imageData[i] = texture->imageData[i+2];
		texture->imageData[i+2] = temp;
	}

	fclose (file);

	glGenTextures (1, &texture->id);
	glBindTexture (GL_TEXTURE_2D, texture->id);

	int channels;
	if (type == GL_RGB) channels = 3;
	else channels = 4;

	if (texture[0].bpp == 24) type = GL_RGB;


	glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, texture->width, texture->height, 0, type,
				  GL_UNSIGNED_BYTE, texture->imageData);

	free (texture->imageData);
	return true;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Stuck in opengl

#2 Scottm  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 42
  • View blog
  • Posts: 238
  • Joined: 11-February 10

Re: Stuck in opengl

Posted 20 December 2010 - 01:05 AM

Try posting in the Game Programming forum. I'm sure they can help more:)
Was This Post Helpful? 0
  • +
  • -

#3 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Re: Stuck in opengl

Posted 20 December 2010 - 03:22 AM

In OpenGL you can use the glBindTexture function to choose which texture should be used for the next section of drawing. So the general idea is to choose which texture to bind and then draw the section you want the texture on, then if you decide you want to use a different texture for a different are then you use a different one.

pseudo code
glBindTexture(texture1);
DrawCube();

glBindTexture(texture2);
DrawTriangle();



In your loadTGA function you create the texture you want to bind. You might want to create and array of textures so that you can easily switch between them.

Hope that helps :)
Was This Post Helpful? 2
  • +
  • -

#4 Tiancailee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 04-December 10

Re: Stuck in opengl

Posted 20 December 2010 - 04:42 AM

ok. then must i do something like:


glTexCoord2f (0, 0); glVertex3f (0, -50, 100);
	glTexCoord2f (1, 0); glVertex3f (100, -50, 100);
	glTexCoord2f (1, 1); glVertex3f (100, -50, 0);
	glTexCoord2f (0, 1); glVertex3f (0, -50, 0);




as well?

oh ya and another thing is. the function given by my teacher rite, can i use a bitmap format pic and fit it in? or must it only be targa format??
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Re: Stuck in opengl

Posted 20 December 2010 - 04:46 AM

That's the actual drawing code. glTexCoord2f sets the texture coordinate and glVertex3f sets the vertex to draw.

something like this:
bool Init()
{
  TextureImage theTexture;
  loadTGA(&theTexture, "theTGA.tga");
}

void Draw()
{
  glBindTexture(GL_TEXTURE_2D, theTexture);
  glTexCoord2f (0, 0); glVertex3f (0, -50, 100);
  glTexCoord2f (1, 0); glVertex3f (100, -50, 100);
  glTexCoord2f (1, 1); glVertex3f (100, -50, 0);
  glTexCoord2f (0, 1); glVertex3f (0, -50, 0);
}


Was This Post Helpful? 0
  • +
  • -

#6 Tiancailee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 04-December 10

Re: Stuck in opengl

Posted 20 December 2010 - 04:48 AM

i tried doing it like this :

void room(void)
{
	//wall1();
	//wall2();
	//wall3();
	//wall4();
	glBindTexture (brick.tga);
	floor();
	//ceiling();
}




and they gave me an error saying

1>c:\users\dragoonz\documents\visual studio 2008\projects\comg_assignment2\comg_assignment2\assignment2.cpp(228) : error C2065: 'brick' : undeclared identifier
1>c:\users\dragoonz\documents\visual studio 2008\projects\comg_assignment2\comg_assignment2\assignment2.cpp(228) : error C2228: left of '.tga' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\dragoonz\documents\visual studio 2008\projects\comg_assignment2\comg_assignment2\assignment2.cpp(228) : error C2660: 'glBindTexture' : function does not take 1 arguments

and i also tried doing


void room(void)
{
	//wall1();
	//wall2();
	//wall3();
	//wall4();
	glBindTexture ("brick.tga");
	floor();
	//ceiling();
}




and they gave me an error like this:

1>c:\users\dragoonz\documents\visual studio 2008\projects\comg_assignment2\comg_assignment2\assignment2.cpp(228) : error C2660: 'glBindTexture' : function does not take 1 arguments

hmm i cant figure out what to put next
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Stuck in opengl

Posted 20 December 2010 - 05:56 AM

Moved to Game Programming.
Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Re: Stuck in opengl

Posted 20 December 2010 - 06:31 AM

If you look at the code I gave you, you will see that glBindTexture takes 2 arguments. One for the texture mode you are using, in your case GL_TEXTURE_2D and also the id of your texture which is an int (GLuint). You need to use your loadTGA function to create a texture to pass into glBindTexture, you cannot just pass in a filename.

Maybe this stuff is a little too advanced for you right now and you should speak to your lecturer about maybe getting some extra lessons. I say this only because the errors you had posted should be pretty self explanatory and easy to fix for anyone ready to tackle OpenGL.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1