Master
Založen: 28. 08. 2007 Příspěvky: 35
|
Zaslal: 9. květen 2009, 17:01:42 Předmět: Jak priradit texturu shaderu v c++ kodu? |
|
|
Zdarec, uz nejakou dobu bojuju se shadery a konkretne, jak priradit nactenou texturu shaderu. At zkousim, co zkousim, proste se mi nic nevykresli (v Render Monkey vse funguje dobre).
Jine modely, kdyz neni akivovan shader se vykresli spravne vcetne textury.Textury jsou vygenerovane spravne (maji ID a normalne se aplikuji bez aktivovane shaderu) a uniformy vraci spravne ID ( ID > -1), takze nekde mi asi chybi neco, cim priradim spravne texturove data shaderu.
Tady je kod shaderu (vertex):
kód: |
uniform vec3 fvLightPosition;
uniform vec3 fvEyePosition;
varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
void main( void )
{
gl_Position = ftransform();
Texcoord = gl_MultiTexCoord0.xy;
vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;
vec3 fvViewDirection = fvEyePosition - fvObjectPosition.xyz;
vec3 fvLightDirection = fvLightPosition - fvObjectPosition.xyz;
vec3 tangent;
vec3 binormal;
vec3 c1 = cross(gl_Normal, vec3(0.0, 0.0, 1.0));
vec3 c2 = cross(gl_Normal, vec3(0.0, 1.0, 0.0));
if(length(c1)>length(c2))
{
tangent = c1;
}
else
{
tangent = c2;
}
tangent = normalize(tangent);
binormal = cross(gl_Normal, tangent);
binormal = normalize(binormal);
vec3 fvNormal = gl_NormalMatrix * gl_Normal;
vec3 fvBinormal = gl_NormalMatrix * binormal;
vec3 fvTangent = gl_NormalMatrix * tangent;
ViewDirection.x = dot( fvTangent, fvViewDirection );
ViewDirection.y = dot( fvBinormal, fvViewDirection );
ViewDirection.z = dot( fvNormal, fvViewDirection );
LightDirection.x = dot( fvTangent, fvLightDirection.xyz );
LightDirection.y = dot( fvBinormal, fvLightDirection.xyz );
LightDirection.z = dot( fvNormal, fvLightDirection.xyz );
}
|
fragment
kód: |
uniform vec4 fvAmbient;
uniform vec4 fvSpecular;
uniform vec4 fvDiffuse;
uniform float fSpecularPower;
uniform sampler2D baseMap;
uniform sampler2D bumpMap;
varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
void main( void )
{
vec3 fvLightDirection = normalize( LightDirection );
vec3 fvNormal = normalize( ( texture2D( bumpMap, Texcoord ).xyz * 2.0 ) - 1.0 );
float fNDotL = dot( fvNormal, fvLightDirection );
vec3 fvReflection = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection );
vec3 fvViewDirection = normalize( ViewDirection );
float fRDotV = max( 0.0, dot( fvReflection, fvViewDirection ) );
vec4 fvBaseColor = texture2D( baseMap, Texcoord );
vec4 fvTotalAmbient = fvAmbient * fvBaseColor;
vec4 fvTotalDiffuse = fvDiffuse * fNDotL * fvBaseColor;
vec4 fvTotalSpecular = fvSpecular * ( pow( fRDotV, fSpecularPower ) );
gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular );
}
|
Prirazovani uniformu
kód: |
GLfloat lightPos[] = { 0.0, 0.0, 0.0 };
GLfloat eyePos[] = { 0.0, 0.0, -100.0 };
GLfloat ambient[] = { 0.3686, 0.3684, 0.3684, 1.0 };
GLfloat diffuse[] = { 0.8863, 0.8850, 0.8850, 1.0 };
GLfloat specular[] = { 0.4902, 0.4887, 0.4887, 1.0 };
jeskyneBumpShader->enable();
int uniform = glGetUniformLocation(jeskyneBumpShader->programObject, "fSpecularPower");
glUniform1f(uniform, 25.0f);
uniform = glGetUniformLocation(jeskyneBumpShader->programObject, "fvLightPosition");
glUniform3fv(uniform, 1, lightPos);
uniform = glGetUniformLocation(jeskyneBumpShader->programObject, "fvEyePosition");
glUniform3fv(uniform, 1, eyePos);
uniform = glGetUniformLocation(jeskyneBumpShader->programObject, "fvAmbient");
glUniform4fv(uniform, 1, ambient);
uniform = glGetUniformLocation(jeskyneBumpShader->programObject, "fvDiffuse");
glUniform4fv(uniform, 1, diffuse);
uniform = glGetUniformLocation(jeskyneBumpShader->programObject, "fvSpecular");
glUniform4fv(uniform, 1, specular);
uniform = glGetUniformLocation(jeskyneBumpShader->programObject, "baseMap");
glUniform1i(uniform, jeskyneDiffuseTexture);
uniform = glGetUniformLocation(jeskyneBumpShader->programObject, "bumpMap");
glUniform1i(uniform, jeskyneBumpTexture);
|
a samotny draw
kód: |
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[objLoad->faceList[0]->material_index]);
glBegin(GL_TRIANGLES);
if (objLoad->textureCount > 0)
{
for (int i = 0;i < objLoad->faceCount;i++)
{
glTexCoord2f(...);
glNormal3f(...);
glVertex3f(...);
glTexCoord2f(...);
glNormal3f(...);
glVertex3f(...);
glTexCoord2f(...);
glNormal3f(...);
glVertex3f(...;
}
}
|
Dik za pomoc, ja uz fakt nevim, cim by to mohlo byt. |
|
nou
Založen: 28. 07. 2007 Příspěvky: 1047
|
Zaslal: 9. květen 2009, 17:43:01 Předmět: |
|
|
do uniform premenej na textutu sa nedava cislo textury ale cislo texturovacej jednotky. teda
kód: |
//pri vytvarani shaderu
uniform = glGetUniformLocation(jeskyneBumpShader->programObject, "baseMap");
glUniform1i(uniform, 0);
uniform = glGetUniformLocation(jeskyneBumpShader->programObject, "bumpMap");
glUniform1i(uniform, 1);
//pred kreslenim. taktiez nezalezi ci je alebo nie je zapnute texturovanie cez glEnable(GL_TEXTURE_xD);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textura1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textura2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_3D, textura3d);
|
by som ti odporucil ako dalsiu vec spravit si VBO. pretoze kreslenie cez glBegin/End je zbytocne pomale. _________________ Najjednoduchšie chyby sa najtažšie hľadajú. |
|