Point doesn't show in OpenGL - 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);

Related

Visual Studio Express 2017 Output not displaying for stroke text function

I've been trying to run this program in visual studio express 2017. Using opengl.
I found the rendering code and stroke code in a pdf and was trying it out but first it showed many errors, once taken care of I compiled the program. Although the run was without any errors the output screen remains blank.
#include "stdafx.h"
#include <windows.h>
#include <gl/GL.h>
#include <glut.h>
#include <gl/GLU.h>
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLineWidth(6.0);
glLoadIdentity();
gluOrtho2D(0.0, 700, 0.0, 700);
}
void drawStrokeText(const char *string, int x, int y, int z)
{
const char *c;
glPushMatrix();
glTranslatef(x, y + 8, z);
glScalef(0.09f, -0.08f, z);
for (c = string; *c != '\0'; c++)
{
glutStrokeCharacter(GLUT_STROKE_ROMAN, *c);
}
glPopMatrix();
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3ub(255, 50, 255);
drawStrokeText("Hello", 300, 400, 0);
glutSwapBuffers();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
render();
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(700, 700);
glutInitWindowPosition(100, 150);
glutCreateWindow("My First Program");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}
Matrix mode is switched to GL_PROJECTION in myInit but never switched back. Therefore the glLoadIdentity() instruction in render will override the projection matrix. You have to switch the matrix mode to GL_MODELVIEW before glLoadIdentity():
void render()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); // <--
glLoadIdentity();
glColor3ub(255, 50, 255);
drawStrokeText("Hello", 300, 400, 0);
glutSwapBuffers();
}

I need to draw a point using OpenGL and GLUT, But I just get a blank black screen

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.

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();

OpenGL Line Drawing

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();