void CChildView::InitGL() { // Create a checkerboard pattern for (int i = 0; i < 64; i++) { for (int j = 0; j < 64; j++) { GLubyte c; c = (((i & 0x8) == 0) ^ ((j & 0x8) == 0)) * 255; image[i][j][0] = c; image[i][j][1] = c; image[i][j][2] = c; } } for (int i = 0; i < 64; i++) { for (int j = 0; j < 64; j++) { GLubyte c; c = (((i & 0x16) == 0) | ((j & 0x16) == 0)) * 255; image2[i][j][0] = c; image2[i][j][1] = 0; image2[i][j][2] = c; } } m_program = LoadShaders( "ShaderWnd/vertex.glsl", "ShaderWnd/fragment.glsl" ); //GLuint vertexbuffer; glUseProgram(m_program); glGenTextures(2, textures); glBindTexture(GL_TEXTURE_2D, textures[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureSize,TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, textures[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureSize,TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image2); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, textures[0]); glActiveTexture( GL_TEXTURE1 ); glBindTexture( GL_TEXTURE_2D, textures[1]); glActiveTexture( GL_TEXTURE2 ); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glBindTexture(GL_TEXTURE_2D, m_brick.TexName()); m_cube->InitGL(m_program); m_wall->InitGL(m_program); point4 light_position (-5.f, 5.f, -5.f, 0.f); color4 light_ambient (0.2f, 0.2f, 0.2f, 1.f); color4 light_diffuse (1.f, 1.f, 1.f, 1.f); color4 light_specular (1.f, 1.f, 1.f, 1.f); color4 material_ambient(.3f, .3f, .3f, 1.f); color4 material_diffuse (0.3f, .3f, 0.3f, 1.f); color4 material_specular (1.f, 1.f, 1.f, 1.f); float material_shininess = 100.0f; color4 ambient_product = light_ambient*material_ambient; color4 diffuse_product = light_diffuse*material_diffuse; color4 specular_product = light_specular*material_specular; glUniform4fv(glGetUniformLocation(m_program, "AmbientProduct"), 1, value_ptr(ambient_product)); glUniform4fv(glGetUniformLocation(m_program, "DiffuseProduct"), 1, value_ptr(diffuse_product)); glUniform4fv(glGetUniformLocation(m_program, "SpecularProduct"), 1, value_ptr(specular_product)); glUniform4fv(glGetUniformLocation(m_program, "LightPosition"), 1, value_ptr(light_position)); glUniform1f(glGetUniformLocation(m_program, "Shininess"), material_shininess); //Set the value of the fragment shader texture sampler variable // ("texture") to the the appropriate texture unit. In this case, // zero, for GL_TEXTURE0 which was previously set by calling // glActiveTexture(). glUniform1i( glGetUniformLocation(m_program, "diffuse_mat"), 0); m_nPVM = glGetUniformLocation(m_program, "mPVM"); m_nVM = glGetUniformLocation(m_program, "mVM"); glClearColor(1.f,1.f,1.f,1.f); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); // Enable blending glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); }
void CChildView::InitGL() { // Create a checkerboard pattern for (int i = 0; i < 64; i++) { for (int j = 0; j < 64; j++) { GLubyte c; c = (((i & 0x8) == 0) ^ ((j & 0x8) == 0)) * 255; image[i][j][0] = c; image[i][j][1] = c; image[i][j][2] = c; image2[i][j][0] = c; image2[i][j][1] = 0; image2[i][j][2] = c; } } colorcube(); m_program = LoadShaders( "ShaderWnd/vertex.glsl", "ShaderWnd/fragment.glsl" ); static const vec4 g_vertex_buffer_data[] = { vec4(-1.0f, -1.0f, 0.0f, 1.0f), vec4(1.0f, -1.0f, 0.0f, 1.0f), vec4(0.0f, 0.5f, 0.0f, 1.0f) }; static const vec4 g_color_buffer_data[] = { vec4(1.0f, 0.0f, 0.0f, 1.0f), vec4(0.0f, 1.0f, 0.0f, 1.0f), vec4(0.0f, 0.0f, 1.0f, 1.0f) }; //GLuint vertexbuffer; glUseProgram(m_program); GLuint vao; glGenVertexArrays( 1, &vao); glBindVertexArray( vao); /*glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);*/ glGenTextures(2, textures); glBindTexture(GL_TEXTURE_2D, textures[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureSize,TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, textures[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureSize,TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image2); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, textures[0]); glActiveTexture( GL_TEXTURE1 ); glBindTexture( GL_TEXTURE_2D, textures[1]); // Create and initialize a buffer object GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(tex_coords) + sizeof(normals), NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points), value_ptr(points[0])); glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), sizeof(normals), value_ptr(normals[0])); glBufferSubData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(normals), sizeof(tex_coords), value_ptr(tex_coords[0])); point4 light_position (-5.f, 5.f, -5.f, 0.f); color4 light_ambient (0.2f, 0.2f, 0.2f, 1.f); color4 light_diffuse (1.f, 1.f, 1.f, 1.f); color4 light_specular (1.f, 1.f, 1.f, 1.f); color4 material_ambient(.3f, .6f, .3f, 1.f); color4 material_diffuse (0.3f, .6f, 0.3f, 1.f); color4 material_specular (1.f, 1.f, 1.f, 1.f); float material_shininess = 100.0f; color4 ambient_product = light_ambient*material_ambient; color4 diffuse_product = light_diffuse*material_diffuse; color4 specular_product = light_specular*material_specular; glUniform4fv(glGetUniformLocation(m_program, "AmbientProduct"), 1, value_ptr(ambient_product)); glUniform4fv(glGetUniformLocation(m_program, "DiffuseProduct"), 1, value_ptr(diffuse_product)); glUniform4fv(glGetUniformLocation(m_program, "SpecularProduct"), 1, value_ptr(specular_product)); glUniform4fv(glGetUniformLocation(m_program, "LightPosition"), 1, value_ptr(light_position)); glUniform1f(glGetUniformLocation(m_program, "Shininess"), material_shininess); // set up vertex arrays (after shaders are loaded) GLuint vPosition = glGetAttribLocation(m_program, "vPosition"); glEnableVertexAttribArray(vPosition); glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); GLuint vNormal = glGetAttribLocation(m_program, "vNormal"); glEnableVertexAttribArray(vNormal); glVertexAttribPointer(vNormal, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points))); GLuint vTex = glGetAttribLocation(m_program, "vTexCoord"); glEnableVertexAttribArray(vTex); glVertexAttribPointer(vTex, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)+sizeof(normals))); // Set the value of the fragment shader texture sampler variable // ("texture") to the the appropriate texture unit. In this case, // zero, for GL_TEXTURE0 which was previously set by calling // glActiveTexture(). glUniform1i( glGetUniformLocation(m_program, "diffuse_mat"), 0); m_nPVM = glGetUniformLocation(m_program, "mPVM"); m_nVM = glGetUniformLocation(m_program, "mVM"); glClearColor(1.f,1.f,1.f,1.f); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); }
// OpenGL initialization void init() { // Subdivide a tetrahedron into a sphere tetrahedron( NumTimesToSubdivide ); // Create a vertex array object GLuint vao; glGenVertexArrays( 1, &vao ); glBindVertexArray( vao ); // Create and initialize a buffer object GLuint buffer; glGenBuffers( 1, &buffer ); glBindBuffer( GL_ARRAY_BUFFER, buffer ); glBufferData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(normals), NULL, GL_STATIC_DRAW ); glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(points), points ); glBufferSubData( GL_ARRAY_BUFFER, sizeof(points), sizeof(normals), normals ); // Load shaders and use the resulting shader program GLuint program = InitShader( "vshader56.glsl", "fshader56.glsl" ); glUseProgram( program ); // set up vertex arrays GLuint vPosition = glGetAttribLocation( program, "vPosition" ); glEnableVertexAttribArray( vPosition ); glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); GLuint vNormal = glGetAttribLocation( program, "vNormal" ); glEnableVertexAttribArray( vNormal ); glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)) ); // Initialize shader lighting parameters point4 light_position( 0.0, 0.0, 2.0, 0.0 ); color4 light_ambient( 0.2, 0.2, 0.2, 1.0 ); color4 light_diffuse( 1.0, 1.0, 1.0, 1.0 ); color4 light_specular( 1.0, 1.0, 1.0, 1.0 ); color4 material_ambient( 1.0, 0.0, 1.0, 1.0 ); color4 material_diffuse( 1.0, 0.8, 0.0, 1.0 ); color4 material_specular( 1.0, 0.0, 1.0, 1.0 ); float material_shininess = 5.0; color4 ambient_product = light_ambient * material_ambient; color4 diffuse_product = light_diffuse * material_diffuse; color4 specular_product = light_specular * material_specular; glUniform4fv( glGetUniformLocation(program, "AmbientProduct"), 1, ambient_product ); glUniform4fv( glGetUniformLocation(program, "DiffuseProduct"), 1, diffuse_product ); glUniform4fv( glGetUniformLocation(program, "SpecularProduct"), 1, specular_product ); glUniform4fv( glGetUniformLocation(program, "LightPosition"), 1, light_position ); glUniform1f( glGetUniformLocation(program, "Shininess"), material_shininess ); // Retrieve transformation uniform variable locations ModelView = glGetUniformLocation( program, "ModelView" ); Projection = glGetUniformLocation( program, "Projection" ); glEnable( GL_DEPTH_TEST ); glClearColor( 1.0, 1.0, 1.0, 1.0 ); /* white background */ }
virtual void updateTexturesAndMeshes(IGameDef *gamedef) { #ifndef SERVER infostream<<"ItemDefManager::updateTexturesAndMeshes(): Updating " <<"textures and meshes in item definitions"<<std::endl; ITextureSource *tsrc = gamedef->getTextureSource(); INodeDefManager *nodedef = gamedef->getNodeDefManager(); IrrlichtDevice *device = tsrc->getDevice(); video::IVideoDriver *driver = device->getVideoDriver(); for(std::map<std::string, ItemDefinition*>::iterator i = m_item_definitions.begin(); i != m_item_definitions.end(); i++) { ItemDefinition *def = i->second; bool need_node_mesh = false; // Create an inventory texture def->inventory_texture = NULL; if(def->inventory_image != "") { def->inventory_texture = tsrc->getTextureRaw(def->inventory_image); } else if(def->type == ITEM_NODE) { need_node_mesh = true; } // Create a wield mesh if(def->wield_mesh != NULL) { def->wield_mesh->drop(); def->wield_mesh = NULL; } if(def->type == ITEM_NODE && def->wield_image == "") { need_node_mesh = true; } else if(def->wield_image != "" || def->inventory_image != "") { // Extrude the wield image into a mesh std::string imagename; if(def->wield_image != "") imagename = def->wield_image; else imagename = def->inventory_image; def->wield_mesh = createExtrudedMesh( tsrc->getTextureRaw(imagename), driver, def->wield_scale * v3f(40.0, 40.0, 4.0)); if(def->wield_mesh == NULL) { infostream<<"ItemDefManager: WARNING: " <<"updateTexturesAndMeshes(): " <<"Unable to create extruded mesh for item " <<def->name<<std::endl; } } if(need_node_mesh) { /* Get node properties */ content_t id = nodedef->getId(def->name); const ContentFeatures &f = nodedef->get(id); u8 param1 = 0; if(f.param_type == CPT_LIGHT) param1 = 0xee; /* Make a mesh from the node */ MeshMakeData mesh_make_data(gamedef); MapNode mesh_make_node(id, param1, 0); mesh_make_data.fillSingleNode(&mesh_make_node); MapBlockMesh mapblock_mesh(&mesh_make_data); scene::IMesh *node_mesh = mapblock_mesh.getMesh(); assert(node_mesh); setMeshColor(node_mesh, video::SColor(255, 255, 255, 255)); /* Scale and translate the mesh so it's a unit cube centered on the origin */ scaleMesh(node_mesh, v3f(1.0/BS, 1.0/BS, 1.0/BS)); translateMesh(node_mesh, v3f(-1.0, -1.0, -1.0)); /* Draw node mesh into a render target texture */ if(def->inventory_texture == NULL) { core::dimension2d<u32> dim(64,64); std::string rtt_texture_name = "INVENTORY_" + def->name + "_RTT"; v3f camera_position(0, 1.0, -1.5); camera_position.rotateXZBy(45); v3f camera_lookat(0, 0, 0); core::CMatrix4<f32> camera_projection_matrix; // Set orthogonal projection camera_projection_matrix.buildProjectionMatrixOrthoLH( 1.65, 1.65, 0, 100); video::SColorf ambient_light(0.2,0.2,0.2); v3f light_position(10, 100, -50); video::SColorf light_color(0.5,0.5,0.5); f32 light_radius = 1000; def->inventory_texture = generateTextureFromMesh( node_mesh, device, dim, rtt_texture_name, camera_position, camera_lookat, camera_projection_matrix, ambient_light, light_position, light_color, light_radius); // render-to-target didn't work if(def->inventory_texture == NULL) { def->inventory_texture = tsrc->getTextureRaw(f.tname_tiles[0]); } } /* Use the node mesh as the wield mesh */ if(def->wield_mesh == NULL) { // Scale to proper wield mesh proportions scaleMesh(node_mesh, v3f(30.0, 30.0, 30.0) * def->wield_scale); def->wield_mesh = node_mesh; def->wield_mesh->grab(); } // falling outside of here deletes node_mesh } } #endif }
bool generate_image(std::string part_of_name, video::IImage *& baseimg, IrrlichtDevice *device, SourceImageCache *sourcecache) { video::IVideoDriver* driver = device->getVideoDriver(); assert(driver); // Stuff starting with [ are special commands if(part_of_name.size() == 0 || part_of_name[0] != '[') { video::IImage *image = sourcecache->getOrLoad(part_of_name, device); if(image == NULL) { if(part_of_name != ""){ errorstream<<"generate_image(): Could not load image \"" <<part_of_name<<"\""<<" while building texture"<<std::endl; errorstream<<"generate_image(): Creating a dummy" <<" image for \""<<part_of_name<<"\""<<std::endl; } // Just create a dummy image //core::dimension2d<u32> dim(2,2); core::dimension2d<u32> dim(1,1); image = driver->createImage(video::ECF_A8R8G8B8, dim); assert(image); /*image->setPixel(0,0, video::SColor(255,255,0,0)); image->setPixel(1,0, video::SColor(255,0,255,0)); image->setPixel(0,1, video::SColor(255,0,0,255)); image->setPixel(1,1, video::SColor(255,255,0,255));*/ image->setPixel(0,0, video::SColor(255,myrand()%256, myrand()%256,myrand()%256)); /*image->setPixel(1,0, video::SColor(255,myrand()%256, myrand()%256,myrand()%256)); image->setPixel(0,1, video::SColor(255,myrand()%256, myrand()%256,myrand()%256)); image->setPixel(1,1, video::SColor(255,myrand()%256, myrand()%256,myrand()%256));*/ } // If base image is NULL, load as base. if(baseimg == NULL) { //infostream<<"Setting "<<part_of_name<<" as base"<<std::endl; /* Copy it this way to get an alpha channel. Otherwise images with alpha cannot be blitted on images that don't have alpha in the original file. */ core::dimension2d<u32> dim = image->getDimension(); baseimg = driver->createImage(video::ECF_A8R8G8B8, dim); image->copyTo(baseimg); image->drop(); } // Else blit on base. else { //infostream<<"Blitting "<<part_of_name<<" on base"<<std::endl; // Size of the copied area core::dimension2d<u32> dim = image->getDimension(); //core::dimension2d<u32> dim(16,16); // Position to copy the blitted to in the base image core::position2d<s32> pos_to(0,0); // Position to copy the blitted from in the blitted image core::position2d<s32> pos_from(0,0); // Blit image->copyToWithAlpha(baseimg, pos_to, core::rect<s32>(pos_from, dim), video::SColor(255,255,255,255), NULL); // Drop image image->drop(); } } else { // A special texture modification /*infostream<<"generate_image(): generating special " <<"modification \""<<part_of_name<<"\"" <<std::endl;*/ /* This is the simplest of all; it just adds stuff to the name so that a separate texture is created. It is used to make textures for stuff that doesn't want to implement getting the texture from a bigger texture atlas. */ if(part_of_name == "[forcesingle") { // If base image is NULL, create a random color if(baseimg == NULL) { core::dimension2d<u32> dim(1,1); baseimg = driver->createImage(video::ECF_A8R8G8B8, dim); assert(baseimg); baseimg->setPixel(0,0, video::SColor(255,myrand()%256, myrand()%256,myrand()%256)); } } /* [crackN Adds a cracking texture */ else if(part_of_name.substr(0,6) == "[crack") { if(baseimg == NULL) { errorstream<<"generate_image(): baseimg==NULL " <<"for part_of_name=\""<<part_of_name <<"\", cancelling."<<std::endl; return false; } // Crack image number and overlay option s32 progression = 0; bool use_overlay = false; if(part_of_name.substr(6,1) == "o") { progression = stoi(part_of_name.substr(7)); use_overlay = true; } else { progression = stoi(part_of_name.substr(6)); use_overlay = false; } // Size of the base image core::dimension2d<u32> dim_base = baseimg->getDimension(); /* Load crack image. It is an image with a number of cracking stages horizontally tiled. */ video::IImage *img_crack = sourcecache->getOrLoad("crack.png", device); if(img_crack && progression >= 0) { // Dimension of original image core::dimension2d<u32> dim_crack = img_crack->getDimension(); // Count of crack stages s32 crack_count = dim_crack.Height / dim_crack.Width; // Limit progression if(progression > crack_count-1) progression = crack_count-1; // Dimension of a single crack stage core::dimension2d<u32> dim_crack_cropped( dim_crack.Width, dim_crack.Width ); // Create cropped and scaled crack images video::IImage *img_crack_cropped = driver->createImage( video::ECF_A8R8G8B8, dim_crack_cropped); video::IImage *img_crack_scaled = driver->createImage( video::ECF_A8R8G8B8, dim_base); if(img_crack_cropped && img_crack_scaled) { // Crop crack image v2s32 pos_crack(0, progression*dim_crack.Width); img_crack->copyTo(img_crack_cropped, v2s32(0,0), core::rect<s32>(pos_crack, dim_crack_cropped)); // Scale crack image by copying img_crack_cropped->copyToScaling(img_crack_scaled); // Copy or overlay crack image if(use_overlay) { overlay(baseimg, img_crack_scaled); } else { img_crack_scaled->copyToWithAlpha( baseimg, v2s32(0,0), core::rect<s32>(v2s32(0,0), dim_base), video::SColor(255,255,255,255)); } } if(img_crack_scaled) img_crack_scaled->drop(); if(img_crack_cropped) img_crack_cropped->drop(); img_crack->drop(); } } /* [combine:WxH:X,Y=filename:X,Y=filename2 Creates a bigger texture from an amount of smaller ones */ else if(part_of_name.substr(0,8) == "[combine") { Strfnd sf(part_of_name); sf.next(":"); u32 w0 = stoi(sf.next("x")); u32 h0 = stoi(sf.next(":")); infostream<<"combined w="<<w0<<" h="<<h0<<std::endl; core::dimension2d<u32> dim(w0,h0); baseimg = driver->createImage(video::ECF_A8R8G8B8, dim); while(sf.atend() == false) { u32 x = stoi(sf.next(",")); u32 y = stoi(sf.next("=")); std::string filename = sf.next(":"); infostream<<"Adding \""<<filename <<"\" to combined ("<<x<<","<<y<<")" <<std::endl; video::IImage *img = sourcecache->getOrLoad(filename, device); if(img) { core::dimension2d<u32> dim = img->getDimension(); infostream<<"Size "<<dim.Width <<"x"<<dim.Height<<std::endl; core::position2d<s32> pos_base(x, y); video::IImage *img2 = driver->createImage(video::ECF_A8R8G8B8, dim); img->copyTo(img2); img->drop(); img2->copyToWithAlpha(baseimg, pos_base, core::rect<s32>(v2s32(0,0), dim), video::SColor(255,255,255,255), NULL); img2->drop(); } else { infostream<<"img==NULL"<<std::endl; } } } /* "[brighten" */ else if(part_of_name.substr(0,9) == "[brighten") { if(baseimg == NULL) { errorstream<<"generate_image(): baseimg==NULL " <<"for part_of_name=\""<<part_of_name <<"\", cancelling."<<std::endl; return false; } brighten(baseimg); } /* "[noalpha" Make image completely opaque. Used for the leaves texture when in old leaves mode, so that the transparent parts don't look completely black when simple alpha channel is used for rendering. */ else if(part_of_name.substr(0,8) == "[noalpha") { if(baseimg == NULL) { errorstream<<"generate_image(): baseimg==NULL " <<"for part_of_name=\""<<part_of_name <<"\", cancelling."<<std::endl; return false; } core::dimension2d<u32> dim = baseimg->getDimension(); // Set alpha to full for(u32 y=0; y<dim.Height; y++) for(u32 x=0; x<dim.Width; x++) { video::SColor c = baseimg->getPixel(x,y); c.setAlpha(255); baseimg->setPixel(x,y,c); } } /* "[makealpha:R,G,B" Convert one color to transparent. */ else if(part_of_name.substr(0,11) == "[makealpha:") { if(baseimg == NULL) { errorstream<<"generate_image(): baseimg==NULL " <<"for part_of_name=\""<<part_of_name <<"\", cancelling."<<std::endl; return false; } Strfnd sf(part_of_name.substr(11)); u32 r1 = stoi(sf.next(",")); u32 g1 = stoi(sf.next(",")); u32 b1 = stoi(sf.next("")); std::string filename = sf.next(""); core::dimension2d<u32> dim = baseimg->getDimension(); /*video::IImage *oldbaseimg = baseimg; baseimg = driver->createImage(video::ECF_A8R8G8B8, dim); oldbaseimg->copyTo(baseimg); oldbaseimg->drop();*/ // Set alpha to full for(u32 y=0; y<dim.Height; y++) for(u32 x=0; x<dim.Width; x++) { video::SColor c = baseimg->getPixel(x,y); u32 r = c.getRed(); u32 g = c.getGreen(); u32 b = c.getBlue(); if(!(r == r1 && g == g1 && b == b1)) continue; c.setAlpha(0); baseimg->setPixel(x,y,c); } } /* [inventorycube{topimage{leftimage{rightimage In every subimage, replace ^ with &. Create an "inventory cube". NOTE: This should be used only on its own. Example (a grass block (not actually used in game): "[inventorycube{grass.png{mud.png&grass_side.png{mud.png&grass_side.png" */ else if(part_of_name.substr(0,14) == "[inventorycube") { if(baseimg != NULL) { errorstream<<"generate_image(): baseimg!=NULL " <<"for part_of_name=\""<<part_of_name <<"\", cancelling."<<std::endl; return false; } str_replace_char(part_of_name, '&', '^'); Strfnd sf(part_of_name); sf.next("{"); std::string imagename_top = sf.next("{"); std::string imagename_left = sf.next("{"); std::string imagename_right = sf.next("{"); // Generate images for the faces of the cube video::IImage *img_top = generate_image_from_scratch( imagename_top, device, sourcecache); video::IImage *img_left = generate_image_from_scratch( imagename_left, device, sourcecache); video::IImage *img_right = generate_image_from_scratch( imagename_right, device, sourcecache); assert(img_top && img_left && img_right); // Create textures from images video::ITexture *texture_top = driver->addTexture( (imagename_top + "__temp__").c_str(), img_top); video::ITexture *texture_left = driver->addTexture( (imagename_left + "__temp__").c_str(), img_left); video::ITexture *texture_right = driver->addTexture( (imagename_right + "__temp__").c_str(), img_right); assert(texture_top && texture_left && texture_right); // Drop images img_top->drop(); img_left->drop(); img_right->drop(); /* Draw a cube mesh into a render target texture */ scene::IMesh* cube = createCubeMesh(v3f(1, 1, 1)); setMeshColor(cube, video::SColor(255, 255, 255, 255)); cube->getMeshBuffer(0)->getMaterial().setTexture(0, texture_top); cube->getMeshBuffer(1)->getMaterial().setTexture(0, texture_top); cube->getMeshBuffer(2)->getMaterial().setTexture(0, texture_right); cube->getMeshBuffer(3)->getMaterial().setTexture(0, texture_right); cube->getMeshBuffer(4)->getMaterial().setTexture(0, texture_left); cube->getMeshBuffer(5)->getMaterial().setTexture(0, texture_left); core::dimension2d<u32> dim(64,64); std::string rtt_texture_name = part_of_name + "_RTT"; v3f camera_position(0, 1.0, -1.5); camera_position.rotateXZBy(45); v3f camera_lookat(0, 0, 0); core::CMatrix4<f32> camera_projection_matrix; // Set orthogonal projection camera_projection_matrix.buildProjectionMatrixOrthoLH( 1.65, 1.65, 0, 100); video::SColorf ambient_light(0.2,0.2,0.2); v3f light_position(10, 100, -50); video::SColorf light_color(0.5,0.5,0.5); f32 light_radius = 1000; video::ITexture *rtt = generateTextureFromMesh( cube, device, dim, rtt_texture_name, camera_position, camera_lookat, camera_projection_matrix, ambient_light, light_position, light_color, light_radius); // Drop mesh cube->drop(); // Free textures of images driver->removeTexture(texture_top); driver->removeTexture(texture_left); driver->removeTexture(texture_right); if(rtt == NULL) { baseimg = generate_image_from_scratch( imagename_top, device, sourcecache); return true; } // Create image of render target video::IImage *image = driver->createImage(rtt, v2s32(0,0), dim); assert(image); baseimg = driver->createImage(video::ECF_A8R8G8B8, dim); if(image) { image->copyTo(baseimg); image->drop(); } } else { errorstream<<"generate_image(): Invalid " " modification: \""<<part_of_name<<"\""<<std::endl; } } return true; }
ClientCached* createClientCachedDirect(const std::string &name, IGameDef *gamedef) const { infostream<<"Lazily creating item texture and mesh for \"" <<name<<"\""<<std::endl; // This is not thread-safe assert(get_current_thread_id() == m_main_thread); // Skip if already in cache ClientCached *cc = NULL; m_clientcached.get(name, &cc); if(cc) return cc; ITextureSource *tsrc = gamedef->getTextureSource(); INodeDefManager *nodedef = gamedef->getNodeDefManager(); IrrlichtDevice *device = tsrc->getDevice(); video::IVideoDriver *driver = device->getVideoDriver(); const ItemDefinition *def = &get(name); // Create new ClientCached cc = new ClientCached(); bool need_node_mesh = false; // Create an inventory texture cc->inventory_texture = NULL; if(def->inventory_image != "") { cc->inventory_texture = tsrc->getTextureRaw(def->inventory_image); } else if(def->type == ITEM_NODE) { need_node_mesh = true; } // Create a wield mesh assert(cc->wield_mesh == NULL); if(def->type == ITEM_NODE && def->wield_image == "") { need_node_mesh = true; } else if(def->wield_image != "" || def->inventory_image != "") { // Extrude the wield image into a mesh std::string imagename; if(def->wield_image != "") imagename = def->wield_image; else imagename = def->inventory_image; cc->wield_mesh = createExtrudedMesh( tsrc->getTextureRaw(imagename), driver, def->wield_scale * v3f(40.0, 40.0, 4.0)); if(cc->wield_mesh == NULL) { infostream<<"ItemDefManager: WARNING: " <<"updateTexturesAndMeshes(): " <<"Unable to create extruded mesh for item " <<def->name<<std::endl; } } if(need_node_mesh) { /* Get node properties */ content_t id = nodedef->getId(def->name); const ContentFeatures &f = nodedef->get(id); u8 param1 = 0; if(f.param_type == CPT_LIGHT) param1 = 0xee; /* Make a mesh from the node */ MeshMakeData mesh_make_data(gamedef); MapNode mesh_make_node(id, param1, 0); mesh_make_data.fillSingleNode(&mesh_make_node); MapBlockMesh mapblock_mesh(&mesh_make_data); scene::IMesh *node_mesh = mapblock_mesh.getMesh(); assert(node_mesh); video::SColor c(255, 255, 255, 255); if(g_settings->getS32("enable_shaders") != 0) c = MapBlock_LightColor(255, 0xffff, decode_light(f.light_source)); setMeshColor(node_mesh, c); /* Scale and translate the mesh so it's a unit cube centered on the origin */ scaleMesh(node_mesh, v3f(1.0/BS, 1.0/BS, 1.0/BS)); translateMesh(node_mesh, v3f(-1.0, -1.0, -1.0)); /* Draw node mesh into a render target texture */ if(cc->inventory_texture == NULL) { core::dimension2d<u32> dim(64,64); std::string rtt_texture_name = "INVENTORY_" + def->name + "_RTT"; v3f camera_position(0, 1.0, -1.5); camera_position.rotateXZBy(45); v3f camera_lookat(0, 0, 0); core::CMatrix4<f32> camera_projection_matrix; // Set orthogonal projection camera_projection_matrix.buildProjectionMatrixOrthoLH( 1.65, 1.65, 0, 100); video::SColorf ambient_light(0.2,0.2,0.2); v3f light_position(10, 100, -50); video::SColorf light_color(0.5,0.5,0.5); f32 light_radius = 1000; cc->inventory_texture = generateTextureFromMesh( node_mesh, device, dim, rtt_texture_name, camera_position, camera_lookat, camera_projection_matrix, ambient_light, light_position, light_color, light_radius); // render-to-target didn't work if(cc->inventory_texture == NULL) { cc->inventory_texture = tsrc->getTextureRaw(f.tiledef[0].name); } } else { if (m_driver == 0) m_driver = driver; m_extruded_textures.push_back(cc->inventory_texture); } /* Use the node mesh as the wield mesh */ // Scale to proper wield mesh proportions scaleMesh(node_mesh, v3f(30.0, 30.0, 30.0) * def->wield_scale); cc->wield_mesh = node_mesh; cc->wield_mesh->grab(); //no way reference count can be smaller than 2 in this place! assert(cc->wield_mesh->getReferenceCount() >= 2); } // Put in cache m_clientcached.set(name, cc); return cc; }
void light_move(Light l, mapVec delta) { light_set_position(l, mapvec_add(light_position(l), delta)); }
/** * draw() * * Draw to the screen. */ void draw() { //set background color to black glClearColor(0.0, 0.0, 0.0, 0.0); //clear info from last draw glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //switch to the drawing perspective glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //rotate and translate the scene to simulate camera movement glRotatef(camera_pitch, 1.0, 0.0, 0.0); glRotatef(camera_yaw, 0.0, 1.0, 0.0); glRotatef(camera_roll, 0.0, 0.0, 1.0); glTranslatef(camera_x, camera_y, camera_z); //initiate camera position and angle gluLookAt (0.0, 5.0, -35.0, //camera position 0.0, 5.0, 0.0, //look-at point 0.0, 1.0, 0.0); //up direction //draw light #1 and its light stand object if (draw_light1) { //turn on light #1 glEnable(GL_LIGHT0); //draw light #1 effect GLfloat diff_light[] = {light1_red, light1_green, light1_blue, 1.0}; GLfloat l_position[] = {light1_x, 1 + light1_height + 2*sinf(PI/18), light1_z, 1.0f}; glLightfv (GL_LIGHT0, GL_DIFFUSE, diff_light); glLightfv (GL_LIGHT0, GL_POSITION, l_position); //draw light #1 stand base and head glPushMatrix(); light_position(light1_x, light1_z); light_angle(light1_x, light1_z); glCallList(light_base_id); glPushMatrix(); glTranslatef(0.0, light1_height, 0.0); glCallList(light_head_id); glPopMatrix(); glPopMatrix(); //light #1 stand pole glPushMatrix(); light_position(light1_x, light1_z); glTranslatef(0.0f, light1_height + 2*sinf(PI/18), 0.0); glRotatef(90.0f, 1.0f, 0.0f, 0.0f); glColor3f(0.2, 0.2, 0.2); GLUquadricObj * light_stand; light_stand = gluNewQuadric(); gluQuadricDrawStyle(light_stand, GLU_FILL); gluCylinder(light_stand, 0.05f, 0.05f, light1_height, 8, 8); glEnd(); glPopMatrix(); } //draw light #2 and its light stand object if (draw_light2) { //turn on light #2 glEnable(GL_LIGHT1); //draw light #2 effect GLfloat diff_light[] = {light2_red, light2_green, light2_blue, 1.0}; GLfloat l_position[] = {light2_x, 1 + light2_height + 2*sinf(PI/18), light2_z, 1.0f}; glLightfv (GL_LIGHT1, GL_DIFFUSE, diff_light); glLightfv (GL_LIGHT1, GL_POSITION, l_position); //draw light #2 stand base and head glPushMatrix(); light_position(light2_x, light2_z); light_angle(light2_x, light2_z); glCallList(light_base_id); glPushMatrix(); glTranslatef(0.0, light2_height, 0.0); glCallList(light_head_id); glPopMatrix(); glPopMatrix(); //light #2 stand pole glPushMatrix(); light_position(light2_x, light2_z); glTranslatef(0.0f, light2_height + 2*sinf(PI/18), 0.0); glRotatef(90.0f, 1.0f, 0.0f, 0.0f); glColor3f(0.2, 0.2, 0.2); GLUquadricObj * light_stand; light_stand = gluNewQuadric(); gluQuadricDrawStyle(light_stand, GLU_FILL); gluCylinder(light_stand, 0.05f, 0.05f, light2_height, 8, 8); glEnd(); glPopMatrix(); } //draw light #3 and its light stand object if (draw_light3) { //turn on light #3 glEnable(GL_LIGHT2); //draw light #3 effect GLfloat diff_light[] = {light3_red, light3_green, light3_blue, 1.0}; GLfloat l_position[] = {light3_x, 6.0 + 2*sinf(PI/18), light3_z, 1.0f}; glLightfv (GL_LIGHT2, GL_DIFFUSE, diff_light); glLightfv (GL_LIGHT2, GL_POSITION, l_position); //draw light #3 stand base and head glPushMatrix(); light_position(light3_x, light3_z); light_angle(light3_x, light3_z); glCallList(light_base_id); glPushMatrix(); glTranslatef(0.0, light3_height, 0.0); glCallList(light_head_id); glPopMatrix(); glPopMatrix(); //light #3 stand pole glPushMatrix(); light_position(light3_x, light3_z); glTranslatef(0.0f, light3_height + 2*sinf(PI/18), 0.0); glRotatef(90.0f, 1.0f, 0.0f, 0.0f); glColor3f(0.2, 0.2, 0.2); GLUquadricObj * light_stand; light_stand = gluNewQuadric(); gluQuadricDrawStyle(light_stand, GLU_FILL); gluCylinder(light_stand, 0.05f, 0.05f, light3_height, 8, 8); glEnd(); glPopMatrix(); } //draw the grid lines if (draw_grid) { glBegin (GL_LINES); glColor3f(0.5f, 0.5f, 0.5f); for (int i = -10; i <= 10; ++i) { //floor glVertex3f(i, 0, -10); glVertex3f(i, 0, 10); glVertex3f(10, 0, i); glVertex3f(-10, 0, i); //back wall glVertex3f(i, 0, 10); glVertex3f(i, 10, 10); //left wall glVertex3f(10, 0, i); glVertex3f(10, 10, i); //right wall glVertex3f(-10, 0, i); glVertex3f(-10, 10, i); } for (int i = 0; i <= 10; ++i) { //back wall glVertex3f(10, i, 10); glVertex3f(-10, i, 10); //left wall glVertex3f(10, i, -10); glVertex3f(10, i, 10); //right wall glVertex3f(-10, i, -10); glVertex3f(-10, i, 10); } glEnd(); //GL_LINES for grid } //DRAW PERSON 1 (GREEN) if (draw_person1) { glPushMatrix(); //set the postition and orientation of person 1 glTranslatef(person1_x, 0.0f, person1_z); glRotatef(person1_xrot, 1.0f, 0.0f, 0.0f); glRotatef(person1_zrot, 0.0f, 0.0f, 1.0f); glRotatef(person1_yrot, 0.0f, 1.0f, 0.0f); glScalef(0.7f, 0.7f, 0.55f); //head for person 1 glPushMatrix(); glColor3f(1.0f, .9f, .6f); glCallList(head_list_id); //call the head display list glPopMatrix(); //body for person 1 glPushMatrix(); glColor3f(0.0f, 0.8f, 0.0f); glCallList(body_list_id); //call the body display list glPopMatrix(); glPopMatrix(); } //DRAW PERSON 2 (RED) if (draw_person2) { glPushMatrix(); //set the postition and orientation of person 2 glTranslatef(person2_x, 0.0f, person2_z); glRotatef(person2_xrot, 1.0f, 0.0f, 0.0f); glRotatef(person2_zrot, 0.0f, 0.0f, 1.0f); glRotatef(person2_yrot, 0.0f, 1.0f, 0.0f); glScalef(0.8f, 0.8f, 0.65f); //head for person 2 glPushMatrix(); glColor3f(0.9f, 0.7f, 0.6f); glCallList(head_list_id); glPopMatrix(); //body for person 2 glPushMatrix(); glColor3f(0.7f, 0.0f, 0.1f); glCallList(body_list_id); glPopMatrix(); glPopMatrix(); } //DRAW PERSON 3 (BLUE) if (draw_person3) { glPushMatrix(); //set the postition and orientation of person 3 glTranslatef(person3_x, 0.0f, person3_z); glRotatef(person3_xrot, 1.0f, 0.0f, 0.0f); glRotatef(person3_zrot, 0.0f, 0.0f, 1.0f); glRotatef(person3_yrot, 0.0f, 1.0f, 0.0f); glScalef(0.7f, 0.65f, 0.6f); //head for person 3 glPushMatrix(); glColor3f(0.9f, 0.9f, 0.8f); glCallList(head_list_id); glPopMatrix(); //body for person 3 glPushMatrix(); glColor3f(0.1f, 0.0f, 0.9f); glCallList(body_list_id); glPopMatrix(); glPopMatrix(); } //DRAW PERSON 4 (YELLOW) if (draw_person4) { glPushMatrix(); //set the postition and orientation of person 4 glTranslatef(person4_x, 0.0f, person4_z); glRotatef(person4_xrot, 1.0f, 0.0f, 0.0f); glRotatef(person4_zrot, 0.0f, 0.0f, 1.0f); glRotatef(person4_yrot, 0.0f, 1.0f, 0.0f); glScalef(0.75f, 0.85f, 0.65f); //head for person 4 glPushMatrix(); glColor3f(0.5f, 0.5f, 0.2f); glCallList(head_list_id); glPopMatrix(); //body for person 4 glPushMatrix(); glColor3f(0.9f, 0.9f, 0.0f); glCallList(body_list_id); glPopMatrix(); glPopMatrix(); } //swap the current window with the buffered window glutSwapBuffers(); }