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 | inline void OpenGLRenderCommands(game_render_commands *commands,
s32 windowWidth, s32 windowHeight){
glViewport(0, 0, windowWidth, windowHeight);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //deleteme
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
f32 a = SafeRatio1(2.0f, (f32)commands->width);
f32 b = SafeRatio1(2.0f, (f32)commands->height);
f32 proj[] = {
a, 0, 0, 0,
0, b, 0, 0,
0, 0, 1, 0,
-1.0f, -1.0f, 0, 1
};
glLoadMatrixf(proj);
u32 sortEntryCount = commands->pushBufferElementCount;
render_sort_entry *sortEntries = (render_sort_entry *)
(commands->pushBuffer + commands->sortEntryAt);
render_sort_entry *entry = sortEntries + (sortEntryCount-1);
for(u32 i = 0; i < sortEntryCount; i++){
render_command_header *header = (render_command_header *)
(commands->pushBuffer + entry->pushBufferOffset);
u8 *data = (u8 *)header + sizeof(render_command_header);
switch(header->type){
case RenderCommand_Clear:
{
render_command_clear *command = (render_command_clear *)data;
glClearColor(command->color.r, command->color.g, command->color.b, command->color.a);
glClear(GL_COLOR_BUFFER_BIT);
} break;
case RenderCommand_BitmapRect:
{
render_command_bitmap_rect *command = (render_command_bitmap_rect *)data;
if (command->bitmap->texHandle){
glBindTexture(GL_TEXTURE_2D, command->bitmap->texHandle);
}else{
// Upload and bind texture.
textureBindCount ++; // Static variable counter
command->bitmap.texHandle = textureBindCount;
glBindTexture(GL_TEXTURE_2D, command->bitmap.texHandle);
glTexImage2D(GL_TEXTURE_2D, 0, openGlDefaultInternalTextureFormat,
command->bitmap.width, command->bitmap.height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, command->bitmap.mem);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
v2 pMin = command->pMin;
v2 pMax = command->pMax;
v2 tMin = command->texPMin;
v2 tMax = command->texPMax;
glBegin(GL_TRIANGLES);
glColor4f(command->color.r, command->color.g, command->color.b, command->color.a);
glTexCoord2f(tMin.x, tMin.y); // Lower triangle
glVertex2f( pMin.x, pMin.y);
glTexCoord2f(tMax.x, tMin.y);
glVertex2f( pMax.x, pMin.y);
glTexCoord2f(tMax.x, tMax.y);
glVertex2f( pMax.x, pMax.y);
glTexCoord2f(tMin.x, tMin.y); // Upper triangle
glVertex2f( pMin.x, pMin.y);
glTexCoord2f(tMax.x, tMax.y);
glVertex2f( pMax.x, pMax.y);
glTexCoord2f(tMin.x, tMax.y);
glVertex2f( pMin.x, pMax.y);
glEnd();
} break;
case RenderCommand_Rectangle:
{
render_command_rectangle *command = (render_command_rectangle *)data;
glDisable(GL_TEXTURE_2D);
glBegin(GL_TRIANGLES);
glColor4f(command->color.r, command->color.g, command->color.b, command->color.a);
glVertex2f(command->p.x, command->p.y); // Lower triangle
glVertex2f(command->p.x+command->dim.x, command->p.y);
glVertex2f(command->p.x+command->dim.x, command->p.y+command->dim.y);
glVertex2f(command->p.x, command->p.y); // Upper triangle
glVertex2f(command->p.x+command->dim.x, command->p.y+command->dim.y);
glVertex2f(command->p.x, command->p.y+command->dim.y);
glEnd();
glEnable(GL_TEXTURE_2D);
} break;
/* ... */
}
entry--;
}
}
|