PDA

View Full Version : texture mapping



chign
06-08-2010, 07:36 AM
can anyone help what is going on in that example.. step by step as fas as possible.. bicause i am very confused...

<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;">

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "glm.h"
#include "readBMP.h"
//#include <glut.h>

struct model {
GLuint tex;
GLuint dlist;
};

struct model g_models[16];
GLfloat angle=0.01;


void loadModel(struct model *m, char * name)
{
char texture_path[256];

//load object
GLMmodel* pmodel = NULL;

m->dlist = glGenLists(1);
glNewList(m->dlist,GL_COMPILE);

pmodel = glmReadOBJ(name);
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);

glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL |GLM_TEXTURE);

glEndList();

// load texture
m->tex=0;
if(pmodel->nummaterials>=2)
if(strlen(pmodel->materials[1].dif_name)) {
Image *texture_bmp=new Image();
sprintf(texture_path, "data/%s", pmodel->materials[1].dif_name);
if(ImageLoad(texture_path, texture_bmp)) {
glGenTextures(1,&amp;m->tex);

glBindTexture(GL_TEXTURE_2D,m->tex);

glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,texture_bmp->sizeX,texture_bmp->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,texture_bmp->data);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
}

delete texture_bmp;
}

glmDelete(pmodel);
}


void init(){
glEnable(GL_TEXTURE_2D);

GLfloat light_specular[] = {1.0,1.0,1.0,1.0};
GLfloat light_diffuse[] = {1.0,1.0,1.0,1.0};
GLfloat light_ambient[] = {0.0,0.0,0.0,1.0};
GLfloat light_position[] = { -2.0, 2.0, 2.0, 1.0 };
GLfloat light_model[] = { 0.2, 0.2, 0.2, 1.0 };

glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
glLightfv(GL_LIGHT0, GL_POSITION,light_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_model);


glClearColor (0.2, 0.2, 0.2, 1.0);
glShadeModel (GL_SMOOTH);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

//blending - antialiasing
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_POINT_SMOOTH);
//glEnable(GL_BLEND);
glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT,GL_NICEST);
glLineWidth(2.0);


//fog
glEnable(GL_FOG);
GLfloat fogColor[4] = {0.2,0.2,0.2,1.0};
GLint fogMode = GL_LINEAR;
glFogi(GL_FOG_MODE,fogMode);
glFogfv(GL_FOG_COLOR,fogColor);
glFogf(GL_FOG_DENSITY,0.15);
glHint(GL_FOG_HINT,GL_NICEST);
glFogf(GL_FOG_START,50.0);
glFogf(GL_FOG_END,100.0);


///////////////////////////////////////////////////////

loadModel(&amp;g_models[0], "data/head.obj");

loadModel(&amp;g_models[1], "data/al.obj");


// exit(0);
}

void display(){
glRotatef(angle,0,1,0);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glPushMatrix();
for(int i=0; i<2; i++) {
glBindTexture(GL_TEXTURE_2D, g_models[i].tex);
glCallList(g_models[i].dlist);
glTranslatef(2.0f, 0.0f, 0.0f);
}
glPopMatrix();

glutSwapBuffers();
}

void reshape(int w , int h){
GLfloat aspectRatio = (GLfloat) w/h;
glViewport( 0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80.0,(GLfloat) w/(GLfloat) h,1.0,40.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,1,0);
}

void animate(void){
glutPostRedisplay();
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 's':
glutIdleFunc(animate);
glutPostRedisplay();
break;
case 'd':
glutIdleFunc(NULL);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}

int main (int argc, char** argv){
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_DEPTH|GLUT_DOUBLE | GLUT_RGB );

glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}


[/QUOTE]</div>
thanks for your time.. hope you can help me.. i will come back later with specific questions..

1)can anyne explain me the LoadObject??
2)
for(int i=0; i<2; i++) {
glBindTexture(GL_TEXTURE_2D, g_models[i].tex);
glCallList(g_models[i].dlist);
glTranslatef(2.0f, 0.0f, 0.0f);
}
what is happening here? i dont get what is the glCallList does?? and its argument?

strattonbrazil
06-08-2010, 12:23 PM
loadModel seems to parse an OBJ file and store the geometry in a structure. It also allocates the textures needed for displaying that model.

glCallList renders an OpenGL display list. In OpenGL, you can combine a group of successive OpenGLs into a static list of calls, which has an OpenGL handle (like a texture handle). With that number, you can simply call glCallList with that number/handle and have those commands--in this case rendering the mesh--called. The advantage of this is the drivers usually do pretty slick job of speeding up all the jobs in that call instead of calling them individually.

chign
06-08-2010, 03:39 PM
okk got that.. my next question is..



void init() {
loadModel(&amp;g_models[0],"datahead.obj");
which is supposed to load the model for the object datahead.. right??

loadModel is in the spoiler<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;">
void loadModel(struct model *m, char * name)
{
char texture_path[256];

//load object
GLMmodel* pmodel = NULL;

m->dlist = glGenLists(1);
glNewList(m->dlist,GL_COMPILE);

pmodel = glmReadOBJ(name);
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);

glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL |GLM_TEXTURE);

glEndList();

// load texture
m->tex=0;
if(pmodel->nummaterials>=2)
if(strlen(pmodel->materials[1].dif_name)) {
Image *texture_bmp=new Image();
sprintf(texture_path, "C:\\data\%s", pmodel->materials[1].dif_name);
if(ImageLoad(texture_path, texture_bmp)) {
glGenTextures(1,&amp;m->tex);

glBindTexture(GL_TEXTURE_2D,m->tex);

glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,texture_bmp->sizeX,texture_bmp->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,texture_bmp->data);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
}

delete texture_bmp;
}

glmDelete(pmodel);
}

[/QUOTE]</div>

next in my display i have this...


else if (x[i][j]=='2'){
glColor3f(0,1,0);
drawcube2(cnt1,cnt2,temp1,temp2);

what i want to do now is this.. instead of drawing the cube, draw that object.. can you help me with that?? give me the row of the things..

what i've done is this:


void drawcube2(int a1,int a2, int b1, int b2) {//a->cnt, b->temp
glPushMatrix();
//pros8iki me tis ifes
glBindTexture(GL_TEXTURE_2D,g_models[0].tex);
glTranslatef( (GLfloat) b1+a1/2, (GLfloat) b2+a2/2,0);//mettopisi tou kentrou tou kivou
if (x[i][j]=='2') glRotatef(45,1,0,0);//ta frouta na fainontai me allh gwnia
if (a1<a2){
glScalef(1.0,GLfloat (a2/a1),1.0);
glutSolidCube(a1);
}
else {
glScalef(1.0,GLfloat (a1/a2),1.0);
glutSolidCube(a2);
}
glColor3f(1,0,1);
if (a1<a2)
glutWireCube(a1);
else
glutWireCube(a1);
glPopMatrix();
}

i know that it is bullshit... but can anyone help me with that?

chign
06-09-2010, 05:58 AM
okk i got those two functions in my code... my code runs and i see what i must see..

void loadModel(struct model *m, char * name)
{
char texture_path[256];

//load object
GLMmodel* pmodel = NULL;

m->dlist = glGenLists(1);
glNewList(m->dlist,GL_COMPILE);

pmodel = glmReadOBJ(name);
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);

glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL |GLM_TEXTURE);

glEndList();

// load texture
m->tex=0;
if(pmodel->nummaterials>=2)
if(strlen(pmodel->materials[1].dif_name)) {
Image *texture_bmp=new Image();
sprintf(texture_path, "D:\\Planets\\Earth.bmp", pmodel->materials[1].dif_name);
if(ImageLoad(texture_path, texture_bmp)) {
glGenTextures(1,&amp;m->tex);

glBindTexture(GL_TEXTURE_2D,m->tex);

glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,texture_bmp->sizeX,texture_bmp->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,texture_bmp->data);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
}

delete texture_bmp;
}

glmDelete(pmodel);
}



void display(){
glRotatef(angle,0,1,0);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glPushMatrix();
for(int i=0; i<2; i++) {
glBindTexture(GL_TEXTURE_2D, g_models[i].tex);
glCallList(g_models[i].dlist);
glTranslatef(10.0f, 0.0f, 0.0f);
}
glPopMatrix();

glutSwapBuffers();
}

my question is: which one function draw the item??
as i get it, what happenis in LoadModel is:
load the object and its geometry, apply a texture to it..

and the in my display i do the following..
by:

glBindTexture(GL_TEXTURE_2D, g_models[i].tex);
i "load" the object 1 and then draw it.. my question is with what command??
with:

glCallList(g_models[i].dlist);
glTranslatef(10.0f, 0.0f, 0.0f);
or not??

am i getting something wrong??

please help me.. i am trying to figure out this example so to make my project..