Bonjour à tous, dans l'optique d'utiliser OpenGL pour faire de la 2D, et uniquement de la 2D, j'aimerai savoir si il est possible d'obtenir des coordonnées OpenGL en pixels, ou tout du moins si il y a un quelqueconque rapport de longueur pour l'affichage.

Mon code effectue un remise au niveau de la taille de l'écran :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
SDL_Init(SDL_INIT_VIDEO | SDL_DOUBLEBUF);
	SDL_WM_SetCaption("Moteur 3D",NULL);
    SDL_SetVideoMode(w,h, 32, SDL_OPENGL);
 
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_MULTISAMPLE);
 
 	glLoadIdentity();
 
	glClearColor(0.631f, 0.631f, 0.631f, 0.0f);	//Applique une couleur Grise
 
 
	if (w<=h)
	{
	H=(GLfloat) (10*h/w);
	L=10.0;
	}
	else
	{
	H=10.0;
	L=(GLfloat) (10*w/h);
	}
	gluOrtho2D(-L/2,L/2,-H/2,H/2);
Ensuite lors de l'affichage de mon carré texturé qui servira de sprite j'essaie de trouver la bonne opération pour convertir les données en pixels :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
taillex = 128; //Valeur x en pixel de l'image
tailley = 192; //Valeur y en pixel de l'image
 
taillex = taillex/60;
tailley = tailley/80;
 
glBindTexture(GL_TEXTURE_2D, texture);
 
	glBegin(GL_QUADS);
       glTexCoord2f(0, 0);    glVertex2d(-taillex,-tailley);
	   glTexCoord2f(0, 1);    glVertex2d(-taillex,tailley);
	   glTexCoord2f(1, 1);    glVertex2d(taillex,tailley);
	   glTexCoord2f(1, 0);      glVertex2d(taillex,-tailley);
 
		glEnd();
Mais j'ai toujours une image déformée, même de quelques pixels, du coup un rendu flou et pas à l'échelle voulue...
En gros l'idée c'est de créer un carré de la taille de l'image (en pixels) et d'afficher la texture, comme un Blit SDL. J'ai également besoin des pixels pour les calculs de déplacement et tout ça dans la suite du codage...


Egalement (je sais j'en demande pas mal xD ) comment obtenir la taille d'une texture chargée en pixels (longueur-hauteur) ? J'utilise un utilitaire d'un des membres du Site dans son tutorial, très pratique qui marche très bien lors du chargement de la texture :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
#ifndef SDLGLUTILS_H
#define SDLGLUTILS_H
 
#include <Opengl/gl.h>
#include <Opengl/glu.h>
#include <SDL/SDL.h>
 
#ifndef GL_CLAMP_TO_EDGE
#define GL_CLAMP_TO_EDGE 0x812F
#endif
 
GLuint loadTexture(const char * filename,bool useMipMap = true);
int takeScreenshot(const char * filename);
void drawAxis(double scale = 1);
int initFullScreen(unsigned int * width = NULL,unsigned int * height = NULL);
int XPMFromImage(const char * imagefile, const char * XPMfile);
SDL_Cursor * cursorFromXPM(const char * xpm[]);
 
#endif //SDLGLUTILS_H
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
 
 
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <OpenGL/glu.h>
 
#include <cstring>
#include <cstdlib>
#include "sdlglutils.h"
 
SDL_Surface * flipSurface(SDL_Surface * surface);
 
GLuint loadTexture(const char * filename,bool useMipMap)
{
    GLuint glID = 0;
    SDL_Surface * picture_surface = NULL;
    SDL_Surface *gl_surface = NULL;
    SDL_Surface * gl_fliped_surface = NULL;
    Uint32 rmask, gmask, bmask, amask;
 
    picture_surface = IMG_Load(filename);
    if (picture_surface == NULL)
        return 0;
 
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
 
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
 
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif
 
    SDL_PixelFormat format = *(picture_surface->format);
    format.BitsPerPixel = 32;
    format.BytesPerPixel = 4;
    format.Rmask = rmask;
    format.Gmask = gmask;
    format.Bmask = bmask;
    format.Amask = amask;
 
    gl_surface = SDL_ConvertSurface(picture_surface,&format,SDL_SWSURFACE);
 
    gl_fliped_surface = flipSurface(gl_surface);
 
    glGenTextures(1, &glID);
 
    glBindTexture(GL_TEXTURE_2D, glID);
 
 
    if (useMipMap)
    {
 
        gluBuild2DMipmaps(GL_TEXTURE_2D, 4, gl_fliped_surface->w,
                          gl_fliped_surface->h, GL_RGBA,GL_UNSIGNED_BYTE,
                          gl_fliped_surface->pixels);
 
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,
                        GL_LINEAR_MIPMAP_LINEAR);
 
    }
    else
    {
        glTexImage2D(GL_TEXTURE_2D, 0, 4, gl_fliped_surface->w,
                     gl_fliped_surface->h, 0, GL_RGBA,GL_UNSIGNED_BYTE,
                     gl_fliped_surface->pixels);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    }
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
 
 
    SDL_FreeSurface(gl_fliped_surface);
    SDL_FreeSurface(gl_surface);
    SDL_FreeSurface(picture_surface);
 
    return glID;
}
 
int takeScreenshot(const char * filename)
{
    GLint viewport[4];
    Uint32 rmask, gmask, bmask, amask;
    SDL_Surface * picture, * finalpicture;
 
    glGetIntegerv(GL_VIEWPORT, viewport);
 
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
 
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
 
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif
 
    picture = SDL_CreateRGBSurface(SDL_SWSURFACE,viewport[2],viewport[3], 32,
                                   rmask, gmask, bmask, amask);
    SDL_LockSurface(picture);
    glReadPixels(viewport[0],viewport[1],viewport[2],viewport[3],GL_RGBA,
                 GL_UNSIGNED_BYTE,picture->pixels);
    SDL_UnlockSurface(picture);
 
    finalpicture = flipSurface(picture);
 
    if (SDL_SaveBMP(finalpicture, filename))
    {
        return -1;
    }
    SDL_FreeSurface(finalpicture);
    SDL_FreeSurface(picture);
 
    return 0;
}
 
SDL_Surface * flipSurface(SDL_Surface * surface)
{
    int current_line,pitch;
    SDL_Surface * fliped_surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
                                   surface->w,surface->h,
                                   surface->format->BitsPerPixel,
                                   surface->format->Rmask,
                                   surface->format->Gmask,
                                   surface->format->Bmask,
                                   surface->format->Amask);
 
 
 
    SDL_LockSurface(surface);
    SDL_LockSurface(fliped_surface);
 
    pitch = surface->pitch;
    for (current_line = 0; current_line < surface->h; current_line ++)
    {
        memcpy(&((unsigned char* )fliped_surface->pixels)[current_line*pitch],
               &((unsigned char* )surface->pixels)[(surface->h - 1  -
                                                    current_line)*pitch],
               pitch);
    }
 
    SDL_UnlockSurface(fliped_surface);
    SDL_UnlockSurface(surface);
    return fliped_surface;
}
 
void drawAxis(double scale)
{
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glPushMatrix();
    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glEnable(GL_LINE_SMOOTH);
    glLineWidth(2);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    glScaled(scale,scale,scale);
    glBegin(GL_LINES);
    glColor3ub(255,0,0);
    glVertex3i(0,0,0);
    glVertex3i(1,0,0);
    glColor3ub(0,255,0);
    glVertex3i(0,0,0);
    glVertex3i(0,1,0);
    glColor3ub(0,0,255);
    glVertex3i(0,0,0);
    glVertex3i(0,0,1);
    glEnd();
    glPopMatrix();
    glPopAttrib();
}
 
int initFullScreen(unsigned int * width,unsigned int * height)
{
    SDL_Rect ** modes;
 
    modes = SDL_ListModes(NULL,SDL_FULLSCREEN|SDL_OPENGL);
    if ((modes == (SDL_Rect **)0)||(modes == (SDL_Rect **)-1))
        return 0;
 
    if (width != NULL)
        *width = modes[0]->w;
    if (height != NULL)
        *height = modes[0]->h;
    if (SDL_SetVideoMode(modes[0]->w,
                         modes[0]->h,
                         SDL_GetVideoInfo()->vfmt->BitsPerPixel,
                         SDL_FULLSCREEN|SDL_OPENGL) == NULL)
        return -1;
    else
    {
        return 0;
    }
}
 
int XPMFromImage(const char * imagefile, const char * XPMfile)
{
    SDL_Surface * image,*image32bits;
    FILE * xpm;
    Uint32 pixel;
    Uint8 r,g,b,a;
    int x,y;
    unsigned int w;
    char * xpm_name;
    Uint32 rmask, gmask, bmask, amask;
 
    image = IMG_Load(imagefile);
    if (image == NULL)
        return -1;
 
    #if SDL_BYTEORDER == SDL_BIG_ENDIAN
 
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
 
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif
 
    image32bits = SDL_CreateRGBSurface(SDL_SWSURFACE,
                                      image->w,image->h,
                                      32,rmask, gmask, bmask, amask);
 
    SDL_BlitSurface(image,NULL,image32bits,NULL);
    SDL_FreeSurface(image);
 
    xpm = fopen(XPMfile,"w");
 
    xpm_name = (char*)malloc(strlen(imagefile)*sizeof(char));
    strcpy(xpm_name,imagefile);
    if (strchr(xpm_name,'.') != NULL)
        *strchr(xpm_name,'.') = '\0';
    fprintf(xpm,"const char *%s[] =\n",xpm_name);
    free(xpm_name);
 
    fprintf(xpm,"\t{\n");
    fprintf(xpm,"\t\t/* width height num_colors chars_per_pixel */\n");
    w = ((image->w%8) == 0)?image32bits->w:8*(image32bits->w/8+1);
 
    fprintf(xpm,"\t\t\" %d %d 3 1 \",\n",w,image32bits->h);
    fprintf(xpm,"\t\t/* colors */\n");
    fprintf(xpm,"\t\t\"X c #000000\",\n");
    fprintf(xpm,"\t\t\". c #ffffff\",\n");
    fprintf(xpm,"\t\t\"  c None\",\n");
    fprintf(xpm,"\t\t/* pixels */\n");
 
    SDL_LockSurface(image32bits);
 
    for (y = 0; y < image32bits->h; y ++)
    {
        fprintf(xpm,"\t\t\"");
        for (x = 0; x < image32bits->w ; x ++)
        {
            pixel = ((Uint32*)image32bits->pixels)[x+y*image32bits->pitch/4];
            SDL_GetRGBA(pixel,image32bits->format,&r,&g,&b,&a);
            if (a < 128)
                fprintf(xpm," ");
            else if ((r >= 128)||(g >= 128)||(b >= 128))
                fprintf(xpm,".");
            else
                fprintf(xpm,"X");
        }
        for (x = image32bits->w ; x < w ; x ++)
            fprintf(xpm," ");
        fprintf(xpm,"\",\n");
    }
 
    SDL_UnlockSurface(image32bits);
    SDL_FreeSurface(image32bits);
    fprintf(xpm,"\t\t\"0,0\"\n");
    fprintf(xpm,"\t};\n");
    return 0;
}
 
SDL_Cursor * cursorFromXPM(const char * xpm[])
{
    int i, row, col;
    int width, height;
    Uint8 * data;
    Uint8 * mask;
    int hot_x, hot_y;
    SDL_Cursor * cursor = NULL;
 
    sscanf(xpm[0], "%d %d", &width, &height);
    data = (Uint8*)calloc(width/8*height,sizeof(Uint8));
    mask = (Uint8*)calloc(width/8*height,sizeof(Uint8));
 
    i = -1;
    for ( row=0; row<height; ++row )
    {
        for ( col=0; col<width; ++col )
        {
            if ( col % 8 )
            {
                data[i] <<= 1;
                mask[i] <<= 1;
            }
            else
            {
                ++i;
                data[i] = mask[i] = 0;
            }
            switch (xpm[4+row][col])
            {
                case 'X':
                data[i] |= 0x01;
                mask[i] |= 0x01;
                break;
                case '.':
                mask[i] |= 0x01;
                break;
                case ' ':
                break;
            }
        }
    }
    sscanf(xpm[4+row], "%d,%d", &hot_x, &hot_y);
    cursor = SDL_CreateCursor(data, mask, width, height, hot_x, hot_y);
    free(data);
    free(mask);
    return cursor;
}
J'ai un peu regardé, mais j'avoue que c'est un peu du chinois. Je vais encore me pencher dessus, mais la réponse à ma première question me suffira déjà bien je dois dire

Voilà, merci d'avance de vos réponses !