Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7697679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:04:16+00:00 2026-05-31T22:04:16+00:00

I have written a simple OpenGL program in C++ that displays a line joining

  • 0

I have written a simple OpenGL program in C++ that displays a line joining the center of the window to the current position of the mouse pointer.

My code is :

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include<iostream>

using namespace std;

void passive(int,int);
void reshape(int,int);
void init(void);
void display(void);
void camera(void);

int x=3,y=3;

int main (int argc,char **argv) {
    glutInit (&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
    glutInitWindowSize(1364,689);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Sample");
    init();
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutPassiveMotionFunc(passive);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

void display(void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    camera();

    glBegin(GL_LINES);
        glVertex3f(0,0,0);
        glVertex3f(x,y,0);
    glEnd();
    glutSwapBuffers();
}    

void camera(void) {
    glRotatef(0.0,1.0,0.0,0.0);
    glRotatef(0.0,0.0,1.0,0.0);
    glTranslated(0,0,-20);
}    

void init(void) {
    glEnable (GL_DEPTH_TEST);
    glEnable (GL_BLEND);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_COLOR_MATERIAL);
}    

void reshape(int w, int h) {
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();
    gluPerspective(60,(GLfloat)w/(GLfloat)h,1.0,100.0);
    glMatrixMode(GL_MODELVIEW);
}

void passive(int x1,int y1) {
    x=x1; y=y1;
}

The problem I am facing is that the x and y values set in the passive() function is not correctly mapped into the screen which uses perspective projection. So the line drawn is joining the center to some other coordinate outside the screen. Any modifications to the code to get it working properly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:04:17+00:00Added an answer on May 31, 2026 at 10:04 pm

    An easy way would be to create an orthographic projection matrix and then render all of your “2D” elements (including this line, using the screen coordinates provided by glutPassiveMotionFunc).

    Something like this:

    void display() {
        // clear
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective( ... ) // create 3D perspective projection matrix
        glMatrixMode(GL_MODELVIEW);
    
        // Render 3D content here
    
        // Render 2D content
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0, width, height, 0); // create 2D orthographic projection matrix with coordinate system roughly equivalent to window position
        glMatrixMode(GL_MODELVIEW);
    
        glBegin(GL_LINES);
        glVertex2f( width / 2, height / 2 ); // now we can use "pixel coordinates"
        glVertex2f( cursorX, cursorY );
        glEnd();
    
        ...
    }
    

    Compare this to your modification of the perspective projection in your reshape method.

    Obviously you’ll also want to disable states that don’t make sense for a “2D” rendering (like depth buffer checking, etc) but it should be pretty obvious. Take a look at this GDSE post for a discussion of how other people do this same task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a simple application that displays a list of candidates for a
I have written this simple C program that changes the rotation file name. Eg:
Ok, so I want to run simple opengl program that I've written, on other
I have written a simple program that does autocorrelation as follows...I've used pgi accelerator
I have written a simple MSN -style program that will send and retrieve messages
I have written a simple WCF service that accepts and stores messages. It works
I have written a simple C++ shell program to parse large XML files and
I have written a very simple WCF service, that worked fine (code below), then
I have written a simple perl script to read a line from a .csv
I am very new to JAVA. I have written simple program (in Linux -VIM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.