Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
iniciante

Open Gl

Recommended Posts

iniciante

alguem me pode ajudar? tenho este programa em c++ mas da-me estes erros sempre que tento meter uma imagem..

#include <gl/glut.h> 
#include <stdio.h>  
#include <gl/glaux.h> 

                                                                        
//Função para Carregar uma imagem .BMP
AUX_RGBImageRec *LoadBMP(char *Filename){
FILE *File=NULL;
if (!Filename) {
	return NULL;         
}
File=fopen(Filename,"r");	
if (File)	// Se o arquivo existe
{
	fclose(File);			        
	return auxDIBImageLoad(Filename);//Retorna a imagem
}
return NULL;			
}

// Variáveis globais
GLfloat angle, fAspect, rotX, rotY, obsZ;
GLuint idTextura;
AUX_RGBImageRec *imagemTextura;           
       
// Função responsável pela especificação dos parâmetros de iluminação
void DefineIluminacao (void)
{
GLfloat luzAmbiente[4]={0.2,0.2,0.2,1.0}; 
GLfloat luzDifusa[4]={0.7,0.7,0.7,1.0};	   // "cor" 
GLfloat luzEspecular[4]={1.0, 1.0, 1.0, 1.0};// "brilho" 
GLfloat posicaoLuz[4]={0.0, -40.0, 0.0, 1.0};

// Capacidade de brilho do material
GLfloat especularidade[4]={1.0,1.0,1.0,1.0}; 
GLint especMaterial = 60;

// Define a refletância do material 
glMaterialfv(GL_FRONT,GL_SPECULAR, especularidade);
// Define a concentração do brilho
glMateriali(GL_FRONT,GL_SHININESS,especMaterial);

// Ativa o uso da luz ambiente 
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, luzAmbiente);

// Define os parâmetros da luz de número 0
glLightfv(GL_LIGHT0, GL_AMBIENT, luzAmbiente); 
glLightfv(GL_LIGHT0, GL_DIFFUSE, luzDifusa );
glLightfv(GL_LIGHT0, GL_SPECULAR, luzEspecular );
glLightfv(GL_LIGHT0, GL_POSITION, posicaoLuz );  

// Habilita o modelo de colorização de Gouraud
glShadeModel(GL_SMOOTH);
}
            
// Função callback chamada para fazer o desenho
void Desenha(void)
{
// Limpa a janela e o depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Desenha um cubo no qual a textura é aplicada
glBegin ( GL_QUADS );
	// Face frontal
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
	// Face posterior
	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
	// Face superior
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
	// Face inferior
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
	// Face lateral direita
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
	// Face lateral esquerda
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
glEnd();

glutSwapBuffers();
}

// Função usada para especificar a posição do observador virtual
void PosicionaObservador(void)
{
// Especifica sistema de coordenadas do modelo
glMatrixMode(GL_MODELVIEW);
// Inicializa sistema de coordenadas do modelo
glLoadIdentity();
DefineIluminacao();
// Especifica posição do observador e do alvo
glTranslatef(0,0,-obsZ);
glRotatef(rotX,1,0,0);
glRotatef(rotY,0,1,0);
}

// Inicializa parâmetros de rendering
void Inicializa (void)
{ 
// Define a cor de fundo da janela de visualização como preta
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
         
// Habilita a definição da cor do material a partir da cor corrente
glEnable(GL_COLOR_MATERIAL);
//Habilita o uso de iluminação
glEnable(GL_LIGHTING);  
// Habilita a luz de número 0
glEnable(GL_LIGHT0);
// Habilita o depth-buffering
glEnable(GL_DEPTH_TEST);

// Inicializa a variável que especifica o ângulo da projeção
// perspectiva
angle=50;
    
// Inicializa as variáveis usadas para alterar a posição do 
// observador virtual
rotX = 30;
rotY = 0;
obsZ = 10; 
    
// Comandos de inicialização para textura
imagemTextura = LoadBMP("imagem.bmp");
glGenTextures(1, &idTextura);
glBindTexture(GL_TEXTURE_2D, idTextura);
glTexImage2D(GL_TEXTURE_2D, 0, 3, imagemTextura->sizeX, 
		imagemTextura->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE,
		imagemTextura->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glEnable(GL_TEXTURE_2D);    
}

// Função usada para especificar o volume de visualização
void EspecificaParametrosVisualizacao(void)
{
// Especifica sistema de coordenadas de projeção
glMatrixMode(GL_PROJECTION);
// Inicializa sistema de coordenadas de projeção
glLoadIdentity();

// Especifica a projeção perspectiva(angulo,aspecto,zMin,zMax)
gluPerspective(angle,fAspect,0.5,500);

PosicionaObservador();
}

// Função callback chamada quando o tamanho da janela é alterado 
void AlteraTamanhoJanela(GLsizei w, GLsizei h)
{
// Para previnir uma divisão por zero
if ( h == 0 ) h = 1;

// Especifica as dimensões da viewport
glViewport(0, 0, w, h);

// Calcula a correção de aspecto
fAspect = (GLfloat)w/(GLfloat)h;

EspecificaParametrosVisualizacao();
}

// Função callback chamada para gerenciar eventos do mouse
void GerenciaMouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
	if (state == GLUT_DOWN) {  // Zoom-in
		if (angle >= 10) angle -= 5;
	}
if (button == GLUT_RIGHT_BUTTON)
	if (state == GLUT_DOWN) {  // Zoom-out
		if (angle <= 130) angle += 5;
	}
EspecificaParametrosVisualizacao();
glutPostRedisplay();
}

// Callback para gerenciar eventos do teclado para teclas especiais 
void TeclasEspeciais(int key, int x, int y)
{
switch (key)
{
	case GLUT_KEY_LEFT:rotY--;
			break;
	case GLUT_KEY_RIGHT:rotY++;
			break;
	case GLUT_KEY_UP:rotX++;
			break;
	case GLUT_KEY_DOWN:rotX--;
			break;
	case GLUT_KEY_HOME:obsZ++;
			break;
	case GLUT_KEY_END:obsZ--;
			break;
	case GLUT_KEY_F10:// "camera reset"
			rotX=30;
			rotY=0;
			obsZ=10;
			angle=50;
			EspecificaParametrosVisualizacao();
			break;
}
PosicionaObservador();
glutPostRedisplay();
}

// Programa Principal
int main(void)
{
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400,350);
glutCreateWindow("Visualizacao 3D com Mapeamento de Textura");
glutDisplayFunc(Desenha);
glutReshapeFunc(AlteraTamanhoJanela);
glutMouseFunc(GerenciaMouse);
glutSpecialFunc(TeclasEspeciais);
Inicializa();
glutMainLoop();
}

isto sao  os erros que dão:

~1>------ Build started: Project: imagem, Configuration: Debug Win32 ------

1>Compiling...

1>ola.cpp

1>c:\program files\microsoft visual studio 9.0\vc\include\stdlib.h(371) : error C2381: 'exit' : redefinition; __declspec(noreturn) differs

1>        c:\program files\microsoft visual studio 9.0\vc\include\gl\glut.h(146) : see declaration of 'exit'

1>c:\program files\microsoft visual studio 9.0\vc\include\stdlib.h(371) : warning C4985: 'exit': attributes not present on previous declaration.

1>        c:\program files\microsoft visual studio 9.0\vc\include\gl\glut.h(146) : see declaration of 'exit'

1>Build log was saved at "file://c:\Users\Ruben\Desktop\imagem\imagem\Debug\BuildLog.htm"

1>imagem - 1 error(s), 1 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


RC

Share this post


Link to post
Share on other sites
IceBrain
There is an incompatibility between glut.h and Visual Studio .NET. that results in compile errors:

error C2381: 'exit' : redefinition; __declspec(noreturn) differs

error C3861: 'exit': identifier not found, even with argument-dependent lookup

To fix the error, right click on the project name in the Solution Explorer tab and select Properties -> C/C++ -> Preprocessor -> Preprocessor definitions and append GLUT_BUILDING_LIB to the existing definitions, seperated by semicolons.

http://www.cs.tufts.edu/research/graphics/resources/GLUT/GLUT.htm

Funciona?

(btw, para a próxima põe o código usando [code=cpp] em vez de apenas [code], fica melhor formatado e com syntax coloring :D )


❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Share this post


Link to post
Share on other sites
iniciante

hm esta bem obrigado pela dica xD!

ainda n esta a funcionar..acho que piorou  :wallbash:  e eu estou a trabalhar no Visual C++ 2008

1>------ Build started: Project: projecto, Configuration: Debug Win32 ------

1>Compiling...

1>aviao.cpp

1>c:\users\ruben\desktop\projecto\projecto\aviao.cpp(26) : warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

1>        c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(237) : see declaration of 'fopen'

1>c:\users\ruben\desktop\projecto\projecto\aviao.cpp(479) : warning C4060: switch statement contains no 'case' or 'default' labels

1>Linking...

1>aviao.obj : error LNK2019: unresolved external symbol _auxDIBImageLoadA@4 referenced in function "struct _AUX_RGBImageRec * __cdecl LoadBMP(char *)" (?LoadBMP@@YAPAU_AUX_RGBImageRec@@PAD@Z)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glTexParameteri@12 referenced in function "int __cdecl LoadGLTextures(char *,unsigned int)" (?LoadGLTextures@@YAHPADI@Z)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glTexImage2D@36 referenced in function "int __cdecl LoadGLTextures(char *,unsigned int)" (?LoadGLTextures@@YAHPADI@Z)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glBindTexture@8 referenced in function "int __cdecl LoadGLTextures(char *,unsigned int)" (?LoadGLTextures@@YAHPADI@Z)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glGenTextures@8 referenced in function "int __cdecl LoadGLTextures(char *,unsigned int)" (?LoadGLTextures@@YAHPADI@Z)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glEnd@0 referenced in function "void __cdecl polygon(int,int,int,int)" (?polygon@@YAXHHHH@Z)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glVertex3fv@4 referenced in function "void __cdecl polygon(int,int,int,int)" (?polygon@@YAXHHHH@Z)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glNormal3fv@4 referenced in function "void __cdecl polygon(int,int,int,int)" (?polygon@@YAXHHHH@Z)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glColor3fv@4 referenced in function "void __cdecl polygon(int,int,int,int)" (?polygon@@YAXHHHH@Z)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glBegin@4 referenced in function "void __cdecl polygon(int,int,int,int)" (?polygon@@YAXHHHH@Z)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glPopMatrix@0 referenced in function "void __cdecl pista_arranque(void)" (?pista_arranque@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glColor3f@12 referenced in function "void __cdecl pista_arranque(void)" (?pista_arranque@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glDisable@4 referenced in function "void __cdecl pista_arranque(void)" (?pista_arranque@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glVertex3f@12 referenced in function "void __cdecl pista_arranque(void)" (?pista_arranque@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glEnable@4 referenced in function "void __cdecl pista_arranque(void)" (?pista_arranque@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glPushMatrix@0 referenced in function "void __cdecl pista_arranque(void)" (?pista_arranque@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol _glutSwapBuffers@0 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glFlush@0 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol _glutSolidSphere@16 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glTranslatef@12 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glScalef@12 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glRotatef@16 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glClear@4 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glClearColor@16 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol _gluLookAt@72 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol _gluPerspective@32 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glLoadIdentity@0 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glMatrixMode@4 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol _glutPostRedisplay@0 referenced in function "void __cdecl rodaCubo3D(void)" (?rodaCubo3D@@YAXXZ)

1>aviao.obj : error LNK2019: unresolved external symbol _glutMainLoop@0 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol _glutSpecialUpFunc@4 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol _glutSpecialFunc@4 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol _glutKeyboardUpFunc@4 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol _glutKeyboardFunc@4 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol _glutMouseFunc@4 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol _glutIdleFunc@4 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol _glutDisplayFunc@4 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol _glutReshapeFunc@4 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol _glutCreateWindow@4 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol _glutInitWindowSize@8 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol _glutInitDisplayMode@4 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol __imp__glTexEnvf@12 referenced in function _main

1>aviao.obj : error LNK2019: unresolved external symbol _glutInit@8 referenced in function _main

1>C:\Users\Ruben\Desktop\projecto\Debug\projecto.exe : fatal error LNK1120: 43 unresolved externals

1>Build log was saved at "file://c:\Users\Ruben\Desktop\projecto\projecto\Debug\BuildLog.htm"

1>projecto - 44 error(s), 2 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


RC

Share this post


Link to post
Share on other sites
IceBrain

Antes falhava na compilação, agora já compila mas falha na linkagem.

O que está a acontecer é que não definiste nas propriedades do projecto (ou lá como se chama) que se devia incluir as bibliotecas do OpenGL na linkagem.

Agora, como é que isso se faz não te sei dizer, visto que há muito tempo que não uso o VS ;)


❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Share this post


Link to post
Share on other sites
iniciante

bem eu ja resolvi o problema..! ordenei os include de outra maneira e faltava um include e já compilo...! obrigado pela ajuda na mesma xD


RC

Share this post


Link to post
Share on other sites
Triton

Não parece que fosse problema de includes, mas sim de libs, mas o que interessa é que está resolvido.


<3 life

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.