Advertisement

Textures aren't working

Started by July 29, 2003 10:56 PM
6 comments, last by frenchfry164 21 years, 7 months ago
I have a square that I want to texture. I tried, but it doesn''t texturize. Here is my code: Main.cpp

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "Utility.h"

bool Fullscreen = false;

struct
{
	float x, y, z, rx, ry, rz;
}Camera;

// Don''t display console window in MSVC++
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

GLuint Texture;

void DeleteTextures(void)
{
	glDeleteTextures(1, &Texture);
}

void Render(void)
{
	glLoadIdentity();
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glTranslatef(Camera.x, Camera.y, Camera.z);

	glBindTexture(GL_TEXTURE_2D, Texture);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(0.0f, 0.0f, 0.0f);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(1.0f, 0.0f, 0.0f);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(1.0f, 1.0f, 0.0f);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(0.0f, 1.0f, 0.0f);
	glEnd();

	glutSwapBuffers();
}

void IdleFunc(void)
{
	if (Fullscreen)
		glutFullScreen();

	Render();
}

void StartupGL(void)
{
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glMatrixMode(GL_MODELVIEW);

	glEnable(GL_TEXTURE_2D);

	Texture = CreateTextureFromRAW("H:\\sample.raw", 64, 64);
	
	glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);
}

void SetupPerspective(int w, int h)
{
	if (h == 0)	// Prevent divide by 0 errors
		h = 1;

	float ratio = 1.0 * w/h;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glViewport(0, 0, w, h);

	gluPerspective(45, ratio, 1, 285);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0, 0.0, -5.0, 0.0, 0.0, 0.0, 0.0f, 1.0f, 0.0f);
}

void KeyboardProc(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
	}
}

void SpecialKeys(int key, int x, int y)
{
	switch (key)
	{
	case GLUT_KEY_UP:
		Camera.z+=0.1f;
		break;
	case GLUT_KEY_DOWN:
		Camera.z-=0.1f;
		break;
	case GLUT_KEY_LEFT:
		Camera.ry--;
		break;
	case GLUT_KEY_RIGHT:
		Camera.ry++;
		break;
	case GLUT_KEY_HOME:
		Camera.y-=0.1f;
		break;
	case GLUT_KEY_END:
		Camera.y+=0.1f;
		break;
	case GLUT_KEY_INSERT:
		Camera.x+=0.1f;
		break;
	case GLUT_KEY_PAGE_UP:
		Camera.x-=0.1f;
		break;
	case GLUT_KEY_F12:
		Fullscreen = true;
		break;
	}
}

int main(int argc, char **argv)
{
	srand(time(NULL));

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(250, 250);
	glutInitWindowPosition(-1, -1);
	glutCreateWindow("Billboarding Demo");
	StartupGL();
	glutDisplayFunc(Render);
	glutIdleFunc(IdleFunc);
	glutReshapeFunc(SetupPerspective);
	glutKeyboardFunc(KeyboardProc);
	glutSpecialFunc(SpecialKeys);
	atexit(DeleteTextures);
	glutMainLoop();
	return 0;
}
 
Utility.cpp

#include "Utility.h"
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>

GLuint CreateTextureFromRAW(char *filename, GLuint width, GLuint height)
{
	GLuint TexID;

	glGenTextures(1, &TexID);
	glBindTexture(GL_TEXTURE_2D, TexID);

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
	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);

	GLbyte *ImageData;
	FILE *ImageHandle;

	ImageData = new GLbyte[width*height*3];

	ImageHandle = fopen(filename, "rb");
	fread(ImageData, width*height*3, 1, ImageHandle);
	fclose(ImageHandle);

	gluBuild2DMipmaps(GL_TEXTURE_2D, 6, width, height, GL_RGB,
		GL_UNSIGNED_BYTE, ImageData);
	delete[] ImageData;

	return TexID;
}

GLuint CreateTextureFromBTF(char *filename)
{
	GLuint TexID;

	glGenTextures(1, &TexID);
	glBindTexture(GL_TEXTURE_2D, TexID);

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
	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);

	GLbyte *ImageData;
	FILE *ImageHandle;

	char width[3], height[3];
	ImageHandle = fopen(filename, "rb");
	fread(&width, 3, 1, ImageHandle);
	fread(&height, 3, 1, ImageHandle);

	ImageData = new GLbyte[atoi(width)*atoi(height)*3];

	fread(ImageData, atoi(width)*atoi(height)*3, 1, ImageHandle);
	fclose(ImageHandle);

	gluBuild2DMipmaps(GL_TEXTURE_2D, 6, atoi(width), atoi(height),
		GL_RGB,	GL_UNSIGNED_BYTE, ImageData);
	delete[] ImageData;

	return TexID;
}
 
All utility.h is is two prototypes from CreateTextureFromRAW and CreateTextureFromBTF.
"Make a world of your own" - Kurt Cobain
Well erm.. you could start by defining texture coordinates that work ^_^

glTexCoord2f(0,0);
glTexCoord2f(1,0);
glTexCoord2f(1,1);
glTexCoord2f(0,1);
Advertisement
hehheh yes, do you understand those lines?
It''s like that because I was experimenting and forgot to change them back, . I tried the right texture coordinates first, but then I tried that, because it should''ve at least made the whole polygon the first pixel color of the texture. I changed it back to the right coords, and it still just makes a white square.

Oh, and before you ask, no the texture is no supposed to be plain white, it is supposed to be grass.
"Make a world of your own" - Kurt Cobain
are you positive your grass.bmp or whatever is 64x64?
I just fixed it. For some reason, the only values gluBuild2DMipmaps takes for the number of mipmaps is 3 or 4. If I try to do 1,2,5,6+ mipmaps, it doesn''t build the mipmaps or upload the texture.
"Make a world of your own" - Kurt Cobain
Advertisement
Usually you would just leave that number to be 0, and OpenGL will create all the mipmaps for you...
I just tried passing 0, and the square just turned white, no texture.
"Make a world of your own" - Kurt Cobain

This topic is closed to new replies.

Advertisement