Jump to content

OpenGl

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
How can I draw a square in OpenGL without using the functions:
glTranslate * (), glScale * () and glRotate * ().

I tried:

#include <gl\gl.h>							 

#include <gl\glut.h>


void display(void)

{

     glMatrixMode(GL_MODELVIEW);

     glLoadIdentity();

     glClear(GL_COLOR_BUFFER_BIT);

     glColor3f(0.0f,0.0f,1.0f);

     glBegin(GL_QUADS);

             glVertex2i(100,150);

             glVertex2i(100,100);

             glVertex2i(150,100);

             glVertex2i(150,150);

     glEnd();

     

     glFlush();

}


void init(void)

{

     glClearColor(0.0, 0.0, 0.0, 1.0);

}


int main(int argc, char** argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

    glutInitWindowSize(500, 500);

    glutInitWindowPosition(100, 100);

    glutCreateWindow("Window");

    glutDisplayFunc(display);

    init();

    glutMainLoop();

    return 0;

}


But it does not appear square :confused:

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,718 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
First of all, coordinates are by default based on a floating-point system where 1.0 is the maximum extent of each axis. So if you want to draw a point at (X,Y) do this:

glVertex2f((float)X / WINDOW_WIDTH, (float)Y / WINDOW_HEIGHT)

I modified your code as such and it worked for me.

Edit: The origin is the center of your screen, so the left-most point is -1.0 and the bottom-most point is -1.0.
sudo rm -rf /

#3
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

dargueta said:

First of all, coordinates are by default based on a floating-point system where 1.0 is the maximum extent of each axis. So if you want to draw a point at (X,Y) do this:

glVertex2f((float)X / WINDOW_WIDTH, (float)Y / WINDOW_HEIGHT)

I modified your code as such and it worked for me.

Edit: The origin is the center of your screen, so the left-most point is -1.0 and the bottom-most point is -1.0.

Sorry for my delay. It worked:

#include <gl\gl.h>							 

#include <gl\glut.h>


void display(void)

{

     glMatrixMode(GL_MODELVIEW);

     glLoadIdentity();

     glClear(GL_COLOR_BUFFER_BIT);

     glColor3f(0.0f,0.0f,1.0f);

     glBegin(GL_QUADS);

             glVertex2i(100,150);

             glVertex2i(100,100);

             glVertex2i(150,100);

             glVertex2i(150,150);

     glEnd();

     

     glFlush();

}


void init(void)

{

     glMatrixMode(GL_PROJECTION);

     glOrtho(0,500, 0,500,-1,1);

     glClearColor(0.0, 0.0, 0.0, 1.0);

}


int main(int argc, char** argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

    glutInitWindowSize(500, 500);

    glutInitWindowPosition(100, 100);

    glutCreateWindow("Window");

    glutDisplayFunc(display);

    init();

    glutMainLoop();

    return 0;

}


I have to tilt the square using only glLoadMatrix () and glMultMatrix () ?

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,718 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
I'm not sure. There are other ways to do it too. If you must use those methods, there are a bunch of tutorials online about that sort of thing.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users