I am having trouble setting a orthographical projection matrix of dimensions 4x2 with (0,0) being in the center, (-2, -1) being at the bottom left corner, and (2, 1) being at the top right corner.
I use glutInitWindowSize(600, 300); to initialize the window size.
In my reshape function, I use glViewport(0, 0, w, h); to set the viewport.
Also in my reshape function, I use gluOrtho2D(-(float)w/h, (float)w/h, -2.0, 2.0); to set the ortho.
However, when I move my mouse around to see the world coordinates, the bottom left corner is (-1, -1) and the top right corner is (1, 1). Is there something I am doing wrong here? I am assuming since I am setting bottom and top to -2 and 2 respectively in the gluOrtho2D call, it should give me the right coordinates.
Please help me out if you see anything that might be wrong.
Here is my code so far, please ignore the arm variables and drawing functions.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#if 0 /*unix*/
#include <GL/glut.h>
#endif
#if 1 /*apple */
#include <GLUT/glut.h>
#include <OPENGL/gl.h>
#include <OPENGL/glext.h>
#endif
#if 0 /*windows*/
#include <io.h>
#include <fcntl.h>
#include <glut.h>
#endif
int GW, GH;
bool animfore, animupper;
float foreangle, upperangle;
using namespace std;
void drawShoulder();
void drawUpper();
void drawFore();
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
//draw shoulder
glPushMatrix();
drawShoulder();
//draw upper arm
glPushMatrix();
drawUpper();
//draw forearm
glPushMatrix();
drawFore();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutPostRedisplay();
glutSwapBuffers();
}
void drawShoulder() {
//cout << "Enters" << endl;
glBegin(GL_POLYGON);
glColor3f((float)30/255, (float)0/255, (float)30/255);
glVertex2f(-2.0, -0.4);
glVertex2f(-2.0, -1.0);
glVertex2f(-1.0, -1.0);
glVertex2f(-1.0, -0.4);
glEnd();
}
void drawUpper() {
}
void drawFore() {
}
void reshape(GLsizei w, GLsizei h) {
GW = w;
GW = h;
glViewport(0, 0, w, h);
glLoadIdentity();
gluOrtho2D(-(float)w/h, (float)w/h, -1.0, 1.0);
cout << "Enters" << endl;
}
void keyboard(unsigned char key, int x, int y) {
switch(key) {
case 'q' : case 'Q' :
exit(EXIT_SUCCESS);
break;
}
}
//control arm animations
void idle() {
if(animupper) {
}
if(animfore) {
}
}
float p2w_x(int gx) {
return (float)2.*GW/(GW*GH-GH)*gx-(float)GW/GH;
}
float p2w_y(int gy) {
int py = GH-1-gy;
return (float)2./(GH-1.)*py-1;
}
void mouseMove(int x, int y) {
cout << "(" << p2w_x(x) << "," << p2w_y(y) << ")" << endl;
}
int main(int argc, char **argv) {
// global variable intializations
GW = 600;
GH = 300;
animfore, animupper = true;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
// initialization
glutInitWindowSize(600, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("Robot Arm");
glClearColor(1.0, 1.0, 1.0, 1.0);
// callback functions
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMotionFunc(mouseMove);
glutMainLoop();
}
Try setting glMatrixMode( GL_PROJECTION ) before you make your ortho call and then set it back to GL_MODELVIEW when you do your transformations.
Related
in this method
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
that uses the structure i made
struct GLintPoint {
GLint x,y;
};
it says that there is an error and that "no operator "[]" matches these operands, operator types are GLintPoint[int]" where i try to assign a value from points to point. Well i did use the right brackets and it is an int in there so what is the problem? FYI this code is to draw the Sierpinski gasket with the user making the initial 3 points by clicking the screen with the mouse. Just in case you would like to see it here is the whole program.
#include <windows.h>
#include <gl/Gl.h>
#include "glut.h"
#include <iostream>
using namespace std;
const int screenWidth = 640;
const int screenHeight = 480;
struct GLintPoint {
GLint x,y;
};
void display (void){
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0,1.0,1.0);
glFlush();
}
void drawDot( GLint x, GLint y)
{
glBegin( GL_POINTS );
glVertex2i( x, y );
glEnd();
}
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
void myMouse(int button, int state, int x, int y){
static GLintPoint corner[2];
static int numCorners = 0;
if(state == GLUT_DOWN){
if(button == GLUT_LEFT_BUTTON){
corner[numCorners].x = x;
corner[numCorners].y = screenHeight - y;
if(++numCorners ==2 ){
glRecti(corner[0].x, corner[0].y, corner[1].x, corner[1].y);
numCorners = 0;
glFlush();
}
}
else if(button == GLUT_RIGHT_BUTTON){
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
}
}
void myInit() {
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
}
void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100,150);
glutCreateWindow("mouse dots");
glutDisplayFunc(display);
glutPostRedisplay();
glutMouseFunc(myMouse);
myInit();
glutMainLoop();
}
I'm inferring from the name of the function argument, points, that you intend the function to accept an array of points. But it's actually just written to take one.
So when you write points[index] the compiler is looking for operator[] in your GLintPoint struct (and there isn't one).
If you change the function prototype to take an array of GLintPoints I think you'll have better luck.
Can someone help me out in checking my code? I'm trying to draw a simple dot by clicking in the window, but I can't see any points drawn. The point recording system is copied as an example from one of the stack overflow posts.
#include <stdlib.h>
#include <stdio.h>
//#include <GL\glew.h>
#include <gl/glut.h>
//#include <GL\freeglut.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "draw.h"
//DRAW draw2;
class Point
{
int Xvalue, Yvalue;
std::string Text;
public:
void xy(int x, int y)
{
Xvalue = x;
Yvalue = y;
}
void text(std::string text)
{
Text = text;
}
//return individual x y value
int x() { return Xvalue; }
int y() { return Yvalue; }
//return text value
std::string rtext() { return Text; }
};
Point point[30];
int count = 0;
void Init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
//glColor3f(0.0, 0.0, 0.0);
//glPointSize(5);
glMatrixMode(GL_PROJECTION); //coordinate system
//glLoadIdentity();
gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT); // clear display window
glColor3f(1.0, 1.0, 1.0);
//glLoadIdentity();
/*Drawing here*/
//redraw
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
//draw2.Dot(x, y);
std::cout << "drawing dot " << x << " " << y << std::endl;
glBegin(GL_POINTS);
glColor3f(0.3, 0.3, 0.3);
glPointSize(5.0f);
glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
glEnd();
}
glFlush();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
point[count].xy(x, y);
//draw2.Dot(x, y);
count++;
}
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv); //initialize toolkit
//Request double buffered true color window with Z-buffer
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); //display mode
glutInitWindowSize(1200, 800); //set window size
glutCreateWindow("NURBS Curve"); //open screen window
Init(); //additional initializations
//CALLBACK FUNCTIONS
glutMouseFunc(mouse);
glutDisplayFunc(Display); //send graphics to display window
/*
GLenum err = glewInit();
if (GLEW_OK != err)
{
fprintf(stderr, "GLEW error");
return 1;
}
*/
//pass control to glut for events, loops
glutMainLoop();
return 0;
}
glBegin(GL_POINTS);
glColor3f(0.3, 0.3, 0.3);
glPointSize(5.0f); // wat
glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
glEnd();
There's a short list of GL functions you can call inside a glBegin()/glEnd() pair and glPointSize() is not on it:
Only a subset of GL commands can be used between glBegin and glEnd.
The commands are
glVertex,
glColor,
glSecondaryColor,
glIndex,
glNormal,
glFogCoord,
glTexCoord,
glMultiTexCoord,
glVertexAttrib,
glEvalCoord,
glEvalPoint,
glArrayElement,
glMaterial, and
glEdgeFlag.
Also,
it is acceptable to use
glCallList or
glCallLists to execute
display lists that include only the preceding commands.
If any other GL command is executed between glBegin and glEnd,
the error flag is set and the command is ignored.
Move the glPointSize() outside the glBegin()/glEnd() pair:
glPointSize(5.0f);
glBegin(GL_POINTS);
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
glColor3f(0.3, 0.3, 0.3);
glVertex2i(x, h - y);
}
glEnd();
All together:
#include <gl/glut.h>
class Point
{
int Xvalue, Yvalue;
public:
void xy(int x, int y)
{
Xvalue = x;
Yvalue = y;
}
//return individual x y value
int x() { return Xvalue; }
int y() { return Yvalue; }
};
Point point[30];
int count = 0;
void Display()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT); // clear display window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const double w = glutGet( GLUT_WINDOW_WIDTH );
const double h = glutGet( GLUT_WINDOW_HEIGHT );
gluOrtho2D(0.0, w, 0.0, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0);
glPointSize(5.0f);
glBegin(GL_POINTS);
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
glColor3f(0.3, 0.3, 0.3);
glVertex2i(x, h - y);
}
glEnd();
glFlush();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
point[count].xy(x, y);
count++;
}
glutPostRedisplay();
}
int main( int argc, char *argv[] )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(1200, 800);
glutCreateWindow("NURBS Curve");
glutMouseFunc(mouse);
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}
Found out that glcolor3f and glpointsize had to be initialized first in my Init() function.
void Init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0); //this one
glPointSize(5); //and this one
glMatrixMode(GL_PROJECTION); //coordinate system
//glLoadIdentity();
gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
}
I'm trying to make this functions like google maps or http://phrogz.net/tmp/canvas_zoom_to_cursor.html.
How to use mouse position like centre of zooming?
How to drag image by moving cursor? Want to do it with button==0 && state==GLUT_DOWN.
Any help much appreciated
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "glut.h"
#define windowSize 900.0
double zoomFactor = 1;
int mouseX=0, mouseY=0;
void reshape (int w, int h)
{
}
void mouse(int button, int state, int x, int y) {
if (button == 0)
{
if (state == GLUT_UP) return;
zoomFactor -= 0.05;
}
else if (button == 2) {
if (state == GLUT_UP) return;
zoomFactor += 0.05;
}
else return;
glutPostRedisplay();
}
void motion(int ax, int ay) {
mouseX = ax;
mouseY = ay;
}
Void display(){
glClear(GL_COLOR_BUFFER_BIT);
glViewport (0.0, 0.0, glutGet(GLUT_INIT_WINDOW_WIDTH), glutGet(GLUT_INIT_WINDOW_HEIGHT));
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (-glutGet(GLUT_INIT_WINDOW_WIDTH)*zoomFactor, glutGet(GLUT_INIT_WINDOW_WIDTH)*zoomFactor,\
-glutGet(GLUT_INIT_WINDOW_HEIGHT)*zoomFactor, glutGet(GLUT_INIT_WINDOW_HEIGHT)*zoomFactor);
glBegin(GL_LINES);
glVertex2f ((0),(0));
glVertex2f ((100),(0));
glEnd();
glBegin(GL_LINES);
glVertex2f ((100),(0));
glVertex2f ((100),(100));
glEnd();
glFlush();
}
int main(int argc,char** argv) {
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(windowSize, windowSize);
glutInitWindowPosition(500,0);
glutCreateWindow("test");
glClearColor(0, 0.1,0.8,0.90);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 200, 200,0);
glutDisplayFunc( display );
glutPassiveMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop(;
return(0);
}
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.