-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.c++
67 lines (62 loc) · 2.22 KB
/
texture.c++
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
#include "shared.h++"
//#include <SDL2/SDL_image.h>
#include "texture.h++"
/*
GLuint loadTexture(std::filesystem::path path){
SDL_Surface *image=IMG_Load(path.string().c_str());
image = SDL_ConvertSurfaceFormat(image, SDL_PIXELFORMAT_RGBA8888, 0);
GLuint object;
glGenTextures(1,&object);
glBindTexture(GL_TEXTURE_2D,object);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,image->w,image->h,0,GL_RGBA,GL_UNSIGNED_BYTE,image->pixels);
SDL_FreeSurface(image);
return object;
}
*/
GLchar *readTexture(GLuint texture){
DEBUGR(TEX_DEBUG,errno=0);
glBindTexture(GL_TEXTURE_2D,texture);
int width;
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&width);
int height;
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&height);
int alignment;
glGetIntegerv(GL_PACK_ALIGNMENT,&alignment);
int rowsize=width*3;
// i think this works
// round the row size up to the next multiple of alignment
if(rowsize&(alignment-1)){
rowsize+=alignment-(rowsize&(alignment-1));
}
int size=rowsize*height*sizeof(GLchar);
GLchar *pixels=(GLchar*)malloc(size);
DEBUGP(TEX_DEBUG,"tex=%i\n",texture);
DEBUGP(TEX_DEBUG,"width=%i\n",width);
DEBUGP(TEX_DEBUG,"width*3=%i\n",width*3);
DEBUGP(TEX_DEBUG,"rowsize=%i\n",rowsize);
DEBUGP(TEX_DEBUG,"alignment=%i\n",alignment);
DEBUGP(TEX_DEBUG,"height=%i\n",height);
DEBUGP(TEX_DEBUG,"area=%i\n",width*height);
DEBUGP(TEX_DEBUG,"size=%i\n",size);
DEBUGP(TEX_DEBUG,"texerrno=%i\n",errno);
glGetTexImage(GL_TEXTURE_2D,0,GL_RGB,GL_BYTE,pixels);
return pixels;
}
int getRowSize(GLuint texture){
glBindTexture(GL_TEXTURE_2D,texture);
int width;
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&width);
int alignment;
glGetIntegerv(GL_PACK_ALIGNMENT,&alignment);
int rowsize=width*3;
// i think this works
// round the row size up to the next multiple of alignment
if(rowsize&(alignment-1)){
rowsize+=alignment-(rowsize&(alignment-1));
}
return rowsize*sizeof(GLchar);
}