void x86_opengl_frame_buffer_clear(struct x86_opengl_frame_buffer_t *fb, GLbitfield mask) { int i; int clear_value; /* Get current set value */ /* FIXME */ clear_value = 0; /* Clear buffer */ if (fb) /* Clear buffers */ if (mask & ~(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_ACCUM_BUFFER_BIT)) x86_opengl_debug("\t\tInvalid mask!\n"); if ((mask & GL_COLOR_BUFFER_BIT) == GL_COLOR_BUFFER_BIT) { x86_opengl_debug("\t\tColor buffer cleared to %d\n", clear_value); for (i = 0; i < COLOR_BUFFER_COUNT; ++i) x86_opengl_render_buffer_clear(fb->color_buffer[i], clear_value); } if ((mask & GL_DEPTH_BUFFER_BIT) == GL_DEPTH_BUFFER_BIT) { x86_opengl_debug("\t\tDepth buffer cleared to %d\n", clear_value); x86_opengl_render_buffer_clear(fb->depth_buffer, clear_value); } if ((mask & GL_STENCIL_BUFFER_BIT) == GL_STENCIL_BUFFER_BIT) { x86_opengl_debug("\t\tStencil buffer cleared to %d\n", clear_value); x86_opengl_render_buffer_clear(fb->stencil_buffer, clear_value); } }
int x86_opengl_matrix_stack_push(struct x86_opengl_matrix_stack_t *mtx_stack, struct x86_opengl_matrix_t *mtx) { int i; int j; if (mtx_stack->depth == mtx_stack->max_depth) fatal("Stack overflow, max depth = %d\n", mtx_stack->max_depth); if (mtx == NULL) fatal("Invalid Matrix\n"); /* Debug */ x86_opengl_debug("\tCurrrent stack %p, depth = %d, max_depth = %d\n", mtx_stack, mtx_stack->depth, mtx_stack->max_depth ); x86_opengl_debug("\tPushing: mtx = %p, mtx->matrix = %p\n", mtx, mtx->matrix); for (i = 0; i < 4; ++i) { x86_opengl_debug("\t\t"); for (j = 0; j < 4; ++j) { x86_opengl_debug("%f\t", mtx->matrix[j*4+i]); } x86_opengl_debug("\n"); } list_push(mtx_stack->stack, mtx); mtx_stack->depth += 1; return 0; }
struct x86_opengl_matrix_t *x86_opengl_matrix_stack_pop(struct x86_opengl_matrix_stack_t *mtx_stack) { /* Variables */ struct x86_opengl_matrix_t *mtx; int i; int j; /* Pop from stack */ if (mtx_stack->depth == 0 ) fatal("Stack underflow, max depth = %d\n", mtx_stack->max_depth); mtx = list_pop(mtx_stack->stack); if (mtx == NULL) fatal("Empty stack!\n"); /* Debug info */ x86_opengl_debug("\tCurrrent stack %p, depth = %d, max_depth = %d\n", mtx_stack, mtx_stack->depth, mtx_stack->max_depth ); x86_opengl_debug("\tPoping: mtx = %p, mtx->matrix = %p\n", mtx, mtx->matrix); for (i = 0; i < 4; ++i) { x86_opengl_debug("\t\t"); for (j = 0; j < 4; ++j) { x86_opengl_debug("%f\t", mtx->matrix[j*4+i]); } x86_opengl_debug("\n"); } x86_opengl_matrix_free(mtx); mtx_stack->depth -= 1; /* Return */ return mtx; }
int x86_opengl_render_buffer_resize(struct x86_opengl_render_buffer_t *rb, int width, int height) { /* Invalid size */ if (width < 1 || height < 1) fatal("%s: invalid size (width = %d, height = %d)\n", __FUNCTION__, width, height); /* If same size, just clear it. */ if (rb->width == width && rb->height == height) { /* FIXME, currently set value == 1 */ x86_opengl_render_buffer_clear(rb, 0); return 0; } x86_opengl_debug("\t\tBuffer resized, W x H = %d x %d\n", width, height); /* Free previous buffer */ if (rb->buffer) free(rb->buffer); /* Allocate new buffer */ rb->buffer = xcalloc(width * height, sizeof(GLuint)); /* Store new size */ rb->width = width; rb->height = height; /* Return */ return 0; }
struct x86_opengl_matrix_t *x86_opengl_matrix_stack_bottom(struct x86_opengl_matrix_stack_t *mtx_stack) { struct x86_opengl_matrix_t *mtx; int i; int j; mtx = list_bottom(mtx_stack->stack); x86_opengl_debug("\t\tMatrix on bottom of stack: mtx = %p, mtx->matrix = %p\n", mtx, mtx->matrix); for (i = 0; i < 4; ++i) { x86_opengl_debug("\t\t"); for (j = 0; j < 4; ++j) { x86_opengl_debug("%f\t", mtx->matrix[j*4+i]); } x86_opengl_debug("\n"); } return mtx; }
struct x86_opengl_matrix_stack_t *x86_opengl_matrix_stack_create(GLenum mode) { struct x86_opengl_matrix_stack_t *stack; struct x86_opengl_matrix_t *mtx; /* Allocate */ stack = xcalloc(1, sizeof(struct x86_opengl_matrix_stack_t)); if(!stack) fatal("%s: out of memory", __FUNCTION__); x86_opengl_debug("\tCreated Matrix Stack %p\n", stack); /* Initialize */ /* Initially, each of the stacks contains an identity matrix */ mtx = x86_opengl_matrix_create(MATRIX_IDENTITY); stack->stack = list_create(); list_add(stack->stack, mtx); stack->depth = 0; switch (mode) { case GL_MODELVIEW: { stack->max_depth = MAX_MODELVIEW_STACK_DEPTH; break; } case GL_PROJECTION: { stack->max_depth = MAX_PROJECTION_STACK_DEPTH; break; } case GL_TEXTURE: { stack->max_depth = MAX_TEXTURE_STACK_DEPTH; break; } case GL_COLOR: { stack->max_depth = MAX_COLOR_STACK_DEPTH; break; } default: break; } /* Return */ return stack; }
static GLfloat x86_opengl_light_attenuation(GLfloat k0, GLfloat k1, GLfloat k2, const GLfloat *v, const GLfloat *ppli) { GLfloat distance; GLfloat denominator; if (fabs(ppli[4] - 0.0f) < 0.0000001) { distance = sqrt((ppli[0] - v[0]) * (ppli[0] - v[0]) + (ppli[1] - v[1]) * (ppli[1] - v[1]) + (ppli[2] - v[2]) * (ppli[2] - v[2])); x86_opengl_debug("\t\t\tDistance = %f\n", distance); denominator = k0 + k1 * distance + k2 * distance * distance; x86_opengl_debug("\t\t\tDenominator = %f\n", denominator); if (distance != 0.0) return 1.0f / denominator; else { x86_opengl_debug("\t\tWarning: Distance = 0!\n"); return 0.0f; } } else return 1.0f; }
struct x86_opengl_edge_t *x86_opengl_edge_create(struct x86_opengl_vertex_t *vtx0, struct x86_opengl_vertex_t *vtx1) { struct x86_opengl_edge_t * edge; edge = xcalloc(1, sizeof(struct x86_opengl_edge_t)); /* Initialize */ edge->vtx0 = vtx0; edge->vtx1 = vtx1; x86_opengl_debug("\t\tEdge \t[%f, %f] - [%f, %f]\n\n", vtx0->pos[X_COMP], vtx0->pos[Y_COMP], vtx1->pos[X_COMP], vtx1->pos[Y_COMP]); /* Return */ return edge; }
struct x86_opengl_vertex_group_t *x86_opengl_vertex_group_create(GLenum primitive_type) { struct x86_opengl_vertex_group_t * vtxgp; vtxgp = calloc(1, sizeof(struct x86_opengl_vertex_group_t)); /* Initialize */ vtxgp->primitive_type = primitive_type; vtxgp->vertex_list = list_create(); x86_opengl_debug("\t\tPrimitive group type %d\n", primitive_type); /* Return */ return vtxgp; }
struct x86_opengl_vertex_t *x86_opengl_vertex_create(GLfloat x, GLfloat y, GLfloat z, GLfloat w) { struct x86_opengl_vertex_t *vtx; vtx = calloc(1, sizeof(struct x86_opengl_vertex_t)); /* Initialize */ vtx->x = x; vtx->y = y; vtx->z = z; vtx->w = w; x86_opengl_debug("\t\tCreate vertex \t[%f, %f, %f, %f]\n", vtx->x, vtx->y, vtx->z, vtx->w); return vtx; }
void x86_opengl_light_apply_all(struct x86_opengl_vertex_t *vtx, struct x86_opengl_light_attrib_t *lgt_attrb) { int i; struct x86_opengl_light_t *lgt; struct x86_opengl_material_t *mtrl; struct x86_opengl_light_model_t *mdl; GLfloat color_final[4] = {0.0f, 0.0f, 0.0f, 0.0f}; GLfloat color_const[4] = {0.0f, 0.0f, 0.0f, 0.0f}; GLfloat color_accum[4] = {0.0f, 0.0f, 0.0f, 0.0f}; GLfloat *acm; GLfloat *dcm; GLfloat *scm; GLfloat *ecm; GLfloat srm; GLfloat *acs; GLfloat atti; GLfloat spoti; GLfloat atti_mul_spoti; GLfloat *acli; GLfloat acm_mul_acli[4]; GLfloat *dcli; GLfloat dcm_mul_dcli[4]; GLfloat unit_v_to_ppli[4]; GLfloat *normal; GLfloat dcm_mul_dcli_factor; GLfloat fi; GLfloat hi[4]; GLfloat pe[4] = { 0.0f, 0.0f, 0.0f, 1.0f}; GLboolean vbs; GLfloat direction; GLfloat scm_mul_scli[4]; GLfloat scm_mul_scli_factor; GLfloat *scli; /* Apply lights */ /* Constant part comes from material itself */ /* Get Materal and Model information */ mtrl = lgt_attrb->Material; mdl = lgt_attrb->Model; ecm = mtrl->Attrib[MAT_ATTRIB_FRONT_EMISSION]; acm = mtrl->Attrib[MAT_ATTRIB_FRONT_AMBIENT]; dcm = mtrl->Attrib[MAT_ATTRIB_FRONT_DIFFUSE]; scm = mtrl->Attrib[MAT_ATTRIB_FRONT_SPECULAR]; srm = mtrl->Attrib[MAT_ATTRIB_FRONT_SHININESS][0]; acs = mdl->Ambient; normal = vtx->normal; /* color_const = ecm + acm * acs */ ACC_SCALE_4V(color_const, acm, acs); ACC_4V(color_const, ecm); x86_opengl_debug("\t\tMaterial Emission = ["); for (i = 0; i < 4; ++i) x86_opengl_debug("%f ", ecm[i]); x86_opengl_debug("]\n"); x86_opengl_debug("\t\tMaterial Ambient = ["); for (i = 0; i < 4; ++i) x86_opengl_debug("%f ", acm[i]); x86_opengl_debug("]\n"); x86_opengl_debug("\t\tModel Ambient of scene = ["); for (i = 0; i < 4; ++i) x86_opengl_debug("%f ", acs[i]); x86_opengl_debug("]\n"); x86_opengl_debug("\t\tColor const = ["); for (i = 0; i < 4; ++i) x86_opengl_debug("%f ", color_const[i]); x86_opengl_debug("]\n"); /* Accumulation part comes from 8 lights */ for (i = 0; i < MAX_LIGHTS; ++i) { lgt = lgt_attrb->Light[i]; vbs = lgt_attrb->Model->LocalViewer; GLfloat intermidiate_color[4] = {0.0f, 0.0f, 0.0f, 0.0f}; if (lgt->Enabled) { /* Calculate the 1st part in lighting equation */ x86_opengl_debug("\t\tLight position= ["); for (i = 0; i < 4; ++i) x86_opengl_debug("%f ", lgt->EyePosition[i]); x86_opengl_debug("]\n"); atti = x86_opengl_light_attenuation(lgt->ConstantAttenuation, lgt->LinearAttenuation, lgt->QuadraticAttenuation, vtx->pos, lgt->EyePosition); x86_opengl_debug("\t\tAtti = %f\n", atti); spoti = x86_opengl_light_spot(lgt->EyePosition, vtx->pos, lgt->SpotDirection, lgt->SpotExponent, lgt->SpotCutoff); x86_opengl_debug("\t\tSpoti = %f\n", spoti); atti_mul_spoti = atti * spoti; x86_opengl_debug("\t\tAtti * Spoti = %f\n", atti_mul_spoti); /* Calculate the 2nd part in lighting equation */ acli = lgt->Ambient; SCALE_4V(acm_mul_acli, acm, acli); COPY_4V(intermidiate_color, acm_mul_acli); x86_opengl_debug("\t\tAcm * Acli = ["); for (i = 0; i < 4; ++i) x86_opengl_debug("%f ", acm_mul_acli[i]); x86_opengl_debug("]\n"); /* Calculate the 3rd part in lighting equation */ dcli = lgt->Diffuse; x86_opengl_debug("\t\tDcli = ["); SCALE_4V(dcm_mul_dcli, dcm, dcli); for (i = 0; i < 4; ++i) x86_opengl_debug("%f ", dcli[i]); x86_opengl_debug("]\n"); x86_opengl_debug("\t\tDcm = ["); for (i = 0; i < 4; ++i) x86_opengl_debug("%f ", dcm[i]); x86_opengl_debug("]\n"); x86_opengl_debug("\t\tDcm * Dcli = ["); for (i = 0; i < 4; ++i) x86_opengl_debug("%f ", dcm_mul_dcli[i]); x86_opengl_debug("]\n"); x86_opengl_vector_unit(unit_v_to_ppli, vtx->pos, lgt->EyePosition); x86_opengl_debug("\t\tV -> Ppli = ["); for (i = 0; i < 4; ++i) x86_opengl_debug("%f ", unit_v_to_ppli[i]); x86_opengl_debug("]\n"); dcm_mul_dcli_factor = DOT4(normal, unit_v_to_ppli); x86_opengl_debug("\t\tNormal = ["); for (i = 0; i < 4; ++i) x86_opengl_debug("%f ", normal[i]); x86_opengl_debug("]\n"); x86_opengl_debug("\t\tFactor = %f\n" , dcm_mul_dcli_factor); SELF_SCALE_SCALAR_4V(dcm_mul_dcli, dcm_mul_dcli_factor); ACC_4V(intermidiate_color, dcm_mul_dcli); /* Calculate the 4th part in lighting equation */ scli = lgt->Specular; SCALE_4V(scm_mul_scli, scm, scli); x86_opengl_debug("\t\tScm * Scli = ["); for (i = 0; i < 4; ++i) x86_opengl_debug("%f ", dcm_mul_dcli[i]); x86_opengl_debug("]\n"); fi = x86_opengl_light_fi(normal, vtx->pos, lgt->EyePosition); x86_opengl_light_hi(hi, vtx->pos, lgt->EyePosition, pe, vbs); direction = x86_opengl_direction_mul(vtx->normal, hi); scm_mul_scli_factor = pow(direction, srm); scm_mul_scli_factor *= fi; x86_opengl_debug("\t\tScm * Scli factor = %f\n", scm_mul_scli_factor); SELF_SCALE_SCALAR_4V(scm_mul_scli, scm_mul_scli_factor); ACC_4V(intermidiate_color, scm_mul_scli); SELF_SCALE_SCALAR_4V(intermidiate_color, atti_mul_spoti); } ACC_4V(color_accum, intermidiate_color); } ADD_4V(color_final, color_const, color_accum); x86_opengl_debug("\t\tFinal Vertex Color = ["); for (int i = 0; i < 4; ++i) x86_opengl_debug("%f ", color_final[i]); x86_opengl_debug("]\n"); for (i = 0; i < 4; ++i) { vtx->color[i] = 255*color_final[i]; } }
void x86_opengl_vertex_free(struct x86_opengl_vertex_t *vtx) { x86_opengl_debug("\t\tFree vertex \t[%f, %f, %f, %f]\n", vtx->x, vtx->y, vtx->z, vtx->w); free(vtx); }
void x86_opengl_vertex_buffer_add_vertex(struct x86_opengl_vertex_buffer_t *vtxbf, struct x86_opengl_vertex_t *vtx) { x86_opengl_debug("\t\tAdd vertex \t[%f, %f, %f, %f]\n", vtx->x, vtx->y, vtx->z, vtx->w); x86_opengl_vertex_group_add_vertex(vtxbf->current_vertex_group, vtx); }