Why does the below code is showing a triangle in front of the quad for:
gluLookAt(10, 10, 60, 0, 0, 0, 0, 1, 0)
and
glOrtho(-30, 30, -30, 30, 0, 90)
Expected Output: Because the 'z' value of the triangle is less than the quad and the observer is at 60 unit in the positive 'z' axis, the rectangle should have been visible as a whole and only a few parts of the triangle would had been visible.
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glVertex3f(10, 10, 15); glVertex3f(20, 10, 15); glVertex3f(20, 20, 15); glVertex3f(10, 20, 15);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0, 1, 0);
glVertex3f(0, 0, 0);
glVertex3f(22, 14, 0);
glVertex3f(16, 22, 0);
glEnd();
First of all, your question is totally unrelated to clipping - Clipping means calculating the intersection of the primitves against some clip area or clip volume.
The triangle does appear because you draw it after the quad. By default, OpenGL will not apply any visibility algorithms. However, OpenGL does support the Z buffer algorithm in form of the depth test.
So finally I have got answer to this.
It required few lines of code to be added.
Adding below 3 lines in the initialization step
glClearDepth(1.0f);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
Initializing display mode to add depth information (GLUT_DEPTH ) as below
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
Below is the complete code for displaying a rectangle behind which is a traingle.
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include<iostream>
using namespace std;
void FPS(void);
GLint gFramesPerSecond = 0;
int TIMER_SPEED = 10000;
float i = 40;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(10, 10, i-=.2, 10, 10, -10, 0, 1, 0);
glBegin(GL_LINES);
glColor3f(1, 0, 0);
glVertex3f(0, 0, 0); glVertex3f(20, 0, 0);
glColor3f(0, 1, 0);
glVertex3f(0, 0, 0); glVertex3f(0, 20, 0);
glColor3f(0, 0, 1);
glVertex3f(0, 0, 0); glVertex3f(0, 0, 50);
glEnd();
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glVertex3f(10, 10, 10); glVertex3f(20, 10, 15); glVertex3f(20, 20, 15); glVertex3f(10, 20, 15);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0, 1, 0);
glVertex3f(12, 12, 20);
glVertex3f(22, 14, 12);
glVertex3f(16, 22, 12);
glEnd();
glutSwapBuffers();
}
void timer(int v)
{
static GLfloat u = 0.0;
u += 0.01;
glutTimerFunc(TIMER_SPEED/60.0, timer, ++v);
display();
}
void Init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0f);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
void Resize(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-15, 30, -15, 30, 15, 40);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);
glutCreateWindow("3D Object in OpenGL");
glutKeyboardFunc(myKeyboard);
Init();
glutDisplayFunc(display);
glutReshapeFunc(Resize);
glutTimerFunc(100, timer, 0);
glutMainLoop();
return 0;
}
Related
I am working on a simple OpenGL project.
I want to get a simple camera to move in perspective mode.
I keep reading about the projection matrix, gluLookAt, and the model view matrix. I keep reading that all I should do are my perspective calls in the projection matrix and then all of my transformations and camera movement in the model view matrix.
#include "GLheaders.h"
void drawWorldAxis() {
glLoadIdentity();
glBegin(GL_LINES);
glNormal3f(0, 0, 1);
glColor3ub(255, 0, 0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glColor3ub(0, 255, 0);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glColor3ub(0, 0, 255);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
}
void keyboard(unsigned char key, int x, int y) {
glutPostRedisplay();
}
static float eye[3] = {.5, .5, .5};
#include <stdio.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(55.0, 1, .1, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawWorldAxis();
printf("eye at <%f, %f, %f>\n", eye[0], eye[1], eye[2]);
fflush(stdout);
gluLookAt(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0);
eye[0] += .1;
eye[1] += .1;
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(55.0, 1, -1, 10000);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
glutInitWindowSize(400,400);
glutCreateWindow("Tiny Test");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return EXIT_SUCCESS;
}
I am expecting this code to display three lines representing the world coordinate system's x, y, and z axises, and as keys are pressed the camera should move and start to look at the origin/coordinate axises from more and more drastic angles.
What is going wrong here? I've been bashing my head into a wall trying to figure out why nothing is moving. It only changes if I put the gluLookAt call in the projection matrix which I keep being told is a terrible idea.
The coordinate cross is drawn before you set the lookAt matrix, thus the matrix has no effect.
You have to change the order such that the matrix is already present when drawing:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0);
drawWorldAxis();
printf("eye at <%f, %f, %f>\n", eye[0], eye[1], eye[2]);
fflush(stdout);
Then there is a second problem: You are resetting the model matrix in the first line of drawWorldAxis. Here, you can either remove the glLoadIdentity call or push the previous matrix to the stack first:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0);
glPushMatrix();
drawWorldAxis();
glPopMatrix();
printf("eye at <%f, %f, %f>\n", eye[0], eye[1], eye[2]);
fflush(stdout);
thanks to #BDL for helping fix this! This is the correct code that I wanted
#include "GLheaders.h"
void drawWorldAxis() {
glBegin(GL_LINES);
glNormal3f(0, 0, 1);
glColor3ub(255, 0, 0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glColor3ub(0, 255, 0);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glColor3ub(0, 0, 255);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
}
void keyboard(unsigned char key, int x, int y) {
glutPostRedisplay();
}
static float eye[3] = {-.1, -.1, 1};
#include <stdio.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(55.0, 1, .1, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
printf("eye at <%f, %f, %f>\n", eye[0], eye[1], eye[2]);
fflush(stdout);
gluLookAt(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0);
drawWorldAxis();
eye[0] += .1;
eye[1] += .1;
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(55.0, 1, -1, 10000);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
glutInitWindowSize(400,400);
glutCreateWindow("Tiny Test");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return EXIT_SUCCESS;
}
I am using GLUT and for some reason my glOrtho isn't working properly.
This is my code:
#include"Dependencies\glew\glew.h"
#include"Dependencies\freeglut\freeglut.h"
#include<iostream>
void render(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1, 1, 1, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1280, 720, 0, 0, 1);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float x = 0.0;
float y = 0;
glBegin(GL_QUADS);
glColor3f(1, 0, 0);
glVertex2f(x, y);
glVertex2f(x + .1, y);
glVertex2f(x + .1, y + .1);
glVertex2f(x, y + .1);
glEnd();
glutSwapBuffers();
}
void closeCallback()
{
std::cout << "GLUT:\t Finished" << std::endl;
glutLeaveMainLoop();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(32, 32);
glutInitWindowSize(1280, 720);
glutCreateWindow("MahGame");
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glewInit();
glutDisplayFunc(render);
glutCloseFunc(closeCallback);
glutMainLoop();
return 0;
}
Even though I set my x and y floats to 0 I renders it in the middle, and when I try something like x + 50, it is SUPER huge, I have to make it x + 0.5f so that my triangle is small.
You are not setting your projection matrix correctly:
glLoadIdentity();
glOrtho(0, 1280, 720, 0, 0, 1);
glLoadIdentity();
The second glLoadIdentity() completely overwrites whatever matrix was set before, so that your glOrtho() does not have any effect at all (besides consuming some CPU cycles).
I have the following working code(I took it from an example).
It produces a cube with its sides coloured differently.
Is there any way that I can assign colour to every(a group) of pixels on the sides depending on their position or some simulation. Some thing like a colour map of a scalar field on to the surface of the cube... ?
Can someone give me an example of doing so on arbitrary surfaces?
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/freeglut_ext.h>
void init(void)
{
glClearColor(0,0,0,0);
glShadeModel(GL_FLAT);
}
void DrawCube(void)
{
glLoadIdentity();
gluLookAt(0, 10, 10, 0, 1, 0, 0, -2, -35.7);
glBegin(GL_QUADS);
//face in xy plane
glColor3f(0.12, 0.91, 0.02);//this the color with which complete cube is drawn.
glVertex3f(0,0 ,0 );
glVertex3f(5, 0, 0);
glVertex3f(5, 5, 0);
glVertex3f(0, 5, 0);
//face in yz plane
glColor3f(1, 0.23, 0.56);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 5);
glVertex3f(0, 5, 0);
glVertex3f(0, 5, 5);
//face in zx plance
glColor3f(0.45, .81, 1);
glVertex3f(0, 0, 0 );
glVertex3f(0, 0, 5);
glVertex3f(5, 0, 5);
glVertex3f(5, 0, 0);
//|| to xy plane.
glColor3f(0.23, 0.78,0.13);
glVertex3f(0, 0, 5);
glVertex3f(5, 0, 5);
glVertex3f(5, 5, 5);
glVertex3f(0, 5, 5);
//|| to yz plane
glColor3f(0.73, 0.58, 0.58);
glVertex3f(0,0 ,5 );
glVertex3f(5, 0, 5);
glVertex3f(5, 5, 5);
glVertex3f(0, 5, 5);
//|| to zx plane
glVertex3f(0.58, 0, 0.82);
glVertex3f(0, 5, 0 );
glVertex3f(0, 5, 5);
glVertex3f(5, 5, 5);
glVertex3f(5, 5, 0);
glEnd();
glFlush();
}
void reshape(int w,int h){
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 1.5, 20);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv){
glutInit(&argc, argv);//we initizlilze the glut. functions
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(800, 800);
glutCreateWindow(argv[1]);
init();
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
I am practicing the exercises from my textbook but I could not get the outputs that I should.
Here is what I have :
#include <math.h>
#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>
//Initialize OpenGL
void init(void) {
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,300.0,0.0,300.0);
}
void drawLines(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.4,0.2);
glPointSize(3.0);
glBegin(GL_LINES);
glVertex2d(180, 15);
glVertex2d(10, 145);
glEnd();
}
int main(int argc, char**argv) {
glutInit(&argc, argv);
glutInitWindowPosition(10,10);
glutInitWindowSize(500,500);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Example");
init();
glutDisplayFunc(drawLines);
glutMainLoop();
}
When I run this piece of code, I get completely blank white screen.
i'm also not an expert on OpenGL but the problem is that you haven't set a viewport to where your scene should be projected. Your init should look somewhat like this:
glClearColor(0, 0, 0, 0);
glViewport(0, 0, 500, 500);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 500, 0, 500, 1, -1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
You also need to put a glFlush(); after your drawing.
void drawLines(void) {
...
glFlush();
}
Determine and draw the output of OpenGL sub function below:
1.
glColor3f(0.0, 0.0, 0.0, 0.0);
glBegin(Gl_LINES);
glVertex2i (200, 200);
glVertex2i (70, 20);
glVertex2i (120, 150);
glVertex2i (200, 20);
glVertex2i (60, 100);
glEnd();
int p1[]={200,100};
int p2[]={70,20};
int p3[]={120,150};
int p4[]={200,20};
int p5[]={60,100};
glBegin(GL_LINE_STRIP);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p3);
glVertex2iv (p4);
glVertex2iv (p5);
glEnd();
Hi
I am trying to draw a cube of size 5*5*5 with six diffrent face colors. How ever,I can not see all the faces colored diffrently, all I see is a cube, with confusing colors format.
Some faces are clearly visible, while top face , parallel to zx plane is not visible.
Thanks in advance
void init(void)
{
glClearColor(0,0,0,0);
glShadeModel(GL_FLAT);
}
void DrawCube(void)
{
glLoadIdentity();
gluLookAt(10, 10, 10, 0, 0, 0, 0, 1, 0);
glBegin(GL_QUADS);
//face in xy plane
glColor3f(0.82, 0.41, 0.12);//this the color with which complete cube is drawn.
glVertex3f(0,0 ,0 );
glVertex3f(5, 0, 0);
glVertex3f(5, 5, 0);
glVertex3f(0, 5, 0);
//face in yz plane
glColor3f(1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 5);
glVertex3f(0, 5, 0);
glVertex3f(0, 5, 5);
//face in zx plance
glColor3f(0, 1, 0);
glVertex3f(0, 0, 0 );
glVertex3f(0, 0, 5);
glVertex3f(5, 0, 5);
glVertex3f(5, 0, 0);
//|| to xy plane.
glColor3f(0, 0, 1);
glVertex3f(0, 0, 5);
glVertex3f(5, 0, 5);
glVertex3f(5, 5, 5);
glVertex3f(0, 5, 5);
//|| to yz plane
glColor3f(0.73, 0.58, 0.58);
glVertex3f(0,0 ,5 );
glVertex3f(5, 0, 5);
glVertex3f(5, 5, 5);
glVertex3f(0, 5, 5);
//|| to zx plane //this face is not visible. I am not understanding why.
glVertex3f(0.58, 0, 0.82);
glVertex3f(0, 5, 0 );
glVertex3f(0, 5, 5);
glVertex3f(5, 5, 5);
glVertex3f(5, 5, 0);
glEnd();
glFlush();
}
void reshape(int w,int h){
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 1.5, 20);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv){
glutInit(&argc, argv);//we initizlilze the glut. functions
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
//|| to zx plane //this face is not visible. I am not understanding why.
glVertex3f(0.58, 0, 0.82);
You want glColor3f. I'm guessing this is a typo.
Two unrelated things:
Your || to xy plane quad is the same as your || to yz plane quad.
You aren't clearing the color buffer before drawing. You probably want glClear(GL_COLOR_BUFFER_BIT) at the start of DrawCube