Hello Sir,Ma'am,

I have a Qt project of a rotating cube using shaders. I want to add the
light effect to it.
How do I combine the two shader files into one mainWindow?

also these fragment and vertex files are in .frag and .vs while, the one
which I have are in .glsl (both). Is it a problem?

Attaching the lighting code which i got online with this mail.

thanks in advance.

Sincere Regards,
Amey Patil
//http://www.youtube.com/user/thecplusplusguy
//GLSL tutorial - per-fragment (per-pixel lighting)
//fragment.frag
#version 120
varying vec3 position;
varying vec3 normal;

uniform vec3 lightPos;

uniform vec3 mambient;  //gl_FrontMaterial
uniform vec3 mdiffuse;
uniform vec3 mspecular;
uniform float shininess;

uniform vec3 lambient;  //gl_LightSource[0]
uniform vec3 ldiffuse;
uniform vec3 lspecular;


void main()
{
        float dist=length(position-lightPos);   //distance from light-source to 
surface
        float att=1.0/(1.0+0.1*dist+0.01*dist*dist);    //attenuation 
(constant,linear,quadric)
        vec3 ambient=mambient*lambient; //the ambient light
        
        vec3 surf2light=normalize(lightPos-position);
        vec3 norm=normalize(normal);
        float dcont=max(0.0,dot(norm,surf2light));
        vec3 diffuse=dcont*(mdiffuse*ldiffuse);
        
        vec3 surf2view=normalize(-position);
        vec3 reflection=reflect(-surf2light,norm);
        
        float scont=pow(max(0.0,dot(surf2view,reflection)),shininess);
        vec3 specular=scont*lspecular*mspecular;
        
        gl_FragColor=vec4((ambient+diffuse+specular)*att,1.0);  //<- don't 
forget the paranthesis (ambient+diffuse+specular)
}
//http://www.youtube.com/user/thecplusplusguy
//GLSL tutorial - per-fragment (per-pixel lighting)
//main.cpp
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "GLee.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include "camera.h"
#include <vector>
#include <string>
#include <fstream>
//#include "objloader.h"
float angle=0.0;
camera cam;
//objloader obj;
//data->vertex->fragment

void loadFile(const char* fn,std::string& str)
{
	std::ifstream in(fn);
	if(!in.is_open())
	{
		std::cout << "The file " << fn << " cannot be opened\n";
		return;
	}
	char tmp[300];
	while(!in.eof())
	{
		in.getline(tmp,300);
		str+=tmp;
		str+='\n';
	}
}

unsigned int loadShader(std::string& source,unsigned int mode)
{
	unsigned int id;
	id=glCreateShader(mode);
	
	const char* csource=source.c_str();
	
	glShaderSource(id,1,&csource,NULL);
	glCompileShader(id);
	char error[1000];
	glGetShaderInfoLog(id,1000,NULL,error);
	std::cout << "Compile status: \n" << error << std::endl;
	return id;
}

unsigned int vs,fs,program;

void initShader(const char* vname,const char* fname)
{
	std::string source;
	loadFile(vname,source);
	vs=loadShader(source,GL_VERTEX_SHADER);
	source="";
	loadFile(fname,source);
	fs=loadShader(source,GL_FRAGMENT_SHADER);
	
	program=glCreateProgram();
	glAttachShader(program,vs);
	glAttachShader(program,fs);
	
	glLinkProgram(program);
	glUseProgram(program);
}

void clean()
{
	glDetachShader(program,vs);
	glDetachShader(program,fs);
	glDeleteShader(vs);
	glDeleteShader(fs);
	glDeleteProgram(program);
}

unsigned int loadTexture(const char* filename)
{
	unsigned int num;
	glGenTextures(1,&num);
	SDL_Surface* img=IMG_Load(filename);
	if(img==NULL)
	{
		std::cout << "img was not loaded" << std::endl;
		return -1;
	}
	SDL_PixelFormat form={NULL,32,4,0,0,0,0,0,0,0,0,0xff000000,0x00ff0000,0x0000ff00,0x000000ff,0,255};
	SDL_Surface* img2=SDL_ConvertSurface(img,&form,SDL_SWSURFACE);
	if(img2==NULL)
	{
		std::cout << "img2 was not loaded" << std::endl;
		return -1;		
	}
	glBindTexture(GL_TEXTURE_2D,num);		
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,img2->w,img2->h,0,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8,img2->pixels);		
	SDL_FreeSurface(img);
	SDL_FreeSurface(img2);
	return num;
}

unsigned int myImg,myImg2/*,monkey*/;

void init()
{
	glClearColor(0,0,0,1);
	glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(50,640.0/480.0,1,1000);
	glMatrixMode(GL_MODELVIEW);
	glEnable(GL_DEPTH_TEST);
	initShader("vertex.vs","fragment.frag");
	myImg=loadTexture("brick.jpg");
	myImg2=loadTexture("concrete.jpg");
	//monkey=obj.load("monkey.obj");
}

void display()
{
	glLoadIdentity();
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	cam.Control();
	cam.UpdateCamera();
	angle+=3.0;
	if(angle>=360)
		angle-=360;
	
	glUniform3f(glGetUniformLocation(program,"lightPos"),0.0,0.0,0.0);	//light position (is the same as the player position)
	
	glUniform3f(glGetUniformLocation(program,"mambient"),0.2,0.2,0.2);	//setting the material property
	glUniform3f(glGetUniformLocation(program,"mdiffuse"),0.6,0.6,0.6);
	glUniform3f(glGetUniformLocation(program,"mspecular"),1.0,1.0,1.0);
	
	glUniform3f(glGetUniformLocation(program,"lambient"),0.2,0.2,0.2);	//setting light property
	glUniform3f(glGetUniformLocation(program,"ldiffuse"),0.6,0.6,0.6);
	glUniform3f(glGetUniformLocation(program,"lspecular"),1.0,1.0,1.0);
		
	glUniform1f(glGetUniformLocation(program,"shininess"),32.0);	//shininess
	
//	glRotatef(angle,0,1,0);
//	glCallList(monkey);
// /*
	glBegin(GL_QUADS);
		glNormal3f(0.0,0.0,1.0);
		glTexCoord2f(1,0);		//gl_MultiTexCoord0
		glVertex3f(-1,1,-4);	//gl_Vertex
		glTexCoord2f(0,0);
		glVertex3f(-1,-1,-4);
		glTexCoord2f(0,1);
		glVertex3f(1,-1,-4);
		glTexCoord2f(1,1);
		glVertex3f(1,1,-4);
	glEnd();
// */
}

int main()
{
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_SetVideoMode(640,480,32,SDL_OPENGL);
	Uint32 start;
	SDL_Event event;
	bool running=true;
	init();
	bool b=false;
	while(running)
	{
		start=SDL_GetTicks();
		while(SDL_PollEvent(&event))
		{
			switch(event.type)
			{
				case SDL_QUIT:
					running=false;
					break;
				case SDL_KEYDOWN:
					switch(event.key.keysym.sym)
					{
						case SDLK_ESCAPE:
							running=false;
							break;
					}
					break;
				case SDL_MOUSEBUTTONDOWN:
					cam.mouseIn(true);
					break;
					
			}
		}
		display();
		SDL_GL_SwapBuffers();
		if(1000.0/30>SDL_GetTicks()-start)
			SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
	}
	clean();
	SDL_Quit();
}
//http://www.youtube.com/user/thecplusplusguy
//GLSL tutorial - per-fragment (per-pixel lighting)
//vertex.vs
#version 120
varying vec3 position;
varying vec3 normal;

void main()
{
	gl_Position=gl_ModelViewProjectionMatrix*gl_Vertex;	//output position with projection
	position=vec3(gl_ModelViewMatrix*gl_Vertex);	//get the position of the vertex after translation, rotation, scaling
	normal=gl_NormalMatrix*gl_Normal;	//get the normal direction, after translation, rotation, scaling
}
_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to