-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlightmap.c
executable file
·144 lines (105 loc) · 4.03 KB
/
lightmap.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
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
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "Header/global.h"
#include "Header/mapas.h"
#include "Header/lightmap.h"
unsigned char *mapLightMap=NULL;
void LightMap_delete() {
if (mapLightMap)
free(mapLightMap);
glDeleteTextures(1, &lightmap.textura);
}
void LightMap_aloca(int width, int height) {
//TODO: redimencionar lightmap sem perder o valor anterior
mapLightMap=(unsigned char *) calloc(width * height * 3, sizeof(unsigned char));
}
void LightMap_carregar(int w, int h, unsigned char cor[3], unsigned char *pixels) {
int x,y;
int i;
if (lightmap.textura == 0)
glGenTextures(1, &lightmap.textura);
int width = next_p2(mapWidth+2);
int height = next_p2(mapHeight+2);
if (mapLightMap != NULL)
free(mapLightMap);
LightMap_aloca(width, height);
for(y=0; y < height ;y++) {
for(x=0; x < width*3 ;x++) {
i=x + y * width*3;
if (pixels != NULL)
mapLightMap[i] = (x>=w*3 || y>=h) ? cor[i%3] : pixels[x + y*w*3];
else
mapLightMap[i] = cor[i%3];
}
}
lightmap.width=width;
lightmap.height=height;
glBindTexture(GL_TEXTURE_2D, lightmap.textura);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, mapLightMap);
}
void calcLighPos(int objX, int objY, int objW, int objH, float w, float h, float lighPos[4][2]) {
int srcX=objX+16;
int srcY=objY+16;
float texX1 = srcX / w;
float texY1 = srcY / h;
float texX2 = (srcX+objW) / w;
float texY2 = (srcY+objH) / h;
lighPos[0][0]=texX1;
lighPos[0][1]=texY1;
lighPos[1][0]=texX1;
lighPos[1][1]=texY2;
lighPos[2][0]=texX2;
lighPos[2][1]=texY2;
lighPos[3][0]=texX2;
lighPos[3][1]=texY1;
}
void DrawImg_LightMap(Textura *textura, int dstX, int dstY, int width, int height, int srcX, int srcY) {
static float lighPos[4][2];
if ((textura->w < width) || (textura->h < height)) printf("tamanho errado\n");
glActiveTextureARB(GL_TEXTURE0_ARB);
trocarTextura(textura->id);
glActiveTextureARB(GL_TEXTURE1_ARB);
if (lightmap.ativo)
glEnable(GL_TEXTURE_2D);
trocarTextura(lightmap.textura);
float texX1 = srcX / (float)textura->w;
float texY1 = srcY / (float)textura->h;
float texX2 = (srcX+width) / (float)textura->w;
float texY2 = (srcY+height) / (float)textura->h;
calcLighPos(dstX+xpos,dstY+ypos,width,height,lightmap.width*16,lightmap.height*16,lighPos);
glBegin(GL_QUADS);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, texX1, texY1);
glMultiTexCoord2fvARB(GL_TEXTURE1_ARB, lighPos[0]);
glVertex3i(dstX,dstY,0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, texX1, texY2);
glMultiTexCoord2fvARB(GL_TEXTURE1_ARB, lighPos[1]);
glVertex3i(dstX,dstY + height,0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, texX2, texY2);
glMultiTexCoord2fvARB(GL_TEXTURE1_ARB, lighPos[2]);
glVertex3i(dstX + width,dstY + height,0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, texX2, texY1);
glMultiTexCoord2fvARB(GL_TEXTURE1_ARB, lighPos[3]);
glVertex3i(dstX + width,dstY,0);
glEnd();
glActiveTextureARB(GL_TEXTURE1_ARB);
glDisable(GL_TEXTURE_2D);
glActiveTextureARB(GL_TEXTURE0_ARB);
}
unsigned char LightMap_getColor(int x, int y, int cor) {
if ((!lightmap.ativo) || (lightmap.width <= x) || (lightmap.height <= y)) return 255;
return mapLightMap[y * lightmap.width * 3 + x * 3 + cor];
}
Uint32 LightMap_getMapColor(SDL_PixelFormat *format, int x, int y) {
return SDL_MapRGB(format, LightMap_getColor(x, y, 0), LightMap_getColor(x, y, 1), LightMap_getColor(x, y, 2));
}
void LightMap_atualizar() {
//glDeleteTextures(1, &lightmap.textura);
trocarTextura(lightmap.textura);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, lightmap.width, lightmap.height, GL_RGB, GL_UNSIGNED_BYTE, mapLightMap);
}