What could be wrong here?

Jones
Unregistered
 
Post: #1
This code here compiles fine, and draws the quad I want it to, but no texture. I'm trying to get DevIL working, but this seems to happen all the time. Annoyed

Here's my code:

Code:
#include <stdlib.h>
#include <cstring>
#include <iostream>

#include <GLUT/glut.h>

#include "IL/ilut.h"

ILuint ImageName;
GLuint tex;

void init(void)
{
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_QUADS);
        glTexCoord2i(0, 0); glVertex2i(0,   0);
        glTexCoord2i(1, 0); glVertex2i(320, 0);
        glTexCoord2i(1, 1); glVertex2i(320, 240);
        glTexCoord2i(0, 1); glVertex2i(0, 240);
    glEnd();

    glutSwapBuffers();
}


void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, width, height, 0.0);
}


int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(320, 240);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("GLUT & OpenGL program");
    
    ilInit();
    iluInit();
    ilutRenderer(ILUT_OPENGL);
    ilGenImages(1, &ImageName);
    ilBindImage(ImageName);
    ilLoadImage("file.bmp");
    
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());    
    
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

And here's the image I want to load...

[Image: file.bmp]

I think I've turned on all the neccessary GL flags, and initiated every ok...

Thanks for any help!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
If you want to use that image, you will need to use GL_TEXTURE_RECTANGLE_ARB instead of GL_TEXTURE_2D. In order to use just 2D textures, the dimensions must be a power of 2. If you use GL_TEXTURE_RECTANGLE_ARB, the texture coordinates must be the dimensions of the image instead of 0 to 1. However, if you just scale the image to, say, 512x512, it will work just fine, and mapping it to the quad will un-distort it.
Quote this message in a reply
Jones
Unregistered
 
Post: #3
Ah, I was convinced the rule was it had to be divisable by 2.. so, 9 would be invalid, but say 200 would. Silly me! Rolleyes

So with ARB insetad of TEXTURE_2D, I'll be able to use that image but with for example (320, 240) would be the equivalent coord of (1,1).

Is this what you mean?

Code:
#include <stdlib.h>
#include <cstring>
#include <iostream>

#include <GLUT/glut.h>

#include "IL/ilut.h"

ILuint ImageName;
GLuint tex;

void init(void)
{
    glEnable(GL_TEXTURE_RECTANGLE_ARB);
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_QUADS);
        glTexCoord2i(0, 0); glVertex2i(0,   0);
        glTexCoord2i(320, 0); glVertex2i(320, 0);
        glTexCoord2i(320, 240); glVertex2i(320, 240);
        glTexCoord2i(0, 240); glVertex2i(0, 240);
    glEnd();

    glutSwapBuffers();
}


void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, width, height, 0.0);
}


int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(320, 240);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("GLUT & OpenGL program");
    
    ilInit();
    iluInit();
    ilutRenderer(ILUT_OPENGL);
    ilGenImages(1, &ImageName);
    ilBindImage(ImageName);
    ilLoadImage("file.bmp");
    
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());    
    
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

Annoyed
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #4
Replace all instances of GL_TEXTURE_2D with GL_TEXTURE_RECTANGLE_ARB. Also, rectangle textures override 2D textures. If you have rectangle textures enabled with glEnable(), and you try to draw a 2D texture, you will get a blank rectangle.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
Quote:I'll be able to use that image but with for example (320, 240) would be the equivalent coord of (1,1).
With GL_TEXTURE_2D it would be
With GL_TEXTURE_RECTANGLE_ARB the coordinate (320, 240) would be (320, 240)

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Jones
Unregistered
 
Post: #6
unknown Wrote:With GL_TEXTURE_2D it would be
With GL_TEXTURE_RECTANGLE_ARB the coordinate (320, 240) would be (320, 240)

That's what I meant. But I explained poorly. Rasp

EDIT: Ah, wonderful!

[Image: picgl.png]
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  What's wrong with my glutSolidTeapot? TomorrowPlusX 4 5,363 Aug 7, 2008 10:27 AM
Last Post: arekkusu
  Can someone help a 'noob'? What's wrong with this? RagingAvatar 7 4,049 Jun 4, 2006 11:54 PM
Last Post: Fenris
  Texture Color is wrong DesertPenguin 7 3,813 Feb 14, 2006 09:04 PM
Last Post: DesertPenguin
  Whats wrong with my collision code? Jake 6 2,864 Sep 11, 2003 12:44 PM
Last Post: MacFiend
  Is this tutorial wrong, or is it just me? Jake 8 3,325 Sep 9, 2003 03:46 PM
Last Post: Jake