Jump to content
  • Advertisement
Sign in to follow this  
neilski_2003

OpenGL Lighting and Texturing!!!

This topic is 5137 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

So i havean asteroids envionment that needs to be created and handed in in 3 weeks time. Fine if i could do anything. So I can draw stuff but basically i can't do texturing. My code is fairly long so here is the snippet i am currently working on. As you should be able to see - i am trying to texture a pyramid with a file called 'rock_texture' - which is basically just this grey gravelly thing. so here goes (NOTE: SORRY FOR THE LENGTH!) #include <windows.h> #include <math.h> #include <gl\glut.h> #pragma comment(lib,"glaux.lib") #define PI 3.1415926535898 #include <stdio.h> #include <gl\glaux.h> GLuint texture[1]; #include "texture.h" #include "normal.h" float colour1[] = {0.0, 0.0, 1.0}; float colour2[] = {1.0, 0.0, 0.0}; float colour3[] = {0.0, 1.0, 0.0}; float colour4[] = {0.0, 1.0, 1.0}; float colour5[] = {1.0, 0.0, 1.0}; float colour6[] = {1.0, 1.0, 0.0}; float colour7[] = {1.0, 1.0, 1.0}; double Px, Py, Pz, roll, pitch, yaw; void initGeneral (void) { glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ; glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); } void initLighting(void) { GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; GLfloat light[] = {1.0, 1.0, 1.0, 1.0}; GLfloat shininess[] = {50.0}; GLfloat specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat lmodel_ambient[] = {01, 0.1, 0.1, 1.0}; glMaterialfv(GL_FRONT, GL_SPECULAR, specular); glMaterialfv(GL_FRONT, GL_SHININESS, shininess); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, light); glLightfv(GL_LIGHT0, GL_SPECULAR, light); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); } void initTextures(void) { //set up and enable OpenGL Texture Mapping LoadGLTextures(0,"rock_Texture.bmp"); // load watch image (512*512) glEnable(GL_TEXTURE_2D); // enable Texture Mapping bool autoTexGen = true; glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); if(autoTexGen) { glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); }else { glDisable(GL_TEXTURE_GEN_S); glDisable(GL_TEXTURE_GEN_T); } } void reshape(int w, int h) { //control the 2d viewport glViewport(0, 0, (GLsizei) w, (GLsizei) h); //control the 3d clipping volume glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-10.0, 10.0, -10.0, 10.0, 10.0, 10.0); glMatrixMode(GL_MODELVIEW); } void draw_pyramid(float size) { glBegin(GL_TRIANGLES); glColor3fv(colour1); glVertex3f(0.0*size, 0.2*size, 0.0*size); glVertex3f(-0.2*size, 0.45*size, -0.1*size); glColor3fv(colour2); glVertex3f(0.0*size, 0.2*size, 0.0*size); glVertex3f(0.0*size, 0.2*size, -0.2*size); glVertex3f(-0.2*size, 0.45*size, -0.1*size); glColor3fv(colour3); glVertex3f(0.0*size, 0.2*size, -0.2*size); glVertex3f(-0.4*size, 0.2*size, -0.2*size); glVertex3f(-0.2*size, 0.45*size, -0.1*size); glColor3fv(colour4); glVertex3f(-0.4*size, 0.2*size, -0.2*size); glVertex3f(-0.4*size, 0.2*size, 0.0*size); glVertex3f(-0.2*size, 0.45*size, -0.1*size); glEnd(); glBegin(GL_QUADS); glColor3fv(colour5); glVertex3f(-0.4*size, 0.2*size, 0.0*size); glVertex3f(-0.0*size, 0.2*size, 0.0*size); glVertex3f(-0.0*size, 0.2*size, -0.2*size); glVertex3f(-0.4*size, 0.2*size, -0.2*size); glEnd(); } void keys(unsigned char key, int x, int y) { switch(key) { case 'd': Px -= 0.1; break; case 'a': Px += 0.1; break; case 'w': Py -= 0.1; break; case 's': Py += 0.1; break; case 'p': pitch += 1.0; break; case 'r': roll += 1.0; break; case 'y': yaw += 1.0; break; } glutPostRedisplay(); glLoadIdentity(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT); //clear colour buffer to black glRotated(roll, 0, 0, 1); glRotated(pitch, 1, 0, 0); glRotated(yaw, 0, 1, 0); glTranslated(-Px, -Py, -Pz); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glBindTexture(GL_TEXTURE_2D, texture[0]); draw_pyramid(1.0); glutSwapBuffers(); // update screen } int main(int argc, char** argv) { glutInit (&argc, argv) ; glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) ; glutInitWindowSize (400, 400) ; glutInitWindowPosition (500, 500) ; glutCreateWindow ("example1") ; glutKeyboardFunc(keys); initGeneral(); //initialise general settings // initLighting(); //initialise opengl lighting initTextures(); glutDisplayFunc (display) ; //set up call back for display glutMainLoop () ; return 0 ; } I will do another post below quickly to show you the texture.h file so you can see everything thats going into this project and hopefully help me out. Basically i can't see anything on ym screen any more except fro blackness!

Share this post


Link to post
Share on other sites
Advertisement
Heres the texture.h file from the example above - is it just my machine this doesn't work on or is this a universal thing?


AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File = NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
return NULL; // If Not Return NULL
File = fopen(Filename,"r"); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}


int LoadGLTextures(int image_count, char *Filename) // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator

AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP(Filename))
{
Status=TRUE; // Set The Status To TRUE
glGenTextures(image_count+1, &texture[image_count]); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[image_count]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

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

if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}
free(TextureImage[0]); // Free The Image Structure
}
return Status; // Return The Status
}

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!