Total members 11895 |It is currently Sun Dec 22, 2024 6:39 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Add texture to teapot object using openGL
Code:
#include <stdlib.h>
#include <GL/glut.h>
#include "RgbImage.h"
GLfloat xRotated, yRotated, zRotated;
GLuint   texture[1];         // Storage For One Texture ( NEW )
/*
* Read a texture map from a BMP bitmap file.
*/
void loadTextureFromFile(char *filename)
{   
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel(GL_FLAT);
   glEnable(GL_DEPTH_TEST);

   RgbImage theTexMap( filename );

   // Pixel alignment: each row is word aligned (aligned to a 4 byte boundary)
   //    Therefore, no need to call glPixelStore( GL_UNPACK_ALIGNMENT, ... );

 
   glGenTextures(1, &texture[0]);               // Create The Texture
      glBindTexture(GL_TEXTURE_2D, texture[0]);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

      // Typical Texture Generation Using Data From The Bitmap
   
      glTexImage2D(GL_TEXTURE_2D, 0, 3, theTexMap.GetNumCols(), theTexMap.GetNumRows(), 0, GL_RGB, GL_UNSIGNED_BYTE, theTexMap.ImageData() );





}

/*
* Draw the texture in the OpenGL graphics window
*/
void drawScene(void)
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glEnable(GL_TEXTURE_2D);
     glBindTexture(GL_TEXTURE_2D, texture[0]);
   glLoadIdentity();
   glTranslatef(0.0,0.0,-5);
     glRotatef(yRotated, 0, 1, 0);
    glRotatef(zRotated, 0, 0, 1);
   glutSolidTeapot(1);
   // glutSolidSphere(1,50,50);
   glFlush();
   glDisable(GL_TEXTURE_2D);
 
}

void resizeWindow(int x, int y)
{
if (y == 0 || x == 0) return;  //Nothing is visible then, so return
    //Set a new projection matrix
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();
    //Angle of view:40 degrees
    //Near clipping plane distance: 0.5
    //Far clipping plane distance: 20.0
     
    gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
    glMatrixMode(GL_MODELVIEW);
    glViewport(0,0,x,y);  //Use the whole window for rendering
}

void keyboard (unsigned char key, int x, int y)
{
   switch (key) {
      case 27:
         exit(0);
         break;
      default:
         break;
   }
}

char* filename = "./salt_on_spoon.bmp";
void idleFunc(void)
{

     yRotated += 0.01;
     zRotated += 0.01;
    drawScene();
}
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize(240, 240);
   glutInitWindowPosition(100, 100);
   glutCreateWindow(argv[0]);

   loadTextureFromFile( filename );
   
   glutDisplayFunc(drawScene);
   glutReshapeFunc(resizeWindow);
   glutKeyboardFunc(keyboard);
     glutIdleFunc(idleFunc);
   glutMainLoop();
   return 0;
}



Similar to c-opengl-examples/add-texture-to-cube3d-t9181.html
but with changing the main file.




Attachments:
tea-pot-teature.png
tea-pot-teature.png [ 27.27 KiB | Viewed 15380 times ]

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Add texture to teapot object using
 add lighting to teapot 3d     -  
 Drawing teapot using OpenGL.     -  
 Add light to texture     -  
 add texture to cube3D     -  
 how to attach a texture to polygon     -  
 Text Texture in java     -  
 Read an image bitmap (.bmp) file and draw it as texture     -  
 object detection     -  
 use out object in a method at jsp     -  
 3D-object loader     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com