Jump to content
  • Advertisement
Sign in to follow this  
popcorn

OpenGL Texture mapping help

This topic is 3078 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm looking into OpenGL at the moment and am using Glut version 3.7. Overall it seems pretty easy to use however I'm having a few problems with texture mapping. I trying to use this as a texture map for a quad. and this too however the results are not what I expect as I get this for the ace and this for the orange rectangle For some reason the colors don't seem to show up properly and I'm not sure I understand why the orange has suddenly turned blue. I'm using SDL to load the bitmap before using glTexImage2D to turn it into a texture. Any ideas why the colors are all wrong? Heres the source:
#include "SDL.h"
#include <string>
#include <iostream>
#include "glut.h"

using std::string;
using namespace std;

//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"") 

void loadTexture()
{	
	SDL_Surface *bmp;
	bmp = SDL_LoadBMP("sCards\\ace.bmp");

	if(bmp == NULL)
		cout << "null" << endl;
	else 
		cout << "not null" << endl;

	SDL_LockSurface( bmp );
	cout << bmp->w << endl;
	cout << bmp->h << endl;
	int bpp = bmp->format->BitsPerPixel;
	cout << bpp << endl;
	SDL_UnlockSurface( bmp );

	GLuint texture;
	GLenum errorCode;

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

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, bmp->w, bmp->h, 0, GL_RGB, GL_UNSIGNED_BYTE, bmp->pixels);

	SDL_FreeSurface(bmp);

	errorCode = glGetError();

	switch(errorCode)
	{
		case GL_INVALID_ENUM: 
			cout << "invalid num" << endl;
			break;
		case GL_INVALID_VALUE:  
			cout << "invalid value" << endl;
			break;
		case GL_INVALID_OPERATION: 
			cout << "invalid operation" << endl;
			break;
		case GL_STACK_OVERFLOW: 
			cout << "stack overflow" << endl;
			break;
		case GL_STACK_UNDERFLOW: 
			cout << "stack underflow" << endl;
			break;
		case GL_OUT_OF_MEMORY:  
			cout << "out of memory" << endl;
			break;
		/*case GL_TABLE_TOO_LARGE:
			cout << "table too large" << endl;
			break;
		case GL_INVALID_FRAMEBUFFER_OPERATION :
			cout << "invalid framebuffer operation" << endl;
			break;*/
	}
}

void init()
{
    glClearColor(0,0,0,0);
    gluOrtho2D(-5,5,-5,5);
}

float time = 0;
int angle = 1;

void display()
{

    loadTexture();

    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_TEXTURE_2D);

	glBegin(GL_QUADS);
		glTexCoord2f(0,1);
		glVertex2f(-1,1);
		glTexCoord2f(1,1);
		glVertex2f(1,1);
		glTexCoord2f(1,0);
		glVertex2f(1,-1);
		glTexCoord2f(0,0);
		glVertex2f(-1,-1);
       glEnd();

	glDisable(GL_TEXTURE_2D);

	glFlush();
	glutSwapBuffers();

	glRotatef(angle, 1.0, 0.0, 0.0); 
	//glRotatef(angle, 0.0, 1.0, 0.0); 
	//glRotatef(angle, 0.0, 0.0, 1.0); 
}

void idle()
{
    time += 0.1;

    if(time > 20.0)//360)
	{
        time = 0;
	}

	glutPostRedisplay();
}

int main(int argc, char* args[])
{
    glutInit(&argc, args);
    glutInitWindowSize(800,600);
    glutInitWindowPosition(10,50);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutCreateWindow("Texture Mapping Test");
    init();
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    glutMainLoop();

    return 0;    
}


A few more questions I would like answered: 1. It seems that the width and height of the texture must be a value that is a power of 2. Any particular reason for this and any way round it? 2. Also I noticed that some enums types like GL_TABLE_TOO_LARGE as well as GL_BGR don't seem to be recognized even though it should because it is specified in the documentation. Is this because I am using an old version(I'm using ver 3.7) of Glut or could it be down to something else? I only have Glut installed and not the OpenGL SDK.

Share this post


Link to post
Share on other sites
Advertisement
GL_BGR is an achieved through an extension. So you either need to use that extension (use GLEW for easiest way), or physically flip your B and R values in memory (which is what was previously suggested). I don't think there really is such a thing as an "OpenGL SDK", but if you install and use glew it will expose the updated extensions to you.

Quote:

From http://www.opengl.org/sdk/docs/man/xhtml/glGetTexImage.xml

The types GL_UNSIGNED_BYTE_3_3_2,
GL_UNSIGNED_BYTE_2_3_3_REV,
GL_UNSIGNED_SHORT_5_6_5,
GL_UNSIGNED_SHORT_5_6_5_REV,
GL_UNSIGNED_SHORT_4_4_4_4,
GL_UNSIGNED_SHORT_4_4_4_4_REV,
GL_UNSIGNED_SHORT_5_5_5_1,
GL_UNSIGNED_SHORT_1_5_5_5_REV,
GL_UNSIGNED_INT_8_8_8_8,
GL_UNSIGNED_INT_8_8_8_8_REV,
GL_UNSIGNED_INT_10_10_10_2,
GL_UNSIGNED_INT_2_10_10_10_REV,
and the formats
GL_BGR, and GL_BGRA are available only if the GL version is
1.2 or greater.




Edit: also

Quote:
It seems that the width and height of the texture must be a value that is a power of 2. Any particular reason for this and any way round it?


This was an old limitation from many years ago which isn't anymore. I think with newer version of OpenGL you can use non-Po2 textures directly with glTexImage if you load the "GL_ARB_texture_non_power_of_two" extension. Again GLEW will help you out here.

Share this post


Link to post
Share on other sites
Thanks karwosts, I managed to fix the problem using this GL_BGR_EXT which seems to display the correct colors.

To be honest I'm a bit confused about all the different versions of OpenGL out there. Is GLEW anything like GLUT, does it provide things like menu support etc for example. Or can I use GLEW in conjunction with GLUT?

edit.
Nevermind I just had a look a Glew and it looks pretty useful, I think I'll try freeglut as well instead of glut. They need to make these thing easier to use..

Also I didn't realise there was an OpenGL forum on this site, maybe this thread needs to be moved.

[Edited by - popcorn on April 16, 2010 8:16:47 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!