C++ OpenGL Glutsphere location issue - c++

i have a problem regarding my OpenGL code, i try to use
glTranslatef
just to renew my glutsolidsphere location but whatever i try to change.. it make all other unnecessary object move to and it to to the edge of the windows screen setting but it turn out like this.."the image" .it move all my other object.. what wrong with my code?
/* * hello.c * This is a simple,
introductory OpenGL program. */
#include <GL/glut.h>
void display(void)
{
/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT);
/* draw white polygon (rectangle) with corners at
*(0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */
glColor3f (1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex3f (0.50, 0.70, 0.0);
glVertex3f (0.40, 0.60, 0.0);
glVertex3f (0.60, 0.60, 0.0);
glEnd();
glFlush ();
glColor3f (0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f (0.40, 0.20, 0.0);
glVertex3f (0.60, 0.20, 0.0);
glVertex3f (0.60, 0.40, 0.0);
glVertex3f (0.40, 0.40, 0.0);
glEnd();
glFlush ();
glColor3f (0.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.30, 0.30, 0.0);
glVertex3f (0.32, 0.30, 0.0);
glVertex3f (0.40, 0.38, 0.0);
glVertex3f (0.40, 0.40, 0.0);
glVertex3f (0.30, 0.40, 0.0);
glEnd();
glFlush ();
glColor3f (0.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.60, 0.38, 0.0);
glVertex3f (0.68, 0.30, 0.0);
glVertex3f (0.70, 0.30, 0.0);
glVertex3f (0.70, 0.40, 0.0);
glVertex3f (0.60, 0.40, 0.0);
glEnd();
glFlush ();
glColor3f (0.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex3f (0.25, 0.24, 0.0);
glVertex3f (0.30, 0.29, 0.0);
glVertex3f (0.35, 0.24, 0.0);
glVertex3f (0.40, 0.29, 0.0);
glVertex3f (0.45, 0.24, 0.0);
glVertex3f (0.50, 0.29, 0.0);
glVertex3f (0.55, 0.24, 0.0);
glVertex3f (0.60, 0.29, 0.0);
glVertex3f (0.65, 0.24, 0.0);
glVertex3f (0.70, 0.29, 0.0);
glVertex3f (0.75, 0.24, 0.0);
glEnd();
glFlush ();
glColor3f (0.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex3f (0.25, 0.19, 0.0);
glVertex3f (0.30, 0.24, 0.0);
glVertex3f (0.35, 0.19, 0.0);
glVertex3f (0.40, 0.24, 0.0);
glVertex3f (0.45, 0.19, 0.0);
glVertex3f (0.50, 0.24, 0.0);
glVertex3f (0.55, 0.19, 0.0);
glVertex3f (0.60, 0.24, 0.0);
glVertex3f (0.65, 0.19, 0.0);
glVertex3f (0.70, 0.24, 0.0);
glVertex3f (0.75, 0.19, 0.0);
glEnd();
glFlush ();
glColor3f (0.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex3f (0.25, 0.14, 0.0);
glVertex3f (0.30, 0.19, 0.0);
glVertex3f (0.35, 0.14, 0.0);
glVertex3f (0.40, 0.19, 0.0);
glVertex3f (0.45, 0.14, 0.0);
glVertex3f (0.50, 0.19, 0.0);
glVertex3f (0.55, 0.14, 0.0);
glVertex3f (0.60, 0.19, 0.0);
glVertex3f (0.65, 0.14, 0.0);
glVertex3f (0.70, 0.19, 0.0);
glVertex3f (0.75, 0.14, 0.0);
glEnd();
glFlush ();
glColor3f (1.0, 1.0, 0.0);
glTranslatef(0.20,0.27,0.0);
glutSolidSphere(0.06, 20, 20);
glFlush ();
/* don't wait!
* start processing buffered OpenGL routines */
}
void init (void)
{
/* select clearing color */
glClearColor (1.0, 1.0, 1.0, 1.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
/*
* Declare initial window size, position, and display mode
* (single buffer and RGBA). Open window with "hello"
* in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events. */
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (900, 600);
glutInitWindowPosition (50, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
/* ANSI C requires main to return int. */
}

Most of OpenGL acts as a state machine, which means that settings "stick" until you change them again. This includes the transformation state. So when you call:
glTranslatef(0.20,0.27,0.0);
everything that you draw after this point will be translated. And if you call glTranslatef() multiple times, the transformations are accumulated, meaning that you will translate more and more.
If this is not the behavior you want, you have to restore the transformation to the previous state after you draw your sphere. The easiest way to do this is with glPushMatrix()/glPopMatrix(). Transformation matrices in legacy OpenGL are managed as a stack, and pushing/popping the matrix will restore the previous state.
The code around drawing the sphere will then look like this:
glPushMatrix();
glTranslatef(0.2f, 0.27f , 0.0f);
glutSolidSphere(0.06, 20, 20);
glPopMatrix();

Related

How to draw a rhombus with freeglut?

I am trying to draw something like this image of the target output using OpenGL. I am using freeglut. I could draw three polygons, but I don't know how to give the three polygons right positioning. Below I attached the target output and my correct output.
The target output
My output
#include <GL/glut.h>
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
}
void drawSquare(void)
{
glBegin(GL_POLYGON);
glVertex2f(0.0f, -1.0f);
glVertex2f(2.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(-2.0f, 0.0f);
glEnd();
}
void myDraw1(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
drawSquare();
glTranslatef(2.0, 3.0, 0.0);
glRotatef(30, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
drawSquare();
glLoadIdentity();
glTranslatef(-2.0, -3.0, 0.0);
glRotatef(-30, 0.0, 0.0, 1.0);
glColor3f(0.0, 0.0, 1.0);
drawSquare();
glFlush();
}
void myDraw2(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
drawSquare();
glPushMatrix();
glTranslatef(2.0, 3.0, 0.0);
glRotatef(30, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
drawSquare();
glPopMatrix();
glTranslatef(-2.0, -3.0, 0.0);
glRotatef(-30, 0.0, 0.0, 1.0);
glColor3f(0.0, 0.0, 1.0);
drawSquare();
glFlush();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(600, 600);
glutCreateWindow("Rotate");
init();
glutDisplayFunc(myDraw1);
glutMainLoop();
}
The angle between the polygons is 120°. The center of the polygons is (0, 0). You have to rotate around the point (-2, 0). Translate the polygon in that way that that (-2, 0) is moved to (0, 0) and rotate the polygon:
glRotatef(120.0, 0.0, 0.0, 1.0);
glTranslatef(2.0, 0.0, 0.0);
Note, operations like glTranslatef and glRotatef, specify a matrix and multiply the current matrix by the new specified matrix. Use glPushMatrix/glPopMatrix to save and restore the matrix (push on and pop from matrix stack).
e.g:
void myDraw1(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
glPushMatrix();
glRotatef(90.0, 0.0, 0.0, 1.0);
glTranslatef(2.0, 0.0, 0.0);
drawSquare();
glPopMatrix();
glColor3f(0.0, 1.0, 0.0);
glPushMatrix();
glRotatef(210.0, 0.0, 0.0, 1.0);
glTranslatef(2.0, 0.0, 0.0);
drawSquare();
glPopMatrix();
glColor3f(0.0, 0.0, 1.0);
glPushMatrix();
glRotatef(330.0, 0.0, 0.0, 1.0);
glTranslatef(2.0, 0.0, 0.0);
drawSquare();
glPopMatrix();
glFlush();
}

Objects shake when rotating

I'm facing a problem in my opengl code
I'm trying to build a house, and rotate it 360°, for simplicity let's assume the house has the front wall with window and dor, and a back wall.
I'm using DEPTH_BUFFER not to see the back wall when viewing the front wall, and the other way around, but when I rotate the house the door and window start to shake and get distorced.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glLoadIdentity();
gluLookAt(0.0, 0.0, 40.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glEnable(GL_DEPTH_TEST);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(8.0, 3.0, 0.0);
glVertex3f(8.0, -10.0, 0.0);
glVertex3f(1.0, -10.0, 0.0);
glVertex3f(1.0, 3.0, 0.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(-9.0, -4.0, 0.0);
glVertex3f(-9.0, 3.0, 0.0);
glVertex3f(-2.0, 3.0, 0.0);
glVertex3f(-2.0, -4.0, 0.0);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(10.0, -10.0, -20.0);
glVertex3f(-10.0, -10.0, -20.0);
glVertex3f(-10.0, 10.0, -20.0);
glVertex3f(10.0, 10.0, -20.0);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(10.0, -10.0, 0.0);
glVertex3f(10.0, 10.0, 0.0);
glVertex3f(-10.0, 10.0, 0.0);
glVertex3f(-10.0, -10.0, 0.0);
glEnd();
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
The issue is called Z-fighting. This is caused, because depth of the "door", "window" and "wall" are equal. The vertiex coordinates are transformed by the model view matrix and projection matrix and interpolated for each fragment which is covered by the polygon. This results in inaccuracies of the final z coordinate (depth).
Enable the polygon fill offset (glPolygonOffset) by before drawing the walls, to solve the issue:
glEnable(GL_DEPTH_TEST);
glDisable( GL_POLYGON_OFFSET_FILL );
// draw door and window
// ...
glEnable( GL_POLYGON_OFFSET_FILL );
glPolygonOffset( 1, 1 );
// draw walls
// ...
Polygon fill offset manipulates the depth of a fragment by a minimum amount. This causes that the depth of the "walls" is different to the depth of the "window" and "door", even after the transformation by the model view and projection matrix.
Since an offset is added to the depth of the "wall", the "wall" is always behind the window and the door, independent of the point of view.

Drawing an A using a single GL_TRIANGLE_STRIP

Is this possible? (I know, it is, but I can't do it.)
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
if (isWire)
glPolygonMode(GL_FRONT, GL_LINE);
else
glPolygonMode(GL_FRONT, GL_FILL);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(10,10,0);
glVertex3f(20,10,0);
glVertex3f(30,80,0);
glVertex3f(40,70,0);
glVertex3f(50,80,0);
glVertex3f(60,10,0);
glVertex3f(70,10,0);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glEnd();
glFlush();
}
There is my attempt, although my next idea would be to bridge the legs of the A. But I would like to do it in one single strip.
With help from my friend geometry :)
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
if (isWire)
glPolygonMode(GL_FRONT, GL_LINE);
else
glPolygonMode(GL_FRONT, GL_FILL);
// Draw a triangle strip.
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(10.0, 10.0, 0.0);
glVertex3f(20.0, 10.0, 0.0);
glVertex3f(12.0, 30.0, 0.0);
glVertex3f(22.0, 30.0, 0.0);
glVertex3f(17.0, 80.0, 0.0);
glVertex3f(26.0, 70.0, 0.0);
glVertex3f(35.0, 80.0, 0.0);
glVertex3f(31.0, 40.0, 0.0);
glVertex3f(42.0, 35.0, 0.0);
glVertex3f(23.0, 40.0, 0.0);
glVertex3f(25.0, 35.0, 0.0);
glVertex3f(21.9, 30.0,0.0);
glVertex3f(42.0, 35.0, 0.0);
glVertex3f(32.0, 30.0, 0.0);
glVertex3f(46.0, 10.0, 0.0);
glVertex3f(36.5, 10.0, 0.0);
glEnd();
glFlush();
}

GL_QUADS state does not support multiple glBindTexture?

To be short, why couldn't I put two glBindTexture into one glBegin(GL_QUADS)...glEnd()?
I am following chapter 9 of OpenGL red book, http://www.glprogramming.com/red/chapter09.html
In example 9-5, program texbind.c produces two quads with different texture, like this:
The quad on the left has a white checker board texture while the quad on the right has a red one.
Here is the code which maps the textures to two quads:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texName[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
glEnd();
glBindTexture(GL_TEXTURE_2D, texName[1]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
glEnd();
glFlush();
}
We can see that even though the code uses GL_QUADS it draws one quad in each GL_QUADS state. However, if I comment out the glEnd() and glBegin(GL_QUADS) in the middle of this code, like this:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texName[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
// glEnd();
glBindTexture(GL_TEXTURE_2D, texName[1]);
// glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
glEnd();
glFlush();
}
It turns out the second glBindTexture(GL_TEXTURE_2D, texName1) does not work anymore. And the result is:
Could anyone tell me why I cannot put two glBindTexture into one GL_QUADS state?
Because OpenGL doesn't support doing that.
The only calls you can make to OpenGL while inside glBegin/glEnd are vertex submission calls like glTexCoord*, glVertex*, etc. Anything else is an error.
As to "why", one reason is "because the specification says so". More likely, the GPU processes the entire data set (or significant chunks of it) in parallel, during which it cannot switch textures.
Also consider the newer way of drawing with OpenGL: uploading your data to a Vertex Buffer Object and calling one of the glDraw* commands. There's no way you can even call glBindTexture in between primitives using that.

OpenGl program uses up all the ram then closes

I made a program written in OpenGL and when ever I run It the program fills up all the ram and then closes at approximately 3300/4000 Mb of ram. Here is my program:
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glut.h>
#include <gl/GLU.h>
#include <gl/glaux.h>
using namespace std;
GLuint texture;
AUX_RGBImageRec* LoadImage (char* file) {
return auxDIBImageLoad (file);
}
int LoadTexture (char* file) {
AUX_RGBImageRec* Textureimage;
Textureimage = LoadImage (file);
glGenTextures (1, &texture);
glBindTexture (GL_TEXTURE_2D, texture);
glTexImage2D (GL_TEXTURE_2D,
0,
GL_RGB,
Textureimage->sizeX,
Textureimage->sizeY,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
Textureimage->data
);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
delete Textureimage;
return 0;
}
void exitkey (unsigned char key, int x, int y) {
switch (key) {
case 27:
exit (0);
}
}
void Render3d () {
glEnable (GL_DEPTH_TEST);
glEnable (GL_NORMALIZE);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glEnable (GL_LIGHT1);
glEnable (GL_TEXTURE_2D);
}
void incaseofresize (int w, int h) {
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (90.0, (double)w / (double)h, 0.7, 300.0);
}
double theangle = 30.0;
void draw () {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.0, 0.0, -6.0);
glPushMatrix ();
glRotatef (theangle, 0.0, 1.0, 0.0);
GLfloat ambientlightcolor [] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light0color [] = { 0.5, 0.5, 0.0, 1.0 };
GLfloat light1color [] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat light0position [] = { 2.0, 0.0, 3.0, 1.0 };
GLfloat light1position [] = { -2.0, 0.0, 3.0, 1.0 };
glLightModelfv (GL_LIGHT_MODEL_AMBIENT, ambientlightcolor);
glLightfv (GL_LIGHT0, GL_DIFFUSE, light0color);
glLightfv (GL_LIGHT0, GL_POSITION, light0position);
glLightfv (GL_LIGHT1, GL_DIFFUSE, light1color);
glLightfv (GL_LIGHT1, GL_POSITION, light1position);
LoadTexture ("me.bmp");
glBegin (GL_QUADS);
//Drawing shape
glColor3f (1.0, 1.0, 1.0);
//Left side
glNormal3f (0.0, 0.0, 1.0);
glTexCoord2f (0.0, 0.0);
glVertex3f (-1.5, -1.0, -1.5);
glTexCoord2f (1.0, 0.0);
glVertex3f (0.0, -1.0, 1.5);
glTexCoord2f (1.0, 1.0);
glVertex3f (0.0, 1.0, 1.5);
glTexCoord2f (0.0, 1.0);
glVertex3f (-1.5, 1.0, -1.5);
glEnd ();
LoadTexture ("mom.bmp");
glBegin(GL_QUADS);
//Right side
glNormal3f (0.0, 1.0, 1.0);
glTexCoord2f (0.0, 0.0);
glVertex3f (0.0, -1.0, 1.5);
glTexCoord2f (1.0, 0.0);
glVertex3f (1.5, -1.0, -1.5);
glTexCoord2f (1.0, 1.0);
glVertex3f (1.5, 1.0, -1.5);
glTexCoord2f (0.0, 1.0);
glVertex3f (0.0, 1.0, 1.5);
glEnd ();
glBegin (GL_QUADS);
//Back right side
glNormal3f (0.0, 1.0, 0.0);
glVertex3f (1.5, -1.0, -1.5);
glVertex3f (0.0, -1.0, -4.5);
glVertex3f (0.0, 1.0, -4.5);
glVertex3f (1.5, 1.0, -1.5);
glEnd ();
glBegin (GL_QUADS);
glNormal3f (0.0, 1.0, 0.0);
glVertex3f (0.0, -1.0, -4.5);
glVertex3f (-1.5, -1.0, -1.5);
glVertex3f (-1.5, 1.0, -1.5);
glVertex3f (0.0, 1.0, -4.5);
glEnd ();
glutSwapBuffers ();
}
void rotate (int value) {
theangle += 1.5;
if (theangle > 360) {
theangle -=360;
}
glutPostRedisplay ();
glutTimerFunc (50, rotate, 0);
}
int main (int argcpp, char** argv) {
glutInit (&argcpp, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (720, 480);
glutCreateWindow ("OpenGL");
Render3d ();
glutDisplayFunc (draw);
glutKeyboardFunc (exitkey);
glutReshapeFunc (incaseofresize);
glutTimerFunc (50, rotate, 0);
glutMainLoop ();
return 0;
}
I think its the LoadTexture function reloading the image over and over again each loop and I cant find a way to delete the image data after each loop.
Every time your program iterates through the display function it will load images into a new texture object, not freeing the previously created ones.
The canonical way is to load textures only once. OpenGL organizes textures in so called texture objects, identified by a so called name ID. LoadTexture does return this ID. You use such a texture by calling glBindTexture(GL_TEXTURE_2D, theTextureID);