1.制作画布大小 2. 制作台体 3. 台体摆放位罝 4. 台体铺满画布 5.设定台体旋转角度 6.绘制纹理用OpenGL写个代码
时间: 2024-03-16 15:47:41 浏览: 190
好的,以下是一个简单的用OpenGL绘制3D画布和台体的示例代码:
```
#include <GL/glut.h>
// 定义画布大小
const int WIDTH = 800;
const int HEIGHT = 600;
// 定义台体的顶点坐标
GLfloat vertices[][3] = {
{-0.5, -0.5, 0.5},
{0.5, -0.5, 0.5},
{0.5, 0.5, 0.5},
{-0.5, 0.5, 0.5},
{-0.5, -0.5, -0.5},
{0.5, -0.5, -0.5},
{0.5, 0.5, -0.5},
{-0.5, 0.5, -0.5}
};
// 定义台体的面片索引
GLubyte indices[][4] = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{0, 4, 7, 3},
{1, 5, 6, 2},
{0, 1, 5, 4},
{3, 2, 6, 7}
};
// 定义台体的纹理坐标
GLfloat texCoords[][2] = {
{0.0, 0.0},
{1.0, 0.0},
{1.0, 1.0},
{0.0, 1.0}
};
// 定义画布旋转角度
GLfloat angle = 0.0;
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.5, 0.5, 0.0);
glRotatef(45.0, 0.0, 0.0, 1.0);
GLubyte texData[256][256][3];
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
texData[i][j][0] = (i + j) % 256;
texData[i][j][1] = (i * j) % 256;
texData[i][j][2] = i % 256;
}
}
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, texData);
glPopMatrix();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
// 绘制台体
glBegin(GL_QUADS);
for (int i = 0; i < 6; i++) {
glTexCoord2fv(texCoords[0]);
glVertex3fv(vertices[indices[i][0]]);
glTexCoord2fv(texCoords[1]);
glVertex3fv(vertices[indices[i][1]]);
glTexCoord2fv(texCoords[2]);
glVertex3fv(vertices[indices[i][2]]);
glTexCoord2fv(texCoords[3]);
glVertex3fv(vertices[indices[i][3]]);
}
glEnd();
// 绘制画布
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex3f(-1.0, -1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h) {
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
} else {
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void spinDisplay() {
angle += 1.0;
if (angle > 360.0) {
angle -= 360.0;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("3D Canvas");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(spinDisplay);
init();
glutMainLoop();
return 0;
}
```
这段代码使用OpenGL绘制了一个3D的画布和台体,并将画布和台体铺满整个窗口。您可以根据需要修改代码中的画布大小和台体的大小、纹理等参数。
阅读全文