Exemplo n.º 1
0
        private static List <string> BuildVertexShader(QShaderData shader, QShaderStage stage)
        {
            List <string> VertexLines = new List <string>();

            VertexLines.Add("PSInput Out = In;");
            VertexLines.Add("float4 defPosition = In.Pos;");

            for (int i = 0; i < shader.VertexDeforms.Count; ++i)
            {
                QShaderDeform deform = shader.VertexDeforms[i];

                switch (deform.Type)
                {
                case "wave":
                {
                    string name    = "deform" + i;
                    string offName = "deformOff" + i;

                    VertexLines.Add(
                        "float " + offName + " = (In.Pos.x + In.Pos.y + In.Pos.z) * " +
                        ParserTools.ToString(deform.Spread) + ";");

                    /*float phase = deform.WaveForm.Phase;
                     * //deform.WaveForm.Phase = phase.toFixed(4) + ' + ' + offName; <-----MIRAR ESTA LINEA
                     * VertexLines.Add(CreateWaveForm(name, deform.WaveForm, "g_time"));
                     * deform.WaveForm.Phase = phase;*/

                    //Parche temporal solo funciona con la funcion seno
                    VertexLines.Add("float " + name + " = " + ParserTools.ToString(deform.WaveForm.Bas) + " + sin((" +
                                    ParserTools.ToString(deform.WaveForm.Phase) + " + " +
                                    "g_time" + " * " + ParserTools.ToString(deform.WaveForm.Freq) + " + " + offName +
                                    ") * 6.283) * " + ParserTools.ToString(deform.WaveForm.Amp) + ";");
                    //FIN parche

                    VertexLines.Add("defPosition += float4(In.Normal * " + name + ",0);");
                }
                break;

                case "bulge":
                {
                    //float alpha = In.Tex0.x*bulgeWidth + g_time;
                    //float deform = sin(alpha)*bulgeHeight;
                    //defPosition += float4(In.Normal * deform0, 0);

                    string deformi = "deform" + i;
                    string alphai  = "alpha" + i;

                    VertexLines.Add("float " + alphai + " = In.Tex0.x*" + deform.BulgeWidth + " + g_time*" +
                                    deform.BulgeSpeed + ";");
                    VertexLines.Add("float " + deformi + " = sin(" + alphai + ")*" + deform.BulgeHeight + ";");
                    VertexLines.Add("defPosition += float4(In.Normal * " + deformi + ",0);");
                }
                break;

                default:
                    break;
                }
            }

            VertexLines.Add("float4 worldPosition = mul( defPosition, g_mWorld );");
            VertexLines.Add("Out.Color = In.Color;");

            if (stage.TcGen.Equals("environment"))
            {
                VertexLines.Add("float3 viewer = normalize(-worldPosition.xyz);");
                VertexLines.Add("float d = dot(In.Normal, viewer);");
                VertexLines.Add("float3 reflected = In.Normal*2.0*d - viewer;");
                VertexLines.Add("Out.Tex0 = float2(0.5, 0.5) + reflected.xy * 0.5;");
            }
            else
            {
                // Standard texturing
                VertexLines.Add("Out.Tex0 = In.Tex0;");
            }

            // tcMods
            for (int i = 0; i < stage.TcMods.Count; ++i)
            {
                QTcMod tcMod = stage.TcMods[i];

                switch (tcMod.Type)
                {
                case "rotate":
                    VertexLines.Add("float r = " + ParserTools.ToString(tcMod.Angle) + " * g_time;");
                    VertexLines.Add("Out.Tex0 -= float2(0.5, 0.5);");
                    VertexLines.Add("Out.Tex0 = float2(Out.Tex0.x * cos(r) - Out.Tex0.y * sin(r), Out.Tex0.y * cos(r) + Out.Tex0.x * sin(r));");
                    VertexLines.Add("Out.Tex0 += float2(0.5, 0.5);");
                    break;

                case "scroll":
                    VertexLines.Add(
                        "Out.Tex0 += float2(" + ParserTools.ToString(tcMod.SSpeed) + " * g_time, " + ParserTools.ToString(tcMod.TSpeed) +
                        " * g_time);");
                    break;

                case "scale":
                    VertexLines.Add(
                        "Out.Tex0 *= float2(" + ParserTools.ToString(tcMod.ScaleX) + ", " + ParserTools.ToString(tcMod.ScaleY) + ");"
                        );
                    break;

                case "stretch":
                    VertexLines.Add(CreateWaveForm("stretchWave", tcMod.WaveForm, ""));
                    VertexLines.Add("stretchWave = 1.0 / stretchWave;");
                    VertexLines.Add("Out.Tex0 *= stretchWave;");
                    VertexLines.Add("Out.Tex0 += float2(0.5 - (0.5 * stretchWave), 0.5 - (0.5 * stretchWave));");
                    break;

                case "turb":
                    var tName = "turbTime" + i;
                    VertexLines.Add("float " + tName + " = " + ParserTools.ToString(tcMod.Turbulance.Phase) + " + g_time * " +
                                    ParserTools.ToString(tcMod.Turbulance.Freq) + ";");
                    VertexLines.Add("Out.Tex0.x += sin( ( ( In.Pos.x + In.Pos.z )* 1.0/128.0 * 0.125 + " + tName +
                                    " ) * 6.283) * " + ParserTools.ToString(tcMod.Turbulance.Amp) + ";");
                    VertexLines.Add("Out.Tex0.y += sin( ( In.Pos.y * 1.0/128.0 * 0.125 + " + tName + " ) * 6.283) * " +
                                    ParserTools.ToString(tcMod.Turbulance.Amp) + ";");
                    break;

                default:
                    break;
                }
            }

            switch (stage.AlphaGen)
            {
            case "lightingspecular":
                VertexLines.Add("Out.Tex1 = In.Tex1;");
                break;

            default:
                break;
            }

            VertexLines.Add("Out.Pos = mul(worldPosition, g_mViewProj);");
            VertexLines.Add("return Out;");


            return(VertexLines);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parsear shader
        /// </summary>
        private static QShaderData ParseShader(QShaderTokenizer tokenizer)
        {
            QShaderData qsd = new QShaderData();

            qsd.Name = tokenizer.GetNext();

            string token = tokenizer.GetNext();

            if (!token.Equals("{"))
            {
                return(null);
            }

            // Parse a shader
            while (!tokenizer.EOF)
            {
                token = tokenizer.GetNext().ToLower();

                if (token.Equals("}"))
                {
                    break;
                }

                switch (token)
                {
                case "{":
                    QShaderStage stage = ParseStage(tokenizer);

                    // I really really really don't like doing this, which basically just forces lightmaps to use the 'filter' blendmode
                    // but if I don't a lot of textures end up looking too bright. I'm sure I'm jsut missing something, and this shouldn't
                    // be needed.
                    if (stage.IsLightMap() && (stage.HasBlendFunc))
                    {
                        stage.BlendSrc  = "GL_DST_COLOR";
                        stage.BlendDest = "GL_ZERO";
                    }

                    // I'm having a ton of trouble getting lightingSpecular to work properly,
                    // so this little hack gets it looking right till I can figure out the problem
                    if (stage.AlphaGen.Equals("lightingspecular"))
                    {
                        stage.BlendSrc     = "GL_ONE";
                        stage.BlendDest    = "GL_ZERO";
                        stage.HasBlendFunc = false;
                        stage.DepthWrite   = true;
                    }

                    if (stage.HasBlendFunc)
                    {
                        qsd.Blend = true;
                    }
                    else
                    {
                        qsd.Opaque = true;
                    }

                    qsd.Stages.Add(stage);
                    break;

                case "cull":
                    qsd.Cull = tokenizer.GetNext();
                    break;

                case "deformvertexes":
                    QShaderDeform deform = new QShaderDeform()
                    {
                        Type = tokenizer.GetNext().ToLower()
                    };

                    switch (deform.Type)
                    {
                    case "wave":
                        deform.Spread   = 1.0f / ParserTools.ToFloat(tokenizer.GetNext());
                        deform.WaveForm = ParseWaveform(tokenizer);
                        break;

                    case "bulge":
                        deform.BulgeWidth  = ParserTools.ToFloat(tokenizer.GetNext());
                        deform.BulgeHeight = ParserTools.ToFloat(tokenizer.GetNext());
                        deform.BulgeSpeed  = ParserTools.ToFloat(tokenizer.GetNext());
                        break;

                    default: deform = null; break;
                    }

                    if (deform != null)
                    {
                        qsd.VertexDeforms.Add(deform);
                    }
                    break;

                case "sort":
                    var sort = tokenizer.GetNext().ToLower();
                    switch (sort)
                    {
                    case "portal": qsd.Sort = 1; break;

                    case "sky": qsd.Sort = 2; break;

                    case "opaque": qsd.Sort = 3; break;

                    case "banner": qsd.Sort = 6; break;

                    case "underwater": qsd.Sort = 8; break;

                    case "additive": qsd.Sort = 9; break;

                    case "nearest": qsd.Sort = 16; break;

                    default: qsd.Sort = int.Parse(sort); break;
                    }
                    ;
                    break;

                case "surfaceparm":
                    var param = tokenizer.GetNext().ToLower();

                    switch (param)
                    {
                    case "sky":
                        qsd.Sky = true;
                        break;

                    default: break;
                    }
                    break;

                default: break;
                }
            }

            if (qsd.Sort > 0)
            {
                qsd.Sort = (qsd.Opaque ? 3 : 9);
            }

            return(qsd);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parsear shader
        /// </summary>
        private static QShaderData ParseShader(QShaderTokenizer tokenizer)
        {
            QShaderData qsd = new QShaderData();

            qsd.Name = tokenizer.GetNext();

            string token = tokenizer.GetNext();

            if (!token.Equals("{"))
            {
                return null;
            }

            // Parse a shader
	        while(!tokenizer.EOF) {
		        token = tokenizer.GetNext().ToLower();

		        if(token.Equals("}")) { break; }
        		
		        switch (token) {
			        case "{":
				        QShaderStage stage = ParseStage(tokenizer);
        				
				        // I really really really don't like doing this, which basically just forces lightmaps to use the 'filter' blendmode
				        // but if I don't a lot of textures end up looking too bright. I'm sure I'm jsut missing something, and this shouldn't
				        // be needed.
				        if(stage.IsLightMap() && (stage.HasBlendFunc))
                        {
					        stage.BlendSrc = "GL_DST_COLOR";
					        stage.BlendDest = "GL_ZERO";
				        }
        				
				        // I'm having a ton of trouble getting lightingSpecular to work properly, 
				        // so this little hack gets it looking right till I can figure out the problem
				        if(stage.AlphaGen.Equals("lightingspecular"))
                        {
					        stage.BlendSrc = "GL_ONE";
					        stage.BlendDest = "GL_ZERO";
					        stage.HasBlendFunc = false;
					        stage.DepthWrite = true;
				        }
        				
				        if(stage.HasBlendFunc)
				        {
				            qsd.Blend = true;
				        }
                        else
				        {
				            qsd.Opaque = true;
				        }
        				
				        qsd.Stages.Add(stage);
			            break;
        			
			        case "cull":
		                qsd.Cull = tokenizer.GetNext();
				        break;
        				
			        case "deformvertexes":
		                QShaderDeform deform = new QShaderDeform() {Type = tokenizer.GetNext().ToLower()};
        				
				        switch(deform.Type) {
					        case "wave":
						        deform.Spread = 1.0f / ParserTools.ToFloat(tokenizer.GetNext());
						        deform.WaveForm = ParseWaveform(tokenizer);
						        break;
								 case "bulge":
                     deform.BulgeWidth = ParserTools.ToFloat(tokenizer.GetNext());
                     deform.BulgeHeight = ParserTools.ToFloat(tokenizer.GetNext());
                     deform.BulgeSpeed = ParserTools.ToFloat(tokenizer.GetNext());
                     break;
					        default: deform = null; break;
				        }
        				
				        if(deform != null) { qsd.VertexDeforms.Add(deform); }
				        break;
        				
			        case "sort":
				        var sort = tokenizer.GetNext().ToLower();
				        switch(sort) {
					        case "portal": qsd.Sort = 1; break;
					        case "sky": qsd.Sort = 2; break;
					        case "opaque": qsd.Sort = 3; break;
					        case "banner": qsd.Sort = 6; break;
					        case "underwater": qsd.Sort = 8; break;
					        case "additive": qsd.Sort = 9; break;
					        case "nearest": qsd.Sort = 16; break;
					        default: qsd.Sort = int.Parse(sort); break; 
				        };
				        break;
        				
			        case "surfaceparm":
				        var param = tokenizer.GetNext().ToLower();
        				
				        switch(param) {
					        case "sky":
						        qsd.Sky = true;
						        break;
					        default: break;
				        }
				        break;
        				
			        default: break;
		        }
	        }

            if (qsd.Sort > 0)
            {
                qsd.Sort = (qsd.Opaque ? 3 : 9);
            }

            return qsd;
	
        }