OpenGL Line Drawing - opengl

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

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

How can I display two shapes instead of one by using C++ OpenGL Glut?

I started to use OpenGL / glut for C++ and I am stuck on being able to display two objects in the window rather than one. So, for the first shape it draws a house-like shape with a square and triangle on the top, with anti-aliasing. The second shape, is supposed to be the same, but does not have anti-aliasing. For now, the second shape just has two dots as opposed to the entire shape. When I have both of the withoutAntiAliasing and withAntiAliasing methods inside of the void render() method, only one shows. How exactly can I make it so that both of the methods described can be shown in the window? Code is below:
#include <GL/glut.h>
void init()
{
glClearColor(1.0, 1.0, 1.0, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 250.0, 0.0, 250.0);
}
void withAntiAliasing()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_LINE_LOOP);
glVertex2i(155, 150);
glVertex2i(125, 125);
glVertex2f(108.5, 162);
glVertex2f(136, 185);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2i(100, 155.9);
glVertex2f(145, 192.5);
glVertex2i(115, 185);
glEnd();
glFlush();
}
void withoutAntiAliasing()
{
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_LINE_SMOOTH);
glDisable(GL_BLEND);
glBegin(GL_POINTS);
glColor3f(0, 0, 0);
glVertex2i(170, 170);
glColor3f(0, 0, 0);
glVertex2i(150, 150);
glEnd();
glFlush();
}
void render()
{
glColor3f(0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
withoutAntiAliasing();
withAntiAliasing();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("Hey");
init();
glutDisplayFunc(render);
glutMainLoop();
}
If you want to draw lines, you must use Line primitives instead of Point primitives.
The first mesh is not displayed because you clear the framebuffer before drawing the second mesh.
Move glClear and glFlush in the render function and use the primitive type GL_LINE_LOOP:
void withAntiAliasing()
{
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_LINE_LOOP);
glVertex2i(155, 150);
glVertex2i(125, 125);
glVertex2f(108.5, 162);
glVertex2f(136, 185);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2i(100, 155.9);
glVertex2f(145, 192.5);
glVertex2i(115, 185);
glEnd();
}
void withoutAntiAliasing()
{
glDisable(GL_LINE_SMOOTH);
glDisable(GL_BLEND);
glBegin(GL_LINE_LOOP);
glColor3f(0, 0, 0);
glVertex2i(170, 170);
glColor3f(0, 0, 0);
glVertex2i(150, 150);
glEnd();
}
void render()
{
glColor3f(0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
withoutAntiAliasing();
withAntiAliasing();
glFlush();
}

How can i draw two object in the same window in openGL c++?

I'm trying to draw 2 different objects in Visual Studio using OpenGL.
I can't seem to draw both object at the same time in the same window. I Tried putting both object in the same function, but it only display one object in the window.
#include<Windows.h>
#include<glut.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
void init()
{
glClearColor(0, 0.4, 1, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 800, 0.0, 600);
}
void kapal()
{
//badan
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3ub(148, 111, 70);
glVertex2i(20 , 100);
glVertex2i(160 , 100);
glColor3ub(107, 65, 17);
glVertex2i(140 , 60 );
glVertex2i(40 , 60);
glColor3ub(9, 5, 0);
glEnd();
//tiang
glColor3ub(97, 65, 28);
glLineWidth(5);
glBegin(GL_LINES);
glVertex2i(90 ,100 );
glVertex2i(90 , 160 );
glEnd();
//layar
glColor3ub(215, 215, 215);
glBegin(GL_TRIANGLES);
glVertex2i(90, 160 );
glVertex2i(120 , 130 );
glVertex2i(90 , 130);
glEnd();
glFlush();
}
void mobil()
{
//bawah
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3ub(148, 111, 70);
glVertex2i(100, 170);
glVertex2i(100, 230);
glVertex2i(450, 230);
glVertex2i(450, 170);
glEnd();
//atas
glBegin(GL_POLYGON);
glColor3ub(148, 111, 70);
glVertex2i(150, 230);
glVertex2i(200, 270);
glVertex2i(400, 270);
glVertex2i(450, 230);
glEnd();
glFlush();
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
kapal();
mobil();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 600);
glutCreateWindow("Kapal APi ");
init();
glutDisplayFunc(display);
glutMainLoop();
}
As you can see void kapal() is the first object and void mobil() is the second.
This the result that i got:
Is there anyway to fix this so i can display both objects in the same windows?
The issue is that you call glClear(GL_COLOR_BUFFER_BIT); before drawing an object.
Clear the framebuffer before drawing anything, but not before drawing a specific object.
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // <-- this is OK
kapal();
mobil();
glutSwapBuffers();
}
void kapal()
{
//badan
// glClear(GL_COLOR_BUFFER_BIT); <--- DELETE
// [...]
}
void mobil()
{
//bawah
// glClear(GL_COLOR_BUFFER_BIT); <--- DELETE
// [...]
}

need help moving a camera in OpenGL

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;
}

OpenGL with GLUT. Not Drawing?

#include <GL/gl.h>
#include <GL/glut.h>
void display();
void init();
int main(int argc, char* argv[])
{
init();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(320, 240);
glutCreateWindow("Main Window");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
glDisable(GL_DEPTH_TEST);
}
void display()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_QUADS);
glColor3i(255,255,255);
glVertex2f(10, 10);
glVertex2f(100, 10);
glVertex2f(100, 100);
glVertex2f(10, 100);
glEnd();
glutSwapBuffers();
}
Theoretically, this code should draw a white rectangle. But all I see is a black empty screen. What's wrong?
Here is my working example with the changes I made noted by comments:
#include <gl/glut.h>
#include <gl/gl.h>
#define WINDOW_WIDTH 320
#define WINDOW_HEIGHT 240
void display();
void init();
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Main Window");
init(); // changed the init function to come directly before display function
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
glClearColor(0, 0, 0, 0); // moved this line to be in the init function
glDisable(GL_DEPTH_TEST);
// next four lines are new
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, 0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_QUADS);
glColor3ub(255,255,255); // changed glColor3i to glColor3ub (see below)
glVertex2f(10, 10);
glVertex2f(100, 10);
glVertex2f(100, 100);
glVertex2f(10, 100);
glEnd();
glFlush(); // added this line
//glutSwapBuffers(); // removed this line
}
glColor3ub is the function you want to use if you want to provide colors in the 0-255 range.
Hope this helps.