📄 vs_gl_draw.c
字号:
{ glLoadIdentity();}void VS3D_PushMatrix(VisualSurface *surf){ glPushMatrix();}void VS3D_MultMatrix(VisualSurface *surf, Fixed *mat){#ifdef GPAC_FIXED_POINT u32 i; Float _mat[16]; for (i=0; i<16; i++) _mat[i] = FIX2FLT(mat[i]); glMultMatrixf(_mat);#else glMultMatrixf(mat);#endif}void VS3D_PopMatrix(VisualSurface *surf){ glPopMatrix();}void VS3D_LoadMatrix(VisualSurface *surf, Fixed *mat){#ifdef GPAC_FIXED_POINT Float _mat[16]; u32 i; for (i=0; i<16; i++) _mat[i] = FIX2FLT(mat[i]); glLoadMatrixf(_mat);#else glLoadMatrixf(mat);#endif}void VS3D_SetClipper2D(VisualSurface *surf, GF_Rect clip){#ifdef GL_MAX_CLIP_PLANES#ifdef GPAC_USE_OGL_ES Fixed g[4]; u32 cp; VS3D_ResetClipper2D(surf); if (surf->num_clips + 4 > surf->max_clips) return; cp = surf->num_clips; g[2] = 0; g[1] = 0; g[3] = clip.x + clip.width; g[0] = -FIX_ONE; glClipPlanex(GL_CLIP_PLANE0 + cp, g); glEnable(GL_CLIP_PLANE0 + cp); g[3] = -clip.x; g[0] = FIX_ONE; glClipPlanex(GL_CLIP_PLANE0 + cp + 1, g); glEnable(GL_CLIP_PLANE0 + cp + 1); g[0] = 0; g[3] = clip.y; g[1] = -FIX_ONE; glClipPlanex(GL_CLIP_PLANE0 + cp + 2, g); glEnable(GL_CLIP_PLANE0 + cp + 2); g[3] = clip.height - clip.y; g[1] = FIX_ONE; glClipPlanex(GL_CLIP_PLANE0 + cp + 3, g); glEnable(GL_CLIP_PLANE0 + cp + 3); surf->num_clips += 4;#else Double g[4]; u32 cp; VS3D_ResetClipper2D(surf); if (surf->num_clips + 4 > surf->max_clips) return; cp = surf->num_clips; g[2] = 0; g[1] = 0; g[3] = FIX2FLT(clip.x) + FIX2FLT(clip.width); g[0] = -1; glClipPlane(GL_CLIP_PLANE0 + cp, g); glEnable(GL_CLIP_PLANE0 + cp); g[3] = -FIX2FLT(clip.x); g[0] = 1; glClipPlane(GL_CLIP_PLANE0 + cp + 1, g); glEnable(GL_CLIP_PLANE0 + cp + 1); g[0] = 0; g[3] = FIX2FLT(clip.y); g[1] = -1; glClipPlane(GL_CLIP_PLANE0 + cp + 2, g); glEnable(GL_CLIP_PLANE0 + cp + 2); g[3] = FIX2FLT(clip.height-clip.y); g[1] = 1; glClipPlane(GL_CLIP_PLANE0 + cp + 3, g); glEnable(GL_CLIP_PLANE0 + cp + 3); surf->num_clips += 4;#endif#endif}void VS3D_ResetClipper2D(VisualSurface *surf){#ifdef GL_MAX_CLIP_PLANES u32 cp; if (surf->num_clips < 4) return; cp = surf->num_clips - 4; glDisable(GL_CLIP_PLANE0 + cp + 3); glDisable(GL_CLIP_PLANE0 + cp + 2); glDisable(GL_CLIP_PLANE0 + cp + 1); glDisable(GL_CLIP_PLANE0 + cp); surf->num_clips -= 4;#endif}void VS3D_SetClipPlane(VisualSurface *surf, GF_Plane p){#ifdef GL_MAX_CLIP_PLANES#ifdef GPAC_USE_OGL_ES Fixed g[4]; if (surf->num_clips + 1 > surf->max_clips) return; gf_vec_norm(&p.normal); g[0] = p.normal.x; g[1] = p.normal.y; g[2] = p.normal.z; g[3] = p.d; glClipPlanex(GL_CLIP_PLANE0 + surf->num_clips, g); #else Double g[4]; if (surf->num_clips + 1 > surf->max_clips) return; gf_vec_norm(&p.normal); g[0] = FIX2FLT(p.normal.x); g[1] = FIX2FLT(p.normal.y); g[2] = FIX2FLT(p.normal.z); g[3] = FIX2FLT(p.d); glClipPlane(GL_CLIP_PLANE0 + surf->num_clips, g); #endif glEnable(GL_CLIP_PLANE0 + surf->num_clips); surf->num_clips++;#endif}void VS3D_ResetClipPlane(VisualSurface *surf){#ifdef GL_MAX_CLIP_PLANES if (!surf->num_clips) return; glDisable(GL_CLIP_PLANE0 + surf->num_clips-1); surf->num_clips -= 1;#endif}void VS3D_SetMaterial(VisualSurface *surf, u32 material_type, Fixed *rgba){ GLenum mode;#if defined(GPAC_USE_OGL_ES) Fixed *_rgba = rgba;#elif defined(GPAC_FIXED_POINT) Float _rgba[4]; _rgba[0] = FIX2FLT(rgba[0]); _rgba[1] = FIX2FLT(rgba[1]); _rgba[2] = FIX2FLT(rgba[2]); _rgba[3] = FIX2FLT(rgba[3]);#else Float *_rgba = rgba;#endif switch (material_type) { case MATERIAL_AMBIENT: mode = GL_AMBIENT; break; case MATERIAL_DIFFUSE: mode = GL_DIFFUSE; break; case MATERIAL_SPECULAR: mode = GL_SPECULAR; break; case MATERIAL_EMISSIVE: mode = GL_EMISSION; break; case MATERIAL_NONE:#ifdef GPAC_USE_OGL_ES glColor4x(_rgba[0], _rgba[1], _rgba[2], _rgba[3]);#else glColor4fv(_rgba);#endif /*fall-through*/ default: return; }#ifdef GPAC_USE_OGL_ES glMaterialxv(GL_FRONT_AND_BACK, mode, _rgba);#else glMaterialfv(GL_FRONT_AND_BACK, mode, _rgba);#endif}void VS3D_SetShininess(VisualSurface *surf, Fixed shininess){ glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, FIX2FLT(shininess) * 128);}void VS3D_SetState(VisualSurface *surf, u32 flag_mask, Bool setOn){ if (setOn) { if (flag_mask & F3D_LIGHT) glEnable(GL_LIGHTING); if (flag_mask & F3D_BLEND) glEnable(GL_BLEND); if (flag_mask & F3D_COLOR) glEnable(GL_COLOR_MATERIAL); } else { if (flag_mask & F3D_LIGHT) glDisable(GL_LIGHTING); if (flag_mask & F3D_BLEND) glDisable(GL_BLEND);#ifdef GPAC_USE_OGL_ES if (flag_mask & F3D_COLOR) glDisable(GL_COLOR_MATERIAL);#else if (flag_mask & F3D_COLOR) glDisable(GL_COLOR_MATERIAL | GL_COLOR_MATERIAL_FACE);#endif }}Bool VS3D_AddSpotLight(VisualSurface *surf, Fixed _ambientIntensity, SFVec3f attenuation, Fixed _beamWidth, SFColor color, Fixed _cutOffAngle, SFVec3f direction, Fixed _intensity, SFVec3f location){ Float vals[4], intensity, cutOffAngle, beamWidth, ambientIntensity, exp; GLint iLight; if (!surf->num_lights) glEnable(GL_LIGHTING); if (surf->num_lights==surf->max_lights) return 0; iLight = GL_LIGHT0 + surf->num_lights; surf->num_lights++; glEnable(iLight); ambientIntensity = FIX2FLT(_ambientIntensity); intensity = FIX2FLT(_intensity); cutOffAngle = FIX2FLT(_cutOffAngle); beamWidth = FIX2FLT(_beamWidth); /*in case...*/ gf_vec_norm(&direction); vals[0] = FIX2FLT(direction.x); vals[1] = FIX2FLT(direction.y); vals[2] = FIX2FLT(direction.z); vals[3] = 1; glLightfv(iLight, GL_SPOT_DIRECTION, vals); vals[0] = FIX2FLT(location.x); vals[1] = FIX2FLT(location.y); vals[2] = FIX2FLT(location.z); vals[3] = 1; glLightfv(iLight, GL_POSITION, vals); glLightf(iLight, GL_CONSTANT_ATTENUATION, attenuation.x ? FIX2FLT(attenuation.x) : 1.0f); glLightf(iLight, GL_LINEAR_ATTENUATION, FIX2FLT(attenuation.y)); glLightf(iLight, GL_QUADRATIC_ATTENUATION, FIX2FLT(attenuation.z)); vals[0] = FIX2FLT(color.red)*intensity; vals[1] = FIX2FLT(color.green)*intensity; vals[2] = FIX2FLT(color.blue)*intensity; vals[3] = 1; glLightfv(iLight, GL_DIFFUSE, vals); glLightfv(iLight, GL_SPECULAR, vals); vals[0] = FIX2FLT(color.red)*ambientIntensity; vals[1] = FIX2FLT(color.green)*ambientIntensity; vals[2] = FIX2FLT(color.blue)*ambientIntensity; vals[3] = 1; glLightfv(iLight, GL_AMBIENT, vals); //glLightf(iLight, GL_SPOT_EXPONENT, 0.5f * (beamWidth+0.001f) /*(Float) (0.5 * log(0.5) / log(cos(beamWidth)) ) */); if (!beamWidth) exp = 1; else if (beamWidth>cutOffAngle) exp = 0; else { exp = 1.0f - (Float) cos(beamWidth); if (exp>1) exp = 1; } glLightf(iLight, GL_SPOT_EXPONENT, exp*128); glLightf(iLight, GL_SPOT_CUTOFF, 180*cutOffAngle/FIX2FLT(GF_PI)); return 1;}/*insert pointlight - returns 0 if too many lights*/Bool VS3D_AddPointLight(VisualSurface *surf, Fixed _ambientIntensity, SFVec3f attenuation, SFColor color, Fixed _intensity, SFVec3f location){ Float vals[4], ambientIntensity, intensity; u32 iLight; if (!surf->num_lights) glEnable(GL_LIGHTING); if (surf->num_lights==surf->max_lights) return 0; iLight = GL_LIGHT0 + surf->num_lights; surf->num_lights++; glEnable(iLight); ambientIntensity = FIX2FLT(_ambientIntensity); intensity = FIX2FLT(_intensity); vals[0] = FIX2FLT(location.x); vals[1] = FIX2FLT(location.y); vals[2] = FIX2FLT(location.z); vals[3] = 1; glLightfv(iLight, GL_POSITION, vals); glLightf(iLight, GL_CONSTANT_ATTENUATION, attenuation.x ? FIX2FLT(attenuation.x) : 1.0f); glLightf(iLight, GL_LINEAR_ATTENUATION, FIX2FLT(attenuation.y)); glLightf(iLight, GL_QUADRATIC_ATTENUATION, FIX2FLT(attenuation.z)); vals[0] = FIX2FLT(color.red)*intensity; vals[1] = FIX2FLT(color.green)*intensity; vals[2] = FIX2FLT(color.blue)*intensity; vals[3] = 1; glLightfv(iLight, GL_DIFFUSE, vals); glLightfv(iLight, GL_SPECULAR, vals); vals[0] = FIX2FLT(color.red)*ambientIntensity; vals[1] = FIX2FLT(color.green)*ambientIntensity; vals[2] = FIX2FLT(color.blue)*ambientIntensity; vals[3] = 1; glLightfv(iLight, GL_AMBIENT, vals); glLightf(iLight, GL_SPOT_EXPONENT, 0); glLightf(iLight, GL_SPOT_CUTOFF, 180); return 1;}Bool VS3D_AddDirectionalLight(VisualSurface *surf, Fixed _ambientIntensity, SFColor color, Fixed _intensity, SFVec3f direction){ Float vals[4], ambientIntensity, intensity; u32 iLight; if (!surf->num_lights) glEnable(GL_LIGHTING); if (surf->num_lights==surf->max_lights) return 0; iLight = GL_LIGHT0 + surf->num_lights; surf->num_lights++; glEnable(iLight); ambientIntensity = FIX2FLT(_ambientIntensity); intensity = FIX2FLT(_intensity); /*in case...*/ gf_vec_norm(&direction); vals[0] = -FIX2FLT(direction.x); vals[1] = -FIX2FLT(direction.y); vals[2] = -FIX2FLT(direction.z); vals[3] = 0; glLightfv(iLight, GL_POSITION, vals); vals[0] = FIX2FLT(color.red)*intensity; vals[1] = FIX2FLT(color.green)*intensity; vals[2] = FIX2FLT(color.blue)*intensity; vals[3] = 1; glLightfv(iLight, GL_DIFFUSE, vals); glLightfv(iLight, GL_SPECULAR, vals); vals[0] = FIX2FLT(color.red)*ambientIntensity; vals[1] = FIX2FLT(color.green)*ambientIntensity; vals[2] = FIX2FLT(color.blue)*ambientIntensity; vals[3] = 1; glLightfv(iLight, GL_AMBIENT, vals); glLightf(iLight, GL_CONSTANT_ATTENUATION, 1.0f); glLightf(iLight, GL_LINEAR_ATTENUATION, 0); glLightf(iLight, GL_QUADRATIC_ATTENUATION, 0); glLightf(iLight, GL_SPOT_CUTOFF, 180); return 1;}void VS3D_RemoveLastLight(VisualSurface *surf){ if (surf->num_lights) { glDisable(GL_LIGHT0+surf->num_lights-1); surf->num_lights--; }}void VS3D_ClearAllLights(VisualSurface *surf){ u32 i; for (i=surf->num_lights; i>0; i--) { glDisable(GL_LIGHT0+i-1); } surf->num_lights = 0; //glDisable(GL_LIGHTING);}void VS3D_SetFog(VisualSurface *surf, const char *type, SFColor color, Fixed density, Fixed visibility){#ifdef GPAC_USE_OGL_ES Fixed vals[4]; glEnable(GL_FOG); if (!type || !stricmp(type, "LINEAR")) glFogx(GL_FOG_MODE, GL_LINEAR); else if (!stricmp(type, "EXPONENTIAL")) glFogx(GL_FOG_MODE, GL_EXP); else if (!stricmp(type, "EXPONENTIAL2")) glFogx(GL_FOG_MODE, GL_EXP2); glFogx(GL_FOG_DENSITY, density); glFogx(GL_FOG_START, 0); glFogx(GL_FOG_END, visibility); vals[0] = color.red; vals[1] = color.green; vals[2] = color.blue; vals[3] = FIX_ONE; glFogxv(GL_FOG_COLOR, vals); glHint(GL_FOG_HINT, surf->render->compositor->high_speed ? GL_FASTEST : GL_NICEST);#else Float vals[4]; glEnable(GL_FOG); if (!type || !stricmp(type, "LINEAR")) glFogi(GL_FOG_MODE, GL_LINEAR); else if (!stricmp(type, "EXPONENTIAL")) glFogi(GL_FOG_MODE, GL_EXP); else if (!stricmp(type, "EXPONENTIAL2")) glFogi(GL_FOG_MODE, GL_EXP2); glFogf(GL_FOG_DENSITY, FIX2FLT(density)); glFogf(GL_FOG_START, 0); glFogf(GL_FOG_END, FIX2FLT(visibility)); vals[0] = FIX2FLT(color.red); vals[1] = FIX2FLT(color.green); vals[2] = FIX2FLT(color.blue); vals[3] = 1; glFogfv(GL_FOG_COLOR, vals); glHint(GL_FOG_HINT, surf->render->compositor->high_speed ? GL_FASTEST : GL_NICEST);#endif}void VS3D_FillRect(VisualSurface *surf, GF_Rect rc, SFColorRGBA color){ glDisable(GL_BLEND | GL_LIGHTING | GL_TEXTURE_2D); glNormal3f(0, 0, 1);#ifdef GPAC_USE_OGL_ES if (color.alpha!=FIX_ONE) glEnable(GL_BLEND); glColor4x(color.red, color.green, color.blue, color.alpha); { Fixed v[8]; u16 indices[3]; indices[0] = 0; indices[1] = 1; indices[2] = 2; v[0] = rc.x; v[1] = rc.y; v[2] = rc.x+rc.width; v[3] = rc.y-rc.height; v[4] = rc.x+rc.width; v[5] = rc.y; glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_FIXED, 0, v); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, indices); v[4] = rc.x; v[5] = rc.y-rc.height; glVertexPointer(2, GL_FIXED, 0, v); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, indices); glDisableClientState(GL_VERTEX_ARRAY); }#else if (color.alpha!=FIX_ONE) { glEnable(GL_BLEND); glColor4f(FIX2FLT(color.red), FIX2FLT(color.green), FIX2FLT(color.blue), FIX2FLT(color.alpha)); } else { glColor3f(FIX2FLT(color.red), FIX2FLT(color.green), FIX2FLT(color.blue)); } glBegin(GL_QUADS); glVertex3f(FIX2FLT(rc.x), FIX2FLT(rc.y), 0); glVertex3f(FIX2FLT(rc.x), FIX2FLT(rc.y-rc.height), 0); glVertex3f(FIX2FLT(rc.x+rc.width), FIX2FLT(rc.y-rc.height), 0); glVertex3f(FIX2FLT(rc.x+rc.width), FIX2FLT(rc.y), 0); glEnd(); glDisable(GL_COLOR_MATERIAL | GL_COLOR_MATERIAL_FACE);#endif glDisable(GL_BLEND);}GF_Err R3D_GetScreenBuffer(GF_VisualRenderer *vr, GF_VideoSurface *fb){ u32 i, hy; char *tmp; Render3D *sr = (Render3D *)vr->user_priv; fb->video_buffer = (char*)malloc(sizeof(char)*3*sr->out_width * sr->out_height); fb->width = sr->out_width; fb->pitch = 3*sr->out_width; fb->height = sr->out_height; fb->pixel_format = GF_PIXEL_RGB_24; glReadPixels(sr->out_x, sr->out_y, sr->out_width, sr->out_height, GL_RGB, GL_UNSIGNED_BYTE, fb->video_buffer); /*flip image (openGL always handle image data bottom to top) */ tmp = (char*)malloc(sizeof(char)*fb->pitch); hy = fb->height/2; for (i=0; i<hy; i++) { memcpy(tmp, fb->video_buffer+ i*fb->pitch, fb->pitch); memcpy(fb->video_buffer + i*fb->pitch, fb->video_buffer + (fb->height - 1 - i) * fb->pitch, fb->pitch); memcpy(fb->video_buffer + (fb->height - 1 - i) * fb->pitch, tmp, fb->pitch); } free(tmp); return GF_OK;}GF_Err R3D_ReleaseScreenBuffer(GF_VisualRenderer *vr, GF_VideoSurface *framebuffer){ free(framebuffer->video_buffer); framebuffer->video_buffer = 0; return GF_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -