PDA

View Full Version : guys anything wrong with the following code to draw 3D line



sumanmali
04-06-2016, 07:07 PM
glLineWidth(2.5);
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINES);
glVertex3f(-200.0, -5.0, 0.5);
glVertex3f(200.0,-5.0,0.5);
glEnd();

codeman_nz
04-06-2016, 07:49 PM
Code looks fine. Is it not working?

sumanmali
04-07-2016, 09:38 AM
nop!
a blank screen comes as output

codeman_nz
04-07-2016, 05:39 PM
You will need to post more of your code.

sumanmali
04-07-2016, 08:26 PM
#include<stdio.h>
#include<glut.h>

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glLineWidth(2.5);
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINES);
glVertex3f(10.0, 5.0, 0.5);
glVertex3f(200.0,50.0,0.5);
glEnd();
glFlush();
}

void myinit()
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
void myReshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)h/(GLfloat)w,-2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("ATRY");
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
myinit();
glutMainLoop();
}

Spoops
04-08-2016, 12:39 PM
Don't load the identity matrix in your display function, as it will reset the orthographic projection if you don't resize the window.

Carmine
04-08-2016, 06:04 PM
First of all the background color you set is not being displayed.
This is because you asked for double buffering but never call glutSwapBuffers.
It must be called at the end of your display() routine.
Set the ClearColor to something other than black or white for debugging purposes.
Once you are SURE the window is being cleared to the correct color, change it back to black or white.

The call to gluOrtho2D in your myInit routine is not doing anything because
it's being overridden by the glOrtho calls in your ReShape function. In fact,
the only line that's doing anything in your myInit routine is the call to glClearColor.
You can comment out all the other lines in myInit.

The glOrtho calls in myReshape are setting the bounds of your window.
Those calls set the x bounds from -2.0 to +2.0.
You are trying to draw a line with x coordinates from 10 to 200.
Of course it's not going to show up. It's totally outside the bounds of the window.
Try commenting out the glOrtho calls and replacing them with -


glOrtho (0.0, 300.0, 0.0, 300.0, -10.0, 10.0);

It worked for me. After that works for you, you can try to get more sophisticated with the glOrtho call.

sumanmali
04-09-2016, 01:53 AM
:)thanks it worked!