-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextureDemo.cpp
More file actions
53 lines (41 loc) · 1.41 KB
/
Copy pathtextureDemo.cpp
File metadata and controls
53 lines (41 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// Created by 86155 on 2023/12/24.
//
#include<gl/glut.h>
GLuint textureID;
void loadTexture(const char* filename) {
// 从文件中加载纹理图片,然后将纹理数据传递给纹理对象
// 这里使用SOIL库来加载图片
int width, height;
unsigned char* image = SOIL_load_image(filename, &width, &height, 0, SOIL_LOAD_RGB);
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
// 设置纹理参数
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SOIL_free_image_data(image);
}
void display() {
// 绘制一个矩形,并设置纹理坐标
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Texture Example");.
loadTexture("texture.jpg");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}