Hi ,
I am learning to implement Texture Mapping on My project.
But I am not able to do it.
I read OpenGL Red Book chapter9.
But none of the code is being executed. It gives some weard error.
I donot know how to upload image and do texture mapping.
Can someone post some basic code that I can run on my system and learn texture mapping.
for the purpose of discussion I am using my old project code…
//…
#include<iostream>
#include<stdlib.h>
#include <gl/glut.h>
using namespace std;
void init(void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1,1.0);
/* clear the matrix /
/ viewing transformation */
//…
// for front view
//gluLookAt (0,0,12.0, 0.0, 0.0, 0.0, 0,1, 0);
// for Angular view
//gluLookAt (-1, 10, 12.0, 2.0, 2.0, 3.0, 0.0, 1, 0.0);
//…
// for top veiw
//gluLookAt (0,15,0, 0.0,0.0, 0.0, 0.0,0, 1.0);
//…
glScalef (1,1,1); /* modeling transformation */
glBegin(GL_QUADS);
glColor3f (0,1,0);
glVertex3f(10,-0.5, 4.0);
glVertex3f(10, -0.5, -10.0);
glVertex3f(-10,-0.5, -10.0);
glVertex3f(-10,-0.5,4.0);
glEnd();
glPushMatrix();
glColor3f (1,0.5,0.0);
glTranslatef (5.0, 0.0, -0.5);
glutSolidCube(1.5);
glPopMatrix();
glPushMatrix();
glTranslatef (4.0, 1.0, -1.5);
glutSolidCube(1.5);
glPopMatrix();
glPushMatrix();
glTranslatef (3.0, 2.0, -2.5);
glutSolidCube(1.5);
glPopMatrix();
glPushMatrix();
glTranslatef (2.0, 3.0,-3.5);
glutSolidCube(1.5);
glPopMatrix();
glPushMatrix();
glColor3f (0.0, 1.0, 1.0);
glTranslatef (0.0, 0.0, 0.0);
glutSolidSphere(0.5,100,100);
glTranslatef (1.0, 0.0, 0.0);
glutSolidSphere(0.5,100,100);
glTranslatef (1.0, 0.0, 0.0);
glutSolidSphere(0.5,100,100);
glTranslatef (1.0, 0.0, 0.0);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glPushMatrix();
glTranslatef (0.5,0.866, 0.0);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glPushMatrix();
glTranslatef (1.5,0.866, 0.0);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glPushMatrix();
glTranslatef (2.5,0.866, 0.0);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glPushMatrix();
glTranslatef (1.0,1.732, 0.0);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glPushMatrix();
glTranslatef (2.0,1.732, 0.0);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glPushMatrix();
glTranslatef (1.5,2.598, 0.0);
glutSolidSphere(0.5,100,100);
glPopMatrix();
// THis is left side sphere balls building up
glPushMatrix();
glTranslatef (-1.0, 0.0, 0.0);
glColor3f (1,0,0.0);
glutSolidSphere(0.5,100,100);
glTranslatef (0.0, 0.0, -1.0);
glutSolidSphere(0.5,100,100);
glTranslatef (0.0, 0.0, -1.0);
glutSolidSphere(0.5,100,100);
glTranslatef (0.0, 0.0, -1.0);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glPushMatrix();
glTranslatef (-1.0, 0.866, -0.5);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glPushMatrix();
glTranslatef (-1.0, 0.866, -1.5);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glPushMatrix();
glTranslatef (-1.0, 0.866, -2.5);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glPushMatrix();
glTranslatef (-1.0, 1.732, -1);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glPushMatrix();
glTranslatef (-1.0, 1.732, -2);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glPushMatrix();
glTranslatef (-1.0, 2.598, -1.5);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glutSwapBuffers();
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0,0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
//glOrtho(-10,20,-10,10,10,-20);
//glOrtho(-15,20,0,0,-15,15);
//glFrustum (-1.0, 1.0, -1.5, 1.2, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (-1, 10, 12.0, 2.0, 2.0, 3.0, 0.0, 1, 0.0);
}
void key(unsigned char key, int x, int y) {
switch(key) {
case 't':
case 'T':
glLoadIdentity();
gluLookAt (0,15,0, 0.0,0.0, 0.0, 0.0,0, 1.0);
glutPostRedisplay();
break;
case 'a':
case 'A':
glLoadIdentity();
gluLookAt (-1, 10, 12.0, 2.0, 2.0, 3.0, 0.0, 1, 0.0);
glutPostRedisplay();
break;
case 'f':
case 'F':
glLoadIdentity();
gluLookAt (0,0,12.0, 0.0, 0.0, 0.0, 0,1, 0);
glutPostRedisplay();
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutMainLoop();
return 0;
}