Jump to content
  • Advertisement
Sign in to follow this  
tororo

OpenGL Bump mapping with GLSL shader

This topic is 3779 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 create the Programmerble shader. http://blogfile.paran.com/BLOG_244808/200805/1213407824_bumpcode.zip this is my full source code. (make Visual Studio C++ 2005) 1. load tga file 2. set texture 3. bump set 4. draw. but my result... like cross stripes pattern. http://blogfile.paran.com/BLOG_244808/200805/1213406142_q.JPG hum~ why my shader that result? vertex shader code : ------------------------------------------------------------`
varying vec3 L;
varying vec3 V;
attribute vec3 objTangent;
void main()
{
	gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
	
	gl_TexCoord[0]= gl_MultiTexCoord0;
	vec3 eyePosition = vec3(gl_ModelViewMatrix*gl_Vertex);
	vec3 eyeLightPos = vec3(gl_LightSource[0].position);
    
    vec3 N = normalize(gl_NormalMatrix*gl_Normal);
    vec3 T = normalize(gl_NormalMatrix*objTangent);
    vec3 B = cross(N, T);

    L.x = dot(T, eyeLightPos-eyePosition);
    L.y = dot(B, eyeLightPos-eyePosition);
    L.z = dot(N, eyeLightPos-eyePosition);


    L = normalize(L);


    V.x = dot(T, -eyePosition.xyz);
    V.y = dot(B, -eyePosition.xyz);
    V.z = dot(N, -eyePosition.xyz);
    V = normalize(V);
}

------------------------------------------------------------ fragment shader code : ------------------------------------------------------------
varying vec3 L;
varying vec3 V;
uniform sampler2D texMap;


void main()
{ 

   vec4 N = texture2D(texMap, gl_TexCoord[0].st);

   vec3 NN =  normalize(2.0*N.xyz-1.0);
   vec3 LL = normalize(L);
   
   float Kd = max(dot(NN, LL), 0.0);
   gl_FragColor = Kd*gl_FrontLightProduct[0].diffuse;
}

------------------------------------------------------------ and... main code ------------------------------------------------------------ ------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>
#include <windows.h>

#include "TargaImage.h"

#include <gl/glew.h> // shader set&#51012; &#50948;&#54620; &#44592;&#48376;
#include <gl/wglew.h> // lib&#47484; &#47553;&#53356; &#49884;&#53020;&#51480;&#50556;&#54620;&#45796;. &#51060;&#51216; &#49345;&#44592;&#54616;&#46020;&#47197;

#include <gl/glut.h>

//#include "readbmp.h"

GLfloat  lightPos[4] = {0.0, 10.0, 10.0, 1.0};

GLuint      program     = 0;
GLuint BMapDecalTex;
GLuint tangentParam	= 0;

 

static void checkError(GLint status, const char *msg)
{
	if (status == GL_FALSE)
	{
		printf("%s\n", msg);
		exit(EXIT_FAILURE);
	}
}

static char* readShaderSource(const char* shaderFile)
{
	struct stat statBuf;
	FILE* fp = fopen(shaderFile, "rb");
	char* buf;

	stat(shaderFile, &statBuf);
	if(fp==NULL) return NULL;
	buf = (char*) malloc((statBuf.st_size + 1) * sizeof(char));

	int i, j;
	for(i=0, j=0; i< statBuf.st_size; i++, j++) 
	{	
		buf[j] = fgetc(fp);
		if(buf[j]=='\r') j--;
	}
	buf[j] = NULL;
	fclose(fp);

	return buf;
}

static void initShader(const GLchar* vShaderFile, const GLchar* fShaderFile)
{
	GLint status; 
	GLchar *vSource, *fSource;
	GLuint vShader, fShader;

	/* read shader files */
	vSource = readShaderSource(vShaderFile);
	if(vSource==NULL)
	{
		printf( "Failed to read vertex shaderi\n");
		exit(EXIT_FAILURE);
	}

	fSource = readShaderSource(fShaderFile);
	if(fSource==NULL)
	{
		printf("Failed to read fragment shader");
		exit(EXIT_FAILURE);
	}

	/* create program and shader objects */
	vShader = glCreateShader(GL_VERTEX_SHADER);
	fShader = glCreateShader(GL_FRAGMENT_SHADER);


	/* attach shaders to the program object */


	/* read shaders */
	glShaderSource(vShader, 1, (const GLchar**) &vSource, NULL);
	glShaderSource(fShader, 1, (const GLchar**) &fSource, NULL);

	/* compile shaders */
	glCompileShader(vShader);
	glCompileShader(fShader);

	/* error check */
	glGetShaderiv(vShader, GL_COMPILE_STATUS, &status);
	checkError(status, "Failed to link the shader program object.");
	glGetShaderiv(fShader, GL_COMPILE_STATUS, &status);
	checkError(status, "Failed to compile the fragment shader.");


	program = glCreateProgram();	
	/* use program object */
	glAttachShader(program, vShader);
	glAttachShader(program, fShader);


	/* link */
	glLinkProgram(program);
	glGetProgramiv(program, GL_LINK_STATUS, &status);
	glUseProgram(program);
	checkError(status, "Failed to link the shader program object.");

	tangentParam = glGetAttribLocation(program, "objTangent");
	BMapDecalTex = glGetUniformLocationARB(program,"DecalTex"); 
}



static void init()
{

	CTargaImage image;        /* &#52628;&#44032; */  
	image.Load("normal2.tga"); /* &#52628;&#44032; */

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.GetWidth(), image.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());  /* &#52628;&#44032; */
	image.Release();          /* &#52628;&#44032; */
    
	glEnable(GL_TEXTURE_2D);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -5.5, 5.5);

    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
}

void mesh()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(3.0, 1.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

	glutSolidTeapot(0.5);

	glPushMatrix();
	glTranslatef(1,0,1);
	glNormal3f(0.0,1.0,0.0);                                      /* &#52628;&#44032; */
	glBegin(GL_QUADS);
       glTexCoord2f(0.0, 0.0);
       glVertex3f(0.0, 0.0, 0.0);
       glTexCoord2f(1.0, 0.0);
       glVertex3f(1.0, 0.0, 0.0);
       glTexCoord2f(1.0, 1.0);
       glVertex3f(1.0,0.0,1.0);
       glTexCoord2f(0.0, 1.0);
       glVertex3f(0.0,0.0,1.0);
    glEnd();
	glPopMatrix();
}

static void draw(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    mesh(); 

    glutSwapBuffers();
}

static void reshape(int w, int h)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	glOrtho(-1.0, 1.0, -1.0, 1.0, -5.5, 5.5);

    glViewport(0, 0, w, h);
    glutPostRedisplay();
}

static void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
    case 27:
    case 'Q':
    case 'q':
        exit(EXIT_SUCCESS);
        break;
    case '1':
         lightPos[0] = 10.0;
         lightPos[1] = 10.0;
         lightPos[2] = 0.0;
		 glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
         glutPostRedisplay();
         break;
    case '2':
         lightPos[0] = 7.0;
         lightPos[1] = 10.0;
         lightPos[2] = 7.0;
		 glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
         glutPostRedisplay();
         break;
    default:
        break;
    }
}



void idle(void)
{
   int t;

   t = glutGet(GLUT_ELAPSED_TIME);
   lightPos[0] = 5.5*sin(0.001*t);
   lightPos[2] = 5.5*cos(0.001*t);
   glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
   glutPostRedisplay();
}



int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(800, 800);
    glutCreateWindow("Bump Mapping");
    glutDisplayFunc(draw);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(idle);

	init();

	// glsl set part
	//
	// &#51060; &#44396;&#47928;&#51032; set&#51032; window&#44032; &#47784;&#46160; set&#46108; &#51060;&#54980;&#50640; mainloop&#51060;&#51204;&#50640; &#46308;&#50612;&#44032;&#50556;&#54620;&#45796;.
	//
	// gl 2.0&#51012; &#51648;&#50896;&#54616;&#51648; &#50506;&#45716;&#44032;&#47484; &#52404;&#53356;.
	// &#51201;&#50612;&#46020; nvidia geforce 7000&#45824;&#45716; &#46104;&#50612;&#50556;&#54620;&#45796;.
	if(glewInit()!=GLEW_OK)
		exit(1);
	if (glewIsSupported("GL_VERSION_2_0"))
		printf("Ready for OpenGL 2.0\n");
	else {
		printf("OpenGL 2.0 not supported\n");
		exit(1);
	}       
	initShader("vbump.glsl", "fbump3.glsl");

	glutMainLoop();

    return 0;
}



------------------------------------------------------------ ------------------------------------------------------------ [Edited by - tororo on June 13, 2008 8:50:12 PM]

Share this post


Link to post
Share on other sites
Advertisement
The most noticeable mistake is your image loading function is wrong. It just failed to read the image data correctly.

By the way, the implementation of your bump mapping is probably not correctly.

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!