Advertisement

Targa Loader, Quick Question

Started by December 14, 2004 02:22 PM
17 comments, last by Zeusbwr 19 years, 11 months ago
ok ok ok [wink]

well I guess because I wrote it in the reply window I wasn't thinking that far ahead [smile]

but points taken :)

heheh
Ok, as always, keep in mind im an opengl nub, as well as c++ freshman.

I cannot get it to bind textures, i used RipTorns version, called the load GL and junk, however when i use the class.bindOpenGL() nothing happens. I tried using "glBindTexture(GL_TEXTURE_2D, class.returnID);" (return(ID) is just a simple accessor method to return openGLid), and it still didnt work. Any ideas on what i am doing wrong? I can paste my code if needed, it's just a simple code with 3 objects, 1 triangle, 2 box, and one glut object, teapot.
Rank: OpenGL & Glut "Nub", C++ Freshman.
Advertisement
I can't think of anything that would cause that to happen...
Is it a case of not showing any texture at all, or not changing to a tga texture, but showing a different texture (eg a bitmap)?

if you could zip up your example I'll take a look for sure.
I'll need to install DevC++ or somthing (I'm on holiday ;) but that doesn't matter, I need something to do anyway.
Make sure you create your textures after you have a valid OpenGL context (which is created by wglCreateContext if using pure Win32). Also make sure that you have enabled texturing (glEnable(GL_TEXTURE_2D);).

Enigma
Awesome! Thanks to you all. And yes Enigma you had my number, iw as not enabling 2D texturing. However, since i have this code copied over (i planned on giving you an example if no one could guess what i was doing wrong), are there any major improvements you can see?
I am sure there are i dunno, 50,000 things stupidly done, keep in mind this has a lot of copy pasted code and whatnot, i am just trying to piece stuff together to get it working enough to learn from (after all, if nothing works, how can i learn lol).

Anyway, so this is simple, if you dont know glut, just ignore the glut stuff and focus on the opengl stuff, the glut stuff is a framework i am using. So there shouldent be anything major wrong with it.

/*#include  <windows.h>#include  <stdlib.h>#include  <stdio.h>*/#include    <iostream>#include    <gl\gl.h>#include    <gl\glu.h>#include    <gl\glut.h>GLfloat	    rquad;GLfloat     rtri;GLfloat     xrot;GLfloat     yrot;GLfloat     zrot;void        draw();class STGA{ 	STGA(STGA & tga); // no copying! (memory errors otherwise)public:	STGA();	STGA(char * file);	~STGA();	void destroy();	int width();	int height();	unsigned char * data();	int channels();	bool load(char *filename);	void loadOpenGL();	void bindOpenGL();	void destroyOpenGL();	unsigned int returnID(){ if(openGLid){ return openGLid; } else{ return 0; } }private:	unsigned int openGLid;	int imageWidth;	int imageHeight;	unsigned char imageTypeCode;	unsigned char bitCount;	unsigned char* imageData;	int imageChannels;};STGA::STGA(STGA & tga) // no copying! (memory errors otherwise){}STGA::STGA() : imageData(0), imageWidth(0), imageHeight(0), imageTypeCode(0), bitCount(0), imageChannels(0), openGLid(0){}STGA::STGA(char * file) : imageData(0), imageWidth(0), imageHeight(0), imageTypeCode(0), bitCount(0), imageChannels(0), openGLid(0){	load(file);}STGA::~STGA(){	destroy(); }void STGA::destroy(){ 	if (imageData)		delete[] imageData;	imageData=0;	imageWidth=0;	imageHeight=0;	imageTypeCode= 0;	bitCount=0;	imageChannels=0;	destroyOpenGL();}int STGA::width(){	return imageWidth;}int STGA::height(){	return imageHeight;}unsigned char * STGA::data(){	return imageData;}int STGA::channels(){	return imageChannels;}bool STGA::load(char *filename){	FILE *file;	unsigned char		badChar;	short int		badInt;	long		  	imageSize;	int			colorMode;			file = fopen(filename, "rb");			if (!file)		return false;			fread(&badChar, sizeof(unsigned char), 1, file);	fread(&badChar, sizeof(unsigned char), 1, file);			fread(&imageTypeCode, sizeof(unsigned char), 1, file);			//image type either 2 (color) or 3 (greyscale)	if ((imageTypeCode != 2) && (imageTypeCode != 3))	{		fclose(file);		return false;	}			//13 bytes of useless data	fread(&badInt, sizeof(short int), 1, file);	fread(&badInt, sizeof(short int), 1, file);	fread(&badChar, sizeof(unsigned char), 1, file);	fread(&badInt, sizeof(short int), 1, file);	fread(&badInt, sizeof(short int), 1, file);			//image dimensions	fread(&imageWidth, sizeof(short int), 1, file);	fread(&imageHeight, sizeof(short int), 1, file);			//image bit depth	fread(&bitCount, sizeof(unsigned char), 1, file);			//1 byte of garbage data	fread(&badChar, sizeof(unsigned char), 1, file);			//colorMode -> 3 = BGR, 4 = BGRA 	colorMode = bitCount / 8;	imageSize = imageWidth * imageHeight * colorMode;			//allocate memory for image data	imageData = new unsigned char[imageSize];			//read in image data	fread(imageData, sizeof(unsigned char), imageSize, file);			//change BGR to RGB (especially for OpenGL later on)	for (int i = 0; i < imageSize; i += colorMode)	{		//swap blue and red colour value 		imageData ^= imageData[i+2] ^=			imageData ^= imageData[i+2];	}			//close file	fclose(file);	imageChannels=colorMode; 	return true;}void STGA::loadOpenGL(){	glGenTextures(1, &openGLid);	glBindTexture(GL_TEXTURE_2D, openGLid);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);	unsigned int mode=GL_RGB;	if (channels()==4)		mode=GL_RGBA;	glTexImage2D(GL_TEXTURE_2D,0,channels(),width(),height(),0,mode,GL_UNSIGNED_BYTE,data());	gluBuild2DMipmaps(GL_TEXTURE_2D,channels(),width(),height(),mode,GL_UNSIGNED_BYTE,data());}void STGA::bindOpenGL(){	if (openGLid)		glBindTexture(GL_TEXTURE_2D, openGLid);}void STGA::destroyOpenGL(){	if (openGLid)		glDeleteTextures(1, &openGLid);	openGLid=0;}STGA      tgaFile;// process menu option 'op'void menu(int op) {  switch(op) {  case 'Q':  case 'q':    exit(0);  }}// executed when a regular key is pressedvoid keyboardDown(unsigned char key, int x, int y) {  switch(key) {  case 'Q':  case 'q':  case  27:   // ESC    exit(0);  }}// executed when a regular key is releasedvoid keyboardUp(unsigned char key, int x, int y) {}// executed when a special key is pressedvoid keyboardSpecialDown(int k, int x, int y) {}// executed when a special key is releasedvoid keyboardSpecialUp(int k, int x, int y) {}// reshaped windowvoid reshape(int width, int height) {  GLfloat fieldOfView = 90.0f;  glViewport (0, 0, (GLsizei) width, (GLsizei) height);  glMatrixMode (GL_PROJECTION);  glLoadIdentity();  gluPerspective(fieldOfView, (GLfloat) width/(GLfloat) height, 0.1, 500.0);  glMatrixMode(GL_MODELVIEW);  glLoadIdentity();}void mouseClick(int button, int state, int x, int y) {}void mouseMotion(int x, int y) {}// executed when program is idlevoid idle() {     draw();}// render the scenevoid draw() {  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  glMatrixMode(GL_MODELVIEW);  glLoadIdentity();  glTranslatef(-1.5f,0.0f,-6.0f);	    glRotatef(rtri,0.0f,1.0f,0.0f);  glBegin(GL_TRIANGLES);					// Start Drawing A Triangle		glColor3f(1.0f,0.0f,0.0f);			// Red		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Front)		glColor3f(0.0f,1.0f,0.0f);			// Green		glVertex3f(-1.0f,-1.0f, 1.0f);			// Left Of Triangle (Front)		glColor3f(0.0f,0.0f,1.0f);			// Blue		glVertex3f( 1.0f,-1.0f, 1.0f);			// Right Of Triangle (Front)		glColor3f(1.0f,0.0f,0.0f);			// Red		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Right)		glColor3f(0.0f,0.0f,1.0f);			// Blue		glVertex3f( 1.0f,-1.0f, 1.0f);			// Left Of Triangle (Right)		glColor3f(0.0f,1.0f,0.0f);			// Green		glVertex3f( 1.0f,-1.0f, -1.0f);			// Right Of Triangle (Right)		glColor3f(1.0f,0.0f,0.0f);			// Red		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Back)		glColor3f(0.0f,1.0f,0.0f);			// Green		glVertex3f( 1.0f,-1.0f, -1.0f);			// Left Of Triangle (Back)		glColor3f(0.0f,0.0f,1.0f);			// Blue		glVertex3f(-1.0f,-1.0f, -1.0f);			// Right Of Triangle (Back)		glColor3f(1.0f,0.0f,0.0f);			// Red		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Left)		glColor3f(0.0f,0.0f,1.0f);			// Blue		glVertex3f(-1.0f,-1.0f,-1.0f);			// Left Of Triangle (Left)		glColor3f(0.0f,1.0f,0.0f);			// Green		glVertex3f(-1.0f,-1.0f, 1.0f);			// Right Of Triangle (Left)  glEnd();    glLoadIdentity();					// Reset The Current Modelview Matrix  glTranslatef(1.5f,0.0f,-6.0f);				// Move Right 1.5 Units And Into The Screen 6.0  glRotatef(rquad,1.0f,0.0f,0.0f);			// Rotate The Quad On The X axis ( NEW )  glRotatef(rquad,0.0f,1.0f,0.0f);			// Rotate The Quad On The X axis ( NEW )  	glColor3f(0.5f,0.5f,1.0f);				// Set The Color To A Nice Blue Shade	glBegin(GL_QUADS);					// Start Drawing A Quad		glColor3f(0.0f,1.0f,0.0f);			// Set The Color To Green		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Top)		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Top)		glVertex3f(-1.0f, 1.0f, 1.0f);			// Bottom Left Of The Quad (Top)		glVertex3f( 1.0f, 1.0f, 1.0f);			// Bottom Right Of The Quad (Top)		glColor3f(1.0f,0.5f,0.0f);			// Set The Color To Orange		glVertex3f( 1.0f,-1.0f, 1.0f);			// Top Right Of The Quad (Bottom)		glVertex3f(-1.0f,-1.0f, 1.0f);			// Top Left Of The Quad (Bottom)		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Bottom)		glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Bottom)				glColor3f(1.0f,0.0f,0.0f);			// Set The Color To Red		glVertex3f( 1.0f, 1.0f, 1.0f);			// Top Right Of The Quad (Front)		glVertex3f(-1.0f, 1.0f, 1.0f);			// Top Left Of The Quad (Front)		glVertex3f(-1.0f,-1.0f, 1.0f);			// Bottom Left Of The Quad (Front)		glVertex3f( 1.0f,-1.0f, 1.0f);			// Bottom Right Of The Quad (Front)		glColor3f(1.0f,1.0f,0.0f);			// Set The Color To Yellow		glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Back)		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Back)		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Back)		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Back)				glColor3f(0.0f,0.0f,1.0f);			// Set The Color To Blue		glVertex3f(-1.0f, 1.0f, 1.0f);			// Top Right Of The Quad (Left)		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Left)		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Left)		glVertex3f(-1.0f,-1.0f, 1.0f);			// Bottom Right Of The Quad (Left)		glColor3f(1.0f,0.0f,1.0f);			// Set The Color To Violet		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Right)		glVertex3f( 1.0f, 1.0f, 1.0f);			// Top Left Of The Quad (Right)		glVertex3f( 1.0f,-1.0f, 1.0f);			// Bottom Left Of The Quad (Right)		glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Right)	glEnd();						// Done Drawing The Quad	glLoadIdentity();	glTranslatef(0.0f,3.0f,-6.0f);	glRotatef(rquad,1.0f,0.0f,0.0f);	tgaFile.bindOpenGL();	//glBindTexture(GL_TEXTURE_2D, tgaFile.returnID());	glutSolidTeapot(1.0f);    glFlush();  glutSwapBuffers();  rtri+=0.2f;  rquad-=0.15f;}// initialize OpenGL settingsvoid initGL(int width, int height) {  reshape(width, height);  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  glClearDepth(1.0f);  glEnable(GL_DEPTH_TEST);  glDepthFunc(GL_LEQUAL);  if (tgaFile.load("data/nehe2.tga"))  {  	tgaFile.loadOpenGL();  }}// initialize GLUT settings, register callbacks, enter main loopint main(int argc, char** argv) {    glutInit(&argc, argv);  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);  glutInitWindowSize(800, 600);  glutInitWindowPosition(100, 100);  glutCreateWindow("EmptyGLUT");  // register glut call backs  glutKeyboardFunc(keyboardDown);  glutKeyboardUpFunc(keyboardUp);  glutSpecialFunc(keyboardSpecialDown);  glutSpecialUpFunc(keyboardSpecialUp);  glutMouseFunc(mouseClick);  glutMotionFunc(mouseMotion);  glutReshapeFunc(reshape);  glutDisplayFunc(draw);    glutIdleFunc(idle);  glutIgnoreKeyRepeat(true); // ignore keys held down  // create a sub menu   int subMenu = glutCreateMenu(menu);  glutAddMenuEntry("Do nothing", 0);  glutAddMenuEntry("Really Quit", 'q');  // create main "right click" menu  glutCreateMenu(menu);  glutAddSubMenu("Sub Menu", subMenu);  glutAddMenuEntry("Quit", 'q');  glutAttachMenu(GLUT_RIGHT_BUTTON);  initGL(800, 600);  glutMainLoop();  return 0;}

Thanks to anyone who wants to glance over :)

[Edited by - Zeusbwr on December 25, 2004 12:18:59 AM]
Rank: OpenGL & Glut "Nub", C++ Freshman.
Cool [smile] Nice to know everything is working for you

Welcome to the wonderful world of the insane nehe posse.

*Here be dragons*
Advertisement
:)
Rank: OpenGL & Glut "Nub", C++ Freshman.
That looks pretty good since its just a simple program. Obviously as you advance to more complicated programs you'll need to start splitting your code up into more functions and classes, but no need for that at the moment. The only two comments I would make are about your glMatrixMode(GL_MODELVIEW); call at the start of draw(). You will always be in <MODELVIEW mode here since you never switch to anything else without switching back, so the call is redundant. The second point is about initGL();. GLut will automatically call reshape for you when it creates the window, so there is no need to call it yourself. This also frees you from passing the window width and height to this function and means you don't need to have two copies of this data, which could easily get out of sync.

Enigma
Thanks :) Btw, is this forum good for posting working code just for review and commenting improvements?
Rank: OpenGL & Glut "Nub", C++ Freshman.

This topic is closed to new replies.

Advertisement