OpenGL纹理贴图

本文将介绍如何在OpenGL中创建一个包含桌子、茶壶、墙壁和地板的三维场景,并利用图片进行纹理贴图,同时关注灯光配置和场景的动态旋转效果。
摘要由CSDN通过智能技术生成

任务:绘制有桌子茶壶,墙壁地板的简单三维场景,加载图片作为纹理,注意灯光的设置和场景的旋转。

#include<windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <malloc.h>
#include "FreeImage.h"

#define Height 16
#define Width 16

GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_pos[] = { 5,5,5,1 };
float eye[] = { -3,3, 7 };
float center[] = { 0, 0, 0 };
float place[] = { 0, 0, 5.6 };
#define ERROR(...)		printf(__VA_ARGS__);exit(0)


float fRotate = 0.0f;
float fScale = 1.0f; //

float tRotate = 0.0f; 

bool bPersp = false; 
bool bAnim = false; 
bool isRotate = false; 
int wHeight = 0;
int wWidth = 0;

//int min(int x, int y)
//{
//	return x < y ? x : y;
//}


typedef struct
{
	int			w;
	int 			h;
	unsigned char	        *buf;
	GLuint			rgb_mode;
}GLBITMAP;


static GLuint	S_png_tex = 0;
static GLuint	S_jpg_tex = 0;
static GLuint 	S_bmp_tex = 0;
static GLuint	S_tga_tex = 0;
static GLuint	S_ico_tex = 0;

GLBITMAP * FIBitmap2GLBitmap(FIBITMAP *fibmp)
{
	int i, j, k;
	int pitch = FreeImage_GetPitch(fibmp);
	unsigned char *bits = FreeImage_GetBits(fibmp);
	int bpp = FreeImage_GetBPP(fibmp);
	GLBITMAP *glbmp = (GLBITMAP *)malloc(sizeof(GLBITMAP));
	RGBQUAD *palette = NULL;

	if (!glbmp) return NULL;

	glbmp->w = FreeImage_GetWidth(fibmp);
	glbmp->h = FreeImage_GetHeight(fibmp);

	switch (bpp) {
	case 8:
		if (!(palette = FreeImage_GetPalette(fibmp))) return NULL;
		if (!(glbmp->buf = (unsigned char *)malloc(glbmp->w*glbmp->h * 3))) return NULL;
		glbmp->rgb_mode = GL_RGB;
		for (i = 0; i < glbmp->h; ++i) {
			for (j = 0; j < glbmp->w; ++j) {
				k = bits[i*pitch + j];
				glbmp->buf[(i*glbmp->w + j) * 3 + 0] = palette[k].rgbRed;
				glbmp->buf[(i*glbmp->w + j) * 3 + 1] = palette[k].rgbGreen;
				glbmp->buf[(i*glbmp->w + j) * 3 + 2] = palette[k].rgbBlue;
			}
		}
		break;
	case 24:
		if (!(glbmp->buf = (unsigned char *)malloc(glbmp->w*glbmp->h * 3))) return NULL;
		glbmp->rgb_mode = GL_RGB;
		for (i = 0; i < glbmp->h; ++i) {
			for (j = 0; j < glbmp->w; ++j) {
				glbmp->buf[(i*glbmp->w + j) * 3 + 0] = bits[i*pitch + j * 3 + 2];
				glbmp->buf[(i*glbmp->w + j) * 3 + 1] = bits[i*pitch + j * 3 + 1];
				glbmp->buf[(i*glbmp->w + j) * 3 + 2] = bits[i*pitch + j * 3 + 0];
			}
		}
		break;
	case 32:
		if (!(glbmp->buf = (unsigned char *)malloc(glbmp->w*glbmp->h * 4))) return NULL;
		glbmp->rgb_mode = GL_RGBA;
		for (i = 0; i < glbmp->h; ++i) {
			for (j = 0; j < glbmp->w; ++j) {
				glbmp->buf[(i*glbmp->w + j) * 4 + 0] = bits[i*pitch + j * 4 + 2];
				glbmp->buf[(i*glbmp->w + j) * 4 + 1] = bits[i*pitch + j * 4 + 1];
				glbmp->buf[(i*glbmp->w + j) * 4 + 2] = bits[i*pitch + j * 4 + 0];
				glbmp->buf[
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值