So my problem is that I try to load multiple textures at start up and then store all of the ID's so that I can bind them to use them. Now I know that the ID's are being stored correctly because I can debug it and see in both assigning and usage that the ID's are the same. Just for every binding it uses the last texture that I load. Here is my code:
GLuint TextureLoader::LoadTexture (const char* fileName,Material& material,int width,int height) {
GLuint textureImage;
FILE* textureFile;
textureFile = fopen(fileName, "rb");
unsigned char* imageData;
if (textureFile == NULL) {
return 0;
}
imageData = (unsigned char*)malloc(width * height * 3);
char header[54];
fread(header,1,54,textureFile);
fread(imageData, width * height * 3, 1, textureFile);
fclose(textureFile);
for (int i = 0; i < width * height; ++i) {
int nextIndex = i * 3;
unsigned char a = imageData[nextIndex];
unsigned char b = imageData[nextIndex+2];
imageData[nextIndex] = b;
imageData[nextIndex+2] = a;
}
glEnable(GL_TEXTURE_2D);
glGenTextures( 1, &textureImage );
glBindTexture( GL_TEXTURE_2D, textureImage );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
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);
/*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 );
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, imageData );*/
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, imageData);
glGenerateMipmap(GL_TEXTURE_2D);
free(imageData);
return textureImage;
}
Here is my usage of this code:
if (showTexture) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, material.texture);
}
Here is my load call:
green1.texture = TextureLoader::LoadTexture("/Users/hewitt/Desktop/OpenGLImages/face6.bmp",green1, 256, 256);
And then I just use:
glTexCoord2f(1.0f,0.0f);
to draw depending on the corner it changes. And this draws the one texture correctly it just repeats the texture even when I glBind a different number.
Any help is greatly appreciated.
Edit ----------
glBegin(mode);
Spatial::Vector3 normal = Mesh::calculateNormal (pointGroup);
glNormal3f(normal.x, normal.y, normal.z);
Material material = mesh.getMaterial();
if (showTexture) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, material.texture);
}
int counter = 0;
for (Spatial::Vector3 point : pointGroup) {
if (showTexture == false) {
Material::Colour colour = material.getColour();
glColor3f(colour.red, colour.green, colour.blue);
} else {
if (counter == 0) {
glTexCoord2f(1.0f,0.0f);
} else if (counter == 1) {
glTexCoord2f(1.0f,1.0f);
} else if (counter == 2) {
glTexCoord2f(0.0f,1.0f);
} else if (counter == 3) {
glTexCoord2f(0.0f,0.0f);
}
}
glVertex3f(point.x,point.y,point.z);
counter ++;
}
glEnd();
You cannot call glEnable or glBindTexture while in between glBegin and glEnd. You must bind your texture before glBegin. In fact the set of OpenGL functions you can call between glBegin and glEnd is limited to those transferring vertex attributes (glVertex, glColor, glTexCoord, etc...).
Note that the immediate mode API (that is glBegin/glEnd and friends) is legacy and was deprecated for more than 10 years. I suggest you switch to the modern OpenGL pipeline, that will sort many things out for you.
Related
I want to apply a texture from a .tga file that includes alpha transparency. When the texture is bound to the object it shows through to the solid color of the object itself. I want the color gone, or at least transparent. If I change the object color to be transparent, then the texture as a whole becomes more transparent, which is not what I want.
Is there a way to either turn off color, or how can I make the glColor4f not affect the texture?
void Sector::DrawPlanets(double gameTime){
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable( GL_BLEND );
glColor4f(0.0f, 0.0f, 1.0f,0.1f);
for(std::vector<Planet *>::iterator it = Planets.begin(); it != Planets.end(); it++){
double x,y,angle;
(*it)->GetPosition(&x,&y,&angle,gameTime);
int radius = (*it)->GetRadius();
glPushMatrix();
glTranslatef(x,y,0);
GLUquadricObj * sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, GL_TRUE);
glBindTexture(GL_TEXTURE_2D, get_textures()[static_cast<int>((*it)->GetPlanetZone())]);
gluSphere(sphere,radius,20,20);
glPopMatrix();
}
glDisable(GL_TEXTURE_2D);
}
Side Info
I have seen that the way the texture is being loaded in might also be the culprit for the problem. I will include that as well. It is code I got from class, that is why I am using .tga because it was already usable. Here is what I figure is relevant, some functions are not included.
const int num_textures = 5;
static GLuint texName[num_textures];
void InitializeTextures(){
bool repeat[num_textures];
bool border[num_textures];
gliGenericImage *image[num_textures];
int n=0;
repeat[n] = false; border[n] = false;
image[n++] = readImage("ice.tga");
repeat[n] = false; border[n] = false;
image[n++] = readImage("jupiter.tga");
repeat[n] = false; border[n] = false;
image[n++] = readImage("world.tga");
repeat[n] = false; border[n] = false;
image[n++] = readImage("volcanic.tga");
repeat[n] = false; border[n] = false;
image[n++] = readImage("stars.tga");
if(n!=num_textures)
{
printf("Error: Wrong number of textures\n");
_getch();
exit(1);;
}
glGenTextures(num_textures, texName);
for(int i=0; i<num_textures; i++)
{
glBindTexture(GL_TEXTURE_2D, texName[i]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
int repeats = repeat[i];
int needs_border = border[i]; // Needed if clamping and not filling the whole polygon.
if(repeats)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
}
if(needs_border)
{
// set a border.
SetBorder(image[i]);
}
bool mipmaps = false;
if(!PowerOf2(image[i]->height) || !PowerOf2(image[i]->width))
{
// WARNING: Images that do not have width and height as
// powers of 2 MUST use mipmaps.
mipmaps = true;
}
if (mipmaps)
{
gluBuild2DMipmaps(GL_TEXTURE_2D, image[i]->components,
image[i]->width, image[i]->height,
image[i]->format, GL_UNSIGNED_BYTE, image[i]->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
//GL_LINEAR_MIPMAP_LINEAR);
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
//GL_LINEAR);
GL_NEAREST);
}
else
{
glTexImage2D(GL_TEXTURE_2D, 0, image[i]->components,
image[i]->width, image[i]->height, 0,
image[i]->format, GL_UNSIGNED_BYTE, image[i]->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
}
}
When you set the TEXTURE_ENV_MODE to DECAL, what you get is the texture overlaying the color:
// This is not what you want
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
You want MODULATE, which is the default, so just delete that line.
Note that MODULATE will multiply the color by the texture, so change your color to white, if you want the texture to show through at full brightness:
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
Change
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
to
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);
Just have to chime in here and tell you, that instead of ripping your hair with the fixed function texture environment you should simply abandon the whole fixed function mess and just use shaders. In a fragment shader you can exactly specify the way base color and texture are combined.
I'm trying to render to texture.Using opengl-es 2.0.I wanna make Black and white postprocessing.But all I see, is black texture.On scene I have lightsources, and two spheres.But when I'm trying to render my texture that attached to framebuffer, I see black square.
this->frameBuffersCount = 3;
this->frameBuffers = new int[frameBuffersCount];
glGenFramebuffers(frameBuffersCount, (GLuint*)this->frameBuffers);
this->texturesCount = this->frameBuffersCount * 2;
this->bufferTextures = new int[this->texturesCount];
glGenTextures(this->texturesCount, (GLuint*)this->bufferTextures);
for(int i = 0; i < this->texturesCount / 2; ++i)
{
glBindTexture(GL_TEXTURE_2D, this->bufferTextures[2*i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D,0);
glBindTexture(GL_TEXTURE_2D, this->bufferTextures[2*i + 1]);
glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT, screenWidth, screenHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D,0);
}
for(int i = 0; i < this->frameBuffersCount; ++i)
{
glBindFramebuffer(GL_FRAMEBUFFER, this->frameBuffers[i]);
glFramebufferTexture2D(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
this->bufferTextures[2*i],
0);
glFramebufferTexture2D(GL_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT,
GL_TEXTURE_2D,
this->bufferTextures[2*i + 1],
0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
this->ordinaryQuad = new ModelVertex[4];
this->ordinaryQuad[0].UV = Vector2(0.0,0.0);
this->ordinaryQuad[0].position = Vector3(-1, -1, 0.0);
this->ordinaryQuad[1].UV = Vector2(0.0,1.0);
this->ordinaryQuad[1].position = Vector3(-1.0, 1.0, 0.0);
this->ordinaryQuad[2].UV = Vector2(1.0,1.0);
this->ordinaryQuad[2].position = Vector3(1.0, 1.0, 0.0);
this->ordinaryQuad[3].UV = Vector2(1.0,0.0);
this->ordinaryQuad[3].position = Vector3(1.0, -1.0, 0.0);
This is rendering code.
void EffectManager::applyBnW()
{
glBindFramebuffer(GL_FRAMEBUFFER, this->frameBuffers[0]);
auto res = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(res == GL_FRAMEBUFFER_COMPLETE)
{
SceneManager::GetInstance().DrawScene();
glDisable(GL_DEPTH_TEST);
unsigned short indices[6] = {0,2,1,2,0,3};
Shaders shaders;
shaders.Init("../Resources/Shaders/BlackAndWhiteVS.vs", "../Resources/Shaders/BlackAndWhiteFS.fs", 0);
glUseProgram(shaders.program);
unsigned int hVBuff,hInBuff;
glGenBuffers(1, &hVBuff);
glGenBuffers(1, &hInBuff);
glBindBuffer(GL_ARRAY_BUFFER, hVBuff);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(SEngine::ModelVertex), this->ordinaryQuad, GL_STATIC_DRAW);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, hInBuff);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned short), indices, GL_STATIC_DRAW);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->bufferTextures[0]);
glGenerateMipmap(GL_TEXTURE_2D);
if(shaders.positionAttribute != -1)
{
glEnableVertexAttribArray(shaders.positionAttribute);
glVertexAttribPointer(shaders.positionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(SEngine::ModelVertex), 0);
}
if(shaders.UVAttribute != -1)
{
glEnableVertexAttribArray(shaders.UVAttribute);
glVertexAttribPointer(shaders.UVAttribute, 2, GL_FLOAT, GL_FALSE, sizeof(SEngine::ModelVertex), (void*)(sizeof(Vector3)));
}
if(shaders.samplerUniform != -1)
{
glUniform1i(shaders.samplerUniform, 0);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)0);
//glDrawArrays(GL_TRIANGLES,0,4);
}
What is wrong?
Not a clear answer can be given since there is not enough information/code provided in your question.
Did you clear the depth buffer ?
When you generated the color in the fragment shader for the rendered objects, did you correctly calculate the White color?
One way I do in these situations to help me debug the issue is to remove potential error areas.
Clear the framebuffer to Red color and Alpha channel = 1.0
Clear depth buffer
Hard code gl_FragColor to vec4(1,1,1,1)
By doing this you can see if the output is all red? Then the issue is with the object rendering, such as transformation, clipping, depth rejection or other reason.
If you can see the objects with White color? Then you know it's the issue with your fragment shader
I would also recommend you to remove the line
glGenerateMipmap(GL_TEXTURE_2D);
As from what I can see in your code, it does not add any extra. If not this is the root-cause to your error.
Generating mipmaps on the texture that still being used and attached to a framebuffer object is not very effecient, especially if the driver internally would have to reallocate the memory for being able to store the new mipmap levels.
Solved.It was problems with index buffer and I forget to disable depth_test.
I'm trying to render to two textures, rendering from the first to the second, and then from the second to the first etc. The problem is that when I'm rendering the first texture to the second, it works fine but rendering the second to the first leaves a white texture, when it's supposed to be a purple one. I'm working with Qt and OpenGL.
Both textures are bound to the same FBO, and I'm switching them through glDrawBuffer(GL_COLOR_ATTACHMENT_i)
Here is my initialization code:
void GlWidget::initializeGL() {
glewInit();
src = true;
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
//glEnable(GL_CULL_FACE);
// Generate the texture to be drawn
tex = new float[256*256*4];
for(int i = 0; i < 256*256*4; i++){
if (i % 4 == 0){
tex[i] = 0.5;
}else if (i % 4 == 1){
tex[i] = 0.3;
}else if (i % 4 == 2){
tex[i] = 0.5;
}else if (i % 4 == 3){
tex[i] = 0;
}
}
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 256, 256, 0, GL_RGBA, GL_FLOAT, tex);
glGenTextures(1, &targetTex);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, targetTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 256, 256, 0, GL_RGBA, GL_FLOAT, NULL);
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
//Attach 2D texture to this FBO
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, targetTex, 0);
glDrawBuffer (GL_COLOR_ATTACHMENT1);
//-------------------------
glGenRenderbuffers(1, &depth_rb);
glBindRenderbuffer(GL_RENDERBUFFER_EXT, depth_rb);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 256, 256);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER, depth_rb);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");
shaderProgram.link();
vertices << QVector3D(-1, -1, -2) << QVector3D(-1, 1, -2) << QVector3D(1, 1, -2) << QVector3D(1, -1, -2);
texCoords << QVector2D(0, 0) << QVector2D(0, 1) << QVector2D(1, 1) << QVector2D(1, 0);
}
And here is my drawing code:
void GlWidget::render_to_screen () {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
qglClearColor(QColor(Qt::blue));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(src){
glActiveTexture(GL_TEXTURE0);
}else{
glActiveTexture(GL_TEXTURE1);
}
shaderProgram.enableAttributeArray("textureCoordinates");
shaderProgram.enableAttributeArray("vertex");
glDrawArrays(GL_QUADS, 0, vertices.size());
shaderProgram.disableAttributeArray("vertex");
shaderProgram.disableAttributeArray("textureCoordinates");
}
void GlWidget::paintGL()
{
qDebug() << "Updating";
glBindFramebuffer(GL_FRAMEBUFFER, fb);
if(src) {
glDrawBuffer(GL_COLOR_ATTACHMENT1);
glActiveTexture(GL_TEXTURE0);
}else {
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glActiveTexture(GL_TEXTURE1);
}
src = !src;
qglClearColor(QColor(Qt::white));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
shaderProgram.bind();
shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
shaderProgram.setAttributeArray ("textureCoordinates", texCoords.constData ());
shaderProgram.enableAttributeArray("textureCoordinates");
shaderProgram.setAttributeArray("vertex", vertices.constData());
shaderProgram.enableAttributeArray("vertex");
glDrawArrays(GL_QUADS, 0, vertices.size());
shaderProgram.disableAttributeArray("vertex");
shaderProgram.disableAttributeArray("textureCoordinates");
render_to_screen ();
shaderProgram.release();
}
I'm supposed to be getting a blue screen with a purple quad in the center, instead I'm getting a white quad in the center. What am I doing wrong here?
I see several places in your code where you set the active texture. But setting the active texture means nothing as far as what texture unit the program will pull from. That's decided by the texture image unit set into the sampler uniform that's accessing the texture. You need to change that uniform, not the active texture.
Or better yet, just bind the other texture to the context. There's no need to set the active texture image unit; just bind the texture you want to sample from. Really, there's no point in any of the glActiveTexture calls you're making.
I'm having a problem trying to get my texture showing correctly on some geometry. Basically, I have some triangles which which describe the shape of a person's face, and then have a png which I have to load with opencv.
I have the following code
void Geometry::loadTex(const char* fileName){
RenderUtils * u = new RenderUtils();
IplImage* img = u->getImg(fileName);
GLuint texture;
// allocate a texture name
glGenTextures( 1, &texture );
// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );
// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->imageData);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//delete [] t;
}
IplImage* RenderUtils::getImg(const char *fileName){
IplImage* img = 0;
img = cvLoadImage(fileName, CV_LOAD_IMAGE_COLOR);
if(!img) cout << "Couldn't load image " << fileName << endl;
return img;
}
void Geometry::display(){
glPushMatrix();
glTranslatef(20.0f, 20.0f, -40.f);
glRotatef(180.f, 0.0, 0.0, 1.0);
for(int i = 0; i < numTris; i+=3){
glBegin(GL_TRIANGLES);
glTexCoord3f(mean[tri[i]] / texWidth, ((mean[meanLength/2 + tri[i]] - maximumMu) * -1) / texHeight, 0 );
glVertex3f(mean[tri[i]], (mean[meanLength/2 + tri[i]] - maximumMu) * -1, 0);
glTexCoord3f(mean[tri[i+1]] / texWidth, ((mean[meanLength/2 + tri[i+1]] - maximumMu) * -1) / texHeight, 0 );
glVertex3f(mean[tri[i+1]], (mean[meanLength/2 + tri[i+1]] - maximumMu) * -1, 0);
glTexCoord3f(mean[tri[i+2]] / texWidth, ((mean[meanLength/2 + tri[i+2]] - maximumMu) * -1) / texHeight, 0 );
glVertex3f(mean[tri[i+2]], (mean[meanLength/2 + tri[i+2]] - maximumMu) * -1, 0);
glEnd();
}
glPopMatrix();
glutSwapBuffers();
glFlush();
}
So loadTex is called first, which calls load getImg, and then the display method is called from Glut.
You might want to take a look at this. It does exactly what you want.
Do I have to set up my gl context in a certain way to bind textures. I'm following a tutorial. I start by doing:
#define checkImageWidth 64
#define checkImageHeight 64
static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
static GLuint texName;
void makeCheckImage(void)
{
int i, j, c;
for (i = 0; i < checkImageHeight; i++) {
for (j = 0; j < checkImageWidth; j++) {
c = ((((i&0x8)==0)^((j&0x8))==0))*255;
checkImage[i][j][0] = (GLubyte) c;
checkImage[i][j][1] = (GLubyte) c;
checkImage[i][j][2] = (GLubyte) c;
checkImage[i][j][3] = (GLubyte) 255;
}
}
}
void initt(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
makeCheckImage();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
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_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
checkImage);
engineGL.current.tex = texName;
}
In my rendering I do:
PolygonTesselator.Begin_Contour();
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, current.tex);
if(layer[currentlayer].Shapes[i].Contour[c].DrawingPoints.size() > 0)
{
glColor4f(
layer[currentlayer].Shapes[i].Color.r,
layer[currentlayer].Shapes[i].Color.g,
layer[currentlayer].Shapes[i].Color.b,
layer[currentlayer].Shapes[i].Color.a);
}
for(unsigned int j = 0; j < layer[currentlayer].Shapes[i].Contour[c].DrawingPoints.size(); ++j)
{
gluTessVertex(PolygonTesselator.tobj,&layer[currentlayer].Shapes[i].Contour[c].DrawingPoints[j][0],
&layer[currentlayer].Shapes[i].Contour[c].DrawingPoints[j][0]);
}
PolygonTesselator.End_Contour();
}
glDisable(GL_TEXTURE_2D);
}
However it still renders the color and not the texture at all. I'd atleast expect to see black or something but its as if the bind fails. Am I missing something?
Thanks
It looks like from that code that you don't set any UVs.
Edit: Does it make any difference using GL_MODULATE instead of GL_DECAL? (Am taking guesses here because I suspect the problem lies in code you haven't provided, or in gluTessVertex itself ...