Advertisement

Prolly very stupid question with source.

Started by February 03, 2004 05:44 PM
2 comments, last by Marty666 21 years, 1 month ago
Why isn''t the quad textured? I must be very blind... This is the entire source. All u need to do is make a tex.bmp and try it out. Ow and don''t forget to add opengl32.lib glu32.lib glut32.lib and glaux.lid to the project. Please help, Marty

#include <windows.h> 
#include <stdio.h> 
#include <gl\gl.h> 
#include <gl\glu.h> 
#include <gl\glut.h>
#include <gl\glaux.h>

int WindWidth, WindHeight;

AUX_RGBImageRec *LoadBMP(char *Filename)
{
	FILE *File=NULL;							// is there a filename?

	if (!Filename){return NULL;}				// if not, return NULL

	File=fopen(Filename,"r");					// does the file exist?

	if (File) 
		{	
			fclose(File);						// then close it 

			return auxDIBImageLoad(Filename);	// and return a pointer

		} 
	return NULL;								// else return null

}

int LoadTexture(char *filename, GLuint *holder)
{
	AUX_RGBImageRec *TextureImage[1];							// storagespace for 1 texture

	memset(TextureImage,0,sizeof(void *)*1);					// clear the mem

	if (TextureImage[0] = LoadBMP(filename))					// load the file

	{
		glGenTextures(1, holder);									// create space for texture

		glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
		glBindTexture(GL_TEXTURE_2D, *holder);						// bind the loaded texture

		glTexImage2D(	GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, 
						GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); // set params

		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // set params

		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
		if (TextureImage[0])
		{
			if (TextureImage[0]->data) 
			{
				free(TextureImage[0]->data);		// free the loaded bmp

			}
			free(TextureImage[0]);					// and the memory

		}
		return 1;
	}
	return 0;
}

void InitGL()
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glEnable(GL_DEPTH_TEST);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (double(WindWidth)/double(WindHeight)), 10.0, 10000.0);
	glMatrixMode(GL_MODELVIEW);
}

GLuint texnum;

void LoadData()
{
	LoadTexture("tex.bmp", &texnum);
}

void Display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
	glLoadIdentity();
	glBindTexture(GL_TEXTURE_2D, texnum);
	glBegin(GL_QUADS);
		glTexCoord2f(0,0);		glVertex3f(0,0,-100);
		glTexCoord2f(1,0);		glVertex3f(10,0,-100);
		glTexCoord2f(1,1);		glVertex3f(10,10,-100);
		glTexCoord2f(0,1);		glVertex3f(0,10,-100);	
	glEnd();
	glutSwapBuffers();
}

void KeyPressed(unsigned char key, int x, int y)
{
	if (key == 27)	{exit(0);}
}

void Idle()
{
	glutPostRedisplay();
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutGameModeString("1024x768:32@60");	WindWidth = 1024; WindHeight = 768;
	LoadData();
	
	glutEnterGameMode();
	
	InitGL();
	glutDisplayFunc(Display);
	glutKeyboardFunc(KeyPressed);
	glutIdleFunc(Idle);
	
	glutMainLoop();

	glutLeaveGameMode();
	return 0;
}
_____ /____ /|| | || MtY | ||_____|/Marty
Move your LoadData() call to after glutEnterGameMode(). You don''t have a valid OpenGL context until after the glutEnterGameMode call returns, so you can''t load textures.

Enigma
Advertisement
Ok, thanx.
Another question:
gamemode changes resolution. It is very irritating when it changes resolution while i''m cout''ing to the (dos) screen. It takes a wile to load all my stuff, and i''d like to read the feedback. Textures are loaded aswell. Can i set opengl for loading textures without having the gamemode resoltion (i''m not posting any redisplays while loading anyways)?

Marty
_____ /____ /|| | || MtY | ||_____|/Marty
I''m afraid I don''t know the ins and outs of glutGameMode. You could try creating a window, then loading your stuff then destroying the window and then entering game mode.

Enigma

This topic is closed to new replies.

Advertisement