Jump to content
  • Advertisement
Sign in to follow this  
Shaderhacker

More Rubberband problems..

This topic is 3318 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

All: I can't seem to wrack my brain around this OpenGL and glLogicOp with getting a rubberband rectangle. I need it on a double buffered canvas and it just doesn't render anything. I'm trying to get the basic stipple line and can't get that to render on a double buffered canvas. What am I doing wrong?
#include <GL/glut.h>
#include <cstdio>

int ww;
float xm, ym, xmm, ymm;
int first = 0;
int xres = 640;
int yres = 480;
bool mouseActive = false;

void init()
{
    printf("Init() called...\n");
    glLineWidth(1.0);
    glClearColor(0.0, 0.0, 0.0, 1.0);
	
}

void display()
{
	printf("display()\n");
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glColor3f(1.0, 1.0, 1.0);
	glutSwapBuffers();
}

void drawRectangle(int x, int y)
{
	glEnable(GL_COLOR_LOGIC_OP);
	glLogicOp(GL_XOR);
	glEnable(GL_LINE_STIPPLE);
	GLint factor = 3;
	GLushort pattern = 0x5555;
	glLineStipple(factor, pattern);
	
	// erase last drawn line stipple
	glBegin(GL_LINES);
	glVertex2f(xm,ym);
	glVertex2f(xmm,ym);
	glEnd();
	glFlush();
	
	// redraw it again
	glBegin(GL_LINES);
	glVertex2f(xm,ym);
	glVertex2f(xmm,ym);
	glEnd;
	glFlush();
	glDisable(GL_LINE_STIPPLE);
	glDisable(GL_COLOR_LOGIC_OP);
	glutSwapBuffers();
	
	
	
}


void reshape(int w, int h)
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D (0.0,  1.0, 0.0,  1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void mouse(int btn, int state, int x, int y)
{
	
	if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) 
	{
		xm = x/(float)xres;
		ym = (yres-y)/(float)yres;
		
	}

	
}

void mouseMotion(int x, int y)
{
	xmm = x/(float)xres;
	ymm = (yres-y)/(float)yres;
	drawRectangle(x,y);		
}

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

/* Initialize mode and open a window in upper left corner of screen */
/* Window title is name of program (arg[0]) */

	glutInit(&argc,argv); 
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowSize(xres,yres);
	glutInitWindowPosition(0,0); 
	glutCreateWindow("rubberband");
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMouseFunc(mouse);
	glutMotionFunc(mouseMotion);
	init();
	glutMainLoop();

}

Share this post


Link to post
Share on other sites
Advertisement
You're calling glutSwapBuffers() in 2 different places, so your rectangle is never drawn in the same frame as display(). Try putting your call to drawRectangle() inside of display(), and remove the extra call to glutSwapBuffers() at the end of drawRectangle(). You'll also probably want to add an if statement to make sure drawRectangle() isn't called if the mouse button isn't down.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!