diff options
Diffstat (limited to 'src/render.c')
-rw-r--r-- | src/render.c | 178 |
1 files changed, 53 insertions, 125 deletions
diff --git a/src/render.c b/src/render.c index 6c7cd5b..0d3f7fa 100644 --- a/src/render.c +++ b/src/render.c @@ -136,20 +136,6 @@ static void render_delete_shaders(RenderContext *r) { } -#define HEMI_ROUND_BITS 16 -#define HEMI_UP_BITS 8 -#define HEMI_NUM_VERTICES (4 * HEMI_ROUND_BITS * HEMI_UP_BITS) -#define ADD_VERTEX \ - r->hemisphere_v[3*i + 0] = xv; \ - r->hemisphere_v[3*i + 1] = yv; \ - r->hemisphere_v[3*i + 2] = zv; \ - r->hemisphere_n[3*i + 0] = xv; \ - r->hemisphere_n[3*i + 1] = yv; \ - r->hemisphere_n[3*i + 2] = zv; \ - r->hemisphere_t[2*i + 0] = 0.5 + 0.5*sin(xv); \ - r->hemisphere_t[2*i + 1] = 0.5 + 0.5*sin(yv); \ - i++; - /* OpenGL initial setup */ RenderContext *render_setup(int width, int height, int disable_vbos, int disable_fbos, int disable_shaders) { @@ -244,39 +230,6 @@ RenderContext *render_setup(int width, int height, int disable_vbos, int disable } - /* Create coordinates for a hemisphere to reuse later */ - r->hemisphere_v = malloc(3*HEMI_NUM_VERTICES*sizeof(GLfloat)); - r->hemisphere_n = malloc(3*HEMI_NUM_VERTICES*sizeof(GLfloat)); - r->hemisphere_t = malloc(2*HEMI_NUM_VERTICES*sizeof(GLfloat)); - double step_up = M_PI_2/(double)HEMI_UP_BITS; - double step_round = 2*M_PI/(double)HEMI_ROUND_BITS; - int is, js; - int i = 0; - for ( is=0; is<HEMI_ROUND_BITS; is++ ) { - for ( js=0; js<HEMI_UP_BITS; js++ ) { - double theta, phi; - GLfloat xv, yv, zv; - theta = (2*M_PI/(double)HEMI_ROUND_BITS) * (double)is; - phi = (M_PI_2/(double)HEMI_UP_BITS) * (double)js; - xv = cos(theta)*cos(phi); - yv = sin(theta)*cos(phi); - zv = sin(phi); - ADD_VERTEX - xv = cos(theta+step_round)*cos(phi); - yv = sin(theta+step_round)*cos(phi); - zv = sin(phi); - ADD_VERTEX - xv = cos(theta+step_round)*cos(phi+step_up); - yv = sin(theta+step_round)*cos(phi+step_up); - zv = sin(phi+step_up); - ADD_VERTEX - xv = cos(theta)*cos(phi+step_up); - yv = sin(theta)*cos(phi+step_up); - zv = sin(phi+step_up); - ADD_VERTEX - } - } - if ( r->shaders ) render_load_shaders(r); r->num_textures = 0; @@ -356,90 +309,65 @@ static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderC glMaterialfv(GL_FRONT, GL_SPECULAR, black); } - if ( p->type != PRIMITIVE_HEMISPHERE ) { - - /* Location and orientation */ - glPushMatrix(); - glTranslatef(x, y, z); - glRotatef(rad2deg(instance->yaw), 0.0, 0.0, -1.0); /* Minus sign defines +yaw as "right" */ - - /* Texture */ - if ( p->attribs & ATTRIB_SWIRLY ) { - if ( r->fbos && r->shaders ) { - glBindTexture(GL_TEXTURE_2D, r->swirly_texture); - glEnable(GL_TEXTURE_2D); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - render_setuni(r->lighting_program, "texture_emits", 1); - } else { - if ( r->shaders ) { - render_setuni(r->lighting_program, "texture_enabled", 0); - } /* else don't enable texturing */ - } - } else if ( p->texture != NULL ) { - Texture *texture; - texture = texture_lookup(r, p->texture); - if ( texture != NULL ) { - glBindTexture(GL_TEXTURE_2D, texture->texname); - glEnable(GL_TEXTURE_2D); - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - if ( r->shaders ) render_setuni(r->lighting_program, "texture_enabled", 1); - if ( r->shaders && texture->has_normals ) { - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, texture->normalmap); - render_setuni(r->lighting_program, "has_normals", 1); - glActiveTexture(GL_TEXTURE0); - } - } else { - if ( r->shaders ) render_setuni(r->lighting_program, "texture_enabled", 0); - } + /* Location and orientation */ + glPushMatrix(); + glTranslatef(x, y, z); + glRotatef(rad2deg(instance->yaw), 0.0, 0.0, -1.0); /* Minus sign defines +yaw as "right" */ + + /* Texture */ + if ( p->attribs & ATTRIB_SWIRLY ) { + if ( r->fbos && r->shaders ) { + glBindTexture(GL_TEXTURE_2D, r->swirly_texture); + glEnable(GL_TEXTURE_2D); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + render_setuni(r->lighting_program, "texture_emits", 1); } else { - if ( r->shaders ) render_setuni(r->lighting_program, "texture_enabled", 0); + if ( r->shaders ) { + render_setuni(r->lighting_program, "texture_enabled", 0); + } /* else don't enable texturing */ } - if ( r->vbos ) { - glBindBuffer(GL_ARRAY_BUFFER, p->vertices_buffer); - glVertexPointer(3, GL_FLOAT, 0, NULL); - glBindBuffer(GL_ARRAY_BUFFER, p->normals_buffer); - glNormalPointer(GL_FLOAT, 0, NULL); - glBindBuffer(GL_ARRAY_BUFFER, p->texcoords_buffer); - glTexCoordPointer(2, GL_FLOAT, 0, NULL); - glBindBuffer(GL_ARRAY_BUFFER, 0); + } else if ( p->texture != NULL ) { + Texture *texture; + texture = texture_lookup(r, p->texture); + if ( texture != NULL ) { + glBindTexture(GL_TEXTURE_2D, texture->texname); + glEnable(GL_TEXTURE_2D); + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + if ( r->shaders ) render_setuni(r->lighting_program, "texture_enabled", 1); + if ( r->shaders && texture->has_normals ) { + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, texture->normalmap); + render_setuni(r->lighting_program, "has_normals", 1); + glActiveTexture(GL_TEXTURE0); + } } else { - glVertexPointer(3, GL_FLOAT, 0, p->vertices); - glNormalPointer(GL_FLOAT, 0, p->normals); - glTexCoordPointer(2, GL_FLOAT, 0, p->texcoords); - } - glDrawArrays(render_gltype(p->type), 0, p->num_vertices); - *nvert += p->num_vertices; - glDisable(GL_TEXTURE_2D); - render_setuni(r->lighting_program, "has_normals", 0); - glPopMatrix(); - - if ( p->attribs & ATTRIB_SWIRLY ) { - if ( r->shaders ) render_setuni(r->lighting_program, "texture_emits", 0); + if ( r->shaders ) render_setuni(r->lighting_program, "texture_enabled", 0); } - } else { + if ( r->shaders ) render_setuni(r->lighting_program, "texture_enabled", 0); + } + if ( r->vbos ) { + glBindBuffer(GL_ARRAY_BUFFER, p->vertices_buffer); + glVertexPointer(3, GL_FLOAT, 0, NULL); + glBindBuffer(GL_ARRAY_BUFFER, p->normals_buffer); + glNormalPointer(GL_FLOAT, 0, NULL); + glBindBuffer(GL_ARRAY_BUFFER, p->texcoords_buffer); + glTexCoordPointer(2, GL_FLOAT, 0, NULL); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } else { + glVertexPointer(3, GL_FLOAT, 0, p->vertices); + glNormalPointer(GL_FLOAT, 0, p->normals); + glTexCoordPointer(2, GL_FLOAT, 0, p->texcoords); + } + glDrawArrays(render_gltype(p->type), 0, p->num_vertices); + *nvert += p->num_vertices; + glDisable(GL_TEXTURE_2D); + render_setuni(r->lighting_program, "has_normals", 0); + glPopMatrix(); - glPushMatrix(); - glTranslatef(x+p->vertices[0], y+p->vertices[1], z+p->vertices[2]); - glScalef(p->radius, p->radius, p->radius); - glRotatef(rad2deg(instance->yaw), 0.0, 0.0, -1.0); /* Minus sign defines +yaw as "right" */ - - glEnable(GL_RESCALE_NORMAL); - - glVertexPointer(3, GL_FLOAT, 0, r->hemisphere_v); - glNormalPointer(GL_FLOAT, 0, r->hemisphere_n); - glTexCoordPointer(2, GL_FLOAT, 0, r->hemisphere_t); - - glDrawArrays(GL_QUADS, 0, HEMI_NUM_VERTICES); - *nvert += HEMI_NUM_VERTICES; - if ( r->shaders ) if ( r->shaders ) render_setuni(r->lighting_program, "texture_emits", 0); - - glDisable(GL_RESCALE_NORMAL); - - glPopMatrix(); - + if ( p->attribs & ATTRIB_SWIRLY ) { + if ( r->shaders ) render_setuni(r->lighting_program, "texture_emits", 0); } } |