Toggle texture on and off in Opengl

Hello,

I am new to openGl and I am trying to make a keybinding that will switch the texturing of an object on and off, till now I have managed to turn it off by creating a function that uses glDeleteTextures but i can’t seem to be able to load the texture back again at the press of a button. Here is snippet of my code:
void handleKeyPress (unsigned char key, int x, int y)
{
switch (key)
{
case ‘l’: //‘l’: Toggle lighting
lightingEnabled = !lightingEnabled;
if (!lightingEnabled)
glDisable(GL_LIGHTING);
else
glEnable(GL_LIGHTING);
break;

case ‘t’:
textureEnabled = !textureEnabled;
if (!textureEnabled)
/glBindTexture(0, _textureId)/glDeleteTextures(1, &textureId);
else
GLuint loadTexture(Image* image);
break;

case 27:
	exit(0);
}

}

Waiting eagerly for you responses, Thx Christian

Why don’t you simply use glEnable and glDisable with GL_TEXTURE_2D parameter or whatever texture target you use?

Tried that also it doesn’t do a thing, I also tried to bind 0 parameter instead of textureid and it still doesn’t work, this was the only solution that works partially…

It should work. If not, then it is a driver bug. What is the vendor of your GPU? Can I guess it’s Intel? :slight_smile:

Well at work where I try to make this project I have ATI RADEON HD 5800. But i’ll post the entire code maybe I made a mistake somewhere, also i must mention that I am using a bmp texture that is loaded through another cpp and header file.

Here is the entire code:
// light2.cpp : Defines the entry point for the console application.
//

#include “stdafx.h”

// lighting.cpp : Defines the entry point for the console application.
//

#include “stdafx.h”
#include <iostream>
#include <stdlib.h>
#include “imageloader.h”

#ifdef APPLE
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include “imageloader.h”

using namespace std;

bool lightingEnabled = false; //Lighting enabled or disabled
bool textureEnabled = true;

int _change=1;
float _coord=1.0;

float _angle=30.0f;
float _xLight2=-6.0f;
float _uvcoordF1=0.0f;
float _uvcoordF2=5.0f;
float _uvcoordF3=10.0f;

//Makes the image into texture and returns the id of the texture
GLuint loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGl which texture to edit
//Map the image to the texture
glTexImage2D (GL_TEXTURE_2D, //Always GL_TEXTURE_2D
0, //0 for now
GL_RGB, //Format OpenGL uses for image
image->width, image->height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are score as unsigned numbers
image->pixels); //The actual pixel data
return textureId; //Returns the id of the texture
}

GLuint textureId;

void handleKeyPress (unsigned char key, int x, int y)
{
switch (key)
{
case ‘l’: //‘l’: Toggle lighting
lightingEnabled = !lightingEnabled;
if (!lightingEnabled)
glDisable(GL_LIGHTING);
else
glEnable(GL_LIGHTING);
break;

case ‘t’:
textureEnabled = !textureEnabled;
if (!textureEnabled)
/glBindTexture(0, _textureId)/glDeleteTextures(1, &textureId);
else
GLuint loadTexture(Image* image);
break;

case 27:
	exit(0);
}

}

void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
glDisable(GL_LIGHTING);

Image* image = loadBMP("vtr.bmp");
textureId = loadTexture(image);
delete image;

}

void handleResize (int w, int h)
{
glViewport (0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();
gluPerspective (45.0, (double) w/ (double) h, 1.0, 200.0);

}

void update (int value)
{
_angle +=2.0f;
if (_angle >360)
{
_angle -=360;
}

if (_change !=3)
	++_change;
else
	_change=1;

if (_xLight2 !=6)
	++_xLight2;
else 
	_xLight2=-6;

_uvcoordF1 +=1.0f/10;
if (_uvcoordF1&gt;2.5)
	{
		_uvcoordF1 -=2;
	}

_uvcoordF2 +=1.0f/60;
if (_uvcoordF2&gt;7.5)
{
	_uvcoordF2 -=2;
}

_uvcoordF3 +=10/100;
if ( _uvcoordF3&gt;30.5)
{
	_uvcoordF3 -=20;
}

glutPostRedisplay();
glutTimerFunc(25, update, 0);

}

/*void update2 (int value)
{
_coord +=1.0f;
if (_coord > 6)
{
_coord -=6;
}

glutPostRedisplay();
glutTimerFunc(250, update2, 0);

}*/

void drawScene()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 1.0, -6.0);

//Add ambient light
GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Add positioned light
GLfloat lightColor0[] = {0.5, 0.5, 0.5, 1.0};
GLfloat lightPos0[] = {4.0, 0.0, 8.0, 1.0};
glLightfv (GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv (GL_LIGHT0, GL_POSITION, lightPos0);
//Add directed light
GLfloat lightColor1[] = {0.5, 0.2, 0.2, 1.0};
//Coming from the direction
GLfloat lightPos1[] = {-1.0, 0.5, 0.5, 0.0};
glLightfv (GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv (GL_LIGHT1, GL_POSITION, lightPos1);

GLfloat lightColor2[] = {0.0, 1.0, 0.0, 1.0};
GLfloat lightPos2[]= {_xLight2, 0.0, 0.0, 1.0};
glLightfv (GL_LIGHT2, GL_DIFFUSE, lightColor2);
glLightfv (GL_LIGHT2, GL_POSITION, lightPos2);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glRotatef(_angle, 0.0, 1.0, 0.0);
glColor3f (1.0, 0.2f, 0.2f);
glBegin(GL_QUADS);

//Front
glNormal3f(0.0, 1.0, 0.0);
glTexCoord2f(0.0, 0.0);
glVertex3f(-2.5, -2.5, 2.5);
glTexCoord2f(1.0, 0.0);
glVertex3f(2.5, -2.5, 2.5);
glTexCoord2f(1.0, 1.0);
glVertex3f(2.5, -2.5, -2.5);
glTexCoord2f(0.0, 1.0);
glVertex3f(-2.5, -2.5, -2.5);

glEnd();
//Back
glEnable(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0, 1.0, 1.0);
glTranslatef(_coord, 0.0, 0.0);

glBegin(GL_TRIANGLES);

glNormal3f(1.0, 0.0, -1.0);
glTexCoord2f(_uvcoordF1, 0.0);
glVertex3f(-2.5, -2.5, -2.5);
glTexCoord2f(_uvcoordF2, 5.0);
glVertex3f(0.0, 2.5, -2.5);
glTexCoord2f(_uvcoordF3, 0.0);
glVertex3f(2.5, -2.5, -2.5);

glEnd();
glPopMatrix();

//Left
glDisable(GL_TEXTURE_2D);
glColor3f(1.0, 0.7, 0.3);
glBegin(GL_QUADS);

glNormal3f(1.0, 0.0, 0.0);
glVertex3f(-2.5, -2.0, 2.5);
glVertex3f(-2.5, -2.5, -2.5);
glVertex3f(-2.5, 2.5, -2.5);
glVertex3f(-2.5, 2.5, 2.5);

glEnd();

glutSwapBuffers();

}

int main(int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (400, 400);

glutCreateWindow ("Lighting -videotutorialsrock.com");
initRendering();
glutKeyboardFunc (handleKeyPress);
glutDisplayFunc(drawScene);
glutReshapeFunc (handleResize);
glutTimerFunc (25, update, 0);
/*glutTimerFunc(100, update2, 0);*/
glutMainLoop();



return 0;

}

This call isn’t valid:

glBindTexture(0, _textureId);

it should be:

glBindTexture(GL_TEXTURE_2D, 0);

Instead of binding texture 0 (or deleting it!) to disable drawing, you should use glDisable(GL_TEXTURE_2D) as aqnuep said.

Does this even work?


if (!textureEnabled)
/*glBindTexture(0, _textureId)*/glDeleteTextures(1, &textureId);
else
GLuint loadTexture(Image* image);

shouldn’t it be something like (extremely slow way):


if (!textureEnabled)
/*glBindTexture(0, _textureId)*/glDeleteTextures(1, &textureId);
else
textureId = loadTexture(image);

or (proper way):


if (!textureEnabled)
  glDisable(GL_TEXTURE_2D);
else
  glEnable(GL_TEXTURE_2D);

Well as you see /glBindTexture(0, _textureId)/ is between lines so that means it is comment now so it is no longer taken into consideration by C++, I 've left it there to remember that I tried glBind and it failed miserably I will try however the part after else and see what happens.

Tried to put
else
textureId = loadTexture(image);

But image is seen as an undeclared identifier and if I define it as char it will give another error: error C2664: ‘loadTexture’ : cannot convert parameter 1 from ‘char’ to ‘Image *’

Yeah, looking at your code closer, you load + free the image in InitRendering.

Just use the proper way of enabling/disabling textures:


if (!textureEnabled)
  glDisable(GL_TEXTURE_2D);
else
  glEnable(GL_TEXTURE_2D);

and only delete/load images if you are never going to use that texture image again, or are loading a new image from disk for the first time.

As I stated earlier glDisable glEnable doesn’t work the texture is not unloaded or loaded back when pressing the key ‘t’.

Dan has already tried to point out to you that you don’t create and delete textures continuously as this has a huge performance hit - and is quite unneccesary.

It it far easier and quicker to enable or disable the GL texture state so that texturing is not being applied for the parts of the rendering where you need to control that state.

The only question left is your implementation…
so before posting back with ‘it does not work’, try including the actual draw code and let’s see where you have gone wrong.

Managed to solve it eventually thx to a buddy, had to remove glDisable from another part of the code. Thx thread is now closed

Yes exactly my point. You really did not post all the parts of the program which affect rendering.

The command that had to be disabled is in red and I did post all the code. Please read the posts carefully Thanks.