Jump to content
  • Advertisement
Sign in to follow this  
Marianne

OpenGL lighting won't work with textures (using Nehe tutorials)

This topic is 4834 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've been basing myself on Nehe'S tutorials, to try making some kind of 3D engine in C++ with opengl (and glut where Nehe uses nativ apis, to keep cross-platform) i've reached without real problems the section describing how to add lights, however these lighting effetcs seem to appear only on polygons with no textures... what am i doing wrong? in case you'd want to check, here's my source: i've removed parts i assume re nopt useful to solve this problem- i try to keep the code i post as small as possible ;)
main.h:

...
GLfloat rtri;
GLfloat rquad;
GLfloat xrot;
GLfloat yrot;
GLfloat zrot;
GLuint  texture[1];
GLfloat LightAmbient[]= { 0.2f, 1.0f, 0.2f, 1.0f };    // Ambient Light Values
GLfloat LightDiffuse[]= { 0.2f, 1.0f, 0.2f, 1.0f };    // Diffuse Light Values
GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };   // Light Position

class Engine3D {
private:

    // manage fullscreen and windowed modes
    bool fullscreen;
    bool highquality_shading;
    bool highquality_textures;
    int window_id;
    int texture_total_amount;
    int texture_current_count;
...
}
...

main.cpp:

#include "main.h"
#include "image.h"

#include <cstdio>
#include <cstdlib>
#include <iostream> // cout
#include <fstream>

#if defined(__MACOSX__) || defined(__APPLE__)
  #include <GLUT/glut.h>
#else
  #include <gl/glut.h>
#endif


using namespace std;
Engine3D* core;

int main(int argc, char** argv){
    core=new Engine3D(argc, argv);
}

// *********************************************** CONSTRUCTOR/DESTRUCTOR ***********************************************
Engine3D::Engine3D(int argc, char** argv){
    cout << "Engine ignition..." << endl;
    
    glutInit(&argc, argv); // always call first in a GLUT program
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); // Double=Double-Buffering, Depth=filter to draw objects back to front
    
    // ---------------------------------------- !!!!!!!!!!!!!! SETUP !!!!!!!!!!!!!! -----------------------------------------
    highquality_shading=true;
    highquality_textures=true;
    texture_total_amount=1;
    fullscreen=false;
    // ----------------------------------------------------------------------------------------------------------------------
    texture_current_count=0;
    // ----------------------------------------------------------------------------------------------------------------------
    
    if(fullscreen){ // fullscren
        glutGameModeString( "640x480:16@60" );
        glutEnterGameMode();
    }else{ // window
        glutInitWindowSize(640, 480);
        glutInitWindowPosition(100, 70);
        window_id = glutCreateWindow("C++/OpenGL/GLUT 3D Test");
    }
    
    initGL();
}

// *********************************************** INIT GL ***********************************************
void Engine3D::initGL(){
    // init
    glEnable(GL_TEXTURE_2D);   // textures
    
    if(highquality_shading) glShadeModel(GL_SMOOTH);	// Enable Smooth Shading
    else glShadeModel(GL_FLAT);
    
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
    glClearDepth(1.0f);	// Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);	// Enables Depth Testing
    glDepthFunc(GL_LEQUAL);	// The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    // textures
    glGenTextures(texture_total_amount, &texture[0]);
    loadTexture("crate.tga");

    // lights
    glEnable(GL_LIGHTING);
    
    glEnable(GL_LIGHT1); // Enable Light One
    glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light
    glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);  // ambient light
    glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);  // diffuse light

    // set functions
    glutDisplayFunc(GL_display);
    glutReshapeFunc(GL_reshape);
    glutIdleFunc(GL_idle);
    glutKeyboardFunc( keyDownEvent );
    glutKeyboardUpFunc( keyUpEvent );
    glutSpecialFunc( SpecialKeyDownEvent );
    glutSpecialUpFunc( SpecialKeyUpEvent );
    
    cout << "Launching main loop" << endl;
    glutMainLoop();
}

// *********************************************** LOAD TEXTURE ***********************************************
void Engine3D::loadTexture(char* name){
    cout << "Loading texture " << name << endl;
    
    glBindTexture(GL_TEXTURE_2D, texture[texture_current_count]); // numtexture == ID
    
    Image* texture_image=new Image();
    if(!texture_image->loadTGA(name)){
        cout << "Error loading texture " << name << " !" << endl;   
    }
    glTexImage2D(GL_TEXTURE_2D, 0, 3, texture_image->getWidth(),  texture_image->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, texture_image->getData());
        
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    
    if(highquality_textures){
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // GL_NEAREST, GL_LINEAR
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
        gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture_image->getWidth(),texture_image->getHeight(), GL_RGB, GL_UNSIGNED_BYTE, texture_image->getData());
    }else{
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    }
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    texture_current_count++;
    
    delete texture_image;
}


Engine3D::~Engine3D(){
    if(fullscreen) glutLeaveGameMode();
    else glutDestroyWindow(window_id);
}

// ******************************************** DISPLAY **************************************************
void GL_display(){
    // reset frame
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity(); // reset matrix to middle

    glTranslatef(0.0f,0.0f,-6.0f); 
    
    // drawing
    rtri+=0.2f;
    glRotatef(rtri,0.0f,1.0f,0.0f);
    glRotatef(rtri,0.0f,0.0f,1.0f);
    
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);{
        // front
        glNormal3f( 0.0f, 0.0f, -2.0f);
        glTexCoord2f(0.0f, 0.0f);  glVertex3f( -2.0f, -2.0f, -2.0f);
        glTexCoord2f(1.0f, 0.0f);  glVertex3f( 2.0f, -2.0f, -2.0f);
        glTexCoord2f(1.0f, 1.0f);  glVertex3f( 2.0f, 2.0f, -2.0f);
        glTexCoord2f(0.0f, 1.0f);  glVertex3f( -2.0f, 2.0f, -2.0f);

        // back
        glNormal3f( 0.0f, 0.0f, 2.0f);
        glTexCoord2f(0.0f, 0.0f);  glVertex3f( -2.0f, -2.0f, 2.0f);
        glTexCoord2f(1.0f, 0.0f);  glVertex3f( 2.0f, -2.0f, 2.0f);
        glTexCoord2f(1.0f, 1.0f);  glVertex3f( 2.0f, 2.0f, 2.0f);
        glTexCoord2f(0.0f, 1.0f);  glVertex3f( -2.0f, 2.0f, 2.0f);
        
        // side
        glNormal3f( -2.0f, 0.0f, 0.0f);
        glTexCoord2f(0.0f, 0.0f);  glVertex3f( -2.0f, 2.0f, -2.0f);
        glTexCoord2f(1.0f, 0.0f);  glVertex3f( -2.0f, 2.0f, 2.0f);
        glTexCoord2f(1.0f, 1.0f);  glVertex3f( -2.0f, -2.0f, 2.0f);
        glTexCoord2f(0.0f, 1.0f);  glVertex3f( -2.0f, -2.0f, -2.0f);

        // side 2
        glNormal3f( 2.0f, 0.0f, 0.0f);
        glTexCoord2f(0.0f, 0.0f);  glVertex3f( 2.0f, 2.0f, -2.0f);
        glTexCoord2f(1.0f, 0.0f);  glVertex3f( 2.0f, 2.0f, 2.0f);
        glTexCoord2f(1.0f, 1.0f);  glVertex3f( 2.0f, -2.0f, 2.0f);
        glTexCoord2f(0.0f, 1.0f);  glVertex3f( 2.0f, -2.0f, -2.0f);
        
    }glEnd();
    
    glutSwapBuffers();
}

// ******************************************** RESHAPE **************************************************
void GL_reshape(int width, int height){
 // window size has changed
    
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);     // Select The Projection Matrix
    glLoadIdentity();                // Reset The Projection Matrix
                                     // Calculate The Aspect Ratio And Set The Clipping Volume
    if (height == 0) height = 1;
    //gluPerspective(80, (float)width/(float)height, 1.0, 5000.0);
    gluPerspective(80, (float)width/(float)height, 1.0, 200.0);
    glMatrixMode(GL_MODELVIEW);      // Select The Modelview Matrix
    glLoadIdentity();                // Reset The Modelview Matrix
    
}
void GL_idle(){
    // frame done
    glutPostRedisplay();
}

fixed lack of source tags [Edited by - phantom on August 20, 2005 9:58:33 AM]

Share this post


Link to post
Share on other sites
Advertisement
Change your GL_TEXTURE_ENV_MODE to GL_MODULATE instead of GL_REPLACE.

And for future reference, when posting large amounts of code (more than a few lines) use source tags. For small amounts of code use code tags. Read the Forum FAQ for more info.

Share this post


Link to post
Share on other sites
1) Use the source tags(do it quick, before the_phantom catches you!)
2) Post only relevant code. I know you said you did, but you could do a lot better. If you don't understand completely the code, since you got it from a tutorial, focus on that first so you can trace the problem yourself and post a question in a way that makes it easy for us to help you. Right now, it's like saying "this is my code, can you fix it?"

Anyway, read up on the glTexEnvf() command. It controls how texturing is handled. In your case, you need to set GL_TEXTURE_ENV_MODE to GL_MODULATE.

-EDIT: Beaten by Kalidor.

Share this post


Link to post
Share on other sites
thanks everyone it works perfectly!

yeah i guess i could have done a little better... i didn't know there were tags here (but i admit i didn't search a lot either), and also, i might have been able to remove more code, and i DO think i understand all of it however as it doesn't work as i expected then i assume there must be some part i misunderstood :S anyway next time i'll try to make a small demo doing exactly the problem :S

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!