一个openGL的函数

最近挺忙很久没写东西。

目前在写一些opengl的东西,对于这个在3年前接触过但是没学好的语言一直耿耿于怀,其实很喜欢opengl。而且最近也没事就一直开始写着,写opengl视图模型的时候其实很重要的一个东西就是它内部的矩阵栈。所以写了一个函数,专门用来输出日志,我自己觉的挺好用。


</pre><span style="white-space:pre">	</span>这个函数的作用是专门输出当前在栈顶的矩阵:<pre name="code" class="html">void printCurrentMatrix(int type){

	printf("---------------------------\n");

	GLfloat mat[16];

	//GL_MODELVIEW_MATRIX
	glGetFloatv(type, mat);

	for (int j=0;j<4;j++)
	{
		for (int i=0;i<4;i++)
		{
			printf("%3.2f ",mat[i*4+j]);
		}
		printf("\n");
	}

}

测试的代码可以看这个,可以看出各个gl函数对栈顶矩阵的影响,很方便调试。

#include <iostream>
#include <stdio.h>
#include <cstdlib>

#ifndef GLUT_DISABLE_ATEXIT_HACK  
#define GLUT_DISABLE_ATEXIT_HACK 
#endif  
#include "GL/glut.h"


#define debug(A) std::cout<<#A<<":"<<A<<std::endl;
#define log(A) std::cout<<A<<std::endl;

void printCurrentMatrix(int type){

	printf("---------------------------\n");

	GLfloat mat[16];

	//GL_MODELVIEW_MATRIX
	glGetFloatv(type, mat);

	for (int j=0;j<4;j++)
	{
		for (int i=0;i<4;i++)
		{
			printf("%3.2f ",mat[i*4+j]);
		}
		printf("\n");
	}

}

void display(){


	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glColor3f(0.0,1.0,0.0);
	log("display land scape");
	glViewport(0,0,100,100);
	//set the projection 
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(120.0,1,1.0,200.0);
	printCurrentMatrix(GL_PROJECTION_MATRIX);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	printCurrentMatrix(GL_MODELVIEW_MATRIX);

	glPushMatrix();
	glTranslated(1,0,0);
	printCurrentMatrix(GL_MODELVIEW_MATRIX);
	glTranslated(2,0,0);
	printCurrentMatrix(GL_MODELVIEW_MATRIX);
	glPopMatrix();
	printCurrentMatrix(GL_MODELVIEW_MATRIX);

	printCurrentMatrix(GL_MODELVIEW_MATRIX);

	glPushMatrix();
	gluLookAt(0,0,-2,0,0,0,0,1,0);
	printCurrentMatrix(GL_MODELVIEW_MATRIX);
	glPopMatrix();

	printCurrentMatrix(GL_MODELVIEW_MATRIX);

	glutSwapBuffers();

}

int main(int argc, char** argv){



	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutInitWindowSize(500,500);
	glutInitWindowPosition(100,100);
	glutCreateWindow(argv[0]);
	glutDisplayFunc(display);
	glutMainLoop();
	//system("pause");


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值