I need help in understanding how to scale my coordinates. The instruction says, modify the code to move the triangle to the mouse click position. Can someone explain how this is done?
Here is the source code I'm working with:
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stddef.h>
#include <iostream>
// values controlled by fast keys
float g_angle = 0.0f;
float g_xoffset = 0.0f;
float g_yoffset = 0.0f;
int x;
int y;
// increments
const float g_angle_step = 32.0f; // degrees
const float g_offset_step = 32.0f; // world coord units
// last cursor click
int g_cursor_x = 0;
int g_cursor_y = 0;
void draw_triangle()
{
// in model cooridnates centred at (0,0)
static float vertex[3][2] =
{
{-1.0f, -1.0f},
{1.0f, -1.0f},
{0.0f, 1.0f}
};
glBegin(GL_LINE_LOOP);
for (size_t i=0;i<3;i++)
glVertex2fv(vertex[i]);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glLineWidth(2.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(500.0f+g_xoffset, 500.0f+g_yoffset, 0.0f);
glScalef(100.0f, 100.0f, 1.0f);
glRotatef(g_angle, 0.0f, 0.0f, 1.0f);
draw_triangle();
glPopMatrix(); // done with stack
glutSwapBuffers();
}
// handles mouse click events
// button will say which button is presed, e.g. GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON
// state will say if the button is GLUT_UP or GLUT_DOWN
// x and y are the poitner position
void mouse_click(int button, int state, int x, int y)
{
if (button==GLUT_LEFT_BUTTON)
{
std::cerr << "\t left mouse button pressed!" << std::endl;
if (state==GLUT_UP)
{
std::cerr << "\t button released...click finished" << std::endl;
g_cursor_x = x;
g_cursor_y = y;
std::cerr << "\t cursor at (" << g_cursor_x << ", " <<
g_cursor_y << ")" << std::endl;
}
}
else
if (button==GLUT_RIGHT_BUTTON)
{
std::cerr << "\t right mouse button pressed!" << std::endl;
}
// Here is my attempt:
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
float x_min = (-x+500)/512;
float x_max = (x-500)/512;
float y_min = (-y+500)/512;
float y_max = (y-500)/512;
gluOrtho2D(x_min, x_max, y_min, y_max);
//glTranslatef(x/512, 1-y/512, 0.0f);
std::cerr << x << ", " << y << std::endl;
}
glutPostRedisplay();
}
void mouse_motion(int x, int y)
{
std::cerr << "\t mouse is at (" << x << ", " << y << ")" << std::endl;
glutPostRedisplay();
}
// will get which key was pressed and x and y positions if required
void keyboard(unsigned char key, int, int)
{
std::cerr << "\t you pressed the " << key << " key" << std::endl;
switch (key)
{
case 'q': exit(1); // quit!
// clockwise rotate
case 'r': g_angle += g_angle_step; break;
}
glutPostRedisplay(); // force a redraw
}
// any special key pressed like arrow keys
void special(int key, int, int)
{
// handle special keys
switch (key)
{
case GLUT_KEY_LEFT: g_xoffset -= g_offset_step; break;
case GLUT_KEY_RIGHT: g_xoffset += g_offset_step; break;
case GLUT_KEY_UP: g_yoffset += g_offset_step; break;
case GLUT_KEY_DOWN: g_yoffset -= g_offset_step; break;
}
glutPostRedisplay(); // force a redraw
}
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 1000, 0, 1000);
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
g_cursor_x = g_cursor_y = 500; // middle of window
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(512, 512);
glutInitWindowPosition(50, 50);
glutCreateWindow("Mouse Test");
glutDisplayFunc(display);
// handlers for keyboard input
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
// mouse event handlers
glutMouseFunc(mouse_click);
glutPassiveMotionFunc(mouse_motion);
init();
glutMainLoop();
return 0;
}
This should solve your issue.replace your code(your attempt) with the code below.
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
g_xoffset = x;
g_yoffset = 1000-y;
} glutPostRedisplay();
Related
I'm trying to left click and drag any 3d Object and if i let go of it, it should stay in its new position, how would i achieve this? The 3d object is loaded from the draw function that i have in a header file.
Someone said i should be using glutMouseFunc or glutMotionFunc.
void MouseClickCallbackFunction(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
if (button == GLUT_LEFT)
{
std::cout << "Left " << x << " " << y <<std::endl;
leftClick.x = x;
leftClick.y = y;
}
else if (button == GLUT_RIGHT_BUTTON) {
std::cout << "Right " << x << " " << y << std::endl;
rightClick.x = x;
rightClick.y = y;
}
}
theGame->mouseClicked(button, state, x, y);
glutPostRedisplay();
}
/* function MouseMotionCallbackFunction()
* Description:
* - this is called when the mouse is clicked and moves
*/
void MouseMotionCallbackFunction(int x, int y)
{
theGame->mouseMoved(x, y);
glutPostRedisplay();
}
int main(int argc, char **argv)
{
/* initialize the window and OpenGL properly */
glutInit(&argc, argv);
glutInitContextVersion(4, 2);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("OpenGL Framework");
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
std::cout << "GLEW could not be initialized. \n";
system("pause");
return 0;
}
//glewInit();
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
/* set up our function callbacks */
glutDisplayFunc(DisplayCallbackFunction);
glutKeyboardFunc(KeyboardCallbackFunction);
glutKeyboardUpFunc(KeyboardUpCallbackFunction);
glutMouseFunc(MouseClickCallbackFunction);
glutMotionFunc(MouseMotionCallbackFunction);
glutTimerFunc(1, TimerCallbackFunction, 0);
/* init the game */
theGame = new Game();
theGame->initializeGame();
/* start the game */
glutMainLoop();
return 0;
}
Here is the Draw fucntion with the object
void Game::draw()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
PassThrough.Bind();
PassThrough.SendUniformMat4("uModel", MonkeyTransform.data, false);
PassThrough.SendUniformMat4("uView", CameraTransform.GetInverse().data, false);
PassThrough.SendUniformMat4("uProj", CameraProjection.data, false);
PassThrough.SendUniform("uTex", 0);
PassThrough.SendUniform("LightPosition", CameraTransform.GetInverse() * vec4(4.0f,0.0f,0.0f,1.0f));
PassThrough.SendUniform("LightAmbient", vec3(0.15f, 0.15f, 0.15f));
PassThrough.SendUniform("LightDiffuse", vec3(0.7f,0.1f,0.2f));
PassThrough.SendUniform("LightSpecular", vec3(0.8f,0.1f,0.1f));
PassThrough.SendUniform("LightSpecularExponent", 50.0f);
PassThrough.SendUniform("Attenuation_Constant", 1.0f);
PassThrough.SendUniform("Attenuation_Linear", 0.1f);
PassThrough.SendUniform("Attenuation_Quadratic", 0.01f);
glBindVertexArray(Monkey.VAO);
glDrawArrays(GL_TRIANGLES, 0, Monkey.GetNumVerticies());
glBindVertexArray(0);
PassThrough.unBind();
glutSwapBuffers();
}
The concept is called mouse picking. A method to achieve this is ray casting. Essentially what you want to do is map the mouse coordinates to a region in your scene. In your case you want to check if the position is within the monkey. Then it is as simple as handling the mouse up, down, and move events.
Mouse down: check if object is being picked. If it is, great- maybe highlight it or something. Store ref to object picked; store position of mouse at beginning of pick (pos)
Mouse move: is mouse down? Is picked object ref? Update object position based on delta between pos and coord in mouse move event
Mouse up: clear picked obj ref, position
This link may be of interest http://antongerdelan.net/opengl/raycasting.html
I would like to plot 3D point specified by the value from column[0] in text file. For example, column[0] equals to 2,then displays 3D point(xyz) following these: (1.529, 0.25,-2.038 ), (1.530,0.253,-2.040), (1.530,0.253,-2.044)
test.txt
1 1.529 0.253 -2.038
1 1.529 0.253 -2.038
2 1.529 0.253 -2.038
2 1.530 0.253 -2.040
2 1.530 0.253 -2.044
3 1.532 0.254 -2.038
3 1.533 0.255 -2.036
3 1.533 0.255 -2.036
3 1.534 0.255 -2.036
3 1.534 0.255 -2.036
This is my code
#include <Windows.h>
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
char title[] = "Point";
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black
glClearDepth(1.0f); // Set background depth to farthest
glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
glDepthFunc(GL_LEQUAL); // Set the type of depth-test
glShadeModel(GL_SMOOTH); // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(1.5f, 0.0f, -7.0f); // Move right and into the screen
vector<vector<double>> tempdouble;
ifstream in("test.txt");
string line;
double Dstage1x = 0.0;
double Dstage1y = 0.0;
double Dstage1z = 0.0;
double Dstage2x = 0.0;
double Dstage2y = 0.0;
double Dstage2z = 0.0;
double Dstage3x = 0.0;
double Dstage3y = 0.0;
double Dstage3z = 0.0;
int ii = 0;
int stage1 = 0;
int stage2 = 0;
int stage3 = 0;
while (std::getline(in, line))
{
tempdouble.push_back(vector<double>());
stringstream ss(line);
double num;
while (ss >> num)
{
tempdouble.back().push_back(num);
}
}
for (int i = 0; i < tempdouble.size(); i++)
{
for (int j = 0; j < tempdouble[i].size(); j++)
{
int st1_x=0; int st1_y=0; int st1_z=0;
int st2_x=0; int st2_y=0; int st2_z=0;
int st3_x=0; int st3_y=0; int st3_z=0;
if(tempdouble[i][0]==1 )
{
glPointSize(5);glColor3f(1.0f, 0.5f, 0.0f);
glBegin(GL_POINTS);
st1_x += Dstage1x*0; st1_y += Dstage1y*0; st1_z += Dstage1z*0; //set value
Dstage1x = st1_x + tempdouble[i][1];
Dstage1y = st1_y + tempdouble[i][2];
Dstage1z = st1_z + tempdouble[i][3];
stage1++;
//cout <<"Check tempdouble[i][0] state1 : " << tempdouble[i][0] <<"\t"<<endl;
cout << "state1(xyz) : " << "( " << Dstage1x << " ," << Dstage1y << "," << Dstage1z << ")"<< endl;
glVertex3f(Dstage1x, Dstage1y, Dstage1z);
//glVertex3f(tempdouble[i][1], tempdouble[i][2], tempdouble[i][3]);
//glVertex3f(tempdouble[i][j], tempdouble[i][j], tempdouble[i][j]);
glEnd();
break;
}
if (tempdouble[i][0] == 2) {
glPointSize(5);glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_POINTS);
st2_x += Dstage2x * 0; st2_y += Dstage2y*0; st2_z += Dstage2z*0; //set value
Dstage2x = st2_x + tempdouble[i][1];
Dstage2y = st2_y + tempdouble[i][2];
Dstage2z = st2_z + tempdouble[i][3];
stage2++;
cout << "state2(xyz) : " << "( " << Dstage2x << " ," << Dstage2y << "," << Dstage2z << ")"<< endl;
glVertex3f(Dstage2x,Dstage2y,Dstage2z);
//glVertex3f(tempdouble[i][1], tempdouble[i][2], tempdouble[i][3]);
//glVertex3f(tempdouble[i][j], tempdouble[i][j], tempdouble[i][j]);
glEnd();
break;
}
if (tempdouble[i][0] == 3) {
glPointSize(5); glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POINTS);
st3_x += Dstage3x*0; st3_y += Dstage3y*0; st3_z += Dstage3z*0; //set value
Dstage3x = st3_x + tempdouble[i][1];
Dstage3y = st3_y + tempdouble[i][2];
Dstage3z = st3_z + tempdouble[i][3];
stage3++;
cout << "state3(xyz) : " << "( " << Dstage3x << " ," << Dstage3y << "," << Dstage3z << ")"<< endl;
glVertex3f(Dstage3x,Dstage3y,Dstage3z);
//glVertex3f(tempdouble[i][0],tempdouble[i][1],tempdouble[i][2]);
//glVertex3f(tempdouble[i][j], tempdouble[i][j], tempdouble[i][j]);
glEnd();
break;
}
}
}
cout << "stage 1: "<< stage1 << endl;
cout << "stage 2: "<<stage2 << endl;
cout << "stage 3: "<<stage3 << endl;
glutSwapBuffers(); // Swap the front and back frame buffers (double buffering)
}
void reshape(GLsizei width, GLsizei height) { // GLsizei for non-negative integer
// Compute aspect ratio of the new window
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping volume to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset
// Enable perspective projection with fovy, aspect, zNear and zFar
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}
void changeViewPort(int w, int h)
{
glViewport(0, 0, w, h);
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
int main(int argc, char* argv[]) {
void display();
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
glutInitWindowSize(640, 480); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutCreateWindow(title); // Create window with the given title
glutDisplayFunc(display); // Register callback handler for window re-paint event
glutReshapeFunc(reshape); // Register callback handler for window re-size event
initGL(); // Our own OpenGL initialization
glutMainLoop();
return 0;
}
My problem is that the point shown in the wrong position. It is impossible that column[0](equal to 1,2,3) have all the same points.Can someone help me with this
setlocale( LC_NUMERIC, "C");
.
https://rocketgit.com/user/bowler17/gl/source/tree/branch/wrench/blob/t.c
line just before big do while
I have this code:
#include <iostream>
#include <GL/glut.h>
using namespace std;
bool* Keys = new bool[256];
void keyboardDown(unsigned char key, int x, int y)
{
Keys[key] = true;
}
void keyboardUp(unsigned char key, int x, int y)
{
Keys[key] = false;
}
void reshape(int width, int height)
{
GLfloat fieldOfView = 90.0f;
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fieldOfView, (GLfloat)width / (GLfloat)height, 0.1, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void draw()
{
if (Keys['e'])
cout << "e" << endl;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
glColor3f(0.1, 0.1, 0.1); glVertex3f(1.0, 1.0, -1.0);
glColor3f(0.1, 0.9, 0.1); glVertex3f(1.0, -1.0, -1.0);
glColor3f(0.9, 0.1, 0.1); glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.1, 0.1, 0.9); glVertex3f(-1.0, 1.0, -1.0);
glEnd();
glFlush();
glutSwapBuffers();
}
void initGL(int width, int height)
{
reshape(width, height);
glClearColor(0.1f, 0.5f, 0.7f, 1.0f);
glClearDepth(1.0f);
glOrtho(0, 1, 0, 1, 1, 10);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
/* initialize GLUT settings, register callbacks, enter main loop */
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800, 800);
glutInitWindowPosition(100, 100);
glutCreateWindow("Perspective's GLUT Template");
glutKeyboardFunc(keyboardDown);
glutKeyboardUpFunc(keyboardUp);
glutSpecialFunc(keyboardSpecialDown);
glutSpecialUpFunc(keyboardSpecialUp);
glutReshapeFunc(reshape);
glutDisplayFunc(draw);
glutIgnoreKeyRepeat(true); // ignore keys held down
initGL(800, 600);
glutMainLoop();
return 0;
}
When I start the program, it writes 'e' only when I press on the mouse. I can't find the problem. Why does it only work when the mouse is held down?
You dont have mouse functions in your code, thats why it is not working. Insert from here : http://www.zeuscmd.com/tutorials/glut/03-MouseInput.php
#include <iostream>
#include <gl/glut.h>
using namespace std;
bool lbuttonDown = false;
bool init()
{
return true;
}
void display()
{
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_RIGHT_BUTTON)
{
if (state == GLUT_DOWN)
cout << "Right button pressed"
<< endl;
else
cout << "Right button lifted "
<< "at (" << x << "," << y
<< ")" << endl;
}
else if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
lbuttonDown = true;
else
lbuttonDown = false;
}
}
void motion(int x, int y)
{
if (lbuttonDown)
cout << "Mouse dragged with left button at "
<< "(" << x << "," << y << ")" << endl;
}
void motionPassive(int x, int y)
{
cout << "Mouse moved at "
<< "(" << x << "," << y << ")" << endl;
}
void entry(int state)
{
if (state == GLUT_ENTERED)
cout << "Mouse Entered" << endl;
else
cout << "Mouse Left" << endl;
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(200, 200);
glutInitWindowSize(200, 200);
glutCreateWindow("03 - Mouse Input");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutPassiveMotionFunc(motionPassive);
glutEntryFunc(entry);
if (!init())
return 1;
glutMainLoop();
return 0;
}
I had the same problem [using freeglut] and calling 'draw()' [or whatever display callback you use] right after glutPostRedisplay() in the key callback function forced it to do the redraw. I actually don't know why it behaves like this. After data is changed by keyboard input and the key callback returned, a redraw is expected but not executed.
I tried to convert the mouse coords in click to opengl coords. Actually it seems to work, but when I uncomment a cout-line that I just wrote for testing it sets my vars to not a number -nan.
How does it happen? How can I fix it?
//global:
GLdouble mouseOgl[3] = {0.0,0.0,0.0};
//handle click events of the mouse
void myMouse(int button, int state, int x, int y)
{
//mouse coords to gl coords
switch (button)
{
case GLUT_LEFT_BUTTON:
if(state == GLUT_UP){ //on release left mouse button
std::cout << x << " * "<< y << std::endl;
GetOGLPos(x, y);
std::cout
<< mouseOgl[0] << " # "
<< mouseOgl[1] << " # "
<< mouseOgl[2] << " # "
<< std::endl;
glutPostRedisplay();
}
break;
}
}
whole code here:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <math.h>
#include <time.h>
#include <GL/glut.h>
const GLint nNumPoints = 5;
GLfloat ctrlpoints[5][3] = {
{ -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0},
{2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}, {6.0, 2.0, 0.0}};
GLdouble mouseOgl[3] = {0.0,0.0,0.0};
void GetOGLPos(int x, int y);
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_FLAT);
glMap1f(GL_MAP1_VERTEX_3, // Type of data generated
0.0f, // Lower u range
1.0f, // Upper u range
3, // Distance between points in the data
nNumPoints, // number of control points
&ctrlpoints[0][0]); // array of control points
// Enable the evaluator
glEnable(GL_MAP1_VERTEX_3);
glEnable(GL_DEPTH);
}
void display(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
//Kontrollpunkte:
glPointSize(5.0);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
for (i = 0; i < nNumPoints; i++)
glVertex3fv(&ctrlpoints[i][0]);
glEnd();
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINE_STRIP);
for (i = 0; i < nNumPoints; i++)
glVertex3fv(&ctrlpoints[i][0]);
glEnd();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//keep aspect ratio:
if (w <= h)
glOrtho(-10.0, 10.0, -10.0*(GLfloat)h/(GLfloat)w,
10.0*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho(-10.0*(GLfloat)w/(GLfloat)h,
10.0*(GLfloat)w/(GLfloat)h, -10.0, 10.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//handle click events of the mouse
void myMouse(int button, int state, int x, int y)
{
//mouse coords to gl coords
switch (button)
{
case GLUT_LEFT_BUTTON:
if(state == GLUT_UP){ //on release left mouse button
std::cout << x << " * "<< y << std::endl;
GetOGLPos(x, y);
std::cout
<< mouseOgl[0] << " # "
<< mouseOgl[1] << " # "
<< mouseOgl[2] << " # "
<< std::endl;
glutPostRedisplay();
}
break;
}
}
// detailed information:
// http://nehe.gamedev.net/article/using_gluunproject/16013/
void GetOGLPos(int x, int y)
{
//init vars:
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;
//get gl specs
glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get Modelmatrix
glGetDoublev( GL_PROJECTION_MATRIX, projection ); //get projection matrix
glGetIntegerv( GL_VIEWPORT, viewport ); //get viewport values
//calculate the gl mouseposition
winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
/*
following line needed to run the program propper?!?!
*/
std::cout << "positions:" << posX << " | " << posY << " | " << posZ << std::endl;
mouseOgl[0] = posX;
mouseOgl[1] = posY;
mouseOgl[2] = posZ;
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (600, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(myMouse);
glutMainLoop();
return 0;
}
mouse callback can be called any time, so you are not sure what is the proper state of your rendering code when you are in that callback...
I think You should mark that the mouse was pressed (save it to some variable wasMousePressed = true;) in this callback and then check mouse hits in your OnRender function. That way it will be synchronized with the opengl. Then check if your cout code works properly.
onMouse() {
if (...)
mousePressed = true;
else
mousePressed = false;
}
onRender() {
clearBuffer();
setupCameraAndProjection();
if (mousePressed)
checkOGLState();
render...
}
I have written a color picker in C++ using OpenGL but I cannot figure out how to color a polygon (or anything else for that matter) with the colors I have stored. Do I need to put the draw function in some kind of loop or is there a better way to do this?
#ifdef __APPLE__
# include <OpenGL/gl.h>
# include <OpenGL/glu.h>
# include <GLUT/glut.h>
#else
# include <GL/gl.h>
# include <GL/glu.h>
# include <GL/glut.h>
#endif
#include <iostream>
#include <cmath>
using namespace std;
#define WIDTH 750
#define HEIGHT 750
int i=0,mousex, mousey;
float pick[3];
bool mouseleftdown = false;
void hex(void){
glBegin(GL_POLYGON);
glColor3f(1,0,0); //red
glVertex2f(0, 2); //top
glColor3f(1,.38,.01); //orange
glVertex2f(2, 1); //top right
glColor3f(1,1,0); //yellow
glVertex2f(2, -1); //bottom right
glColor3f(0,1,0); //green
glVertex2f(0, -2); //bottom
glColor3f(0,0,1); //blue
glVertex2f(-2, -1); //bottom left
glColor3f(.8,0,.8); //purple
glVertex2f(-2, 1); //top left
glEnd();
glEnd();
}
void square(void){
glBegin(GL_POLYGON);
glVertex2i(1, -1);
glVertex2i(1, 1);
glVertex2i(-1, 1);
glVertex2i(-1, -1);
glEnd();
}
void mouse(int button, int state, int x, int y){
// Save the left button state
/*if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
glutPostRedisplay(); // Left button has changed; redisplay!
}*/
// Save the mouse position
mousex = x;
mousey = y;
glReadPixels(mousex , mousey , 1 , 1 , GL_RGB , GL_FLOAT , &pick);
cout << pick[0] <<"pick";
cout << " mouse x " << mousex << "\n";
cout << " mouse y " << mousey << "\n";
fflush(stdout);
cout << "pick R: " << pick[1] << "\n";
cout << "pick G: " << pick[0] << "\n";
cout << "pick B: " << pick[2] << "\n";
}
void draw(void){
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glScalef(20,20,1);
hex();
glPopMatrix();
glPushMatrix();
glTranslatef(100,100,0);
glColor3f(pick[1],pick[0],pick[2]);
glScalef(20,20,1);
square();
glPopMatrix();
glFlush();
}
void my_init(void){
//glClearColor(0, 0, 0, 1); //sets clear color
glLineWidth(4);
gluOrtho2D(-100, 100, -100, 100); //sets origin
}
int main(int argc, char* argv[ ]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Color Picker");
glutDisplayFunc(draw);
glutMouseFunc(mouse);
my_init();
glClearColor(1, 1, 1, 0);
glutMainLoop(); //listens for events
return 0;
}
You have to simply add the line:
glutPostRedisplay();
at the end of your mouse callback function.
This is because you always have to explicitly tell glut when to refresh the window.