码迷,mamicode.com
首页 > 其他好文 > 详细

opengl编程指南 第七版 源码有bug Page35 lines.c 红宝书

时间:2014-11-08 02:04:14      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:opengl   编程指南   第七版   红宝书   gl_line_stipple   

问题1:当我照着源码敲进去的时候发现,运行的结果不对。哪里不对?源码中没有glPushAttrib(GL_LINE_STIPPLE) glPopAttrib()。所以会出现每次更新点画线时在下一次绘制时会以最后一次设置的信息重绘而覆盖掉之前已绘制好的点线。所以要进行状态压栈处理和谈栈处理,这样子在刷帧重绘才不会影响其他帧的绘制结果。(原因是什么目前我也不清楚,估计是状态机的管理问题。要了解内部的绘制机制才行。)但是每次手动添加压栈和弹栈太麻烦了,所以我处理C_style的宏定义形式去实现


问题2:reshape的操作屏蔽掉之后发现只要display中正确设置窗口参数,还是能正常绘制的。。问题来了,reshape不应该是我拉伸窗口才会触发的吗?但是情况如修改仍能正常显示。百思不得其解,求指教!!


修改1源码如下:

#include "stdafx.h"
#include <GL/freeglut.h>

#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES);	glVertex2f((x1),(y1));glVertex2f((x2),(y2));glEnd();
	


void init(void)
{
	glClearColor(0.0,0.0,0.0,0.0);
	glShadeModel(GL_FLAT);
	
}




void display(void)
{
	//初初始化定义 窗口,我故意把这段搬出来测试reshape函数的调用,问题2:
	int w = 400,h =150;
	glViewport(0,0,static_cast<GLsizei>(w),static_cast<GLsizei>(h));
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0,static_cast<GLfloat>(w),0.0,static_cast<GLfloat>(h));
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	//int i;

	glClear(GL_COLOR_BUFFER_BIT);

	glPushMatrix();

	

	glColor3f(1.0,1.0,1.0);

	glEnable(GL_LINE_STIPPLE);


	//1st row

	glPushAttrib(GL_LINE_STIPPLE);
	//以二进制读取,从低位开始绘制,第一个参数为拉伸参数
	glLineStipple(1,0x0101);
	drawOneLine(50.0,125.0,150,125.0);
	glPopAttrib();

	glPushAttrib(GL_LINE_STIPPLE);
	glLineStipple(1,0x00FF);
	drawOneLine(150.0,125.0,250.0,125.0);
	glPopAttrib();

	glPushAttrib(GL_LINE_STIPPLE);
	glLineStipple(1,0x1c47);
	drawOneLine(250.0,125.0,350.0,125.0);
	glPopAttrib();



	//2nd row


	glLineWidth(5.0);

	glPushAttrib(GL_LINE_STIPPLE);
	
	//以二进制读取,从低位开始绘制,第一个参数为拉伸参数
	glLineStipple(1,0x0101);
	drawOneLine(50.0,100.0,150.0,100.0);
	glPopAttrib();

	glPushAttrib(GL_LINE_STIPPLE);
	glLineStipple(1,0x00ff);
	drawOneLine(150.0,100.0,250.0,100.0);
	glPopAttrib();

	glPushAttrib(GL_LINE_STIPPLE);
	glLineStipple(1,0x1c47);
	drawOneLine(250.0,100.0,350.0,100.0);
	glPopAttrib();

	glLineWidth(1.0);


	//3rd row
	glPushAttrib(GL_LINE_STIPPLE);
	glLineStipple(1,0x1c47);
	glBegin(GL_LINE_STRIP);
	for (int i = 0; i < 7; i++)
	{
		glVertex2f(50.0+static_cast<GLfloat>(i*50.0),75.0);
	}
	glEnd();
	glPopAttrib();





	glDisable(GL_LINE_STIPPLE);

	glPopMatrix();

	glFlush();
}

void reshape(int w,int h)
{
	
}

int _tmain(int argc, char * argv[])
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize(400,150);
	glutInitWindowPosition(0,0);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(NULL);
	glutMainLoop();
	return 0;
}



正确源码示范:

// Lines_P35.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <GL/freeglut.h>

#define drawOneLine(scale,stipple,x1,y1,x2,y2) 	glPushAttrib(GL_LINE_STIPPLE);	glLineStipple(scale,stipple);	glBegin(GL_LINES);	glVertex2f((x1),(y1));glVertex2f((x2),(y2));	glEnd();	glPopAttrib();
	


void init(void)
{
	glClearColor(0.0,0.0,0.0,0.0);
	glShadeModel(GL_FLAT);
	
}




void display(void)
{


	glClear(GL_COLOR_BUFFER_BIT);

	glPushMatrix();

	

	glColor3f(1.0,1.0,1.0);

	glEnable(GL_LINE_STIPPLE);


	//1st row

	
	//以二进制读取,从低位开始绘制,第一个参数为拉伸参数
	drawOneLine(1,0x0101,50.0,125.0,150,125.0);

	drawOneLine(1,0x00FF,150.0,125.0,250.0,125.0);

	drawOneLine(1,0x1c47,250.0,125.0,350.0,125.0);




	//2nd row


	glLineWidth(5.0);	
	//以二进制读取,从低位开始绘制,第一个参数为拉伸参数
	drawOneLine(1,0x0101,50.0,100.0,150.0,100.0);

	drawOneLine(1,0x00ff,150.0,100.0,250.0,100.0);

	drawOneLine(1,0x1c47,250.0,100.0,350.0,100.0);


	glLineWidth(1.0);


	//3rd row
	glPushAttrib(GL_LINE_STIPPLE);
	glLineStipple(1,0x1c47);
	glBegin(GL_LINE_STRIP);
	for (int i = 0; i < 7; i++)
	{
		glVertex2f(50.0+static_cast<GLfloat>(i*50.0),75.0);
	}
	glEnd();
	glPopAttrib();

	//4th
	for (int i = 0; i < 6; i++)
	{
		drawOneLine(1,0x1c47,50.0+static_cast<GLfloat>(i*50.0),50.0,50.0+static_cast<GLfloat>(i+1)*50.0,50.0);
	}

	//5th
	drawOneLine(5,0x1c47,50.0,25.0,350.0,25.0);


	glDisable(GL_LINE_STIPPLE);

	glPopMatrix();

	glFlush();
}

void reshape(int w,int h)
{
	glViewport(0,0,static_cast<GLsizei>(w),static_cast<GLsizei>(h));
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0,static_cast<GLfloat>(w),0.0,static_cast<GLfloat>(h));
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

int _tmain(int argc, char * argv[])
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize(400,150);
	glutInitWindowPosition(0,0);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMainLoop();
	return 0;
}




opengl编程指南 第七版 源码有bug Page35 lines.c 红宝书

标签:opengl   编程指南   第七版   红宝书   gl_line_stipple   

原文地址:http://blog.csdn.net/jingzhewangzi/article/details/40912047

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!