Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 06 May 2012
Offline Last Active Mar 18 2015 10:52 AM

Topics I've Started

[C++, OpenGL, GLSL] Point light lits up objects like a directional light.

21 August 2014 - 07:39 AM

Hello everybody.


As I said in the topic, I have a problem with my shader for point lights. I followed a ton of tutorials about phong lighting and per fragment point lighting. Still, after all these tutorials, I still have this problem:



The light only lits up objects in the positive X direction.


Here is my shader code:


#version 120

varying vec2 texCoord;
varying vec3 position;
varying vec3 normal;
varying vec3 tangentSurf2light;
varying vec3 tangentSurf2view;

uniform vec3 lightPos;

void main()
	gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
	position = vec3(gl_ModelViewMatrix * gl_Vertex);
	normal = normalize(gl_NormalMatrix * gl_Normal);
	vec3 tangent;
	vec3 v1=cross(gl_Normal,vec3(0.0,0.0,-1.0));
	vec3 v2=cross(gl_Normal,vec3(0.0,-1.0,0.0));
	vec3 t = normalize(gl_NormalMatrix * tangent);
	vec3 b = cross(normal,t);
	mat3 mat = mat3(t.x,b.x,normal.x,
	vec4 var = vec4(lightPos,1.0);
	vec3 lightLoc = vec3(gl_ModelViewMatrix * var);
	vec3 vector = normalize(lightLoc-position);

#version 120

uniform sampler2D texture;
uniform sampler2D normalmap;
uniform sampler2D specularmap;

uniform vec3 lightPos;

varying vec2 texCoord;
varying vec3 position;
varying vec3 normal;

uniform vec3 matDiffuse;
uniform vec3 matSpecular;

uniform vec3 lightAmbient;
uniform vec3 lightDiffuse;
uniform vec3 lightSpecular;

varying vec3 tangentSurf2light;
varying vec3 tangentSurf2view;

void main(){
	vec4 var = vec4(lightPos,1.0);
	vec3 lightLoc = vec3(gl_ModelViewMatrix * var);

	float dist=length(position-lightLoc);
	float att=1.0/(1.0+0.1*dist+0.01*dist*dist);
	vec3 ambient = vec3(texture2D(texture,texCoord)) * lightAmbient;
	vec3 surf2light = normalize(tangentSurf2light);
	vec3 norm = normalize(texture2D(normalmap,texCoord).xyz*2.0-1.0);
	float dcont = max(0.0, dot(norm,surf2light));
	vec3 diffuse = dcont * vec3(texture2D(texture,texCoord)) * lightDiffuse;
	vec3 surf2view = normalize(tangentSurf2view);
	vec3 reflection = reflect(-surf2light,norm);
	float scont = pow(max(0.0,dot(surf2view,reflection)),texture2D(specularmap,texCoord).x*50);
	vec3 specular = scont*lightSpecular*matSpecular*att;
	gl_FragColor = vec4(ambient+diffuse+specular,1.0);

Thanks in advance!

Cutting of a piece texture with a different texture.

11 April 2014 - 09:20 AM

Hello everybody!


Okay, let's say that my application screen looks like that:





And I want it to look like this:




The scenery at the first picture contains of two planes on top of each other that have different textures.


What I want is the thing that happened at the second picture. It want it to cut the part of the plane wherever I want based of this texture:



I am using SDL for handling events, and OpenGL for drawing.


Thanks in advance smile.png.


Need help: a vector of different extended classes

15 March 2014 - 09:58 AM

Hello there.


I want to make a list of entities that are gonna be updated every tick. So, I created a class named Entity, and gave it a couple of functions:

class Entity{
        int uniqueID;

        Vec3f loc;
        Vec2f res;
        Vec2f vel;
        float rot;

        virtual void onUpdate();
        virtual void onSpawned();
        virtual string getNameID();
        vector<GameData> gameData;

Then, I created a vector of these entities:

vector<Entity> entities;

After that, I wanted an arrow entity, so I created a new class EntityArrow that is an extension of Entity:

class EntityArrow : public Entity{
        EntityArrow(Vec3f argLoc,Vec2f argRes);
        string getNameID();
        void onUpdate();
        void onSpawned();

And now comes the problem, when I try to add a new EnttiyArrow to the entities list: entities.push_back(EntityArrow()); it is still treated as an Entity. For instance, if I overrided the onUpdate() function in EntityArrow to print out something in the console, it would do something that is in the Entity's onUpdate() , and not the EntityArrow's. I have already tried to make a vector out of references to Entity class : vector<Entity*> entities , but I still have a lot of problems with it.


Is there a better way to do it? Thanks in advance :).

In need of an algorythm!

10 January 2014 - 12:10 PM


The picture shows a point and a line in a 2d space. Does someone know what algorythm do I use to calculate the '?' in the picture? It is the distance between the 'point' and the line, but the angle between the line and the "distance line" has to be 90 degrees. I want to use that in my physics engine. And can someone help me find the 3D equivalent of that as well?


Thanks in advance!



[HELP] Texture Problems And Low FPS

02 August 2013 - 03:17 AM

Hello everyone. My problem is that on my computer, eveything works fine, I mean, all of the textures are working, fps is great and all. Here comes the problem... on other computers, it doesn't, some of the textures aren't working, and FPS is horrible. The texture thing is probably a power of two thingy, cuz every power of two resolution texture is working, but others are not. What is wrong with the FPS's?


Drawing: OpenGL

Texture Loading: DevIL

Language: C++


Thanks in advance!