-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworldgfx.c
154 lines (108 loc) · 3.46 KB
/
worldgfx.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
145
146
147
148
149
150
151
152
153
154
#include "world.h"
#include "utils.h"
private void renderSegment(World* world, Segment* this, Vec4i pos)
{
assert(this!=NULL);
this->rendered=true;
if(this->empty)
return;
static Vertex data[SEGMENT_SIZE*SEGMENT_SIZE*SEGMENT_SIZE*6*4];
VertexBuffer buffer=
{
.maxSize=sizeof(data)/sizeof(*data),
.size=0,
.data=data,
};
for(int z=0;z<SEGMENT_SIZE;z++)
for(int y=0;y<SEGMENT_SIZE;y++)
for(int x=0;x<SEGMENT_SIZE;x++)
{
blockDraw(world,pos*SEGMENT_SIZEV+(Vec4i){x,y,z},&buffer);
}
if(buffer.size==0)
return;
this->n=buffer.size;
if(this->vbo==0)
glGenBuffers(1,&this->vbo);
glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(*data)*this->n, data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
private void drawSegment(World* world, Segment* this, Vec4i pos)
{
if(this!=NULL && this->vbo!=0)
{
glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
glVertexPointer(3, GL_INT, sizeof(Vertex), (void*)offsetof(Vertex,pos));
glColorPointer(4, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex,color));
glTexCoordPointer(2,GL_FLOAT,sizeof(Vertex), (void*)offsetof(Vertex,texCoord));
glDrawArrays(GL_QUADS, 0, this->n);
}
}
public void worldDrawSelection(World* world, Vec4i pos)
{
static const Vec4i face[6][4]={
{{0,0,0},{0,1,0},{0,1,1},{0,0,1}},
{{1,1,0},{1,0,0},{1,0,1},{1,1,1}},
{{1,0,0},{0,0,0},{0,0,1},{1,0,1}},
{{0,1,0},{1,1,0},{1,1,1},{0,1,1}},
{{0,0,0},{1,0,0},{1,1,0},{0,1,0}},
{{0,0,1},{0,1,1},{1,1,1},{1,0,1}},
};
glDisable(GL_DEPTH_TEST);
glBegin(GL_LINE_STRIP);
for(int i=0;i<4;i++)
glVertexi(pos+face[pos[3]][i]);
glVertexi(pos+face[pos[3]][0]);
glEnd();
}
private void worldDrawSegment(World *this,int x, int y, int z)
{
Vec4i pos=(Vec4i){x,y,z}+this->scroll;
/*
if(this->segment[z][y][x]==NULL && SDL_GetTicks()-this->time<10)
{
this->segment[z][y][x]=newSegment();
generateSegment(this, this->segment[z][y][x],(Vec4i){x,y,z}+this->scroll);
}
*/
if( this->segment[z][y][x]!=NULL && this->segment[z][y][x]->rendered==false && SDL_GetTicks()-this->drawStart<10)
{
renderSegment(this, this->segment[z][y][x], pos);
}
drawSegment(this, this->segment[z][y][x], pos);
}
public void worldDraw(World *world)
{
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_ALPHA_TEST);
glEnable(GL_TEXTURE_2D);
glAlphaFunc(GL_GREATER,0.1);
glBindTexture(GL_TEXTURE_2D, world->terrain);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
worldSpiral(world,worldDrawSegment);
glBindTexture(GL_TEXTURE_2D, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
for(int i=0;i<world->maxActors;i++)
{
Actor* actor=worldGetActor(world,i);
if(actor!=NULL)
{
//actorDrawBBox(actor);
humanDraw(actor);
}
}
}
public void worldDisplayInit(World *this)
{
this->terrain=loadTexture("data/terrain.png");
assert(this->terrain!=0);
this->font=ftglCreateBitmapFont("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf");
assert(this->font!=NULL);
ftglSetFontFaceSize(this->font, 18, 0);
}