Jump to content
  • Advertisement
Sign in to follow this  
StPapyna

Flow Control In GLSL : Single Shader Problem

This topic is 4431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just want to using single shader in my program. I have 2 texture Units,one is for CubeMap,other is for 2D texture. Now, In draw function,structure like this: glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_CUBEMAP,cubetex); glUniform1i(getUniLoc(ShaderProg,"NeedCubeMap"),1); /* Draw something too */ glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D,2dtex); glUniform1i(getUniLoc(ShaderProg,"NeedCubeMap"),0); /* Draw something too... */ In Fragment Shader,Just like this: uniform samplerCube cubemap; //0 uniform sampler2D tex2d; //1 uniform bool NeedCubeMap; varying float LightCoeff; varying vec3 vUV3D; varying vec2 vUV2D; void main(void) { if( NeedCubeMap ){ vec4 cubecolor = textureCube(cubemap,vUV3D); gl_FragColor = cubecolor*LightCoeff; }else{ vec4 tex2dcolor = texture2D(tex2d,vUV2D); gl_FragColor = tex2dcolor*LightCoeff; } } but when the program is running,it's black,nothing...I thinks maybe the frame buffer was not written,but when I cut off anything about the "NeedCubeMap",it just good. Where is the wrong ?

Share this post


Link to post
Share on other sites
Advertisement
Not sure what you mean about when it is breaking. Do both paths work independantly and is it adding the if statement that breaks it?

Test the shader without the if statement first and make sure that you get the correct output with the 2d texture and the cubemap.

Otherwise that code should work.

A few suggestions though, if you are running this on SM2 hardware both paths will get executed and then one result just thrown away (not good). You can also save the location of the variables, don't need to get the location each time you use them (faster).

EDIT: One more thing, if your texture units aren't correctly bound then the result in the shader is usually a black texture (on my hardware atleast). So make sure that you have cubemaps enabled etc.

Share this post


Link to post
Share on other sites
Thanks for your reply

I am using a 6200A,so it supports SM3 but maybe too slow,but enough for programming

Share this post


Link to post
Share on other sites
Quote:

#include "rk.h"

void draw(void)
{
glUniform1i(getUniLoc(brickProg,"cubemap"),1);
glUniform1i(getUniLoc(brickProg,"tex2d"),0);
GLint fcloc = getUniLoc(brickProg,"NeedCubeMap");

glUseProgram(ShaderProg);

glActiveTexture(GL_TEXTURE0);
glUniform1i(fcloc,0);// Pass 0

glBindTexture(GL_TEXTURE_2D,wall[3]);
glBegin(GL_QUADS);
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(1,1);glVertex3f(-2.0f,-2.0f,-2.0f);
glTexCoord2f(0,1);glVertex3f(-2.0f,-2.0f, 2.0f);
glTexCoord2f(0,0);glVertex3f( 2.0f,-2.0f, 2.0f);
glTexCoord2f(1,0);glVertex3f( 2.0f,-2.0f,-2.0f);
glEnd();

glBindTexture(GL_TEXTURE_2D,wall[2]);
glBegin(GL_QUADS);
glNormal3f(0.0f,-1.0f, 0.0f);
glTexCoord2f(1,0);glVertex3f(-2.0f, 2.0f, 2.0f);
glTexCoord2f(1,1);glVertex3f(-2.0f, 2.0f,-2.0f);
glTexCoord2f(0,1);glVertex3f( 2.0f, 2.0f,-2.0f);
glTexCoord2f(0,0);glVertex3f( 2.0f, 2.0f, 2.0f);
glEnd();

glBindTexture(GL_TEXTURE_2D,wall[5]);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0,0);glVertex3f(-2.0f, 2.0f,-2.0f);
glTexCoord2f(0,1);glVertex3f(-2.0f,-2.0f,-2.0f);
glTexCoord2f(1,1);glVertex3f( 2.0f,-2.0f,-2.0f);
glTexCoord2f(1,0);glVertex3f( 2.0f, 2.0f,-2.0f);
glEnd();

glBindTexture(GL_TEXTURE_2D,wall[4]);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f,-1.0f);
glTexCoord2f(0,0);glVertex3f( 2.0f, 2.0f, 2.0f);
glTexCoord2f(0,1);glVertex3f( 2.0f,-2.0f, 2.0f);
glTexCoord2f(1,1);glVertex3f(-2.0f,-2.0f, 2.0f);
glTexCoord2f(1,0);glVertex3f(-2.0f, 2.0f, 2.0f);
glEnd();

glBindTexture(GL_TEXTURE_2D,wall[1]);
glBegin(GL_QUADS);
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0,0);glVertex3f(-2.0f, 2.0f, 2.0f);
glTexCoord2f(0,1);glVertex3f(-2.0f,-2.0f, 2.0f);
glTexCoord2f(1,1);glVertex3f(-2.0f,-2.0f,-2.0f);
glTexCoord2f(1,0);glVertex3f(-2.0f, 2.0f,-2.0f);
glEnd();

glBindTexture(GL_TEXTURE_2D,wall[0]);
glBegin(GL_QUADS);
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0,0);glVertex3f( 2.0f, 2.0f,-2.0f);
glTexCoord2f(0,1);glVertex3f( 2.0f,-2.0f,-2.0f);
glTexCoord2f(1,1);glVertex3f( 2.0f,-2.0f, 2.0f);
glTexCoord2f(1,0);glVertex3f( 2.0f, 2.0f, 2.0f);
glEnd();
glPopMatrix();


glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_CUBE_MAP);
glUniform1i(fcloc,1);

glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMap);
GLushort (*vi)[3] = new GLushort[2849][3];
for(int i=0;i<sizeof(face_indicies)/sizeof(face_indicies[0]);i++)
{
for(int j=0;j<3;j++)
{
vi[j]=face_indicies[j];
}
}
glPointSize(4.0);
glScaled(2,2,2);
glTranslatef(-0.6,0.6,-0.4);
glPushMatrix();
glTranslatef(0.4,0.4,0.6);
glRotatef(90, 0,0,1);
glScalef(2, 2, 2);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT,0,normals);
glVertexPointer(3,GL_FLOAT,0,vertices);
glDrawElements(GL_TRIANGLES,2849*3,GL_UNSIGNED_SHORT,vi);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glPopMatrix();
glPushMatrix();
glTranslatef(1.0,0.9,1.0);
glutSolidSphere(0.2f, 64, 64);
glPopMatrix();
glPushMatrix();
glPointSize(8);
glBegin(GL_LINES);
glVertex3f(0.,0.,0.);
glVertex3f(3.0,0.,0.);
glEnd();

glBegin(GL_LINES);
glVertex3f(0.,0.,0.);
glVertex3f(0.,3.0,0.);
glEnd();

glBegin(GL_LINES);
glVertex3f(0.,0.,0.);
glVertex3f(0.,0.,3.0);
glEnd();
glPopMatrix();
glPushMatrix();

delete [] vi;
}



My Shader is on the top..

Who can help me ...

Share this post


Link to post
Share on other sites
Does "single shader" mean that you aren't using a vertex shader? If so, these:
Quote:
varying float LightCoeff;
varying vec3 vUV3D;
varying vec2 vUV2D;

... won't work!

You shoudl use the vertex shader to set these - I think you can use gl_TexCoord[n] and get away with the texture coordinates your way.

Share this post


Link to post
Share on other sites

I just find where I was wrong !

I am wrong to use Sampler3D instead of SamplerCube

and the same to function texture3D and textureCube

Now Everying is correct!

Thanks Everyone!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!