opengl入门教程(精)的学习笔记

这篇博客详细记录了OpenGL的学习过程,包括小球的旋转实现,涉及光照、视角、材质和矩阵变换的基本操作。同时,介绍了如何计算FPS以及BMP文件的正确读入方法。此外,还讲解了纹理的加载和贴图的应用技巧。
摘要由CSDN通过智能技术生成

1.小球旋转

知识点:1.光照、视角、材质、矩阵变换的基本用法

2.计算FPS


#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <gl/glut.h>
#include <time.h>
#include <math.h>

#define WIDTH 400
#define HEIGHT 400

//#define ColoredVertex(c,v) do{glColor3fv(c);glVertex3fv(v);}while(0)

GLfloat angle=0.0f;
static int day =200;


double CalFrequency()
{
	static int count;
	static double save;
	static clock_t last,current;
	double timegap;

	++count;
	if(count<=50)
		return save;
	count = 0;
	last = current;
	current = clock();
	timegap = (current-last)/(double)CLK_TCK;
	save = 50.0/timegap;
	return save;
}

void myDisplay(void)
{
	double FPS=CalFrequency();
	printf("FPS=%f\n",FPS);
	
	
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(90.0f,1.0f,1.0f,20.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0,5.0,-10.0,0,0,0,0,1,0);

	
	{
		GLfloat sun_light_position[]={0.0f,0.0f,0.0f,1.0f};
		GLfloat sun_light_ambient[]={0.0f,0.0f,0.0f,1.0f};
		GLfloat sun_light_diffuse[]={1.0f,1.0f,1.0f,1.0f};
		GLfloat sun_light_specular[]={1.0f,1.0f,1.0f,1.0f};

		glLightfv(GL_LIGHT0,GL_POSITION,sun_light_position);
		glLightfv(GL_LIGHT0,GL_AMBIENT,sun_light_ambient);
		glLightfv(GL_LIGHT0,GL_DIFFUSE,sun_light_diffuse);
		glLightfv(GL_LIGHT0,GL_SPECULAR,sun_light_specular);

		glEnable(GL_LIGHT0);
		glEnable(GL_LIGHTING);
		glEnable(GL_DEPTH_TEST);
	}
	
	{
		GLfloat sun_mat_ambient[]={0.0f,0.0f,0.0f,1.0f};
		GLfloat sun_mat_diffuse[]={0.0f,0.0f,0.0f,1.0f};
		GLfloat sun_mat_specular[]={0.0f,0.0f,0.0f,1.0f};
		GLfloat sun_mat_emission[]={0.5f,0.0f,0.0f,1.0f};
		GLfloat sun_mat_shininess = 0.0f;

		glMaterialfv(GL_FRONT,GL_AMBIENT,sun_mat_ambient);
		glMaterialfv(GL_FRONT,GL_DIFFUSE,sun_mat_diffuse);
		glMaterialfv(GL_FRONT,GL_SPECULAR,sun_mat_specular);
		glMaterialfv(GL_FRONT,GL_EMISSION,sun_mat_emission);
		glMaterialfv(GL_FRONT,GL_SHININESS,&sun_mat_shininess);

		glutSolidSphere(2.0,40,32);
	}
	
	{
		GLfloat earth_mat_ambient[]={0.0f,0.0f,0.5f,1.0f};
		GLfloat earth_mat_diffuse[]={0.0f,0.0f,0.5f,1.0f};
		GLfloat earth_mat_specular[]={0.0f,0.0f,1.0f,1.0f};
		GLfloat earth_mat_emission[]={0.0f,0.0f,0.0f,1.0f};
		GLfloat earth_mat_shininess = 30.0f;

		glMaterialfv(GL_FRONT,GL_AMBIENT,earth_mat_ambient);
		glMaterialfv(GL_FRONT,GL_DIFFUSE,earth_mat_diffuse);
		glMaterialfv(GL_FRONT,GL_SPECULAR,earth_mat_specular);
		glMaterialfv(GL_FRONT,GL_EMISSION,earth_mat_emission);
		glMaterialfv(GL_FRONT,GL_SHININESS,&earth_mat_shininess);
		
		glRotatef(angle,0.0f,1.0f,0.0f);
		glTranslatef(5.0f,0.0f,0.0f);
		glutSolidSphere(2.0,40,32);
	}
	
	//glFlush();
	glutSwapBuffers();
}

void myIdle(void)
{
	angle+=1.0f;
	if(angle>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值