Sign in to follow this  
FeralShadow

Picking problem no hits detected

Recommended Posts

Hi all, We're trying to get picking to work with fltk.. but sadly enough it doesn´t seem to work. We've tried diffrent tutorials and some samples (which do work) but still we're doing something wrong. When we call glRenderMode(GL_RENDER) it returns 0 and it remains 0, right now we have the code stated below. hopefully someone can help us.
#include "Renderer.h"

#include <stdio.h>
#include <stdlib.h>

Renderer::Renderer(int x, int y, int width, int height) : Fl_Gl_Window(x, y, width, height) {
	Fl::add_idle(idle, this);
}

void Renderer::draw() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	if (!valid()) {
		glEnable(GL_CULL_FACE);
		glCullFace(GL_BACK);
	      	glEnable(GL_DEPTH_TEST);
       		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glEnable(GL_NORMALIZE);
	}

	drawClear();		
	drawSetupTransform();

	float ratio = w() / h();
	glFrustum(-0.1f * ratio, 0.1f * ratio, -0.1f, 0.1f, 0.2f, 100.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0, 0, 0, 10, 10, 10, 0, 1, 0);

	glTranslatef(20,20,20);
	drawObjects();
}

void Renderer::drawClear() {
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glClearColor(0,0,0,0);
	glClear(GL_COLOR_BUFFER_BIT);
}

void Renderer::drawObjects() {
	glLoadName(7);
	glBegin(GL_LINES);
		glColor3f(1.0f, 0.0f, 0.0f);
			glVertex3f(0.0f, 0.0f, 0.0f);
			glVertex3f(5.0f, 0.0f, 0.0f);
		glColor3f(0.0f, 1.0f, 0.0f);
			glVertex3f( 0.0f, 0.0f, 0.0f);
			glVertex3f( 0.0f, 5.0f, 0.0f);
		glColor3f(0.0f, 0.0f, 1.0f);
			glVertex3f(0.0f, 0.0f, 0.0f);
			glVertex3f(0.0f, 0.0f, 5.0f);
	glEnd();
}

void Renderer::drawSetupTransform() {
	glOrtho(-1,1,-1,1,-1,1);
}


int Renderer::handle(int event) {
	switch (event) {
		case FL_FOCUS:
			break;
		case FL_UNFOCUS:
			event = 1;
			break;
		case FL_PUSH:
			pick(Fl::event_x(), Fl::event_y());
			break;
	}

	return Fl_Gl_Window::handle(event);
}

void Renderer::idle(void* data) {
	Renderer* window = (Renderer*)data;
	window->redraw();
}

void Renderer::pick(int x, int y) {
	//0,0 in GL leftunder, WINDOW is 0,0 uperleft
	y = 600 -y;

	make_current();

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	int viewport[4];
	glGetIntegerv(GL_VIEWPORT, viewport);

	gluPickMatrix(x,viewport[3]-y,5,5,viewport);
	drawSetupTransform();

	GLuint buf[100];
	glSelectBuffer(100,buf);
	glRenderMode(GL_SELECT);
	glInitNames();
	glPushName(0);

	drawObjects();
	int hits = glRenderMode(GL_RENDER);
	printf("number of hits %d\n", hits);
}
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this