Example #1
0
File: gl.cpp Project: ZeXx86/gen
void gl_render ()
{
	fps_stick = SDL_GetTicks ();
	
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity ();
	
	camera_update ();
	
	/* Ziskame nasi kameru */
	camera_t *c = camera_get ();
	
	/* Vykreslovani SkyBoxu */
	gl_render_skybox (c);
	
	terrain_t *t;	
	for (t = terrain_list.next; t != &terrain_list; t = t->next) {
		if (!t->gl_buf_len) {
			/* mazani kontextu terenu z pameti */
			terrain_t *tmp = t->prev;
			if (terrain_del (t))
				t = tmp;
			
			continue;
		}

		gl_render_terrain (t, c);	
	}

	/* Vykreslovani vody */
	gl_water_render ();

	/* Vykreslovani HUD/textu */
	char sfps[32];
	sprintf (sfps, "FPS: %.0f", 1.0f/(fps_dtick/1000.0f));
	font_render (0.500, -0.400, sfps);
	
	glFlush ();
	
	 /* Prohodi predni a zadni buffer */
	SDL_GL_SwapWindow (g_window);

	/* vypocitame hodnotu FPS na zaklade soucasneho casu a casu z predchoziho cyklu */
	fps_dtick = SDL_GetTicks () - fps_stick;

	/* Omezime shora FPS, protoze nam vyssi uz nic neprinese */
	if (1000/FPS_MAX > fps_dtick)
		SDL_Delay (1000.0f/FPS_MAX - fps_dtick);
}
Example #2
0
int octree_thread2 (void *unused)
{	
	int size_v = TERRAIN_DIM*8;
	
	camera_t *c = camera_get ();

	int a = 0;
	
	/*terrain_t *t = terrain_list.next;
	t->root = octree_node_alloc (NULL, 0);
				
	octree_build (t, (octree_t *) t->root);
	
	t = terrain_add (-32, 0, 0);
	t->root = octree_node_alloc (NULL, 0);	
	octree_build (t, (octree_t *) t->root);*/
	
	/*t = terrain_add (0, 32, 0);
	t->root = octree_node_alloc (NULL, 0);	
	octree_build (t, (octree_t *) t->root);
	
	t = terrain_add (32, 32, 0);
	t->root = octree_node_alloc (NULL, 0);	
	octree_build (t, (octree_t *) t->root);*/
	
/*	while (1) {
		terrain_t *t;
		
		if (SDL_TryLockMutex (gl_mutex) == 0) {
			for (t = terrain_list.next; t != &terrain_list; t = t->next) {
				if (contact (-c->pos_x, -c->pos_y, 0, t->origin_x, t->origin_y, 0) > size_v) {
					if (t->gl_buf_len) {
						terrain_del (t);
					}
				}
			}

			SDL_UnlockMutex (gl_mutex);
		}
		
		SDL_Delay (1);
	}*/
	
	return 0;
}
void mouse_update()
{
	int mouseX, mouseY;
	Vect2d mousePosition;
	SDL_Event click_event;
	Entity *cam = camera_get();
	Actor *actor = NULL;
	
	SDL_GetMouseState(&mouseX, &mouseY);
	mousePosition = vect2d_new(mouseX, mouseY);

	if(cam)
	{
		mousePosition.x += cam->position.x;
		mousePosition.y += cam->position.y;
	}

	mouse->position = mousePosition;

	SDL_PollEvent(&click_event);
	if(click_event.type == SDL_MOUSEBUTTONDOWN)
	{
		mouse->clicked = 1;
		if(editor && mouse->position.y < HUD_HEIGHT)
		{
			actor = actor_new(mouse->position, currentType);
		}
	}
	else
	{
		mouse->clicked = 0;
	}

	if(mouse->frameNumber < mouse->sprite->fpl)
	{
		mouse->frameNumber++;
	}
	else
	{
		mouse->frameNumber = 0;
	}
	
	mouse_draw();
}
static void keyboard(unsigned char key, int x, int y) {
    
    if(camera_keyboard(key))
        return;
    
	switch(key) {
        case KEY_ESC:
            exit(0);
            
        case KEY_RESET_TRANSFORM:
            break;
            
        case KEY_ACTION:
            camera_get(eyePos, cenPos, upPos);
            xDist = fabs(eyePos[0] - actionCenter[0]);
            yDist = fabs(eyePos[1] -actionCenter[1]);
            zDist = fabs(eyePos[2] - actionCenter[2]);
            dist = sqrtf(powf(xDist, 2.0) + powf(yDist, 2.0) + powf(zDist, 2.0));
            
            if(dist < 1.5){
                if(action){
                    action = false;
                }else{
                    action = true;
                }
            }
            break;
            
      case KEY_HELP:
      	if(display_help)
            display_help = false;
        else
            display_help = true;
      default:
            break;
	}
}
Example #5
0
File: water.cpp Project: ZeXx86/gen
void gl_water_render ()
{	
#ifdef OLD
	glPushMatrix ();	
	
	unsigned int indice;
	unsigned int preindice;
	
	const unsigned int length = 2 * (WATER_RES + 1);

	const float t = SDL_GetTicks () / 2000.0f;	// "casovani" vln
	const float delta = 2. / WATER_RES;
	const float xn = (WATER_RES + 1) * delta + 1;
	
	float v1[3];
	float v2[3];
	float v3[3];
	float va[3];
	float vb[3];
	float n[3];
	float x, y;
	
	/* vrcholy */
	for (int j = 0; j < WATER_RES; j ++) {
		y = (j + 1) * delta - 1;
		
		for (int i = 0; i <= WATER_RES; i ++) {
			indice = 6 * (i + j * (WATER_RES + 1));

			x = i * delta - 1;
			water_surf[indice + 3] = x * WATER_DIM;
			water_surf[indice + 4] = gl_water_z (x, y, t) * 150;
			water_surf[indice + 5] = y * WATER_DIM;
			
			if (j) {
				/* Hodnoty predpocitany z predchoziho cyklu */
				preindice = 6 * (i + (j - 1) * (WATER_RES + 1));
				water_surf[indice] = water_surf[preindice + 3];
				water_surf[indice + 1] = water_surf[preindice + 4];
				water_surf[indice + 2] = water_surf[preindice + 5];
			} else {
				water_surf[indice] = x * WATER_DIM;
				water_surf[indice + 1] = gl_water_z (x, -1, t) * 150;
				water_surf[indice + 2] = -1 * WATER_DIM;
			}
		}
	}

	/* normaly */
	for (int j = 0; j < WATER_RES; j ++)
	for (int i = 0; i <= WATER_RES; i ++) {
		indice = 6 * (i + j * (WATER_RES + 1));

		v1[0] = water_surf[indice + 3];
		v1[1] = water_surf[indice + 4];
		v1[2] = water_surf[indice + 5];

		v2[0] = v1[0];
		v2[1] = water_surf[indice + 1];
		v2[2] = water_surf[indice + 2];

		if (i < WATER_RES) {
			v3[0] = water_surf[indice + 9];
			v3[1] = water_surf[indice + 10];
			v3[2] = v1[2];
		} else {
			v3[0] = xn;
			v3[1] = gl_water_z (xn, v1[2], t);
			v3[2] = v1[2];
		}

		va[0] = v2[0] - v1[0];
		va[1] = v2[1] - v1[1];
		va[2] = v2[2] - v1[2];

		vb[0] = v3[0] - v1[0];
		vb[1] = v3[1] - v1[1];
		vb[2] = v3[2] - v1[2];

		n[0] = (vb[1] * va[2]) - (vb[2] * va[1]);
		n[1] = (vb[2] * va[0]) - (vb[0] * va[2]);
		n[2] = (vb[0] * va[1]) - (vb[1] * va[0]);

		float l = sqrtf (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
		
		if (l) {
			l = 1 / l;
			water_norm[indice + 3] = n[0] * l;
			water_norm[indice + 4] = n[1] * l;
			water_norm[indice + 5] = n[2] * l;
		} else {
			water_norm[indice + 3] = 0;
			water_norm[indice + 4] = 1;
			water_norm[indice + 5] = 0;
		}

		if (j) {
			/* Hodnoty predpocitany z predchoziho cyklu */
			preindice = 6 * (i + (j - 1) * (WATER_RES + 1));
			water_norm[indice] = water_norm[preindice + 3];
			water_norm[indice + 1] = water_norm[preindice + 4];
			water_norm[indice + 2] = water_norm[preindice + 5];
		} else {
			/* v1[0] = v1[0]; */
			v1[1] = gl_water_z (v1[0], (j - 1) * delta - 1, t);
			v1[2] = (j - 1) * delta - 1;

			/* v3[0] = v3[0]; */
			v3[1] = gl_water_z (v3[0], v2[2], t);
			v3[2] = v2[2];

			va[0] = v1[0] - v2[0];
			va[1] = v1[1] - v2[1];
			va[2] = v1[2] - v2[2];

			vb[0] = v3[0] - v2[0];
			vb[1] = v3[1] - v2[1];
			vb[2] = v3[2] - v2[2];

			n[0] = (vb[1] * va[2]) - (vb[2] * va[1]);
			n[1] = (vb[2] * va[0]) - (vb[0] * va[2]);
			n[2] = (vb[0] * va[1]) - (vb[1] * va[0]);

			float l = sqrtf (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
			
			if (l) {
				l = 1 / l;
				water_norm[indice] = n[0] * l;
				water_norm[indice + 1] = n[1] * l;
				water_norm[indice + 2] = n[2] * l;
			} else {
				water_norm[indice] = 0;
				water_norm[indice + 1] = 1;
				water_norm[indice + 2] = 0;
			}
		}
	}

	glTranslatef (0, 0.4f, 0);	

	glEnable (GL_TEXTURE_GEN_S);
	glEnable (GL_TEXTURE_GEN_T);
	glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
	glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);

	glBindTexture (GL_TEXTURE_2D, tex_get (7));

	glEnableClientState (GL_NORMAL_ARRAY);
	glEnableClientState (GL_VERTEX_ARRAY);
	glNormalPointer (GL_FLOAT, 0, water_norm);
	glVertexPointer (3, GL_FLOAT, 0, water_surf);

	for (int i = 0; i < WATER_RES; i ++)
		glDrawArrays (GL_TRIANGLE_STRIP, i * length, length);
	
	glDisable (GL_TEXTURE_GEN_S);
	glDisable (GL_TEXTURE_GEN_T);
	
	glPopMatrix ();	
#endif
#define WATER_SIZE	2000.0f
	
	camera_t *c = camera_get ();
	
	if (!c)
		return;
	
	glBindBuffer (GL_ARRAY_BUFFER, vbo_id);
	
	glUseProgram (shader);
	
	glm::mat4 proj = c->projection;
	glm::mat4 tmp = c->view * glm::translate (glm::vec3 (-WATER_SIZE/2-c->pos[0], 0, -WATER_SIZE/2-c->pos[2])) * glm::rotate (90.0f, glm::vec3 (1, 0, 0)) * glm::scale (glm::vec3 (WATER_SIZE, WATER_SIZE, 1.0));

	int uniform = glGetUniformLocation (shader, "MVMatrix");
	glUniformMatrix4fv (uniform, 1, GL_FALSE, (float*)&tmp[0]);
	uniform = glGetUniformLocation (shader, "PMatrix");
	glUniformMatrix4fv (uniform, 1, GL_FALSE, (float*)&proj[0]);
	
	glm::vec3 cam_pos = c->pos / WATER_SIZE;
	uniform = glGetUniformLocation (shader, "cam_pos");
	glUniform3fv (uniform, 1, (float*)&cam_pos[0]);
	
	float time_wave = SDL_GetTicks () / 1000000.0f;
	uniform = glGetUniformLocation (shader, "time_wave");
	glUniform1f (uniform, time_wave);
	
	GLuint tex_id = glGetUniformLocation (shader, "tex_sampler0");
	glUniform1i (tex_id, 0);

	/* texura */
	glActiveTexture (GL_TEXTURE0); 
	glBindTexture (GL_TEXTURE_2D, tex_get (5));

	glEnableVertexAttribArray (0);
	glEnableVertexAttribArray (1);
	
	glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, (5 * sizeof(GLfloat)), 0);
	glVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, (5 * sizeof(GLfloat)), 0);
		
	/* k vykresleni pouzivame Vertex Buffer Object */
	glDrawArrays (GL_TRIANGLES, 0, 6);
	
	glDisableVertexAttribArray (0);
	glDisableVertexAttribArray (1);

	glBindBuffer (GL_ARRAY_BUFFER, 0);

	/* vypneme shader */
	glUseProgram (0);
}
Example #6
0
int octree_thread (void *unused)
{	
	int size_v = TERRAIN_DIM*7;
	
	camera_t *c = camera_get ();

	int a = 0;
	
	/*terrain_t *t = terrain_list.next;
	t->root = octree_node_alloc (NULL, 0);
				
	octree_build (t, (octree_t *) t->root);
	
	t = terrain_add (-32, 0, 0);
	t->root = octree_node_alloc (NULL, 0);	
	octree_build (t, (octree_t *) t->root);*/
	
	/*t = terrain_add (0, 32, 0);
	t->root = octree_node_alloc (NULL, 0);	
	octree_build (t, (octree_t *) t->root);
	
	t = terrain_add (32, 32, 0);
	t->root = octree_node_alloc (NULL, 0);	
	octree_build (t, (octree_t *) t->root);*/
	
	while (1) {

		//	a = 0;
			
			/*octree_t *node = (octree_t *) t->root;
			
			if (!node) {
				
					node = octree_node_alloc (t, NULL, 0);
					
					t->root = (void *) node;
					
					octree_build (t, node);
					
				//	SDL_UnlockMutex (gl_mutex);
				//}
			}*/
						
			//bool b = false;
			
			/*for (int y = -size_v; y < size_v; y ++) {
				for (int x = -size_v; x < size_v; x ++) {
					int d = TERRAIN_DIM;
					
					int cx = -((int) ((c->pos_x) + x) / d) * d;
					int cy = -((int) ((c->pos_y) + y) / d) * d;
		
					//printf ("%d %d\n", cx, cy);
					//int cz = -((int) (c->pos_z / 2) / d) * d;
					
					if (contact (-c->pos_x, -c->pos_y, 0, cx, cy, 0) <= size_v) {
						terrain_add (cx, cy, 0);
						terrain_add (cx, cy, TERRAIN_DIM);
					}
				}
			}*/
			
			//printf ("- %f .. %f .. %f\n", c->pos_x, c->pos_y, c->pos_z);
			

	
				
				
					
/*				int cx = -((int) (c->pos_x) / d) * d;
				int cy = -((int) (c->pos_y) / d) * d;
				int cz = -((int) (c->pos_z) / d) * d;
					
				if (c->state & CAMERA_STATE_MOUSECLICK) {
					if (t->origin_x == cx && t->origin_y == cy && t->origin_z == cz) {
						//printf (": %d %d - %d %d\n", t->origin_x, t->origin_y, -cx, -cy);
						//printf (": %d %d - %d %d\n", t->origin_x, t->origin_y, -cx, -cy);
						float bx = c->pos_x+(float) t->origin_x;
						float by = c->pos_y+(float) t->origin_y;
						float bz = c->pos_z+(float) t->origin_z;
						
						if (bx < 0)
							bx += 32;
						if (by < 0)
							by += 32;
						if (bz < 0)
							bz += 32;
				
						bx = 32 - bx;
						by = 32 - by;
						bz = 32 - bz;
						
						voxel_t *v;
						
						bool v_c = false;
						
						int v_size = 3;
						
						for (int i_x = -v_size; i_x < v_size; i_x ++)
						for (int i_y = -v_size; i_y < v_size; i_y ++)
						for (int i_z = -v_size; i_z < v_size; i_z ++) {
							v = terrain_voxel_get (t, (int) bx+i_x, (int) by+i_y, (int) bz+i_z);
							
							if (v->value >= 0) {
								v->value = -1;
								v_c = true;
							}
						}
						
						printf ("v: %f %f %f\n", v->value, bx, by);			

						if (v_c)
							gl_prepare_terrain (t);
					
						c->state &= ~CAMERA_STATE_MOUSECLICK;
					}
				}*/
			//}
			
			/*if (((node->origin_x-size_v) < -c->pos_x/TERRAIN_VOXEL_DIM) &&
				((node->origin_x+size_v) > -c->pos_x/TERRAIN_VOXEL_DIM) &&
				((node->origin_y-size_v) < -c->pos_y/TERRAIN_VOXEL_DIM) &&
				((node->origin_y+size_v) > -c->pos_y/TERRAIN_VOXEL_DIM) &&
				((node->origin_z-size_v) < -c->pos_z/TERRAIN_VOXEL_DIM) &&
				((node->origin_z+size_v) > -c->pos_z/TERRAIN_VOXEL_DIM))
				a ++;
		
			if (!a) {
				printf ("chybi!\n");
				int d = TERRAIN_DIM;
				
				int x = -((int) (c->pos_x / 2 + d) / d) * d;
				int y = -((int) (c->pos_y / 2 + d) / d) * d;
				int z = -((int) (c->pos_z / 2) / d) * d;
				
				if (SDL_LockMutex (gl_mutex) == 0) {
					printf ("pridavam teren: %d %d %d\n", x, y, z);
					terrain_add (x, y, 0);
					
					SDL_UnlockMutex (gl_mutex);
				}
				
				//break;
			}*/
			
			
		//}
		
		SDL_Delay (1);
	}
	
	return 0;
}
Example #7
0
File: font.cpp Project: ZeXx86/gen
void font_render (const double &X, const double &Y, const char *text)
{
#ifndef ANDROID
	unsigned Texture = 0;

	/* barva pisma.*/
	SDL_Color color = {255, 255, 255};
	
	SDL_Surface *msg = TTF_RenderText_Blended (const_cast<TTF_Font*>(font), text, color);
 
	/* vygenerujeme texturu v OpenGL z SDL_Surface.*/

	glGenTextures (1, &Texture);
	glBindTexture (GL_TEXTURE_2D, Texture);
 
	glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
	glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, msg->w, msg->h, 0, GL_BGRA,
	             GL_UNSIGNED_BYTE, msg->pixels);

	camera_t *c = camera_get ();
	
	glBindBuffer (GL_ARRAY_BUFFER, vbo_id);
	
	glUseProgram (shader);
		
	glm::mat4 proj = c->projection;
	glm::mat4 tmp = glm::translate (glm::vec3 (X, Y, -1.0f)) * glm::rotate (180.0f, glm::vec3 (1, 0, 0)) * glm::scale (glm::vec3 (0.1f, 0.05f, 1.0));

	int uniform = glGetUniformLocation (shader, "MVMatrix");
	glUniformMatrix4fv (uniform, 1, GL_FALSE, (float*)&tmp[0]);
	uniform = glGetUniformLocation (shader, "PMatrix");
	glUniformMatrix4fv (uniform, 1, GL_FALSE, (float*)&proj[0]);
	
	GLuint tex_id = glGetUniformLocation (shader, "tex_sampler0");
	glUniform1i (tex_id, 0);

	/* texura */
	glActiveTexture (GL_TEXTURE0); 

	glEnableVertexAttribArray (0);
	glEnableVertexAttribArray (1);
	
	glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, (5 * sizeof(GLfloat)), 0);
	glVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, (5 * sizeof(GLfloat)), 0);
	
	/* k vykresleni pouzivame Vertex Buffer Object */
	glDrawArrays (GL_TRIANGLES, 0, 6);
	
	glDisableVertexAttribArray (0);
	glDisableVertexAttribArray (1);

	glBindBuffer (GL_ARRAY_BUFFER, 0);

	/* vypneme shader */
	glUseProgram (0);

	/*Clean up.*/
	glDeleteTextures(1, &Texture);
	SDL_FreeSurface(msg);
#endif
}