我想用 Visual Studio 2010 在 OpenGL 中创建一个房间,并添加一些对象,如椅子、地毯等。我只使用纹理来设计墙壁。当我尝试从 3DS Max 导入对象时遇到一些问题。我看到了那个物体,但它全是白色的。我应该能看到它的颜色。我做错了什么?谢谢。 <-我在 3ds MAX 上的对象 <-OpenGL
#include "stdafx.h"
#include "tga.h"
#include "glut.h"
#include <gl/gl.h>
#include "glm.h"
int screen_width=1040;
int screen_height=580;
GLuint skyboxTexture[6];//skybox
GLfloat fSkyboxSizeX, fSkyboxSizeY, fSkyboxSizeZ; //box size on X, Y and Z axes
GLMmodel *3dsobj;
GLfloat fGlobalAngleX, fGlobalAngleY, fGlobalAngleZ; //global rotation angles
void initOpenGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glShadeModel(GL_SMOOTH);
glViewport(0, 0, screen_width, screen_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)screen_width/(GLfloat)screen_height, 1.0f, 1000.0f);
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glMatrixMode(GL_MODELVIEW);
glGenTextures(6,skyboxTexture);
loadTGA("Textures\\greywall.tga",skyboxTexture[0]);
loadTGA("Textures\\carpet2.tga",skyboxTexture[1]);
loadTGA("Textures\\wallpaper.tga",skyboxTexture[2]);
loadTGA("Textures\\wallpaper.tga",skyboxTexture[3]);
loadTGA("Textures\\green.tga",skyboxTexture[4]);
loadTGA("Textures\\wallpaper.tga",skyboxTexture[5]);
//set skybox size
fSkyboxSizeX =60.0;
fSkyboxSizeY =9.0;
fSkyboxSizeZ =60.0;
}
////////////////////////////////////////
// function used to create the skybox //
////////////////////////////////////////
void DrawSkybox (GLfloat sizeX, GLfloat sizeY, GLfloat sizeZ)
{
glEnable(GL_TEXTURE_2D); //enable 2D texturing
//////////////////////////////////////////////////////////////////
// please consult the orientation convention for this skybox //
// ("orientation_convention.png" file in the "Textures" folder) //
//////////////////////////////////////////////////////////////////
//negative x plane
glBindTexture(GL_TEXTURE_2D, skyboxTexture[0]); //select the current texture
glBegin(GL_QUADS);
glTexCoord2f(6, 6);glVertex3f(-sizeX, sizeY, -sizeZ); //assign each corner of the texture to a 3D vertex in the OpenGL scene
glTexCoord2f(0, 6);glVertex3f(-sizeX, sizeY, sizeZ); //(0,0) is the left lower corner, while (1,1) is the right upper corner of the texture
glTexCoord2f(0, 0);glVertex3f(-sizeX, -sizeY, sizeZ);
glTexCoord2f(6, 0);glVertex3f(-sizeX, -sizeY, -sizeZ);
glEnd();
//negative y plane
glBindTexture(GL_TEXTURE_2D, skyboxTexture[1]);
glBegin(GL_QUADS);
glTexCoord2f(6, 6);glVertex3f(sizeX, -sizeY, -sizeZ);
glTexCoord2f(0, 6);glVertex3f(-sizeX, -sizeY, -sizeZ);
glTexCoord2f(0, 0);glVertex3f(-sizeX, -sizeY, sizeZ);
glTexCoord2f(6, 0);glVertex3f(sizeX, -sizeY, sizeZ);
glEnd();
//negative z plane
glBindTexture(GL_TEXTURE_2D, skyboxTexture[2]);
glBegin(GL_QUADS);
glTexCoord2f(6, 6);glVertex3f(-sizeX, sizeY, sizeZ);
glTexCoord2f(0, 6);glVertex3f(sizeX, sizeY, sizeZ);
glTexCoord2f(0, 0);glVertex3f(sizeX, -sizeY, sizeZ);
glTexCoord2f(6, 0);glVertex3f(-sizeX, -sizeY, sizeZ);
glEnd();
//positive x plane
glBindTexture(GL_TEXTURE_2D, skyboxTexture[3]);
glBegin(GL_QUADS);
glTexCoord2f(6, 6);glVertex3f(sizeX, sizeY, sizeZ);
glTexCoord2f(0, 6);glVertex3f(sizeX, sizeY, -sizeZ);
glTexCoord2f(0, 0);glVertex3f(sizeX, -sizeY, -sizeZ);
glTexCoord2f(6, 0);glVertex3f(sizeX, -sizeY, sizeZ);
glEnd();
//positive y plane
glBindTexture(GL_TEXTURE_2D, skyboxTexture[4]);
glBegin(GL_QUADS);
glTexCoord2f(6, 6);glVertex3f(sizeX, sizeY, sizeZ);
glTexCoord2f(0, 6);glVertex3f(-sizeX, sizeY, sizeZ);
glTexCoord2f(0, 0);glVertex3f(-sizeX, sizeY, -sizeZ);
glTexCoord2f(6, 0);glVertex3f(sizeX, sizeY, -sizeZ);
glEnd();
//positive z plane
glBindTexture(GL_TEXTURE_2D, skyboxTexture[5]);
glBegin(GL_QUADS);
glTexCoord2f(6, 6);glVertex3f(sizeX, sizeY, -sizeZ);
glTexCoord2f(0, 6);glVertex3f(-sizeX, sizeY, -sizeZ);
glTexCoord2f(0, 0);glVertex3f(-sizeX, -sizeY, -sizeZ);
glTexCoord2f(6, 0);glVertex3f(sizeX, -sizeY, -sizeZ);
glEnd();
glDisable(GL_TEXTURE_2D); //disable 2D texuring
}
//////////////////////////////////////////////
///DrawModel ///
//////////////////////////////////////////////
void drawModel(GLMmodel *pmodel,char*filename,GLuint mode)
{
if(!pmodel)
{
pmodel=glmReadOBJ(filename);
//printf("read the model\n");
if(!pmodel)
{
exit(0);
}
}
//generate facet normal vectors for model
glmFacetNormals(pmodel);
//generate vertex normal vectors (called after generating facet normals)
glmVertexNormals(pmodel,90.0);
glmDraw(pmodel,mode);
}
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//place the camera 1.2 units above the negative Y plane of the skybox
gluLookAt(0.0, -fSkyboxSizeY + 1.2 , 3.0, 0.0, -fSkyboxSizeY + 1.2, -10.0, 0.0, 1.0, 0.0);
//set global rotation on the X,Y and Z axes
glRotatef(fGlobalAngleX, 1.0, 0.0, 0.0);
glRotatef(fGlobalAngleY, 0.0, 1.0, 0.0);
glRotatef(fGlobalAngleZ, 0.0, 0.0, 1.0);
//draw skybox
glPushMatrix();
DrawSkybox(fSkyboxSizeX, fSkyboxSizeY, fSkyboxSizeZ);
glPopMatrix();
glTranslatef(0.0, -fSkyboxSizeY + fTreeSize, 0.0);
//draw the tree slice
/*glPushMatrix();
DrawSingleTreeTexture(fTreeSize);
glPopMatrix();*/
glPushMatrix();
// glTranslatef(-30,0 ,0);
glRotatef(30, 0, 1, 0);
// glTranslatef(3, -20,-200);
//place the tree on the negative Y plane of the skybox
glTranslatef(19, -fSkyboxSizeY + fTreeSize+10, -6);
glScalef(0.1,0.1,0.1);
drawModel(3dsobj,"myObj.obj",GLM_NONE|GLM_FLAT);
glPopMatrix();
glutSwapBuffers(); //swap the buffers used in the double-buffering technique
}
void changeSize(int w, int h)
{
screen_width=w;
screen_height=h;
if(h == 0)
h = 1;
float ratio = 1.0*w/h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45.0f, ratio, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 50.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
}
void processNormalKeys(unsigned char key, int x, int y)
{
switch(key)
{
case 't':
//process
glutPostRedisplay();
break;
case 27: //esc
exit(1);
break;
//control the global Y rotation angle using 'a' and 'd'
case 'a':
fGlobalAngleY += 1;
if (fGlobalAngleY >= 360) //clamp the rotation angle in the [0,360) interval
fGlobalAngleY = (GLint)fGlobalAngleY % 360;
break;
case 'd':
fGlobalAngleY -= 1;
if (fGlobalAngleY <= -360) //clamp the rotation angle in the [0,360) interval
fGlobalAngleY = (GLint)fGlobalAngleY % 360;
break;
//control the global X rotation angle using 'w' and 's'
case 'w':
fGlobalAngleX += 1;
if (fGlobalAngleX >= 360) //clamp the rotation angle in the [0,360) interval
fGlobalAngleX = (GLint)fGlobalAngleX % 360;
break;
case 's':
fGlobalAngleX -= 1;
if (fGlobalAngleX <= -360) //clamp the rotation angle in the [0,360) interval
fGlobalAngleX = (GLint)fGlobalAngleX % 360;
break;
//control the global Z rotation angle using 'q' and 'e'
case 'q':
fGlobalAngleZ += 1;
if (fGlobalAngleZ >= 360) //clamp the rotation angle in the [0,360) interval
fGlobalAngleZ = (GLint)fGlobalAngleZ % 360;
break;
case 'e':
fGlobalAngleZ -= 1;
if (fGlobalAngleZ <= -360) //clamp the rotation angle in the [0,360) interval
fGlobalAngleZ = (GLint)fGlobalAngleZ % 360;
break;
}
}
int main(int argc, char* argv[])
{
//Initialize the GLUT library
glutInit(&argc, argv);
//Set the display mode
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
//Set the initial position and dimensions of the window
glutInitWindowPosition(100, 100);
glutInitWindowSize(screen_width, screen_height);
//creates the window
glutCreateWindow("First OpenGL Application");
//Specifies the function to call when the window needs to be redisplayed
glutDisplayFunc(renderScene);
//Sets the idle callback function
glutIdleFunc(renderScene);
//Sets the reshape callback function
glutReshapeFunc(changeSize);
//Keyboard callback function
glutKeyboardFunc(processNormalKeys);
//Initialize some OpenGL parameters
initOpenGL();
//Starts the GLUT infinite loop
glutMainLoop();
return 0;
}