OpenGL glBegin and glPushMatrix - c

I try to design a scene with 3 spheres and one line horizontal as equator. I got to draw the 3 spheres but I don't know why the line is not draw.
This is my code, for if you can see where I'm wrong:
#include <GL/gl.h>
#include <GL/glut.h>
void render(void);
void reshape(int w, int h);
int angle = 90;
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(800, 600);
glutCreateWindow("Planets");
glutDisplayFunc(render);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
void render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
// Equator
glBegin(GL_LINES);
glColor3f(1,1,1);
glLineWidth(1);
glTranslated(0, 0, 0);
glVertex2f(0, 2);
glVertex2f(2,2);
glEnd();
// Sun
glPushMatrix();
glLoadIdentity();
glColor3f(1.0, 1.0, 0.0);
glTranslated(0, 0, -2);
glRotated(angle, 1, 0, 0);
glutWireSphere(.3, 20, 20);
glPopMatrix();
//Earth
glPushMatrix();
glLoadIdentity();
glColor3f(0.0, 0.0, 1.0);
glTranslated(0.7, 0, -2);
glRotated(angle, 1, 0, 0);
glutWireSphere(.15, 20, 20);
glPopMatrix();
// Moon
glPushMatrix();
glLoadIdentity();
glColor3f(1.0, 0.0, 1.0);
glTranslated(1, 0, -2);
glRotated(angle, 1, 0, 0);
glutWireSphere(.05, 10, 10);
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h) {
const double ar = (double) w / (double) h;
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

You specify a frustum that has the near clip plane at z=-2. Your intended line would be drawn at z=0, thus outside the projection volume, thereby clipped into non-rendering.
glTranslate(0,0,0) is a no-op BTW.

Related

OpenGL GL_POINTS result differs from input

I want to draw something with GL_POINTS but after ~totalpoint/3 result starts differ from input by 1 pixel
I tried different glOrtho and glViewport arguments but nothing changed
my test program:
int w = atoi(argv[1]);
int h = atoi(argv[2]);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, h, 0, 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glLoadIdentity();
unsigned int wf,hf;
unsigned char rgb[3];
while(!glfwWindowShouldClose(window)){
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(1);
glBegin(GL_POINTS);
for(hf=0;hf<h;hf++){
for(wf=0;wf<w;wf++){
memset(rgb,0,3);
rgb[wf%3]=0xff;
glColor3ub(rgb[0],rgb[1],rgb[2]);
glVertex2f(wf,hf);
}
}
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
Results:
Not Colored
Colored
Michael Roy's way its solved my problem i just changed this line
GLFWwindow* wmain = glfwCreateWindow(atoi(argv[1]), atoi(argv[2]), "test", 0, 0);
to
GLFWwindow* wmain = glfwCreateWindow(atoi(argv[1]) + 1, atoi(argv[2]) + 1, "test", 0, 0);

Enlarging a cube and putting it into a 3D space

I've created a program to display a lined cube on a white canvas but I am unsure how to multiple that cube into lets say? 10 x 10.
Another question is how would I go about creating the same cube in a 3D space?
Here's my code:
void drawScene(void)
{
int i, j;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glLoadIdentity();
glTranslatef(0.0, 0.0, -25.0);
glutWireCube(5.0); // Box.
glColor3f(1.0, 0.0, 0.0);
for(i=5; i<5; i++)
{
for (j = -5; j < 5; j++)
{
glPushMatrix();
glTranslatef(i*5, j*5, -35.0);
glColor3f(1.0, 1.0, 0);
glutSolidCube(5.0);
glColor3f(0.0, 0.0, 1.0);
glutWireCube(5.0);
glPopMatrix();
}
}
glFlush();
}
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
}
void resize (int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-10.0, 10.0, -10.0, 10.0, 10.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void KeyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500); /* Size of the Program Window */
glutInitWindowPosition(100,100);
glutCreateWindow("Box.cpp");
setup();
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glutKeyboardFunc(KeyInput);
glutMainLoop();
return 0;
}
The glu library has a lot of useful tidbits like gluLookAt( xfrom, yfrom, zfrom, xto, yto, zto, xup, yup, zup );
You can scale things using glScaled( factor ) / glScalef( factor )
You should poke around Google for some GL 1.X documentation.

Rotating object around itself in OpenGL

i'm having trouble rotating a room around itself. its center is at (5,5,0) so i thought if i translated the room and the objects in the room with glTranslatef(5,5,5) then glRotatef(rotateroom,0,0,1) and then draw the items and use glTranslate(-5,-5,0) it would rotate the room and everything in it around 5,5,0 but it seems to still be rotating around (0,0,0) and i'm not really sure what I'm doing wrong. Thanks for the help in advance.
void drawside(){
int i,j;
/*for (j = 0; j <= 8; j++) {
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord2f((GLfloat)i/30.0, (GLfloat)j/8.0);
glEnd();
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord2f((GLfloat)j/8.0, (GLfloat)i/30.0);
glEnd();
}*/
glEvalMesh2(GL_FILL, 0, 20, 0, 20);
}
void drawRoom(){
//floor
glBegin(GL_POLYGON);
glColor3f(1,1,1);
glNormal3f(0,0,0);
glVertex3f(0,0,0);
glNormal3f(0,10,0);
glVertex3f(0,10,0);
glNormal3f(10,10,0);
glVertex3f(10,10,0);
glNormal3f(10,0,0);
glVertex3f(10,0,0);
glEnd(
);
//wall
glBegin(GL_POLYGON);
glColor3f(0,0,1);
glNormal3f(0,10,0);
glVertex3f(0,10,0);
glNormal3f(0,10,10);
glVertex3f(0,10,10);
glNormal3f(10,10,10);
glVertex3f(10,10,10);
glNormal3f(10,10,0);
glVertex3f(10,10,0);
glEnd();
//wall2
glBegin(GL_POLYGON);
glColor3f(0,1,0);
glNormal3f(10,10,0);
glVertex3f(10,10,0);
glNormal3f(10,10,0);
glVertex3f(10,10,10);
glNormal3f(10,0,10);
glVertex3f(10,0,10);
glNormal3f(10,0,0);
glVertex3f(10,0,0);
glEnd();
}
void drawObjects(){
glColor3f(1,0,1);
glTranslatef(2,2,0);
if(conefill == 1)
glShadeModel(GL_FLAT);
else
glShadeModel(GL_SMOOTH);
glutSolidCone(cone,5,10,2);
glTranslatef(5,5,0);
glColor3f(1,0,0);
if(spherefill == 1)
glShadeModel(GL_FLAT);
else
glShadeModel(GL_SMOOTH);
glutSolidSphere(sphere,20,20);
}
void display(){
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(viewx,viewy, viewz,viewx +lx, 5, viewz + ly,0.0f, 0.0f, 1.0f);
//gluLookAt(viewx,viewy,viewz,headup,headright,5,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
GLfloat light_position[] = {-1.0,5.0,5.0,0.0};
//GLfloat light_direction[] = {-5,-5,-5};
//GLfloat amb[] = {0,0,0,1};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,light_direction);
//glLightfv(GL_LIGHT0, GL_AMBIENT,amb);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(5,5,0);
glRotatef(rotateroom,0,0,1);
glPushMatrix();
drawRoom();
glPopMatrix();
glColor3f(0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(6,3,1);
glPushMatrix ();
drawside();
glPopMatrix();
glPushMatrix();
//glTranslatef(5,0,0);
glRotatef(90,1,0,0);
//glTranslatef(-5,0,0);
glTranslatef(0,.5,.5);
drawside();
glPopMatrix();
glPushMatrix();
//glTranslatef(5,0,0);
glRotatef(180,1,0,0);
//glTranslatef(-5,0,0);
glTranslatef(0,1,0);
drawside();
glPopMatrix();
glPushMatrix();
glRotatef(90,0,0,1);
glTranslatef(-.5,.5,0);
drawside();
glPopMatrix();
glPushMatrix();
glRotatef(270,0,0,1);
glTranslatef(.5,.5,0);
drawside();
glPopMatrix();
glPopMatrix();
drawObjects();
glTranslatef(-5,-5,0);
glPopMatrix();
glutSwapBuffers();
glFlush();
}
You only need to invert the order of the istruction:
glRotatef(rotateroom,0,0,1);
glTranslatef(5,5,0);
Whenever you trasform a point you must invert the sequence of trasformation you want to apply. This dipend on the way the transformation matrix are multiplicated.

gluCylinder() how works OpenGL

I wanted to build a cricket ground with OpenGL. I made several polygons to indicate field, pitch and bowling lines. But the problem is when I am trying to use gluCylinder to make stumps I made depth glEnable(GL_DEPTH_TEST), but my polygons are then not working. I just want to know how Can I use gluCylinder to make stumps with those polygons I have made.
I have following code, but want to add stumps here but I cant,
#include <GL/gl.h>
#include <GL/glut.h>
static double deg=0.0;
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glRotatef(deg, 0.0, 0.0, 1.0); // Rotate by deg
// field
glColor3f (0.0, 0.5, 0.0);
glBegin(GL_POLYGON);
glVertex3f (0, 0, 0.0);
glVertex3f (1, 0, 0.0);
glVertex3f (1, 0.75, 0.0);
glVertex3f (0.8, 0.82, 0.0);
glVertex3f (0.6, 0.85, 0.0);
glVertex3f (0.4, 0.85, 0.0);
glVertex3f (0.2, 0.82, 0.0);
glVertex3f (0.0, 0.75, 0.0);
glEnd();
// pitch
glColor3f (0.25, 0.30, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0.5,0.65,0.0);
glVertex3f(0.47,0.35,0.0);
glVertex3f(0.60,0.35,0.0);
glVertex3f(0.57,0.65,0.0);
glEnd();
//ump line
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.49,0.63,0.0);
glVertex3f(0.49,0.6315,0.0);
glVertex3f(0.58,0.6315,0.0);
glVertex3f(0.58,0.63,0.0);
glEnd();
//bat line
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.46,0.40,0.0);
glVertex3f(0.46,0.4025,0.0);
glVertex3f(0.61,0.4025,0.0);
glVertex3f(0.61,0.40,0.0);
glEnd();
glFlush ();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: // "esc" on keyboard
exit(0);
break;
case 97: // "a" on keyboard
deg = deg+5.0;
glutPostRedisplay();
break;
case 100:
deg = deg-5.0;
glutPostRedisplay();
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (600, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
glEnable(GL_DEPTH_TEST) requires a depth buffer. Make sure to allocate one via oring in GLUT_DEPTH in your glutInitDisplayMode() call.
Make sure to clear your new depth buffer via oring in GL_DEPTH_BUFFER_BIT in your glClear() call.
#include <GL/glut.h>
static double deg=0.0;
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(deg, 0.0, 0.0, 1.0); // Rotate by deg
// field
glColor3f (0.0, 0.5, 0.0);
glBegin(GL_POLYGON);
glVertex3f (0, 0, 0.0);
glVertex3f (1, 0, 0.0);
glVertex3f (1, 0.75, 0.0);
glVertex3f (0.8, 0.82, 0.0);
glVertex3f (0.6, 0.85, 0.0);
glVertex3f (0.4, 0.85, 0.0);
glVertex3f (0.2, 0.82, 0.0);
glVertex3f (0.0, 0.75, 0.0);
glEnd();
// pitch
glColor3f (0.25, 0.30, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0.5,0.65,0.0);
glVertex3f(0.47,0.35,0.0);
glVertex3f(0.60,0.35,0.0);
glVertex3f(0.57,0.65,0.0);
glEnd();
//ump line
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.49,0.63,0.0);
glVertex3f(0.49,0.6315,0.0);
glVertex3f(0.58,0.6315,0.0);
glVertex3f(0.58,0.63,0.0);
glEnd();
//bat line
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.46,0.40,0.0);
glVertex3f(0.46,0.4025,0.0);
glVertex3f(0.61,0.4025,0.0);
glVertex3f(0.61,0.40,0.0);
glEnd();
glPopMatrix();
glFlush ();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: // "esc" on keyboard
exit(0);
break;
case 'a': // "a" on keyboard
deg = deg+5.0;
glutPostRedisplay();
break;
case 'z':
deg = deg-5.0;
glutPostRedisplay();
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (600, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
Be aware that GL_POLYGON only supports convex polygons.

fatal error C1083: Cannot open include file: 'aux.h': Permission denied

I copied the following code from OpenGL's Redbook (Chapter 4: Display Lists) into my editor (Visual Studio Express Edition 2008), named it list.c and got compilation error.
#include "aux.h"
#include <GL/gl.h>
#include <GL/glu.h>
GLuint listName = 1;
void myinit (void)
{
glNewList (listName, GL_COMPILE);
glColor3f(1.0, 0.0, 0.0);
glBegin (GL_TRIANGLES);
glVertex2f (0.0, 0.0);
glVertex2f (1.0, 0.0);
glVertex2f (0.0, 1.0);
glEnd ();
glTranslatef (1.5, 0.0, 0.0);
glEndList ();
glShadeModel (GL_FLAT);
}
void drawLine (void)
{
glBegin (GL_LINES);
glVertex2f (0.0, 0.5);
glVertex2f (15.0, 0.5);
glEnd ();
}
void display(void)
{
GLuint i;
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
for (i = 0; i < 10; i++)
glCallList (listName);
drawLine ();
glFlush ();
}
void myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D (0.0, 2.0, -0.5 * (GLfloat) h/(GLfloat) w,
1.5 * (GLfloat) h/(GLfloat) w);
else
gluOrtho2D (0.0, 2.0 * (GLfloat) w/(GLfloat) h, -0.5,
1.5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
auxInitDisplayMode (AUX_SINGLE | AUX_RGBA);
auxInitPosition (0, 0, 400, 50);
auxInitWindow (argv[0]);
myinit ();
auxReshapeFunc (myReshape);
auxMainLoop(display);
}
Don't use not aux, nor glaux. They're too old. GLUT is much more easier, while many of people use SDL or SFML (they're more flexible and feature-full than GLUT). You should try SFML - it has lots of features inside, it's lightweight and portable. You should be satisfied using this one =)
Still if you wanna use GLUT, here are your problems:
glutInit() should be called before
any other operations
glutInitDisplayMode() and other
window functions must be called
before any drawing
operations
Here's some sample code:
#include <GL/glut.h >
// rendering function
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glBegin (GL_LINES);
glVertex2f (0.0, 0.5);
glVertex2f (15.0, 0.5);
glEnd ();
glFlush();
}
// some initializations
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int argc, char **argv)
{
// initialize GLUT library
glutInit(&argc, argv);
// set display mode
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// set initial window size
glutInitWindowSize(250,250);
// set initial window position
glutInitWindowPosition(100,100);
// create the window
glutCreateWindow("moofoo");
// do our initialization routines
init();
// set function which will be called each frame
glutDisplayFunc(display);
// enter main rendering loop
glutMainLoop();
return 0;
}
You need to include the Windows header file. Fix:
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
// etc...
I have changed the code placing glutXxx functions instead of auxXxx ones. It compiles but doesn't run, I'd appreciate feedback here:
#include "GL/glut.h"
#include <stdio.h>
//#include <windows.h>
GLuint listName = 1;
void myinit (void)
{
glNewList (listName, GL_COMPILE);
glColor3f(1.0, 0.0, 0.0);
glBegin (GL_TRIANGLES);
glVertex2f (0.0, 0.0);
glVertex2f (1.0, 0.0);
glVertex2f (0.0, 1.0);
glEnd ();
glTranslatef (1.5, 0.0, 0.0);
glEndList ();
glShadeModel (GL_FLAT);
}
void drawLine (void)
{
glBegin (GL_LINES);
glVertex2f (0.0, 0.5);
glVertex2f (15.0, 0.5);
glEnd ();
}
void display(void)
{
GLuint i;
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
for (i = 0; i < 10; i++)
glCallList (listName);
drawLine ();
glFlush ();
}
void myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D (0.0, 2.0, -0.5 * (GLfloat) h/(GLfloat) w,
1.5 * (GLfloat) h/(GLfloat) w);
else
gluOrtho2D (0.0, 2.0 * (GLfloat) w/(GLfloat) h, -0.5,
1.5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition (100, 100);
glutInitWindowPosition (100,100);
glutInit (&argc, argv);
myinit ();
glutDisplayFunc (display);
glutReshapeFunc (myReshape);
glutMainLoop();
}

Resources