Sign in to follow this  
acchuthan76

OpenGL Vertex illumination formulas

Recommended Posts

acchuthan76    122
Hi! All, can anyone help me how i can add textures and lightings by using illumination formula for each vertex. I am new to OpenGL and need someone to help me. This is how my programe looks, i am loading the model from a .m model file. Initially i got a problem on loading the m files , later i solved it, now i am trying to add textures and lighting on the model. COuld you help me to learn. Thanks you in advance /*---------------------------------------------------------------------------- MENUS - RIGHT MOUSE BUTTON ROTATE - LEFT MOUSE BUTTON TRANSLATE - MIDDLE MOUSE BUTTON ZOOM IN/OUT - ALT + MIDDLE MOUSE BUTTON KEYBOARD INPUTS 'w' - WIREFIRM 's' - SOLID 'e' - WIREFRAME + SOLID 'f' - SHADING FLAT 'O' - SHADING SMOOTH 'l' - LIGHTS 'P' - VIEW PERSPECTIVE OR ORTHO ---------------------------------------------------------------------------*/ #include <iostream> #include <GL/glut.h> #include <cstdlib> #include <vector> #include <string> using namespace std; #define TRANSFORM_NONE 0 #define TRANSFORM_ROTATE 1 #define TRANSFORM_SCALE 2 #define TRANSFORM_TRANSLATE 3 #define OBJ_WIREFRAME 0 #define OBJ_SOLID 1 #define OBJ_EDGE 2 #define OBJ_FLAT_SHADING 3 #define OBJ_SMOOTH_SHADING 4 #define OBJ_NORM 5 #define ON 1 #define OFF 0 static int LIGHTING=OFF; static int PERSPECTIVE = ON; static int SMOOTH_SHADING = ON; static char TITLE[64] = "MANI MESH VIEWER"; static int win; static int menid; static int primitive = 0; static int press_x, press_y; static float x_angle = 0.0; static float y_angle = 0.0; static float scale_size = 1; static float tran_x = 0, tran_y = 0, tran_z = 0; static int obj_mode = 0; static int xform_mode = 0; static char type; static float x,y,z,nx,ny,nz; static int vid,fid,v1,v2,v3; int i; //const char *s; char b[12]; double max_x(-1e100), max_y(-1e100), max_z(-1e100); double min_x(1e100), min_y(1e100), min_z(1e100); struct Vertex { double x,y,z; double nx,ny,nz; }; struct Face { int v1,v2,v3; }; vector<Vertex> vertices; vector<Face> faces; void loadMFile( ) { FILE* f = fopen(b, "r"); while ((type=fgetc(f)) != EOF) { switch(type) { case 'V': //it's a vertex if (fscanf(f,"ertex %d %f %f %f {normal=(%f %f %f)}\n", &vid, &x, &y, &z, &nx, &ny, &nz) == 7 ) { Vertex vertex; vertex.x = x; vertex.y = y; vertex.z = z; vertex.nx = nx; vertex.ny = ny; vertex.nz = nz; vertices.push_back(vertex); } break; case 'F': if (fscanf(f,"ace %d %d %d %d\n", &fid, &v1, &v2, &v3) == 4 ) { Face face; face.v1 = v1-1; face.v2 = v2-1; face.v3 = v3-1; faces.push_back(face); } break; default: do { type = fgetc(f); } while (type !=EOF && type !='\n'); break; } //switch } //while for (int i=0; i<(int)vertices.size(); i++) { Vertex v = vertices[i]; if (v.x < min_x) min_x = v.x; if (v.y < min_y) min_y = v.y; if (v.z < min_z) min_z = v.z; if (v.x > max_x) max_x = v.x; if (v.y > max_y) max_y = v.y; if (v.z > max_z) max_z = v.z; } double cent_x = (max_x + min_x) /2; double cent_y = (max_y + min_y) /2; double cent_z = (max_z + min_z) /2; double W = max_x - min_x; double H = max_y - min_y; double D = max_z - min_z; #define max(a,b) (((a)>(b))?(a):(b)) double d = max(W,max(H,D)); for ( i=0; i<(int)vertices.size(); i++) { vertices[i].x = (vertices[i].x - cent_x) / d; vertices[i].y = (vertices[i].y - cent_y) / d; vertices[i].z = (vertices[i].z - cent_z) / d; } } void dispNorm(void) { for (int i=0; i<(int)faces.size(); i++) { Face f = faces[i]; Vertex v1 = vertices[f.v1]; Vertex v2 = vertices[f.v2]; Vertex v3 = vertices[f.v3]; glBegin(GL_LINES); glColor3f(0.0,.5,1.0); glVertex3f(v1.x, v1.y, v1.z); glVertex3f(v1.x + v1.nx,v1.y + v1.ny, v1.z+v1.nz); glVertex3f(v2.x, v2.y, v2.z); glVertex3f(v2.x + v2.nx,v2.y + v2.ny, v2.z+v2.nz); glVertex3f(v3.x, v3.y, v3.z); glVertex3f(v3.x + v3.nx,v3.y + v3.ny, v3.z+v3.nz); glEnd(); } } void drawWireframe(void) { for (int i=0; i<(int)faces.size(); i++) { Face f = faces[i]; Vertex v1 = vertices[f.v1]; Vertex v2 = vertices[f.v2]; Vertex v3 = vertices[f.v3]; glBegin(GL_LINES); glColor3f(1,0,0); glVertex3f(v1.x,v1.y,v1.z); glVertex3f(v2.x,v2.y,v2.z); glVertex3f(v2.x,v2.y,v2.z); glVertex3f(v3.x,v3.y,v3.z); glVertex3f(v3.x,v3.y,v3.z); glVertex3f(v1.x,v1.y,v1.z); glEnd(); } } void drawSolid(void) { for (int i=0; i<(int)faces.size(); i++) { Face f = faces[i]; Vertex v1 = vertices[f.v1]; Vertex v2 = vertices[f.v2]; Vertex v3 = vertices[f.v3]; glBegin(GL_TRIANGLES); float colour1 = (v1.z/max_z) * 10; float colour2 = (v2.z/max_z) * 10; float colour3 = (v3.z/max_z) * 10; glColor3f(1,1,colour3); glNormal3f(v1.nx, v1.ny, v1.nz); glVertex3f(v1.x, v1.y, v1.z); glNormal3f(v2.nx, v2.ny, v2.nz); glVertex3f(v2.x, v2.y, v2.z); glNormal3f(v3.nx, v3.ny, v3.nz); glVertex3f(v3.x, v3.y, v3.z); glEnd(); } } void menu(int value) { if (value == 0) { glutDestroyWindow(win); exit(0); } else { primitive=value; } // you would want to redraw now glutPostRedisplay(); } void createmenu(void) { // Create the menu, this menu becomes the current menu menid = glutCreateMenu(menu); // Create an entry glutAddMenuEntry("Clear", 1); //glutAddSubMenu("Draw", submenid1); glutAddMenuEntry("Draw", 2); //glutAddSubMenu("Shading", submenid2); // Create an entry glutAddMenuEntry("Quit", 0); // Let the menu respond on the right mouse button glutAttachMenu(GLUT_RIGHT_BUTTON); } void disp(void) { glEnable(GL_DEPTH_TEST); // Just clean the screen glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // setup the perspective projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (PERSPECTIVE) gluPerspective(60, 1, .1, 100); else glOrtho(-2.5,2.5,-2.5,2.5,-10000,10000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0,0,5,0,0,0,0,1,0); // rotate and scale the object glRotatef(x_angle, 0, 1,0); glRotatef(y_angle, 1,0,0); glScalef(scale_size, scale_size, scale_size); // translate the object glTranslatef(tran_x,tran_y,0); //lighting GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat lmodel_ambient[] = { 1, 0.1, 0.1, 1.0 }; glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light); glLightfv(GL_LIGHT0, GL_SPECULAR, white_light); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); // toggle the lighting if (LIGHTING) { glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); } else { glDisable(GL_LIGHTING); glDisable(GL_LIGHT0); } // set flat or smooth shading if (obj_mode== OBJ_SMOOTH_SHADING) glShadeModel(GL_SMOOTH); else if (obj_mode== OBJ_FLAT_SHADING) glShadeModel(GL_FLAT); // draw what the user asked if (primitive == 2) { if (obj_mode == OBJ_WIREFRAME) { drawWireframe(); } if (obj_mode == OBJ_SOLID) { drawSolid(); } else if (obj_mode == OBJ_NORM) { drawSolid(); double width; glGetDoublev(GL_LINE_WIDTH, &width); glLineWidth(2); drawWireframe(); glLineWidth(width); dispNorm(); } else { drawSolid(); //glColor3f(0.0f, 1.0f, 0.0f); double width; glGetDoublev(GL_LINE_WIDTH, &width); glLineWidth(4); drawWireframe(); glLineWidth(width); } } // swap the buffers glutSwapBuffers(); } void mymouse(int button, int state, int x, int y) { if (state == GLUT_DOWN) { press_x = x; press_y = y; if (button == GLUT_LEFT_BUTTON) xform_mode = TRANSFORM_ROTATE; else if (button == GLUT_MIDDLE_BUTTON) { int mod = glutGetModifiers(); if (mod == GLUT_ACTIVE_ALT) xform_mode = TRANSFORM_SCALE; else xform_mode = TRANSFORM_TRANSLATE; } } else if (state == GLUT_UP) { xform_mode = TRANSFORM_NONE; } } void mymotion(int x, int y) { if (xform_mode == TRANSFORM_ROTATE) { x_angle += (x - press_x)/5.0; if (x_angle > 180) x_angle -= 360; else if (x_angle <-180) x_angle += 360; press_x = x; y_angle += (y - press_y)/5.0; if (y_angle > 180) y_angle -= 360; else if (y_angle <-180) y_angle += 360; press_y = y; } else if (xform_mode == TRANSFORM_SCALE) { float old_size = scale_size; scale_size *= (1 + (y - press_y)/60.0); if (scale_size <0) scale_size = old_size; press_y = y; } else if (xform_mode == TRANSFORM_TRANSLATE) { float dist_x = (x - press_x) * .5; float dist_y = -(y - press_y) * .5; tran_x += dist_x * .1; tran_y += dist_y * .1; //tran_z += .1 * (x - press_x); press_x = x; press_y = y; } // force the redraw function glutPostRedisplay(); } void mykey(unsigned char key, int x, int y) { switch(key) { case 'w': case 'W': cout << "key 'w' is pressed! draw the object in wireframe" << endl; obj_mode = OBJ_WIREFRAME; break; case 's': case 'S': cout << "key 's' is pressed! draw the object in solid" << endl; obj_mode = OBJ_SOLID; break; case 'n': case 'N': cout<<"Normal display" <<endl; obj_mode = OBJ_NORM; break; case 'e': case 'E': cout << "key 'e' is pressed! draw the object in solid+wireframe" << endl; obj_mode = OBJ_EDGE; break; case 'f': case 'F': cout << "key 'f' is pressed! draw the object in flat shading" << endl; obj_mode = OBJ_FLAT_SHADING; break; case 'o': case 'O': cout << "key 'O' is pressed! draw the object in smooth shading" << endl; obj_mode = OBJ_SMOOTH_SHADING; break; case 'l': case 'L': cout << "key 'l' or 'L' is pressed! " << endl; if (LIGHTING) { LIGHTING = OFF; glutSetWindowTitle("Light OFF"); cout << "light off" << endl; } else { LIGHTING = ON; glutSetWindowTitle("Light ON"); cout << "light on" << endl; } break; case 'p': case 'P': cout << "key 'P' or 'p' is pressed! " << endl; if (PERSPECTIVE) { PERSPECTIVE = OFF; glutSetWindowTitle("Orthogonal Projection"); cout << "Orthogonal Projection" << endl; } else { PERSPECTIVE = ON; glutSetWindowTitle("Perspective Projection"); cout << "Perspective Projection" << endl; } break; } // force the redraw function glutPostRedisplay(); } void reshape( int w, int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); } int main(int argc, char **argv) { // const char *s ="eight.m"; //loadMFile(s); //cout<<"enter file name"; cin>>b; // normal initialisation glutInit(&argc, argv); // use double buffer to get better results on animation // use depth buffer for hidden surface removal glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(500,500); glutInitWindowPosition(100,100); win = glutCreateWindow(TITLE); // put all the menu functions in one nice procedure createmenu(); // set the clearcolor and the callback glClearColor(0.0,0.0,0.0,0.0); //glutCreateWindow("color"); // register your callback functions glutDisplayFunc(disp); glutReshapeFunc(reshape); glutMouseFunc(mymouse); glutMotionFunc(mymotion); glutKeyboardFunc(mykey); loadMFile(); // enter the main loop glutMainLoop(); return 1 ; } [Edited by - LessBread on November 7, 2006 1:25:34 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
1: Try Using Source Tags. If you don't know how to do this then see the forum FAQ.

2: Try reading Nehe. http://nehe.gamedev.net

3: Google is your friend. Use it

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
  • Popular Now