I have made a c++ code in openGL to draw seven vertices. My sample code is as follows:
#include <GL/glut.h>
void init2D(float r, float g, float b)
{
glClearColor(r, g, b, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
glPointSize(4.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
//draw two points
glBegin(GL_POINTS);
glVertex2i(30, 30);
glVertex2i(47, 76);
glVertex2i(76, 150);
glVertex2i(130, 240);
glVertex2i(300, 200);
glVertex2i(250, 50);
glVertex2i(60, 20);
glEnd();
glFlush();
}
void main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("points and lines");
init2D(0.0, 0.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();
}
But when I run the code, I am only getting 4 of them. Is there anything wrong in the code? Is there any way of displaying all the points?
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
You are setting up your projection such that (0,0) maps to the bottom left corner, and (200, 150) to the top right corner, hence
glVertex2i(130, 240);
glVertex2i(300, 200);
glVertex2i(250, 50);
are all outside of the view volume.
Related
I have written this code for displaying the line segment but I am not able to understand why the line is not getting displayed. Can someone please help?
#include<GL/glut.h>
#include<iostream>
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void line_segment()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE);
glVertex2i(180, 15);
glVertex2i(10, 145);
glEnd();
glFlush();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
// optional
glutInitWindowSize(400, 300);
glutInitWindowPosition(50, 100);
// Till here
glutCreateWindow("Window.....");
init();
glutDisplayFunc(line_segment);
// without infinite loop window onl displayed for a very short time
glutMainLoop();
}
OUTPUT
GL_LINE is not a valid line primitive type. GL_LINE is a polygon mode (see glPolygonMode). The primitive type you want to use is GL_LINES:
glBegin(GL_LINE);
glBegin(GL_LINES);
I am trying to print a point using OpenGL and GLUT, but I just get blank screen when I run the following code. Any help will be appreciated.
Thank You.
#include<GL/glut.h>
void display() {
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex2f(0.0, 0.0);
glEnd();
glFlush();
}
void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutCreateWindow("example");
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutDisplayFunc(display);
glutMainLoop();
}
You need to call glPointSize before glBegin. Example:
void display() {
glColor3f(1.0, 0.0, 0.0);
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex2f(0.0f, 0.0f);
glEnd();
glFlush();
}
If the point size is going to be the same each frame then you can just call glPointSize once on initialisation.
I am developing a project and after I added menu for changing color,the object or shape in window changes color but keep moving other way or out of range every time an item in menu selected such as color. I also tried changing GLUT_DOUBLE to GLUT_SINGLE but still no luck.
#include "stdafx.h"
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#define RED 1
#define GREEN 2
#define BLUE 3
#define WHITE 4 //white colour for sphere
float red =1.0,green =1.0,blue=0.0,white = 0.0;
void init(void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void){
// Sphere
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(red,green,blue);
glPushMatrix();
glTranslatef (-3.5, -1.5, 0.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (3.0, 3.0, 0.0);
glutSolidSphere(0.4,40,40);
glPopMatrix();
//Cone
glColor3f(0.0,0.0,0.0);
glTranslatef (0.95,-0.2, 0.0);
glPushMatrix();
glRotated(300,1.0,4.0,1.0);
glutSolidCone(0.6, 0.9, 30, 30);
glPopMatrix();
//Sphere(eye)
glColor3f(0.0,0.0,0.0);
glTranslatef (-0.75,0.8, 0.0);
glPushMatrix();
glScalef(0.6,0.6,0.0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
glFlush();
}
void reshape (int w, int h){
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
}
void mouse(int option){
switch(option){
case RED:
red = 1.0,green = 0.0,blue = 0.0;
break;
case GREEN:
red = 0.0,green = 1.0,blue = 0.0;
break;
case BLUE:
red = 0.0,green = 0.0,blue = 1.0;
break;
case WHITE:
red = 1.0,green = 1.0,blue = 1.0;
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (700, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Testing");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
int sphere = glutCreateMenu(mouse); //change colour of pacman
glutAddMenuEntry("Red",RED);
glutAddMenuEntry("Green",GREEN);
glutAddMenuEntry("Blue",BLUE);
glutAddMenuEntry("White",WHITE);
glutCreateMenu(mouse);
glutAddSubMenu("Pacman",sphere);
glutAttachMenu(GLUT_RIGHT_BUTTON);
init ();
glutMainLoop();
return 0;
}
You have to do one glPopMatrix for each glPushMatrix. Ther is missing 1 glPopMatrix in the function display:
void display(void){
// Sphere
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(red,green,blue);
glPushMatrix();
glTranslatef (-3.5, -1.5, 0.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (3.0, 3.0, 0.0);
glutSolidSphere(0.4,40,40);
glPopMatrix();
//Cone
glColor3f(0.0,0.0,0.0);
glTranslatef (0.95,-0.2, 0.0);
glPushMatrix();
glRotated(300,1.0,4.0,1.0);
glutSolidCone(0.6, 0.9, 30, 30);
glPopMatrix();
//Sphere(eye)
glColor3f(0.0,0.0,0.0);
glTranslatef (-0.75,0.8, 0.0);
glPushMatrix();
glScalef(0.6,0.6,0.0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
glPopMatrix(); // <--------------------- this is missing
glFlush();
}
Since the pop is missing, the translation of the first rendering is kept on the top of the matrix stack. When the rendering is done a 2nd time with the new color, translation is applied again. This causes a progressive change of position.
I wanna create a program with OpenGL in my PC. I use this code
#include "stdafx.h"
#include <GL/glut.h>
bool bFullsreen = false;
int nWindowID;
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,1.0,1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
// Don't wait start processing buffered OpenGL routines)
//glFlush();
glutSwapBuffers();
}
void init(void)
{
//select clearing (background) color
glClearColor(0.0, 0.0, 0.0, 0.0);
//initialize viewing values
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void keyboardFunc(unsigned char key, int x, int y) {
switch (key) {
case 'f':
bFullsreen = !bFullsreen;
if (bFullsreen)
glutFullScreen();
else {
glutSetWindow(nWindowID);
glutPositionWindow(100, 100);
glutReshapeWindow(640, 480);
}
break;
}
}
void idleFunc(void) {
glutPostRedisplay();
}
int _tmain(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(250,250);
glutInitWindowPosition(100,100);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
nWindowID = glutCreateWindow("Hello World");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboardFunc);
glutIdleFunc(idleFunc);
glutMainLoop();
return 0;
}
when i run it in my PC it coesn't show anything and only show background. like this picture:
But when I switch to FullScreen mode, it shows correctly. I searched about it and I found changing GLUT_Single to GLUT_DOUBLE and glFlush() to glutSwapBuffers() but it doesn't work for me.
I have windows 10 with NVIDIA GForce 7300 LE. when I run my program in other PC it runs correctly. so
What is my problem? Is it about my graphic card or my code is incorrect? and
How can I fixed it?
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();