当前位置:网站首页>在屏幕上绘制一个正方形,用ice.bmp对正方形做纹理映射;在正方形后绘制一个黄色的茶壶,假设正方形是透明的,绘制茶壶与正方形的混合效果;通过A,D,W和K按键调整茶壶在X轴和Y轴的位置,具体如下
在屏幕上绘制一个正方形,用ice.bmp对正方形做纹理映射;在正方形后绘制一个黄色的茶壶,假设正方形是透明的,绘制茶壶与正方形的混合效果;通过A,D,W和K按键调整茶壶在X轴和Y轴的位置,具体如下
2022-07-24 05:16:00 【陌小呆^O^】
#define GLUT_DISABLE_ATEXIT_HACK
#include "windows.h"
#include <gl/glut.h>
#include "math.h"
float p1 = 0.0;
float p2 = 0.0;
BYTE* gltReadBMPBits(const char* szFileName, int* nWidth, int* nHeight)
{
HANDLE hFileHandle;
BITMAPINFO* pBitmapInfo = NULL;
unsigned long lInfoSize = 0;
unsigned long lBitSize = 0;
BYTE* pBits = NULL; // Bitmaps bits
BITMAPFILEHEADER bitmapHeader;
DWORD dwBytes;
// Open the Bitmap file
hFileHandle = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
// Check for open failure (most likely file does not exist).
if (hFileHandle == INVALID_HANDLE_VALUE)
return NULL;
// File is Open. Read in bitmap header information
ReadFile(hFileHandle, &bitmapHeader, sizeof(BITMAPFILEHEADER),
&dwBytes, NULL);
// Check for a couple of simple errors
if (dwBytes != sizeof(BITMAPFILEHEADER))
return FALSE;
// Check format of bitmap file
if (bitmapHeader.bfType != 'MB')
return FALSE;
// Read in bitmap information structure
lInfoSize = bitmapHeader.bfOffBits - sizeof(BITMAPFILEHEADER);
pBitmapInfo = (BITMAPINFO*)malloc(sizeof(BYTE) * lInfoSize);
ReadFile(hFileHandle, pBitmapInfo, lInfoSize, &dwBytes, NULL);
if (dwBytes != lInfoSize)
{
free(pBitmapInfo);
CloseHandle(hFileHandle);
return FALSE;
}
// Save the size and dimensions of the bitmap
*nWidth = pBitmapInfo->bmiHeader.biWidth;
*nHeight = pBitmapInfo->bmiHeader.biHeight;
lBitSize = pBitmapInfo->bmiHeader.biSizeImage;
// If the size isn't specified, calculate it anyway
if (pBitmapInfo->bmiHeader.biBitCount != 24)
{
free(pBitmapInfo);
return FALSE;
}
if (lBitSize == 0)
lBitSize = (*nWidth *
pBitmapInfo->bmiHeader.biBitCount + 7) / 8 *
abs(*nHeight);
// Allocate space for the actual bitmap
free(pBitmapInfo);
pBits = (BYTE*)malloc(sizeof(BYTE) * lBitSize);
// Read in the bitmap bits, check for corruption
if (!ReadFile(hFileHandle, pBits, lBitSize, &dwBytes, NULL) ||
dwBytes != (sizeof(BYTE) * lBitSize))
pBits = NULL;
// Close the bitmap file now that we have all the data we need
CloseHandle(hFileHandle);
return pBits;
}
//初始化OpenGL
void init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//设置背景颜色
glShadeModel(GL_SMOOTH);//设置明暗处理,有两种选择模式:GL_FLAT(不渐变)和GL_SMOOTH(渐变过渡)
// 获取位图数据
BYTE* pBytes;
int nWidth, nHeight;
pBytes = gltReadBMPBits("D://ice.BMP", &nWidth, &nHeight);
定义二维纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, nWidth, nHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pBytes);
//控制滤波
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//说明映射方式
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
//主要的绘制过程
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除颜色缓存
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
glPushMatrix();
glTranslatef(p1, p2, -6.0);
glColor3f(1.0, 1.0, 0.0);
glutSolidTeapot(0.5);
glPopMatrix();
//绘制不规则四边形
glPushMatrix();
glTranslatef(-1, -1, -4.0);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glEnable(GL_TEXTURE_2D);
glColor4f(1.0, 1.0, 1.0, 1.0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
//glBlendFunc(GL_ZERO, GL_ONE);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(2.0, 0.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.0, 2.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, 2.0, 0.0);
glEnd();
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glFlush();
glutSwapBuffers();
}
//在窗口改变大小时调用
void reshape(int w, int h) {
glViewport(0, 0, w, h);//设置视口
glMatrixMode(GL_PROJECTION);//设置当前为投影变换模式
glLoadIdentity();//用单位矩阵替换当前变换矩阵
gluPerspective(90, (float)w / h, 4, 10.0);//设置正交投影视图体
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
//处理键盘
void keyboard(unsigned char key, int x, int y) {
switch (key) {
//通过A,D,W和K按键调整茶壶在X轴和Y轴的位置,具体如下:[1] A:x轴上的值减少;[2] D:x轴上的值增加;[3] W:y轴上的值增加; [4] K:y轴上的值减少。
case 27://esc键退出
exit(0);
break;
case 'A':
case 'a':
p1 -= 0.2;
glutPostRedisplay();
break;
case 'D':
case 'd':
p1 += 0.1;
glutPostRedisplay();
break;
case 'W':
case 'w':
p2 += 0.2;
glutPostRedisplay();
break;
case 'K':
case 'k':
p2 -= 0.1;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char* argv[]) //主函数: 参数数量&参数值
{
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutCreateWindow("Basic");//设置窗口标题
init();//初始化OpenGL
glutDisplayFunc(display);//设置显示回调函数
glutReshapeFunc(reshape);//设置reshape回调函数
glutKeyboardFunc(keyboard);//设置键盘回调函数
glutMainLoop();//进入主循环
}
边栏推荐
- I'm interested in reading efficient reading - the most cost-effective self investment
- Ain 0722 sign in
- JDBC encapsulates a parent class to reduce code duplication
- C语言实现扫雷游戏
- SSM integration
- 反射的介绍
- 【STL】Map &unordered_map
- Implementation and comparison of nine sorting (ten thousand words summary)
- JMeter upload and download files
- Generics and annotations
猜你喜欢
随机推荐
Data annotation learning summary
1. Pedestrian recognition based on incremental occlusion generation and confrontation suppression
Create and delete databases using databases
文本摘要 ACL2021
Relationship between sample and population in Statistics: sample success ratio + central limit theorem (sample mean)
View progress!!! RPM installation!!!
Pure white tutorial using Druid database connection pool in idea
What are the core strengths of a knowledge base that supports customers quickly?
Generics and annotations
Pointer learning diary (I)
线程的介绍
NFS shared services
使用d2l包和相关环境配置的一些血泪心得
Career planning route
C语言实现扫雷游戏
Pointer learning diary (IV) use structure and pointer (linked list)
【Pytorch】Dataset_DataLoader
【sklearn】数据预处理
JDBC encapsulates a parent class to reduce code duplication
Performance test process

![Embedded system transplantation [3] - uboot burning and use](/img/36/69daec5f1fe41bd3d0433d60816bba.png)





![Knowledge record of College Physics C in advance in summer [update]](/img/c4/76b669c3229a365a5e2567f7fb824e.png)

