#Texturing with GLUT

1 messages · Page 1 of 1 (latest)

somber cloud
#

Hello,
I am trying to render 2D and 3D using GLUT, but my square stays green with the textures. I tried using renderdoc but it only works with modern opengl. glGetError() returns 0 after loading the texture, so I don't know what I am doing wrong...

This is the square:

float vertices[] = {
    -0.5, 0.5, 0,
    -0.5, -0.5, 0,
    0.5, -0.5, 0,
    0.5, 0.5, 0
};
int indices[] = {
    0, 1, 3,
    3, 1, 2
};
float texture_coords[] = {
    0, 0,
    0, 1,
    1, 1,
    1, 0
};

GFXModel square = {vertices, indices, texture_coords, 0, 1, 1, 2};

...

int main(int argc, char **argv) {
    square.texture = gfx_load_texture("test.png");
    gfx_run(&argc, argv, draw_2d, draw_3d);
    gfx_free(square);
    return EXIT_SUCCESS;
}
#

I am drawing the square like this:

void gfx_draw_model(GFXModel *model, float x, float y, float z, float rx,
                    float ry, float rz) {
    int i;
    
    if(model->has_texture){
        glBindTexture(GL_TEXTURE_2D, model->texture);
    }
    
    glTranslatef(x, y, z);
    glRotatef(rx, 1, 0, 0);
    glRotatef(ry, 0, 1, 0);
    glRotatef(rz, 0, 0, 1);
    glBegin(GL_TRIANGLES);
    for(i=0;i<model->triangles;i++){
        float x1, y1, z1;
        float x2, y2, z2;
        float x3, y3, z3;
        
        float u1, v1;
        float u2, v2;
        float u3, v3;
        int indice1, indice2, indice3;
#
        if(model->has_indices){
            indice1 = model->indices[i*3];
            indice2 = model->indices[i*3+1];
            indice3 = model->indices[i*3+2];
            
            x1 = model->vertices[indice1*3];
            y1 = model->vertices[indice1*3+1];
            z1 = model->vertices[indice1*3+2];
            
            x2 = model->vertices[indice2*3];
            y2 = model->vertices[indice2*3+1];
            z2 = model->vertices[indice2*3+2];
            
            x3 = model->vertices[indice3*3];
            y3 = model->vertices[indice3*3+1];
            z3 = model->vertices[indice3*3+2];
            
            u1 = model->uv_coords[indice1*2];
            v1 = model->uv_coords[indice1*2+1];
            
            u2 = model->uv_coords[indice2*2];
            v2 = model->uv_coords[indice2*2+1];
            
            u3 = model->uv_coords[indice3*2];
            v3 = model->uv_coords[indice3*2+1];
        }else{
            x1 = model->vertices[i*9];
            y1 = model->vertices[i*9+1];
            z1 = model->vertices[i*9+2];
            
            x2 = model->vertices[i*9+3];
            y2 = model->vertices[i*9+4];
            z2 = model->vertices[i*9+5];
            
            x3 = model->vertices[i*9+6];
            y3 = model->vertices[i*9+7];
            z3 = model->vertices[i*9+8];
            
            u1 = model->uv_coords[i*6];
            v1 = model->uv_coords[i*6+1];
            
            u2 = model->uv_coords[i*6+2];
            v2 = model->uv_coords[i*6+3];
            
            u3 = model->uv_coords[i*6+4];
            v3 = model->uv_coords[i*6+5];
        }
        glTexCoord2f(u1, v1);
        glVertex3f(x1, y1, z1);
        glTexCoord2f(u2, v2);
        glVertex3f(x2, y2, z2);
        glTexCoord2f(u3, v3);
        glVertex3f(x3, y3, z3);
    }
    glEnd();
}
#

And I am loading the texture like this:

unsigned int gfx_load_texture(char *texture_file) {
    int width = 0, height = 0, channels = 0;
    GLuint id = 0;
    stbi_uc *texture;
    
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);
    
    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_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    
    texture = stbi_load(texture_file, &width, &height, &channels, 3);
    
    if(texture){
        printf("%d, %d\n", width, height);
        
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB,
                     GL_UNSIGNED_BYTE, texture);
        
        stbi_image_free(texture);
        printf("Texture loaded, id: %d\n", id);
        printf("%d\n", glGetError());
    }else{
        puts("Failed to load texture!");
    }
    return id;
}

Thanks

rapid ibex
#

Might be easier for you in the long run to switch to modern opengl now

somber cloud
#

Yes, but I want it to be able to run on hardware that does not support fragment shaders

#

And I already coded everything i need except texturing

mild finch
#

do you have a gfx card from the 90s or is this just theoretical

somber cloud
mild finch
#

i see

#

well good luck

#

here's the first challenge lol. how to debug it

rapid ibex
#

Afaik you can still run modern opengl on hardware that doesn't support fragment shaders, it may fall back to a software rasterizer depending on your drivers.

Also, do you think your glbegin/glvertrx3f calls will work on hardware that does not support fragment shaders?

mild finch
#

they will, bc that was the original api