I tried implementing a shader into my program.
The shader is from an example program from http://www.swiftless...g_perpixel.html
I included all the necessary files, shader.cpp, shadder.h, i have shader.vert and shader.frap in the appropriate directories. Also i have glew32.lib linked appropriately as well, just as in the example on the website.
Running my program, i get this error ->
If it helps, here's my .exe directory ->
OpenGL shader problem
Do you call [font=courier new,courier,monospace]glewInit()[/font]? Is [font=courier new,courier,monospace]shader_vp [/font]a [font=courier new,courier,monospace]GLuint[/font]? It's incredibly hard to say what's going on without seeing more of the code.
I apologize, here is the render context ->
main.cpp
I pulled the shader.cpp and .h files directly from the swiftless.com.
[font=courier new,courier,monospace][font="Arial"]"[/font]shader_vp [/font]a [font=courier new,courier,monospace]GLuint[/font]?" - I found in the header that it is an unsigned int
main.cpp
#include <stdlib.h>
#include <SOIL.h>
#include <string>
#include <iostream>
#include "shader.h"
using namespace std;
Shader shader;
void init(){
...
shader.init("shader.vert", "shader.frag");
...
}
void draw(){
...
shader.bind();
//draw stuff
shader.unbind();
...
}
int main(){
//init glut
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT RGB | GLUT_DEPTH);
glutIinitWindowSize(800,600);
//init glew
glewInit();
//create the window
glutCreateWindow("Lighting")
initRendering();
//set handler functions
...
//glut timer
...
glutMainLoop();
return 0;
}
I pulled the shader.cpp and .h files directly from the swiftless.com.
[font=courier new,courier,monospace][font="Arial"]"[/font]shader_vp [/font]a [font=courier new,courier,monospace]GLuint[/font]?" - I found in the header that it is an unsigned int
unsigned int shader_id;
unsigned int shader_vp;
unsigned int shader_fp;
Ah, double post (http://www.gamedev.net/topic/623431-opengl-shader-problem/).
glewInit should be after you create the window.
glewInit should be after you create the window.
Whoops! If a mod could delete the other post i'd be grateful.
Having the glewInit() called after like you suggested:
Produces the same error unforunetly
Having the glewInit() called after like you suggested:
//create the window
glutCreateWindow("Lighting");
initRendering();
glewInit();
//set handler functions
...
Produces the same error unforunetly
That is not after window creation, that is after "initRendering" :/.
If the offending "glCreateShader" is called anywhere in there then it would obviously guarantee a crash as without "initGlew" almost all of the gl* functions are pointing nowhere.
If the offending "glCreateShader" is called anywhere in there then it would obviously guarantee a crash as without "initGlew" almost all of the gl* functions are pointing nowhere.
First of all i'd like to thank everyone for their help!
glewInit() was the issue, and i wouldn't have known about it without your help.
@tanzanite7 Your right, the problem is with the placement of glewInit(). glewInit() needs to be called before " shader.init("shader.vert", "shader.frag"); ", not after.
Good news - the shader now compiles.
Bad news - the shader isn't performing correctly.
---------------------------------------------------------------------------
This is the default opengl per-vertex shading. It may look like decent lighting, however, it is extremely jumpy when moving the light and completely unreliable ->
This is the new shader implemented, wanting to produce PPL ->
The light is simply a spot-light positioned over the green cube. I dont know why the shader produced all black and gray pixels.
Here are the .vert and .frag files used in the shader->
.vert
.frag
glewInit() was the issue, and i wouldn't have known about it without your help.
@tanzanite7 Your right, the problem is with the placement of glewInit(). glewInit() needs to be called before " shader.init("shader.vert", "shader.frag"); ", not after.
Good news - the shader now compiles.
Bad news - the shader isn't performing correctly.
---------------------------------------------------------------------------
This is the default opengl per-vertex shading. It may look like decent lighting, however, it is extremely jumpy when moving the light and completely unreliable ->
This is the new shader implemented, wanting to produce PPL ->
The light is simply a spot-light positioned over the green cube. I dont know why the shader produced all black and gray pixels.
Here are the .vert and .frag files used in the shader->
.vert
varying vec3 lightDir,normal;
void main()
{
normal = normalize(gl_NormalMatrix * gl_Normal);
lightDir = normalize(vec3(gl_LightSource[0].position));
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
.frag
varying vec3 lightDir,normal;
uniform sampler2D tex;
void main()
{
vec3 ct,cf;
vec4 texel;
float intensity,at,af;
intensity = max(dot(lightDir,normalize(normal)),0.0);
cf = intensity * (gl_FrontMaterial.diffuse).rgb +
gl_FrontMaterial.ambient.rgb;
af = gl_FrontMaterial.diffuse.a;
texel = texture2D(tex,gl_TexCoord[0].st);
ct = texel.rgb;
at = texel.a;
gl_FragColor = vec4(ct * cf, at * af);
}
I also found another vert+frag shader that everything almost perfect, except the shader has no awareness of the original color/texture so you can see it just turns the plane black ->
.vert
.frag
.vert
varying vec3 N;
varying vec3 v;
void main(void)
{
v = vec3(gl_ModelViewMatrix * gl_Vertex);
N = normalize(gl_NormalMatrix * gl_Normal);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
.frag
varying vec3 N;
varying vec3 v;
void main(void)
{
vec3 L = normalize(gl_LightSource[0].position.xyz - v);
vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(N,L), 0.0);
Idiff = clamp(Idiff, 0.0, 1.0);
gl_FragColor = Idiff;
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement