Jump to content
  • Advertisement
Sign in to follow this  
Shawn619

OpenGL OpenGL shader problem

This topic is 2287 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 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 ->
21k0bw2.jpg
If it helps, here's my .exe directory ->
10f80m8.jpg

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
I apologize, here is the render context ->

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;

Share this post


Link to post
Share on other sites
Ah, double post (http://www.gamedev.net/topic/623431-opengl-shader-problem/).

glewInit should be after you create the window.

Share this post


Link to post
Share on other sites
Whoops! If a mod could delete the other post i'd be grateful.

Having the glewInit() called after like you suggested:

//create the window
glutCreateWindow("Lighting");
initRendering();

glewInit();

//set handler functions
...


Produces the same error unforunetly

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ->



1gsv2c.jpg


This is the new shader implemented, wanting to produce PPL ->
15qaiaw.jpg


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);
}

Share this post


Link to post
Share on other sites
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 ->

v8gth.jpg

.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;
}

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!