Today we will look at how to draw a simple quad in openGL.
First of all lets look at what is a Quad. A quad is the short term for Quadrangle. A quadrangle can be explained as a four sided plane figure.
To do this we will use the same code from the previous tutorial on how to draw a line.
The only snippet of code we are concerned about is code within the display function.
///////////////////////////////////////////////////////////
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
glColor3f(1.0, 0.0, 0.0);
glPointSize(5.0);
glLineWidth(5.0);
glBegin(GL_LINES);
glVertex2d(0.0, 0.0);
glVertex2d(0.5,0.5);
glEnd();
}
////////////////////////////////////////////////////////////
To draw a quad, we have to first specify that we are drawing a quad, to do this we can say
glBegin(GL_QUADS); instead of glBegin(GL_LINES);
also as i mentioned before a quad is sometthing with four sides, now we have to specify where the vertex or the point of each corner of the quad is located. To do this we can use glVertex2D(); or if you want to draw the quad in 3 dimensional space we can use glVertex3D(); and specify the location of the points accordingly. For this tutorial we will look as only 2D space. so to draw the quad we can say:
glBegin(GL_QUADS);
glVertex2d(-0.5, -0.5);
glVertex2d(-0.5, 0.5);
glVertex2d(0.5, 0.5);
glVertex2d(0.5, -0.5);
glEnd();
Note that the glPointSize() and glLineWidth() will have no effect on the quad we draw, so we can simply delete them.
when modified your code should now look like this :
////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "freeglut.h"
#include <Windows.h>
#include <iostream>
using namespace std;
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2d(-0.5, -0.5);
glVertex2d(-0.5, 0.5);
glVertex2d(0.5, 0.5);
glVertex2d(0.5, -0.5);
glEnd();
}
int main(int argc, char* argv[]) {
// Initialize GLUT
glutInit(&argc, argv);
// Set up some memory buffers for our display
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
// Set the window size
glutInitWindowSize(800, 600);
// Create the window with the title "Hello,GL"
glutCreateWindow("Hello, GL");
// Bind the two functions (above) to respond when necessary
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
////////////////////////////////////////////////////////////////////////////////////
Now just go ahead and click on local windows debugger and you should get the following result.
congratulations , u have successfully drawn your first quad.
You can now play around and find out the other primitive shape types on openGL and draw somethings, until we we meet again in the next post to discuss something a bit more advanced and complex.
First of all lets look at what is a Quad. A quad is the short term for Quadrangle. A quadrangle can be explained as a four sided plane figure.
To do this we will use the same code from the previous tutorial on how to draw a line.
The only snippet of code we are concerned about is code within the display function.
///////////////////////////////////////////////////////////
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
glColor3f(1.0, 0.0, 0.0);
glPointSize(5.0);
glLineWidth(5.0);
glBegin(GL_LINES);
glVertex2d(0.0, 0.0);
glVertex2d(0.5,0.5);
glEnd();
}
////////////////////////////////////////////////////////////
To draw a quad, we have to first specify that we are drawing a quad, to do this we can say
glBegin(GL_QUADS); instead of glBegin(GL_LINES);
also as i mentioned before a quad is sometthing with four sides, now we have to specify where the vertex or the point of each corner of the quad is located. To do this we can use glVertex2D(); or if you want to draw the quad in 3 dimensional space we can use glVertex3D(); and specify the location of the points accordingly. For this tutorial we will look as only 2D space. so to draw the quad we can say:
glBegin(GL_QUADS);
glVertex2d(-0.5, -0.5);
glVertex2d(-0.5, 0.5);
glVertex2d(0.5, 0.5);
glVertex2d(0.5, -0.5);
glEnd();
Note that the glPointSize() and glLineWidth() will have no effect on the quad we draw, so we can simply delete them.
when modified your code should now look like this :
////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "freeglut.h"
#include <Windows.h>
#include <iostream>
using namespace std;
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2d(-0.5, -0.5);
glVertex2d(-0.5, 0.5);
glVertex2d(0.5, 0.5);
glVertex2d(0.5, -0.5);
glEnd();
}
int main(int argc, char* argv[]) {
// Initialize GLUT
glutInit(&argc, argv);
// Set up some memory buffers for our display
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
// Set the window size
glutInitWindowSize(800, 600);
// Create the window with the title "Hello,GL"
glutCreateWindow("Hello, GL");
// Bind the two functions (above) to respond when necessary
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
////////////////////////////////////////////////////////////////////////////////////
Now just go ahead and click on local windows debugger and you should get the following result.
congratulations , u have successfully drawn your first quad.
You can now play around and find out the other primitive shape types on openGL and draw somethings, until we we meet again in the next post to discuss something a bit more advanced and complex.
Comments
Post a Comment