The line isn't showing up. What is wrong with the code?
#include<windows.h>
//#ifdef __APPLE__
//#include <GLUT/glut.h>
//#else
#include <GL/glut.h>
//#endif
//#include <stdlib.h>
void init(void){
glClearColor(1.0, 1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0,150.0);
}
void lineSegment(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f); // Red
//glColor3f(0.2, 0.4, 0.2);
glBegin(GL_LINE);
glVertex2i(180,15);
glVertex2i(10,145);
glEnd();
glFlush();
}
int main(int argc, char* argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
//glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,100);
glutInitWindowSize(400,300);
glutCreateWindow("An example OpenGL Program");
init();
glutDisplayFunc(lineSegment);
glutMainLoop();
return 0;
}
You're requesting a GLUT_DOUBLE-buffered window but are failing to call glutSwapBuffers() at the end of your glutDisplayFunc() callback. glFlush() is not sufficient.
GL_LINE is not a valid argument for glBegin(). You're thinking of GL_LINES.
All together:
#include <GL/glut.h>
void init()
{
glClearColor( 1.0, 1.0, 1.0, 0.0 );
glMatrixMode( GL_PROJECTION );
gluOrtho2D( 0.0, 200.0, 0.0, 150.0 );
}
void lineSegment()
{
glClear( GL_COLOR_BUFFER_BIT );
glColor3f( 1.0f, 0.0f, 0.0f ); // Red
glBegin( GL_LINES );
glVertex2i( 180, 15 );
glVertex2i( 10, 145 );
glEnd();
glutSwapBuffers();
}
int main( int argc, char* argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE ); // Enable double buffered mode
glutInitWindowSize( 400, 300 );
glutCreateWindow( "An example OpenGL Program" );
init();
glutDisplayFunc( lineSegment );
glutMainLoop();
return 0;
}
Related
I tried to draw a simple line in OpenGL:
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <freeglut.h>
float startX = -180.0f;
float startZ = -100.0f;
float qtyX = 36;
float qtyZ = 20;
float red = 0.0f, green = 0.0f, blue = 0.0f;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 50.0f, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(50.0f, 50.0f, 50.0f);
glEnd();
//glutWireSphere(20.0f, 20.0f, 20.0f);
glutSwapBuffers();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-64.0, 64.0, -36.0, 36.0, 10, 10000.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1280, 720);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
However, when I run the program, the performance drop significantly then after a few seconds, it stops responding (I have to run task manager and force quit the program). However, when I disable the line and draw the glutWireSphere (or a polygon), the performance is normal.
Is there a problem with my code? Or perhaps GL_LINES is deprecated?
Windows 10, 1070 MaxQ, Visual Studio
I draw patterns which have detailed pixels and they often face with Moire effect. I am not good at shading and I am not sure if this problem will be solved by shaders. I have not found any basic, understandable and complete example of shaders. Most of the tutorial websites start a program from the middle omitting the header file includes!
This is a MWE of my code. Is it possible to mitigate or remove the Moire effect from it?
#include <cmath>
#include <vector>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
const int w=640,h=480;
float cam_angle=0.0f;
void init()
{
GLfloat LightAmbient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat LightDiffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightPosition[] = { 5.0f, 5.0f, -10.0f, 1.0f };
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
glEnable(GL_LIGHT1);
}
void onDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (90, float(w)/float(h), 0.01, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
const double pi=3.1415926;
double cam_x=2.0*cos(pi/4.0)*cos(cam_angle);;
double cam_y=2.0*sin(pi/4.0)*cos(cam_angle);;
double cam_z=2.0*sin(cam_angle);
gluLookAt(cam_x, cam_y, cam_z, 0, 0, 0, 0, 0, 1);
struct Point3D
{
double x, y, z;
unsigned char r, g, b, a=255;
};
for(double r=0.5;r<=1.0;r+=0.03)
{
std::vector<Point3D> points;
for(int i=0;i<1000;i++)
{
double theta=double(i)/1000.0*pi*2.0;
Point3D p;
p.x=r*sin(theta);
p.y=r*cos(theta);
p.z=r;
p.r=128;
p.g=200;
p.b=50;
points.push_back(p);
}
// draw
glPushMatrix();
glColor3ub(255,255,255);
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glVertexPointer(3, GL_DOUBLE, sizeof(Point3D), &points[0].x );
glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point3D), &points[0].r );
// glPointSize( 3.0 );
glLineWidth(2.0);
glDrawArrays( GL_LINE_STRIP, 0, int(points.size()) );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glPopMatrix();
}
glFlush();
glutSwapBuffers();
}
void Timer(int /*value*/)
{
cam_angle+=0.01f;
glutPostRedisplay();
// 100 milliseconds
glutTimerFunc(100, Timer, 0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize (w, h);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowPosition (100, 100);
glutCreateWindow ("my window");
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
init();
glutDisplayFunc(onDisplay);
Timer(0);
glutMainLoop();
return 0;
}
If you use Multisampling, then the result will be significantly improved:
glutSetOption(GLUT_MULTISAMPLE, 8);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
without GLUT_MULTISAMPLE:
with GLUT_MULTISAMPLE:
See also the answer to GLUT + OpenGL multisampling anti aliasing (MSAA)
My code looks like this Modification in this code will be appreciated. I tried, only rectangle appears not the text.
#include <GL/glut.h>
#include<bits/stdc++.h>
using namespace std;
void init2D(float r, float g, float b)
{
glClearColor(r, g, b, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
void RenderToDisplay()
{
int l,lenghOfQuote, i;
char str[80];
strcpy(str,"Have courage and be kind");
cout<<str;
lenghOfQuote = (int)strlen(str);
for (i = 0; i < lenghOfQuote; i++)
{
glColor3f(1.0, 0.0, 0.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, str[i]);
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex3f(150.0f, 200.0f, 0.0f);
glColor3f(0.940, 0.37, 0.47);
glVertex3f(450.0f, 200.0f, 0.0f);
glColor3f(0.940, 0.37, 0.47);
glVertex3f(450.0f, 400.0f, 0.0f);
glColor3f(0.69, 0.27, 0.57);
glVertex3f(150.0f, 400.0f, 0.0f);
glColor3f(0.69, 0.27, 0.57);
glEnd();
RenderToDisplay();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0,0);
glLineWidth(3);
glutCreateWindow("Assignment Q2");
init2D(0.0, 0.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();
}
You could tell me the comments if you need to ask anything else. I used codeblocks to test this program.
Okay after a day I got it, I'm so dumb :')
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
void init2D(float r, float g, float b)
{
glClearColor(r, g, b, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
void rectangle()
{
glBegin(GL_POLYGON);
glColor3f(0.4,0,0.8);
glVertex3f(150.0f, 200.0f, 0.0f);
glColor3f(0.4,0,0.8);
glVertex3f(450.0f, 200.0f, 0.0f);
glColor3f(0.6,0,0.6);
glVertex3f(450.0f, 400.0f, 0.0f);
glColor3f(0.6,0,0.6);
glVertex3f(150.0f, 400.0f, 0.0f);
glEnd();
}
void text()
{
char menu[80];
strcpy(menu,"Have courage and be kind");
int len;
len = strlen(menu);
glColor3f(1,1,1);
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
gluOrtho2D( 0, 600, 0, 600 );
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glRasterPos2i(190, 300);
for ( int i = 0; i < len; ++i )
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, menu[i]);
}
glPopMatrix();
glMatrixMode( GL_PROJECTION );
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
rectangle();
text();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(600, 600);
glutCreateWindow("Assignment 1 Question 2");
init2D(0.0f, 0.0f, 0.0f);
glutDisplayFunc(display);
glutMainLoop();
}
I'm messing around with glutBitmapString from the OpenGlut library. Right now I just want to write a string on the screen to make sure it works, but I cannot get it to appear:
const char* texto = "texto";
glRasterPos2i(100, 120);
glColor3f(0.0f, 0.0f, 1.0f);
glutBitmapString(GLUT_BITMAP_HELVETICA_10, (UCHAR*)texto);
I've also tried with glutBitmapCharacter in a for loop, but with no success. Could someone give me a clear example or tell me what I'm doing wrong?
glRasterPos() positions are transformed by the current projection/modelview matrices. Make sure they're reasonable.
Or use glWindowPos2i() to bypass them.
Example:
#include <GL/freeglut.h>
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( 0, w, 0, h, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
const char* texto = "texto";
glRasterPos2i( 100, 120 );
glColor3f( 0.0f, 0.0f, 1.0f );
glutBitmapString(GLUT_BITMAP_HELVETICA_10, (const unsigned char*)texto );
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 300, 300 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
I want to draw a series of connected lines (GL_LINE_STRIP) in following way.
I had tried to code by my own, but not get desired result, so i come here, help me to find out where i was wrong. here i am giving only my draw() function.
glBegin(GL_LINE_STRIP);
glVertex2f(-4.00, 0.00);
glVertex2f(-3.00, 2.00);
glVertex2f(-2.00, 0.00);
glVertex2f(-1.00, 2.00);
glVertex2f(0.0, 0.00);
glVertex2f(1.00, 2.00);
glVertex2f(2.00, 0.00);
glVertex2f(3.00, 2.00);
glVertex2f(4.00, 0.00);
glEnd();
Workin' perfectly here:
#include <GL/glut.h>
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -6, 6, -6, 6, -1, 1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3ub( 255, 255, 255 );
glBegin(GL_LINE_STRIP);
glVertex2f(-4.00, 0.00);
glVertex2f(-3.00, 2.00);
glVertex2f(-2.00, 0.00);
glVertex2f(-1.00, 2.00);
glVertex2f(0.0, 0.00);
glVertex2f(1.00, 2.00);
glVertex2f(2.00, 0.00);
glVertex2f(3.00, 2.00);
glVertex2f(4.00, 0.00);
glEnd();
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 600, 600 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}