|
Post by shadow008 on Apr 27, 2010 19:48:50 GMT -5
i have googled this topic countless times yet i fail to understand how its done in code. In theory, i have it perfected, yet i cant seem to grasp how to do this in any program. I'm probably sounding like a total noob right now but, please, if you know how to do said task, post an example. I really want to know how to do it.
Thanks! ~Shadow008
|
|
|
Post by DJLinux on Apr 27, 2010 20:40:43 GMT -5
you must setup your light and material propertiece and you need an vertex and fragment shader (of course you can use the ARBShader plugin) Joshy
|
|
|
Post by DJLinux on Apr 27, 2010 21:12:58 GMT -5
Get the latest version of the ARBShader Plugin and run "test06.gb" Joshy ' test06.gb '----------- ' test for: PerPixelLighting data "// vertex shader" data "varying vec3 v;" data "varying vec3 lightvec;" data "varying vec3 normal;" data "varying vec4 FrontColor;" data "void main(void) {" data " normal = normalize(gl_NormalMatrix * gl_Normal);" data " v = vec3(gl_ModelViewMatrix * gl_Vertex);" data " lightvec = normalize(gl_LightSource[0].position.xyz - v);" data " gl_TexCoord[0] = gl_MultiTexCoord0;" data " FrontColor = gl_Color;" data "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;}" data "" ' <- end of vertex code ' data "// fragment shader" data "varying vec3 v;" data "varying vec3 lightvec;" data "varying vec3 normal;" data "varying vec4 FrontColor;" data "uniform sampler2D Texture0;" data "void main(void) {" data " vec3 Eye = normalize(-v);" data " vec3 Reflected = normalize(reflect( -lightvec, normal)); " data " vec4 IAmbient = gl_LightSource[0].ambient * gl_FrontMaterial.ambient;" data " vec4 IDiffuse = gl_LightSource[0].diffuse * max(dot(normal, lightvec), 0.0) * gl_FrontMaterial.diffuse;" data " vec4 ISpecular = gl_LightSource[0].specular * pow(max(dot(Reflected, Eye), 0.0), gl_FrontMaterial.shininess) * gl_FrontMaterial.specular;" data " gl_FragColor = vec4((gl_FrontLightModelProduct.sceneColor + IAmbient + IDiffuse) * texture2D(Texture0, vec2(gl_TexCoord[0])) + ISpecular);}" data "" ' <- end of fragment code
declare function CreateCube() declare function CreateShaders(VertexCode$,FragmentCode$)
dim LF$=chr$(13)+chr$(10) ' if ShaderInit()=False then print "error: no shader support !" input$():end end if ' dim line$ ' ' read vertex shader code in a string dim VCode$ do read line$:VCode$=VCode$+Line$+LF$ loop until line$="" ' ' read fragment shader code in a string dim FCode$ do read Line$:FCode$=FCode$+Line$+LF$ loop until line$="" ' create program object from shader codes dim ProgramObj=CreateShaders(VCode$,FCode$) if ProgramObj=0 then print "error: CreatreShaders() !" end end if
' dim CubeTex = LoadTex(".\media\Basic4GL.jpg") glBindTexture(GL_TEXTURE_2D, CubeTex)
glEnable(GL_LIGHT0) glLightfv(GL_LIGHT0,GL_AMBIENT ,vec4(0.2,0.2,0.2,1.0)) glLightfv(GL_LIGHT0,GL_DIFFUSE ,vec4(1.0,1.0,1.0,1.0)) glLightfv(GL_LIGHT0,GL_SPECULAR,vec4(1.0,0.6,0.8,1.0)) glLightfv(GL_LIGHT0,GL_POSITION,vec4(0,10,0,1)) ' we use our own lightning :-) glDisable(GL_LIGHTING) ' dim y#,frames ' ' create textured cube dim CubeID=CreateCube() ' ' disable fps limit 'wglSwapIntervalEXT(FALSE)
'now use it glUseProgramObjectARB(ProgramObj)
while inkey$()<>"q" frames=frames+1
glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT) glLoadIdentity() glLightfv(GL_LIGHT0,GL_POSITION,vec4(0,0,0,1))
glTranslatef(0,0,-8) glRotatef(y#*1,1,0,0) glRotatef(y#*2,0,1,0) glRotatef(y#*3,0,0,1)
glBindTexture(GL_TEXTURE_2D,CubeTex) glScalef(2,2,2) glCallList(CubeID) SwapBuffers() WaitTimer(1000/50) y#=y#+0.1 wend ' ' disable the shader program glUseProgramObjectARB(0) ' ' free the program object glDeleteObjectARB(ProgramObj) ' end
' create shaders function CreateShaders(VertexCode$,FragmentCode$) dim hVertex=-1,hFragment=-1,hProgram=-1 ' create a vertex shader if len(VertexCode$) then hVertex=glCreateShaderObjectARB(GL_VERTEX_SHADER) glShaderSourceARB(hVertex,VertexCode$) glCompileShaderARB(hVertex) if CompileStatus(hVertex)=0 then return false end if end if ' create a fragment shader if len(FragmentCode$) then hFragment=glCreateShaderObjectARB(GL_FRAGMENT_SHADER) glShaderSourceARB(hFragment,FragmentCode$) glCompileShaderARB(hFragment) if CompileStatus(hFragment)=0 then return False end if end if ' create program object hProgram=glCreateProgramObjectARB() ' attach shaders if hVertex<>-1 then glAttachObjectARB(hProgram,hVertex) glDeleteObjectARB(hVertex) end if if hFragment<>-1 then glAttachObjectARB(hProgram,hFragment) glDeleteObjectARB(hFragment) end if glLinkProgramARB(hProgram) return hProgram end function
' ######################################## ' # create a unit 1 x 1 x 1 textured box # '######################################## function CreateCube() dim ret = glGenLists(1) glNewList(ret,GL_COMPILE)
const m#=-0.5 const p#= 0.5 glBegin(GL_QUADS) glMaterialfv(GL_FRONT,GL_AMBIENT ,vec4(0.2, 0.2, 0.2, 1.0)) glMaterialfv(GL_FRONT,GL_DIFFUSE ,vec4(0.5, 0.5, 0.0, 1.0)) glMaterialfv(GL_FRONT,GL_SPECULAR,vec4(1,1,1,1)) glMaterialfv(GL_FRONT,GL_EMISSION,vec4(0,0,0,1)) glMateriali (GL_FRONT,GL_SHININESS,100)
' Front Face glNormal3f( 0.0, 0.0, 1.0) glTexCoord2f(0.0, 0.0): glVertex3f(m#, m#, p#) glTexCoord2f(1.0, 0.0): glVertex3f(p#, m#, p#) glTexCoord2f(1.0, 1.0): glVertex3f(p#, p#, p#) glTexCoord2f(0.0, 1.0): glVertex3f(m#, p#, p#) ' Back Face glNormal3f( 0.0, 0.0,-1.0) glTexCoord2f(1.0, 0.0): glVertex3f(m#, m#, m#) glTexCoord2f(1.0, 1.0): glVertex3f(m#, p#, m#) glTexCoord2f(0.0, 1.0): glVertex3f(p#, p#, m#) glTexCoord2f(0.0, 0.0): glVertex3f(p#, m#, m#) ' Top Face glNormal3f( 0.0, 1.0, 0.0) glTexCoord2f(0.0, 1.0): glVertex3f(m#, p#, m#) glTexCoord2f(0.0, 0.0): glVertex3f(m#, p#, p#) glTexCoord2f(1.0, 0.0): glVertex3f(p#, p#, p#) glTexCoord2f(1.0, 1.0): glVertex3f(p#, p#, m#) ' Bottom Face glNormal3f( 0.0,-1.0, 0.0) glTexCoord2f(1.0, 1.0): glVertex3f(m#, m#, m#) glTexCoord2f(0.0, 1.0): glVertex3f(p#, m#, m#) glTexCoord2f(0.0, 0.0): glVertex3f(p#, m#, p#) glTexCoord2f(1.0, 0.0): glVertex3f(m#, m#, p#) ' Right face glNormal3f( 1.0, 0.0, 0.0) glTexCoord2f(1.0, 0.0): glVertex3f( p#, m#, m#) glTexCoord2f(1.0, 1.0): glVertex3f( p#, p#, m#) glTexCoord2f(0.0, 1.0): glVertex3f( p#, p#, p#) glTexCoord2f(0.0, 0.0): glVertex3f( p#, m#, p#) ' Left Face glNormal3f(-1.0, 0.0, 0.0) glTexCoord2f(0.0, 0.0): glVertex3f(m#, m#, m#) glTexCoord2f(1.0, 0.0): glVertex3f(m#, m#, p#) glTexCoord2f(1.0, 1.0): glVertex3f(m#, p#, p#) glTexCoord2f(0.0, 1.0): glVertex3f(m#, p#, m#) glEnd()
glEndList() return ret end function
[/size]
|
|
|
Post by DJLinux on Apr 29, 2010 23:45:03 GMT -5
whats up
you got it ?
Joshy
|
|
|
Post by shadow008 on Apr 30, 2010 7:10:57 GMT -5
yeah, but i try to run on schools comps and "no gl_arb_shader_support".... try when i get home
|
|
|
Post by shadow008 on May 3, 2010 11:26:23 GMT -5
Ha, that's awesome. I wonder how extensively i could use this before it gets too slow....
|
|
|
Post by DJLinux on May 3, 2010 12:15:23 GMT -5
Ha, that's awesome. I wonder how extensively i could use this before it gets too slow.... I don't see any problem. You disable default OpenGL Lightning and you replace with shaders the real part of the render pipelines. Not all Lights in your 3D world should produce per pixel lightning. As an good programmer you should test on program start if shaders are supported (return code ARBShader commands) If you found it you can render 30-60 hidden frames (without SwapBuffers()) count the frame rate (FPS) if no shadersupport or fps to low use "normal" OpenGL Lightning and Color Materils. bla bla bla Joshy
|
|
|
Post by shadow008 on May 3, 2010 14:21:00 GMT -5
Ha, that's awesome. I wonder how extensively i could use this before it gets too slow.... I don't see any problem. You disable default OpenGL Lightning and you replace with shaders the real part of the render pipelines. Not all Lights in your 3D world should produce per pixel lightning. As an good programmer you should test on program start if shaders are supported (return code ARBShader commands) If you found it you can render 30-60 hidden frames (without SwapBuffers()) count the frame rate (FPS) if no shadersupport or fps to low use "normal" OpenGL Lightning and Color Materils. bla bla bla Joshy that sums up exactly what i was thinking. However, if a computer doesnt support the fragment/vertex shaders then it doesnt even load, you get a popup message saying: "Error initializing plugin arbshaderplugin.dll: no GL_ARB_shader_support !" and the code doesnt even run... sooooooo... theres really no way of testing shader support in code if the computer doesnt support it.... damn intel graphics cards....
|
|
|
Post by shadow008 on May 3, 2010 17:13:25 GMT -5
hmmm.... after looking it over, i realized this isnt quite what i wanted. I wanted something like this: where a spotlight could illuminate the box on more than one face at a time. Any suggestions? Or does that only work with pixel shaders? Edit: stupid images... never working the way i want them to.... click
|
|
|
Post by shadow008 on May 10, 2010 13:49:01 GMT -5
i figure this question wont be answered if it werent for me posting an update, sooooo.....
|
|
|
Post by DJLinux on May 10, 2010 18:53:38 GMT -5
Sorry I don't write shaders for you. You must learn C and read ton's of Shader specs.
Joshy
|
|