3ds model -- place

This chapter loads a surface map, that is to say, draw a plane (rectangle), and paste a map (texture) on it as the parking place of the model.

In order to facilitate the use of a single cpp.

Define some variables:

GLint DibiaoChangX,DibiaoKuanZ,DibiaoGaoY;//Length, width, height (Pinyin)
GLuint Dibiaotexture;//Texture ID
Load and set it in the init section:

//Loading surface map
//Loading surface map
Draw into drawAllModel:

Front< opengl Show textured tetrahedron >There is a function to load bmp as texture. The bmp graph is too large. Here, use the buildtexture (char * szpathname, glutent & textid) function in CLoad3DS.cpp.
Complete cpp:

//Surface rendering (pinyin dibio. CPP)

#include "CLoad3DS.h"

GLint DibiaoChangX,DibiaoKuanZ,DibiaoGaoY;//Length, width, height (Pinyin)
GLuint Dibiaotexture;//Texture ID

// Read in a texture (taken from CLoad3DS.cpp)
int BuildTexture(char *szPathName, GLuint &texid)
	HDC      hdcTemp;                        // DC used to save bitmaps
	HBITMAP    hbmpTemp;                        // Save bitmap temporarily
	IPicture  *pPicture;                        // Figure interface
	OLECHAR    wszPath[MAX_PATH+1];                  // Full path of picture (WCHAR)
	char    szPath[MAX_PATH+1];                    // Full path to picture
	long    lWidth;                          // Logical unit width
	long    lHeight;                        // Height of logical unit
	long    lWidthPixels;                      // Width in pixels
	long    lHeightPixels;                      // Height in pixels
	GLint    glMaxTexDim ;                      // Maintain maximum texture size

	if (strstr(szPathName, "http://") / / if the pathname (szPathName) contains" http: / / "
		strcpy(szPath, szPathName);                    // Attach pathname to szPath
	else                                // Otherwise... We load it from the file
		WCHAR tempPath[MAX_PATH+1]; 
		GetCurrentDirectory(MAX_PATH, tempPath);              // Get our working directory
		int lengthOfMbs = WideCharToMultiByte( CP_ACP, 0, tempPath, -1, NULL, 0, NULL, NULL); 
		WideCharToMultiByte( CP_ACP, 0, tempPath, -1, szPath, lengthOfMbs, NULL, NULL); //Wide character to multi character
		if(!strstr(szPath, "Data\\3DS"))
			strcat(szPath, PICPATH);                      // Append "\" after working directory
			strcat(szPath, "\\"); //If already included (on the command line)
		strcat(szPath, szPathName);                    // Append pathname
	//printf("image path:% s\n",szPath);

	MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH);    // Convert from ASCII to Unicode
	HRESULT hr = OleLoadPicturePath(wszPath, 0, 0, 0, IID_IPicture, (void**)&pPicture);

	if(FAILED(hr)) {                           // If the load fails
		printf("Error loading texture, possibly file %s non-existent!\n",szPathName);
		return FALSE;                          // Return to leave

	hdcTemp = CreateCompatibleDC(GetDC(0));                // Create a Windows compatible device context
	if(!hdcTemp)                            // Has creation failed?
		pPicture->Release();                      // Decrement picture reference count
		return FALSE;                          //Return false (failed)

	glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glMaxTexDim);          // Get the maximum texture size supported

	pPicture->get_Width(&lWidth);                    // Get graph width (convert to pixels)
	lWidthPixels  = MulDiv(lWidth, GetDeviceCaps(hdcTemp, LOGPIXELSX), 2540);
	pPicture->get_Height(&lHeight);                    // Get graph height (convert to pixels)
	lHeightPixels  = MulDiv(lHeight, GetDeviceCaps(hdcTemp, LOGPIXELSY), 2540);

	// Adjust the image to the nearest 2 power
	if (lWidthPixels <= glMaxTexDim) // Is the image width less than or equal to the card limit
		lWidthPixels = 1 << (int)floor((log((double)lWidthPixels)/log(2.0f)) + 0.5f); 
	else // Otherwise, set the width to "two of the maximum power" cards to handle
		lWidthPixels = glMaxTexDim;

	if (lHeightPixels <= glMaxTexDim) // Is Image Height Greater Than Cards Limit
		lHeightPixels = 1 << (int)floor((log((double)lHeightPixels)/log(2.0f)) + 0.5f);
	else // Otherwise Set Height To "Max Power Of Two" That The Card Can Handle
		lHeightPixels = glMaxTexDim;

	//  Create temporary bitmap
	BITMAPINFO  bi = {0};                        // Bitmap type we want
	DWORD    *pBits = 0;                        // Pointer to bitmap bit

	bi.bmiHeader.biSize      = sizeof(BITMAPINFOHEADER);        // Set structure size
	bi.bmiHeader.biBitCount    = 32;                  // 32 place
	bi.bmiHeader.biWidth    = lWidthPixels;              // Power Of Two Width
	bi.bmiHeader.biHeight    = lHeightPixels;            // Make image up (positive Y axis)
	bi.bmiHeader.biCompression  = BI_RGB;                // RGB coding
	bi.bmiHeader.biPlanes    = 1;                  // 1 Bitplane

	//  Creating bitmaps in this way allows us to specify the color depth and allows us to access the bits immediately
	hbmpTemp = CreateDIBSection(hdcTemp, &bi, DIB_RGB_COLORS, (void**)&pBits, 0, 0);

	if(!hbmpTemp)                            // Did Creation Fail?
		DeleteDC(hdcTemp);                        // Delete device description table (context)
		pPicture->Release();                      // Decrement picture reference count
		return FALSE;                          // Return False (Failure)

	SelectObject(hdcTemp, hbmpTemp);                  // Choose to process our temp DC and our temp bitmap objects

	// Rendering a picture to a bitmap
	pPicture->Render(hdcTemp, 0, 0, lWidthPixels, lHeightPixels, 0, lHeight, lWidth, -lHeight, 0);

	// Convert BGR to RGB format and add a value of 255
	for(long i = 0; i < lWidthPixels * lHeightPixels; i++)        // Loop through all pixels
		BYTE* pPixel  = (BYTE*)(&pBits[i]);              // Get current pixel
		BYTE temp    = pPixel[0];                  // Store the first color in the temp variable (blue)
		pPixel[0]    = pPixel[2];                  // Move the red value to the correct position (first)
		pPixel[2]    = temp;                      // Move the temp value to the correct blue position (3rd)

		// This will make any black pixels completely transparent (hardcode the value if necessary)
		if ((pPixel[0]==0) && (pPixel[1]==0) && (pPixel[2]==0))      // Is the pixel completely black?
			pPixel[3]  = 0;                      // Set α value to 0
		else                              // Otherwise
			pPixel[3]  = 255;                      // Set The Alpha Value To 255

	glGenTextures(1, &texid);                      // Create texture

	// Use the data in bitmap to generate typical texture
	glBindTexture(GL_TEXTURE_2D, texid);                // Bind to texture id
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);    // (modify this option for the desired filter type)
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // (modify this option for the desired filter type)
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, lWidthPixels, lHeightPixels, 0, GL_RGBA, GL_UNSIGNED_BYTE, pBits);  // (Modify This If You Want Mipmaps)

	DeleteObject(hbmpTemp);                        // Delete The Object
	DeleteDC(hdcTemp);                          // Delete The Device Context

	pPicture->Release();                        // Decrement picture reference count

	//printf( "load %s!" , szPath );
	return TRUE;                            // Return True (all good)


//Loading surface map
void loadDibiaoPic(char *szPathName)
	// Read in surface texture
	if(BuildTexture(szPathName, Dibiaotexture)==false)
		printf( "Loading surface map failed!" );


//Set size and height
void setDibiao(GLint w,GLint h,GLint zero)
	cout <<"Size and location:"<< w<<"," <<h<<","<<zero<< '\n';  //

void DrawDibiao()
	//Half length and width
	GLfloat bw=DibiaoChangX/2;
	GLfloat bh=DibiaoKuanZ/2;

	// Turn on texture mapping
	glBindTexture(GL_TEXTURE_2D, Dibiaotexture);

		// Give normal vector
		glNormal3f(0.0f, 1.0f, 0.0f);//Upward

		glTexCoord2f(0.0f, 0.0f); glVertex3f(-bw, DibiaoGaoY,  -bh);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( bw, DibiaoGaoY,  -bh);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( bw, DibiaoGaoY,  bh);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-bw, DibiaoGaoY,  bh);

design sketch:

Why is the blackout so dark? We can only hope for experts.

Tags: ascii Windows less

Posted on Sat, 02 May 2020 01:19:44 -0700 by yhingsmile