小白学opengl之三维物体绘制

#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/glu.h>


using namespace std;

bool mouseLeftDown;
bool mouseRightDown;
float mouseX, mouseY;
float cameraDistance;
float cameraAngleX;
float cameraAngleY;
 

void init()
{
	glClearColor(0,0,0,0);
	GLfloat light_position[]={1,1,1,0};
	glLightfv(GL_LIGHT0,GL_POSITION,light_position);
	GLfloat light_diffuse[]={1,1,1,1};
	glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
	GLfloat mat_emission[] = {1, 0, 0, 0.0}; //决定了三为物体表面颜色 
	glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);   

	//glColorMaterial(GL_FRONT,GL_DIFFUSE);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_DEPTH_TEST);
}

void reshape(int w,int h)
{
	glViewport(0,0,(GLsizei)w,(GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

}

void display()
{
	
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	
	glTranslatef(0, 0, cameraDistance);
	glRotatef(cameraAngleX, 1, 0, 0);
	glRotatef(cameraAngleY, 0, 1, 0);


	//glColorMaterial(GL_FRONT,GL_DIFFUSE);

 
	//glColor3f(1,0,0);//这个函数只对二维图形绘制起作用
	glutSolidCube(1);

	glFlush();


}

void mouse(int button,int state,int x,int y)
{
	//printf("mouse\n");
	mouseX = x;
    mouseY = y;

    if(button == GLUT_LEFT_BUTTON)
    {
        if(state == GLUT_DOWN)
        {
            mouseLeftDown = true;
        }
        else if(state == GLUT_UP)
            mouseLeftDown = false;
    }

    else if(butt
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值