Setting camera and frustum in OpenGL - opengl

Why isn't the quad rendered? I have tried with glOrtho and glFrustrum, and the output is the same: a black screen. From what I understand, this code says that the camera is set in position (0,0,-3) and its looking at the origin. The near plane is at -2 (1 m away from the camera). What am I doing wrong?
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//glOrtho (-5,5,-5,5,-5,5);
glFrustum(-5,5,-5,5,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-3,0,0,0,0,1,0);
glColor3f(1.0,1.0,1.0);
glBegin(GL_QUADS);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(0.5,-0.5,-0.5);
glVertex3f(0.5,0.5,-0.5);
glVertex3f(-0.5,0.5,-0.5);
glEnd();
// Don't wait start processing buffered OpenGL routines
glFlush();
glutSwapBuffers();

It works fine for me, something must be wrong in another part of your code.
Here's the complete code I used:
#include <GL/glut.h>
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//glOrtho (-5,5,-5,5,-5,5);
glFrustum(-5,5,-5,5,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-3,0,0,0,0,1,0);
glColor3f(1.0,1.0,1.0);
glBegin(GL_QUADS);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(0.5,-0.5,-0.5);
glVertex3f(0.5,0.5,-0.5);
glVertex3f(-0.5,0.5,-0.5);
glEnd();
// Don't wait start processing buffered OpenGL routines
glFlush();
glutSwapBuffers();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(200,200);
glutInitWindowSize(512,512);
glutCreateWindow("Test");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
I compiled it like this:
gcc test.cpp -lGL -lglut -lGLU -o test
Here's a screenshot:

Related

Line won't appear in opengl

I am trying to draw a simple line in opengl and i have set up the environemt properly and when executing the code i get a blank screen rather than the line.
This is my code
#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);
glPointSize(5.0);
glLineWidth(5.0);
glBegin(GL_LINES);
glVertex2d(0.0, 0.0);
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;
}
In your display function you have swapped the buffers before the line is drawn. you have to swap the buffers after the line is drawn . so your code should appear as follows:
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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();
glutSwapBuffers();
}

opengl glTranslatef() cant get more than one in my code

i have to make 4 rectangles all in the middle and transform them to different
locations.
with this all my glrects get the first translate i have also pushMatrix but nothing.
Help me please
#include <GL\glut.h>
void display() {
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(3);
glColor3f(0,0,1);
glBegin(GL_LINES);
glVertex2f(0,-24);
glVertex2f(0,24);
glVertex2f(-32,0);
glVertex2f(32,0);
glEnd();
glTranslatef(-32,-14,0);
glColor3f(1,0,0);
glRecti(0,0,20,10);
glTranslatef(10,5,0);
glColor3f(0,1,0);
glRecti(0,0,20,10);
glFlush();
}
int main(int argc, char** argv){
glutInit(&argc,argv);
glutInitWindowPosition(50,50);
glutInitWindowSize(640,480);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutCreateWindow("Example");
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-32,32,-24,24);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
It's because you let GL_PROJECTION active:
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-32,32,-24,24);
After that you have to make modelview active:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Glrotate function not working with the other ones

I need to perform a simple rotation i dont where it went wrong
i want to perform a rotation about a arbitary point.along with reflection and translation,basically its a composite tranformation.
void drawRectangle(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPushMatrix ();
glRotatef(75.0,1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,100.0,0.0);
glVertex3f(100.0,100.0,0.0);
glVertex3f(100.0,0.0,0.0);
glEnd();
glPopMatrix ();
glFlush();
}
void init_Visualizer(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION); //projection,3D-2D
glLoadIdentity();
glMatrixMode (GL_MODELVIEW);
gluOrtho2D(-640.0,640.0,-480.0,480.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//single buffering(?)
glutInitWindowSize(1200,540);//actual window where to be displayed
glutInitWindowPosition(100,100);
glutCreateWindow("Lab1");
init_Visualizer();
glutIdleFunc(drawRectangle);
glutMainLoop();
}

My display() function only displays when it enters it the first time. Then it shows a blank window

void init(void)
{
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
void display(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glScalef(1.0,1.0,1.0);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glutSwapBuffers();
}
void reshape(int w, int h)
{
int height = h;
int width = w;
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char* argv[])
{
Complex c(0,0);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(512, 512);
glutInitWindowPosition(100, 100);
winID = glutCreateWindow("Fractal");
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
// Compute the update rate here...
glutMainLoop();
return 0;
}
I get a square if I put the code in display(), except the glutSwapBuffers() in if condition which checks whether the code has entered display the first time. If I remove the if, I get a white window
Put an extra glTranslatef(0.0f, 0.0f, -5.0f) in your display function, to push everything 5 units back into the scene, otherwise you wont see what you're drawing at the origin.
Btw, the line glScalef(1.0,1.0,1.0) doesn't do anything, remove it to avoid unnecessary calculations.
EDIT:
There is no "eye" in OpenGL. More specifically, it is always fixed at (0,0,0) looking down at the negative z-axis. You have to move the whole scene with the inverse of the eye transformations. You can either move the scene manually so your eye can see it, or use gluLookAt, where you can specify the "eye", but in the background it just transforms the scene so the fixed "eye" can see it.
Further reading: http://www.opengl.org/archives/resources/faq/technical/viewing.htm

Point doesn't show in OpenGL

I am trying to draw a point using OpenGL like below, but it only displays a black window. Can someone tell me what's the error?
#include "stdafx.h"
#include <gl/GL.h>
#include <gl/GLU.h>
#include <gl/glut.h>
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,0,1.0);
glPointSize(10);
//glShadeModel(GL_FLAT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (0.0,0.0,400, 150);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
float pointSize = 5;
glPointSize(10);
glBegin(GL_POINTS); // render with points
glVertex2i(50,40); //display a point
glEnd();
glFlush();
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,(GLdouble)w,0.0,(GLdouble)h,-1.0,1.0);
}
int _tmain(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 150);
glutCreateWindow("Draw Simple Object");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
The parameters you're passing to gluOrtho2d look wrong. The order is left, right, top, bottom. You've set both the left and right to 0.0. Based on your glutInitWindowSize call, I'd guess what you want is something like gluOrtho2d(0.0, 400.0, 0.0, 150.0); (or maybe gluOrtho2d(0.0, 400.0, 150.0, 0.0);) instead.
Could it be that the points you draw are black and the background is, too?
Have you tried adding this line to the beginning of your display function:
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);