IndoorModel::IndoorModel(char* fileName, stack<mat4> *mvStack, GLuint program, vec4 transVec, vec3 rotVec, vec3 scalVec, bool texFlag, char* fileTextrue, int txWidth, int txHeight ) { _model = glmReadOBJ(fileName); //Loading the model if (!_model) exit(0); // See if it success glmUnitize(_model); // Normalize vertices glmFacetNormals(_model); // Compute facet normals glmVertexNormals(_model, 90.0); // Compute vertex normals glmLinearTexture(_model); //Map the texture to Model glmLoadGroupsInVBO(_model); // Load the model (vertices and normals) into a vertex buffer //World Coordinates Information _mvStack = mvStack; _program = program; _transVec = transVec; _rotVec = rotVec; _scalVec = scalVec; //Textures Information _texFlag = texFlag; _alphaFlag = false; if(texFlag){ _textures = glmReadPPM(fileTextrue, txWidth, txHeight); _txWidth = txWidth; _txHeight = txHeight; } }
SphereModel::SphereModel(char* frontName, char* BackName, char* LeftName, char* RightName, char* UpperName, char* BelowName, int txWidth, int txHeight, stack<mat4> *mvStack, GLuint program, GLuint cubemap, bool rotateFlag, bool indoorFlag, vec4 transVec, vec3 rotVec, GLfloat ScaleSize){ glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); glGenTextures(1, &cubemap); _frontTex = glmReadPPM(frontName, txWidth, txHeight); _backTex = glmReadPPM(BackName, txWidth, txHeight); _leftTex = glmReadPPM(LeftName, txWidth, txHeight); _rightTex = glmReadPPM(RightName, txWidth, txHeight); _topTex = glmReadPPM(UpperName, txWidth, txHeight); _bottomTex = glmReadPPM(BelowName, txWidth, txHeight); _RotateAngle = 0; _mvStack = mvStack; _program = program; _txWidth = txWidth; _txHeight = txHeight; _transVec = transVec; _rotVec = rotVec; _scalVec.x = ScaleSize; _rotateFlag = rotateFlag; _indoorFlag = indoorFlag; }
void world_menu(int value) { char* name = 0; switch (value) { case 'f': name = "data/fishermen.ppm"; break; case 'o': name = "data/opengl.ppm"; break; case 'c': name = "data/checker.ppm"; break; case 'm': name = "data/marble.ppm"; break; case 't': name = "data/train.ppm"; break; } if (name) { free(image); image = glmReadPPM(name, &iwidth, &iheight); if (!image) image = glmReadPPM("data/fishermen.ppm", &iwidth, &iheight); } glutSetWindow(screen); texture(); glutSetWindow(world); texture(); redisplay_all(); }
/* don't try alpha=GL_FALSE: gluScaleImage implementations seem to be buggy */ GLuint glmLoadTexture(const char *filename, GLboolean alpha, GLboolean repeat, GLboolean filtering, GLboolean mipmaps, GLfloat *texcoordwidth, GLfloat *texcoordheight) { GLuint tex; int width, height,pixelsize; int type; int filter_min, filter_mag; GLubyte *data, *rdata; double xPow2, yPow2; int ixPow2, iyPow2; int xSize2, ySize2; GLint retval; if(glm_do_init) glmImgInit(); #ifdef HAVE_SOIL tex = SOIL_load_OGL_texture(filename, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_POWER_OF_TWO ); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &height); //glTexEnvf(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_REPLACE); *texcoordwidth = width; *texcoordheight = height; return tex; #endif /* fallback solution (PPM only) */ data = glmReadPPM(filename, alpha, &width, &height, &type); if(data != NULL) { DBG_(__glmWarning("glmLoadTexture(): got PPM for %s",filename)); goto DONE; } #ifdef HAVE_DEVIL data = glmReadDevIL(filename, alpha, &width, &height, &type); if(data != NULL) { DBG_(__glmWarning("glmLoadTexture(): got DevIL for %s",filename)); goto DONE; } #endif #ifdef HAVE_LIBJPEG data = glmReadJPG(filename, alpha, &width, &height, &type); if(data != NULL) { DBG_(__glmWarning("glmLoadTexture(): got JPG for %s",filename)); goto DONE; } #endif #ifdef HAVE_LIBPNG data = glmReadPNG(filename, alpha, &width, &height, &type); if(data != NULL) { DBG_(__glmWarning("glmLoadTexture(): got PNG for %s",filename)); goto DONE; } #endif #ifdef HAVE_LIBSDL_IMAGE data = glmReadSDL(filename, alpha, &width, &height, &type); if(data != NULL) { DBG_(__glmWarning("glmLoadTexture(): got SDL for %s",filename)); goto DONE; } #endif #ifdef HAVE_LIBSIMAGE data = glmReadSimage(filename, alpha, &width, &height, &type); if(data != NULL) { DBG_(__glmWarning("glmLoadTexture(): got simage for %s",filename)); goto DONE; } #endif __glmWarning("glmLoadTexture() failed: Unable to load texture from %s!", filename); DBG_(__glmWarning("glmLoadTexture() failed: tried PPM")); #ifdef HAVE_LIBJPEG DBG_(__glmWarning("glmLoadTexture() failed: tried JPEG")); #endif #ifdef HAVE_LIBSDL_IMAGE DBG_(__glmWarning("glmLoadTexture() failed: tried SDL_image")); #endif return 0; DONE: /*#define FORCE_ALPHA*/ #ifdef FORCE_ALPHA if(alpha && type == GL_RGB) { /* if we really want RGBA */ const unsigned int size = width * height; unsigned char *rgbaimage; unsigned char *ptri, *ptro; int i; rgbaimage = (unsigned char*)malloc(sizeof(unsigned char)* size * 4); ptri = data; ptro = rgbaimage; for(i=0; i<size; i++) { *(ptro++) = *(ptri++); *(ptro++) = *(ptri++); *(ptro++) = *(ptri++); *(ptro++) = 255; } free(data); data = rgbaimage; type = GL_RGBA; } #endif /* FORCE_ALPHA */ switch(type) { case GL_LUMINANCE: pixelsize = 1; break; case GL_RGB: case GL_BGR: pixelsize = 3; break; case GL_RGBA: case GL_BGRA: pixelsize = 4; break; default: __glmFatalError( "glmLoadTexture(): unknown type 0x%x", type); pixelsize = 0; break; } if((pixelsize*width) % 4 == 0) glPixelStorei(GL_UNPACK_ALIGNMENT, 4); else glPixelStorei(GL_UNPACK_ALIGNMENT, 1); xSize2 = width; if (xSize2 > gl_max_texture_size) xSize2 = gl_max_texture_size; ySize2 = height; if (ySize2 > gl_max_texture_size) ySize2 = gl_max_texture_size; if (_glmTextureTarget == GL_TEXTURE_2D) { //if(1) { /* scale image to power of 2 in height and width */ xPow2 = log((double)xSize2) / log(2.0); yPow2 = log((double)ySize2) / log(2.0); ixPow2 = (int)xPow2; iyPow2 = (int)yPow2; if (xPow2 != (double)ixPow2) ixPow2++; if (yPow2 != (double)iyPow2) iyPow2++; xSize2 = 1 << ixPow2; ySize2 = 1 << iyPow2; } DBG_(__glmWarning("gl_max_texture_size=%d / width=%d / xSize2=%d / height=%d / ySize2 = %d", gl_max_texture_size, width, xSize2, height, ySize2)); if((width != xSize2) || (height != ySize2)) { /* TODO: use glTexSubImage2D instead */ DBG_(__glmWarning("scaling texture")); rdata = (GLubyte*)malloc(sizeof(GLubyte) * xSize2 * ySize2 * pixelsize); if (!rdata) return 0; retval = gluScaleImage(type, width, height, GL_UNSIGNED_BYTE, data, xSize2, ySize2, GL_UNSIGNED_BYTE, rdata); free(data); data = rdata; } glGenTextures(1, &tex); /* Generate texture ID */ glBindTexture(_glmTextureTarget, tex); DBG_(__glmWarning("building texture %d",tex)); if(mipmaps && _glmTextureTarget != GL_TEXTURE_2D) { DBG_(__glmWarning("mipmaps only work with GL_TEXTURE_2D")); mipmaps = 0; } if(filtering) { filter_min = (mipmaps) ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR; filter_mag = GL_LINEAR; } else { filter_min = (mipmaps) ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST; filter_mag = GL_NEAREST; } glTexParameteri(_glmTextureTarget, GL_TEXTURE_MIN_FILTER, filter_min); glTexParameteri(_glmTextureTarget, GL_TEXTURE_MAG_FILTER, filter_mag); glTexParameteri(_glmTextureTarget, GL_TEXTURE_WRAP_S, (repeat) ? GL_REPEAT : GL_CLAMP); glTexParameteri(_glmTextureTarget, GL_TEXTURE_WRAP_T, (repeat) ? GL_REPEAT : GL_CLAMP); if(mipmaps && _glmTextureTarget == GL_TEXTURE_2D) { /* only works for GL_TEXTURE_2D */ #ifdef GL_GENERATE_MIPMAP_SGIS if(gl_sgis_generate_mipmap) { DBG_(__glmWarning("sgis mipmapping")); glTexParameteri(_glmTextureTarget, GL_GENERATE_MIPMAP_SGIS, GL_TRUE ); glTexImage2D(_glmTextureTarget, 0, type, xSize2, ySize2, 0, type, GL_UNSIGNED_BYTE, data); } else #endif { DBG_(__glmWarning("glu mipmapping")); gluBuild2DMipmaps(_glmTextureTarget, type, xSize2, ySize2, type, GL_UNSIGNED_BYTE, data); } } else { glTexImage2D(_glmTextureTarget, 0, type, xSize2, ySize2, 0, type, GL_UNSIGNED_BYTE, data); } /* Clean up and return the texture ID */ free(data); if (_glmTextureTarget == GL_TEXTURE_2D) { *texcoordwidth = 1.; /* texcoords are in [0,1] */ *texcoordheight = 1.; } else { *texcoordwidth = xSize2; /* size of texture coords */ *texcoordheight = ySize2; } return tex; }
int main(int argc, char** argv) { image = glmReadPPM("data/terra.ppm", &iwidth, &iheight); if (!image) exit(0); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowSize((512+GAP*3)*3/2, 512+GAP*3); glutInitWindowPosition(50, 50); glutInit(&argc, argv); window = glutCreateWindow("Texture"); glutReshapeFunc(main_reshape); glutDisplayFunc(main_display); glutKeyboardFunc(main_keyboard); world = glutCreateSubWindow(window, GAP, GAP, 256, 256); glutReshapeFunc(world_reshape); glutDisplayFunc(world_display); glutKeyboardFunc(main_keyboard); glutCreateMenu(world_menu); glutAddMenuEntry("Textures", 0); glutAddMenuEntry("", 0); glutAddMenuEntry("Fishermen", 'f'); glutAddMenuEntry("OpenGL Logo", 'o'); glutAddMenuEntry("Checker", 'c'); glutAddMenuEntry("Marble", 'm'); glutAddMenuEntry("Train", 't'); glutAttachMenu(GLUT_RIGHT_BUTTON); texture(); screen = glutCreateSubWindow(window, GAP+256+GAP, GAP, 256, 256); glutReshapeFunc(screen_reshape); glutDisplayFunc(screen_display); glutKeyboardFunc(main_keyboard); glutMotionFunc(screen_motion); glutMouseFunc(screen_mouse); texture(); command = glutCreateSubWindow(window, GAP+256+GAP, GAP+256+GAP, 256, 256); glutReshapeFunc(command_reshape); glutMotionFunc(command_motion); glutDisplayFunc(parameters_display); glutMouseFunc(parameters_mouse); glutKeyboardFunc(main_keyboard); glutCreateMenu(command_menu); glutAddMenuEntry("Texture", 0); glutAddMenuEntry("", 0); glutAddMenuEntry("Matrix", 'm'); glutAddMenuEntry("Environment/Parameters", 'p'); glutAddMenuEntry("Reset parameters (r)", 'r'); glutAddMenuEntry("", 0); glutAddMenuEntry("Quit", 27); glutAttachMenu(GLUT_RIGHT_BUTTON); redisplay_all(); glutTimerFunc(500, timer, 0); glutMainLoop(); return 0; }
// OpenGL initialization void init() { /** *** Create and initialize buffer objects **/ glGenBuffers( 4, buffers ); //Vertex buffer for the vertex coordinates glBindBuffer( GL_ARRAY_BUFFER, buffers[cube_vertices] ); glBufferData( GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW ); glBindBuffer( GL_ARRAY_BUFFER, buffers[cube_vertices_explicit] ); glBufferData( GL_ARRAY_BUFFER, sizeof(cube_vertices_positions), cube_vertices_positions, GL_STATIC_DRAW ); std::cout<< "sizeof(cube_vertices_positions)" << sizeof(cube_vertices_positions) << std::endl; //Elements buffer for the pointers glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, buffers[cube_indeces] ); glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glGenVertexArrays(2, VAO); // Load shaders and use the resulting shader programs program[0] = InitShader( "./shaders/vshader30_TwoCubes_FullPipe.glsl", "./shaders/fshader30_TwoCubes.glsl" ); program[1] = InitShader( "./shaders/skyboxvertex.glsl", "./shaders/skyboxfragment.glsl" ); //VAO[1] the skybox glUseProgram( program[1] ); glBindVertexArray(VAO[1]); glBindBuffer( GL_ARRAY_BUFFER, buffers[cube_vertices_explicit] ); vPosition = glGetAttribLocation( program[1], "vPosition" ); glEnableVertexAttribArray( vPosition ); glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); //done with this packet glBindVertexArray(0); glUseProgram(0); //VAO[0] the Cube glUseProgram( program[0] ); glBindVertexArray(VAO[0]); glBindBuffer( GL_ARRAY_BUFFER, buffers[cube_vertices] ); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[cube_indeces]); vPosition = glGetAttribLocation( program[0], "vPosition" ); glEnableVertexAttribArray( vPosition ); glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); //done with this packet glBindVertexArray(0); glUseProgram(0); //Skybox textures //Load Skybox Images. 6 images to represent the 6 angles of view. Inside it's own structured Cubemap skybox.top = glmReadPPM("skybox\\sky-top.ppm", &skybox.topWidth, &skybox.topHeight); skybox.bottom = glmReadPPM("skybox\\sky-bottom.ppm", &skybox.bottomWidth, &skybox.bottomHeight); skybox.right = glmReadPPM("skybox\\sky-right.ppm", &skybox.rightWidth, &skybox.rightHeight); skybox.left = glmReadPPM("skybox\\sky-left.ppm", &skybox.leftWidth, &skybox.leftHeight); skybox.front = glmReadPPM("skybox\\sky-front.ppm", &skybox.frontWidth, &skybox.frontHeight); skybox.back = glmReadPPM("skybox\\sky-back.ppm", &skybox.backWidth, &skybox.backHeight); glActiveTexture(GL_TEXTURE0); glGenTextures(1, &texture); int isEnabled=0; if (glIsEnabled(GL_TEXTURE_CUBE_MAP) == GL_TRUE) {isEnabled = 1;} else {isEnabled = 0;}; std::cout << isEnabled << std::endl; glEnable(GL_TEXTURE_CUBE_MAP); if (glIsEnabled(GL_TEXTURE_CUBE_MAP) == GL_TRUE) {isEnabled = 1;} else {isEnabled = 0;}; std::cout << isEnabled << std::endl; glBindTexture(GL_TEXTURE_CUBE_MAP, texture); glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_GENERATE_MIPMAP, GL_TRUE); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, skybox.topWidth, skybox.topHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,skybox.top); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, skybox.bottomWidth, skybox.bottomHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,skybox.bottom); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, skybox.rightWidth, skybox.rightHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,skybox.right); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, skybox.leftWidth, skybox.rightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE,skybox.left); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, skybox.frontWidth, skybox.frontHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,skybox.front); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, skybox.backWidth, skybox.backHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,skybox.back); glGenerateMipmap(GL_TEXTURE_CUBE_MAP); glClearColor( 1.0, 1.0, 1.0, 0.0 ); glClearDepth( 1.0 ); glEnable( GL_DEPTH_TEST ); glDepthFunc(GL_LEQUAL); }