Jump to content
  • Advertisement
Sign in to follow this  
Decrius

OpenGL [C++/OpenGL] Texture loading using a class goes wrong

This topic is 3792 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

It worked, the texture loading, untill I made a class that would handle it =/ This is the class (only thr relevant code):
#include <iostream>
#include <GL/glfw.h>
#include <corona.h>

const int n_textures = 2;
char *texture_paths[2] = {"image.png", "image2.png"};

class Graphics
{
    GLuint textures[2];

    void draw_rect(GLuint, int, int, int, int);

    public:
        Graphics();
        ~Graphics();
        bool init(int, int);
        void resize();
        void draw();
};

Graphics::Graphics()
{
    corona::Image *image;
    for (int i = 0; i < n_textures; i++)
    {
        image = corona::OpenImage(texture_paths);
        if (!image)
        {
            printf("Error");
            //return false;
        }

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

        //corona::PixelFormat format = image->getFormat();

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

        gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->getWidth(), image->getHeight(), GL_RGB, GL_UNSIGNED_BYTE, image->getPixels());
    }
    delete image;
}

Graphics::~Graphics()
{
    glDeleteTextures(n_textures, textures);
    glfwTerminate();
}

bool Graphics::init(int width, int height)
{
    glfwInit();
    if(!glfwOpenWindow(width, height, 0, 0, 0, 0, 0, 0, GLFW_WINDOW))
    {
        return false;
    }
    glfwSetWindowTitle("GLFW Application");

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glEnable(GL_TEXTURE_2D);
    return true;
}

void Graphics::resize()
{
    int width, height;
    glfwGetWindowSize(&width, &height);
    height = height > 0 ? height : 1;

    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0f, width, 0.0f, height, -1.0f, 1.0f);

    glMatrixMode(GL_MODELVIEW);
}

void Graphics::draw()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    draw_rect(textures[0], 100, 100, 200, 200);
    glfwSwapBuffers();
}

void Graphics::draw_rect(GLuint texture, int x, int y, int w, int h)
{
    glBindTexture(GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
        glTexCoord2i(0, 0); glVertex2i(x, y);
        glTexCoord2i(1, 0); glVertex2i(x+w, y);
        glTexCoord2i(1, 1); glVertex2i(x+w, y+h);
        glTexCoord2i(0, 1); glVertex2i(x, y+h);
    glEnd();
}

int main()
{
    Graphics graphics;
    graphics.init(512, 512);

    bool running = true;
    while(running)
    {
        graphics.resize();
        graphics.draw();
    }
    return 0;
}

The texture being displayed is white =/ while it should be a wonderfull coloured photo ^^. What is wrong? I guess the variables or something like that. I've tried almost every combination to get this to work, though it keeps displaying a white rectangle...while it worked perfect when it was just a simple function =/ Thanks! Decrius

Share this post


Link to post
Share on other sites
Advertisement
You load the textures in the constructor, and create the rendering context in the init function which must be called after the constructor if executed. That's your problem, you're trying to load textures without a rendering context.

Share this post


Link to post
Share on other sites
First you should check for possible gl errors. A tool like glIntercept is very helpful in that. You will at least get one gl error when you set the texture mag filter since for the texture mag filter GL_LINEAR_MIPMAP_LINEAR is not supported!!! Only GL_NEAREST and GL_LINEAR is supported for the mag filter.

You should also not use gluBuild2DMipmaps (which makes the mipmaps in software but rather use the GL_SGIS_GENERATE_MIPMAP extension (there is also an EXT or whatever version of the extension and it has been integrated into the core OpenGL as well I think). It's just setting a texture parameter nothing more. You could also generate the mipmaps explicitly with a function call provided by the FBO extension.

You generally should use named internal formats preferable the sized ones insted of the numeric ones (second parameter in gluBuild2DMipmaps).

Share this post


Link to post
Share on other sites
Ah thanks :), that worked...pretty straightforward...never thought of it like that =/

As for the extension you mention (rhyme!), I downloaded GLEW and loaded it into my application, but I'm a bit uncertain on how to use it.

glew.h defines three things:
#define GL_GENERATE_MIPMAP_SGIS 0x8191
#define GL_GENERATE_MIPMAP_HINT_SGIS 0x8192

#define GLEW_SGIS_generate_mipmap GLEW_GET_VAR(__GLEW_SGIS_generate_mipmap)

I guess I'll have to use the first two, but which one? And where to put it? In glTexParameterf() somewhere?

Thanks.

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!