Hey all, I am currently trying (desperately trying if I might add) learning how to use GLUT for school.

Our professor gave us an assignment to make a 2d tube of some sort, but I only managed to make a circle. The tube will be only displayed on its front so it is basically like a donut, a circle within a circle.

Anyways, so I tried running my code and all, but when I run it I just get a white screen on my newly created window where my object should be, why this happens I do not know.

I have followed a basic tutorial on the internet on how to render a simple shape, but still it won't render properly. I also tried using NeHe's tutorials but I get a bunch of errors with it (errors that I cannot solve unless I erase that template of his that enables a full size window)so I gave studying that one up.

Anyways, I'm getting off topic, here is my code:

#include <windows.h>
#include <gl\gl.h>
#include <gl\glut.h>
#include <math.h>

using namespace std;

typedef struct
{
    float x;
    float y;
}CIRCLE;
CIRCLE circle;

void DrawCircle(void)
{
    int k = 5, r = 5, h = 5;
    glBegin(GL_LINES);

    for (int i = 0; i < 180; i++)
    {
        circle.x = r*cos(i) - h;
        circle.y = r*sin(i) + k;

        glVertex3f((circle.x +k), (circle.y - h), 0);

        circle.x = r*cos(i + 0.1) - h;
        circle.y = r*sin(i + 0.1) + k;

        glVertex3f((circle.x +k), (circle.y - h), 0);
    }
    glEnd();
}

int main(int argc, char * argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize (800, 600);
    glutCreateWindow ("Simple Circle");
    glutDisplayFunc(DrawCircle);
    glutMainLoop();

}

Please tell me where did I mess up, I just can't display it..I am using Windows 7 professional btw, if that informations is useful.

Thanks!

Recommended Answers

All 16 Replies

I stopped using GLUT a while ago but I'm pretty sure you have to set up the projection matrix and all that still.

void DrawCircle(void)
{
	glViewport(0, 0, (GLsizei) WINDOW_WIDTH, (GLsizei) WINDOW_HEIGHT);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(60, (GLfloat) WINDOW_WIDTH/ (GLfloat) WINDOW_HEIGHT, 0.1, 60.0);
	
	glTranslatef(0.0, 0.0, -10); //move camera back 10 units (so you can see what you are drawing)

	glMatrixMode(GL_MODELVIEW);

	glLoadIdentity();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glPushMatrix();
		glPushMatrix();
			int k = 5, r = 5, h = 5;
    			glBegin(GL_LINES);
    		
				for (int i = 0; i < 180; i++)
				{
					circle.x = r*cos(i) - h;
					circle.y = r*sin(i) + k;
	
					glVertex3f((circle.x +k), (circle.y - h), 0);

					circle.x = r*cos(i + 0.1) - h;
					circle.y = r*sin(i + 0.1) + k;

					glVertex3f((circle.x +k), (circle.y - h), 0);
				}
    			glEnd();
		glPopMatrix();
	glPopMatrix();
}

Try this out and if it doesn't work I'll look into it a bit more since I pulled this outta one of my OpenGL projects that don't use GLUT.

Hey! thanks for the reply! I tried it, still nothing just that stupid white screen D: Man I am getting hopeless with this haha

I found an old project of mine that uses GLUT. I'll grab out the stuff that you need just gimme a few min.

Okay, thank you very much!!

I would test this code out but I do not have glut on this computer.

The main issue that you have is that you are not setting up your view port projection matrix and model matrix.

#include <windows.h>

#include <gl/gl.h>
#include <gl/glut.h>
#include <math.h>

typedef struct
{
	float x;
	float y;
} CIRCLE;

CIRCLE circle;

int WINDOW_HEIGHT = 800, WINDOW_WIDTH = 600;

void init()
{
	glClearColor(0.0, 0.0, 0.0, 1.0);
	glShadeModel(GL_SMOOTH);

	glEnable(GL_DEPTH_TEST);
}

void DrawCircle()
{
	glViewport(0, 0, (GLsizei) WINDOW_WIDTH, (GLsizei) WINDOW_HEIGHT);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	gluPerspective(60, (GLfloat) WINDOW_WIDTH/ (GLfloat) WINDOW_HEIGHT, 1, 20);
	
	glTranslatef(0, 0, -10);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//objects
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
		glTranslatef( 0.0, 0.0, 0.0 );
		int k = 5, r = 5, h = 5;
		glBegin(GL_LINES);
			for (int i = 0; i < 180; i++)
			{
				circle.x = r*cos(i) - h;
				circle.y = r*sin(i) + k;

				glVertex3f((circle.x +k), (circle.y - h), 0);

				circle.x = r*cos(i + 0.1) - h;
				circle.y = r*sin(i + 0.1) + k;

				glVertex3f((circle.x +k), (circle.y - h), 0);
			}
		glEnd();
	glPopMatrix();
	glutSwapBuffers();
}

void Reshape(int w, int h)
{
	WINDOW_WIDTH = w;
	WINDOW_HEIGHT = h;
}

int main(int argc, char *argv[])
{
	alutInit(NULL, 0);
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Simple Circle");
	
	init();
	
	glutDisplayFunc(DrawCircle);
	glutReshapeFunc(Reshape);
	glutMainLoop();
	
    return 0;
}
commented: Very very helpful :D +1

Yes! It finally draws!

But in this part of the code I believe it is either a typo or unnecessary(?) I say unnecessary because when I ran the code with this line here, it crashed

This is the line:

alutInit(NULL, 0);

Also, I ask what is the use of the viewpoint matrix and model matrix? I am really new to GLUT and know zilch about it. I am just mainly fiddling around some codes and see what works, I know this is a wrong way to learn but I am not really that good with C++ I am mainly a C# type of guy. D:

The alutInit() thing is a useless line because its for ALUT the sound equivalent of GLUT so I didn't mean to include that.

An easy way to think of it is that the GL_PROJECTION matrix is the "camera" and GL_MODELVIEW is the "world" for what you are doing you can move either thing around but if you were going to make a game you would want your camera to move around in the world not the world move around your camera. This is a really basic description of it but its how I thought of it until I played with it more and figured out how stuff works.

The viewport is the portion of the screen that is going to get rendered onto. So this one renders from 0, 0 (the bottom left) to 800, 600 or whatever it gets changed to by the Reshape function.

One thing you might have noticed about GLUT is that it hogs your CPU usage with all its overhead so if you really like using OpenGL I suggest after a while you move on to a WinAPI structure instead of the GLUT one. I would suggest NeHe's but I really hate his tutorials because they are super old and are pretty confusing because of the lack of telling you what is going on.

So if I understand you correctly, this part of the code here:

glViewport(0, 0, (GLsizei) WINDOW_WIDTH, (GLsizei) WINDOW_HEIGHT);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	gluPerspective(60, (GLfloat) WINDOW_WIDTH/ (GLfloat) WINDOW_HEIGHT, 1, 20);

you were just setting up the camera so that i can see what I render is that right?

and in this part here:

gluPerspective(60, (GLfloat) WINDOW_WIDTH/ (GLfloat) WINDOW_HEIGHT, 1, 20);
	
	glTranslatef(0, 0, -10);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

you were readying the world unto which I will be creating my object, correct?

and in the reshape part, if I understand correctly, I basically start from the bottom left and draw unto the entire 800x600 dimension through reshaping? (the reshaping part confused me a little, sorry for my very vague reasoning)

And I agree with you on NeHe's tutorials, even though he tries really hard to explain stuff to you, he'd still overwhelm you with the plethora of information that he throws to you, it is just my luck that my professor uses NeHe's tutorials and I have to just get used to his complexity.

This sets up the viewport (part of the window that will get rendered onto)
You can have more than one viewport but for this we are using one.

glViewport(0, 0, (GLsizei) WINDOW_WIDTH, (GLsizei) WINDOW_HEIGHT);

This sets up the perspective and near far distances (anything drawn 1 to 20 units from the camera will be seen, anything else is not drawn. The camera is also positioned.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(60, (GLfloat) WINDOW_WIDTH/ (GLfloat) WINDOW_HEIGHT, 1, 20);

glTranslatef(0, 0, -10); //move camera back 10 units from the origin (0, 0, 0)

This part now effects the world that you draw.

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, 0); //moves the objects by what you set it to

Also remember that cosine and sinus function takes in radians and not degrees. So this

circle.x = r*cos(i) - h;
circle.y = r*sin(i) + k;

should be this :

float rad = 3.14f/180.0f * i;
circle.x = r*cos(rad) - h;
circle.y = r*sin(rad) + k;

@sfuo

I get it now! Thank you so much!

I ask though in the gluPerspective part, if the 1 and 20 are arguments for the near far distance what does the 60 do? And why do we have to type cast the window width and height to (GLsizei) everytime? Is it the only acceptable data type for each of the functions that use the window height and sizes?

And lastly what does glutInit and your created Init function do?

Also remember that cosine and sinus function takes in radians and not degrees. So this

circle.x = r*cos(i) - h;
circle.y = r*sin(i) + k;

should be this :

float rad = 3.14f/180.0f * i;
circle.x = r*cos(rad) - h;
circle.y = r*sin(rad) + k;

Hello, thank you for your suggestion! :D

In gluPerspective() the 60 is the angle of viewing if you think about your eyes you have a limited field of vision I'm not 100% sure what angle we can see at but most examples that use perspective use 60 (from what I've seen at least). For the type casting I'm pretty sure I tried it without casting it as a GLsizei and I got a warning so I just cast it to the right type.

I'm not too sure what glutInit() does but if there is an open source version of glut (freeglut) that you can check to see what goes on in that function but it will just be a general initialization that acts based on your OS (GLUT is multi-platform). As for my Init() function I use this to set up all the OpenGL state variables (ie depth alpha lighting materials) and maybe assign some objects values when I'm testing since it gets called right at the start.

Ah, so now I've just realized that Init means initialize. I can safely say I guess that when creating these Init functions I am just like setting the preferences on how my drawings would appear during each render.

Is the matrix mode solely used for viewing purposes only? As I can see by the source code, you mainly used it for the projection and the modelview only.

I'm sorry if I ask too much questions, thank you though for answering all of them, it means so much!

So far I have only seen GL_PROJECTION and GL_MODELVIEW being used but I looked up the function and you can have GL_TEXTURE and GL_COLOR as parameters. No idea what the others do but OpenGL has documentation online so for the most part you can just google the function name.

Okay, thank you very much for your help sfuo! Considering I really know zilch about GLUT, I've learned a lot today!

@all:

Is this an acceptable algorithm for drawing a circle inscribed in a circle? I feel like I am kind of cheating it in someway, there must be something more mathematically efficient and correct way to do this.

Here is my code that draws the circle with its inscribed circle:

for (int i = 0; i < 360; i++)
{
 float rad = (PI_CONSTANT/180.0f) * i;
 mainCircle.x = r*cos(rad) - h;
 mainCircle.y = r*sin(rad) + k;
 glVertex3f((mainCircle.x + k), (mainCircle.y - h), 0);
 mainCircle.x = r*cos(rad + 0.1) - h;
 mainCircle.y = r*sin(rad + 0.1) + k;
 glVertex3f((mainCircle.x + k), (mainCircle.y - h), 0);

 inscribedCircle.x = (((r - reduce)*cos(rad)) - ((h - reduce)));
 inscribedCircle.y = (((r - reduce)*sin(rad)) + ((k - reduce)));
 glVertex3f((inscribedCircle.x + (k - reduce)), (inscribedCircle.y - (h - reduce)), 0);
 inscribedCircle.x = (r -reduce)*cos(rad + 0.1) - (h - reduce);
 inscribedCircle.y = (r -reduce)*sin(rad + 0.1) + (k - reduce);
 glVertex3f((inscribedCircle.x + (k - reduce)), (inscribedCircle.y - (h - reduce)), 0);
}

Oh, the reduce part is for the size in how much smaller should the inscribed circle be with respect to the original circle.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.