OpenGL and...TEXTURES! Anybody knows this?

Well, let's start. I'm doing a proyect with OpenGl, a little videogame, but, the textures, dont charge! There is not matter, i've tried everything but the textures still not working. Here's a little code of a tutorial. The textures dont work me too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
 
GLuint texture;
 
GLfloat angle = 0.0;
 
 
 
 
GLuint LoadTexture( const char * filename, int width, int height )
{
    GLuint texture;
    unsigned char * data;
 
    FILE * file;
 
 
 
    file = fopen("/home/user/Downloads/menu/untitled2/", "texture" );
    if ( file == NULL ) return 0;
    data = (unsigned char *)malloc( width * height * 3 );
    fread( data, width * height * 3, 1, file );
    fclose( file );
 
    glGenTextures( 1, &texture );
    glBindTexture( GL_TEXTURE_2D, texture );
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_MODULATE );
 
 
 
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
 GL_LINEAR );
    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 );
 
 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
 GL_RGB, GL_UNSIGNED_BYTE, data);
    free( data );
    return texture;
}
 
void FreeTexture( GLuint texture )
{
  glDeleteTextures( 1, &texture );
}
 
void square (void)
{
    glBindTexture( GL_TEXTURE_2D, texture );
    glRotatef( angle, 1.0f, 1.0f, 1.0f );
    glBegin (GL_QUADS);
    glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
    glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
    glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
    glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
    glEnd();
 
 
}
 
void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glEnable( GL_TEXTURE_2D );
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    square();
    glutSwapBuffers();
    angle ++;
}
void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0)
;
    glMatrixMode (GL_MODELVIEW);
}
 
int main (int argc, char **argv) {
 
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    glutIdleFunc (display);
    glutReshapeFunc (reshape);
 
    texture = LoadTexture( "texture.raw", 256, 256 );
 
    glutMainLoop ();
 
    FreeTexture( texture );
 
    return 0;
}


I use Ubuntu and QTCreator. The texture.bmp is in the same directoty than the code source. And of course, i use the LIBS.

1
2
3
LIBS += -lGL \
        -lGLU \
        -lglut


I need your help, basically. I appreciate very much.

The las thing: Sorry for my English if i make some mistakes.
First off, your English is pretty good for a non-native speaker.

Secondly, I'm not sure if you mean that you don't see any texture appear on your square, or if the texture doesn't change from what the original texture is. If the problem is the first option, then it looks like you're giving the wrong file path in the line file = fopen("/home/user/Downloads/menu/untitled2/", "texture" ); if the file is in your solution's directory. If that doesn't work, you may want to look at SOIL.lib here: http://nehe.gamedev.net/tutorial/lesson_06_texturing_update/47002/
First of all sir, thank you for your answer.

I moved the directoty to an easier ubication. The trouble is, that the square only shows a white color. I've made some caps.

Here, i have my directory:

http://i.imgur.com/2TVFj.png

Then, this is the response when i run the programme.

http://i.imgur.com/rqvuo.png

Curiously, if i put something with "r", where i have "texture", the drawing change to this.

http://i.imgur.com/r2oLB.png

It's all so strange. Now, im trying to see what SOIL.lib does :)

Thank you!

Last edited on
This:
fopen("/home/user/Downloads/menu/untitled2/", "texture" )
makes no sense.

http://cplusplus.com/reference/cstdio/fopen/
In short, you need "rb" as the second parameter to fopen().
Thanks for the answe helios.

I've got this writing "rb".

http://i.imgur.com/utv1G.png

The code is the same of the first reply. Serioulsy...this is being me mad.
Ah, yes. The first parameter is also wrong. You're trying to open a directory, not a file. You pass something through filename, but you never use it in LoadTexture().
it works! IT WORKS!

http://i.imgur.com/na4d5.png

Thank you people, THANKS helios & Balrog!
Topic archived. No new replies allowed.