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();
}
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
#include <stdio.h> // this library is for standard input and output
#include "glut.h" // this library is for glut the OpenGL Utility Toolkit
#include <math.h>
float squareX = 0.0f;
float squareY = 200.0f;
static int flag = 1;
void drawShape(void) {
float width = 58.0f;
float height = 40.0f;
glTranslatef(squareX, squareY, 0);
// test
// glScalef(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0, 0);
glVertex2f(width, 0);
glVertex2f(width, height);
glVertex2f(0, height);
glVertex2f(0, 0);
glEnd();
}
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
// called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, (float)w, 0.0f, (float)h, -1.0f, 1.0f);
}
int state = 1;
void update(int value) {
if (state == 1) { // 1 : move right
squareX += 1.0f;
if (squareX > 400.0) {
state = 0;
}
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawShape();
glutSwapBuffers();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("Moving Square");
initRendering();
glutDisplayFunc(display);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);
glutMainLoop();
return(0);
}
I want to make the square get bigger while it is moving to the right. See the second GIF below. I know that I need glScalef to make the square bigger but I don't know how to make it bigger while it is moving.
Code preview:
I need it to do something similar to this (sorry about the quality, I created the GIF myself):
Use glScale to scale the rectangle dependent on the X position (squareX):
float rectScale = 1.0f + (squareX / 400.0f);
glScalef(rectScale, rectScale, 1.0f);
Note squareX is in range [0.0, 400.0], so 1.0f + (squareX / 400.0f) is in range [1.0, 2.0].
First the scaling has to be applied to the rectangle. This means it has to be the last operation, which is applied to the model view matrix, before the rectangle is drawn. the final function drawShape may look like this:
void drawShape(void) {
float width = 58.0f;
float height = 40.0f;
glTranslatef(squareX, squareY, 0);
float rectScale = 1.0f + (squareX / 400.0f);
glScalef(rectScale, rectScale, 1.0f);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0, 0);
glVertex2f(width, 0);
glVertex2f(width, height);
glVertex2f(0, height);
glVertex2f(0, 0);
glEnd();
}
Preview:
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)
I am not using glutMainLoop() in my program but I am using glutMainLoopEvent() in a simple while loop. This is fine but the reshape callback is never called. I register my reshape event in the main(). Here's my source code:
#include <iostream>
#include "GL/freeglut.h"
float angle = 0.0f;
void ResizeEvent(int w, int h)
{
std::cout << "Resizing... << w << " " << h << std::Endl;
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLdouble)w / h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void InitializeGL()
{
glClearColor(0.0, 0.0, 0.0, 1.0f);
glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 640.0 / 480.0, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Update()
{
angle += 0.05f;
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -3.0f);
glRotatef(angle, 0.5f, 1.0f, 0.75f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutCreateWindow("Learning freeglut");
InitializeGL();
glutReshapeFunc(ResizeEvent);
while(true)
{
Update();
Display();
glutMainLoopEvent();
}
return 0;
}
Thanks in advance!
Edit: Anyone?
glutReshapeFunc requires glutDisplayFunc to be set to something. This can even be an empty function as long as you don't need the window to update while resizing.
Possible solution
...
glutReshapeFunc(ResizeEvent);
glutDisplayFunc(Display);
while(true)
{
Update();
Display();
glutMainLoopEvent();
}
...
I'm trying to implement a pure stationary spotlight into my simple OpenGL scene, which doesn't move with the camera. It should keep highlighting the center of the scene (coords of 0,0,0), where I put the teapot.
Here is a simplified code:
#include <windows.h>
#define GLUT_DISABLE_ATEXIT_HACK
#include <gl/glut.h>
#include <gl/glu.h>
#include <gl/gl.h>
#include <math.h>
#include <iostream>
const int windowWidth = 640;
const int windowHeight = 480;
float alpha=0.0, alphaDelta=0.0, ratio, beta=0.0, betaDelta=0.0;
float x=0.0, y=1.75, z=5.0;
float lx=0.0, ly=0.0, lz=-1.0;
int moveDelta = 0;
float lastMouseX=windowWidth*0.5, lastMouseY=windowHeight*0.5;
void reshape(int w, int h){
//...
}
// handles angle changes
void orientMe(float horizontalAngle, float verticalAngle) {
lx = sin(horizontalAngle);
if(beta > -1.5 && beta < 1.5)
ly = sin(verticalAngle);
lz = -cos(horizontalAngle);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
0.0, 1.0, 0.0);
}
// handles x,y,z coords changes
void flatMovement(int i) {
x = x + i*(lx)*0.1;
z = z + i*(lz)*0.1;
glLoadIdentity();
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
0.0, 1.0, 0.0);
}
void display() {
// orient observer
if(moveDelta)
flatMovement(moveDelta);
if(alphaDelta || betaDelta) {
alpha += alphaDelta;
alphaDelta = 0;
beta += betaDelta;
betaDelta = 0;
orientMe(alpha, beta);
}
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_QUADS);
glNormal3d(0,1,0);
glVertex3f(-100.0, 0.0, -100.0);
glVertex3f(-100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, -100.0);
glEnd();
glColor3f(1,0,0);
glutSolidTeapot(1);
glutSwapBuffers();
}
void pressSpecialKey(int key, int x, int y) {
//...
}
void releaseSpecialKey(int key, int x, int y) {
//...
}
static void idle(void)
{
glutPostRedisplay();
}
void mouseMove(int x, int y) {
//...
}
void setupLights() {
GLfloat spotDirection[] = {0.0f, -1.0f, 0.0f, 1.0f};
GLfloat spotPosition[] = {0.0f, 4.0f, 0.0f, 1.0f};
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 40);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection);
glLightfv(GL_LIGHT0, GL_POSITION, spotPosition);
glEnable(GL_LIGHT0);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(windowWidth,windowHeight);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Spotlight");
glutSpecialFunc(pressSpecialKey);
glutSpecialUpFunc(releaseSpecialKey);
glutPassiveMotionFunc(mouseMove);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
setupLights();
glutMainLoop();
return EXIT_SUCCESS;
}
Unfortunately, the spotlight seems to follow camera changes and I have no idea what's wrong with the code above.
EDIT
Thanks to what #genpfault posted, I've alreadysorted things out.
The answer was to leave only the glEnable(GL_LIGHT0) in the main() function, and move the rest of the code responsible for lighting to the very end of the display() function (right before calling glutSwapBuffers().
So the final, simplified code looks as follows:
#include <windows.h>
#define GLUT_DISABLE_ATEXIT_HACK
#include <gl/glut.h>
#include <gl/glu.h>
#include <gl/gl.h>
#include <math.h>
#include <iostream>
const int windowWidth = 640;
const int windowHeight = 480;
float alpha=0.0, alphaDelta=0.0, ratio, beta=0.0, betaDelta=0.0;
float x=0.0, y=1.75, z=5.0;
float lx=0.0, ly=0.0, lz=-1.0;
int moveDelta = 0;
float lastMouseX=windowWidth*0.5, lastMouseY=windowHeight*0.5;
void reshape(int w, int h){
//...
}
// handles angle changes
void orientMe(float horizontalAngle, float verticalAngle) {
//...
}
// handles x,y,z coords changes
void flatMovement(int i) {
//...
}
void setupLights() {
//THE CONTENTS SLIGHTLY CHANGED HERE
GLfloat spotPosition[] = {2.0f, 4.0f, 0.0f, 1.0f};
GLfloat spotDirection[] = {0.0f, -1.0f, 0.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, spotPos
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 40);ition);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection);
}
void display() {
// orient observer
if(moveDelta)
flatMovement(moveDelta);
if(alphaDelta || betaDelta) {
alpha += alphaDelta;
alphaDelta = 0;
beta += betaDelta;
betaDelta = 0;
orientMe(alpha, beta);
}
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_QUADS);
glNormal3d(0,1,0);
glVertex3f(-100.0, 0.0, -100.0);
glVertex3f(-100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, -100.0);
glEnd();
glColor3f(1,0,0);
glutSolidTeapot(1);
setupLights(); // PUT THE LIGHTING SETUP AT THE END OF DISPLAY()
glutSwapBuffers();
}
void pressSpecialKey(int key, int x, int y) {
//...
}
void releaseSpecialKey(int key, int x, int y) {
//...
}
static void idle(void)
{
glutPostRedisplay();
}
void mouseMove(int x, int y) {
//...
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(windowWidth,windowHeight);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Spotlight");
glutSpecialFunc(pressSpecialKey);
glutSpecialUpFunc(releaseSpecialKey);
glutPassiveMotionFunc(mouseMove);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0); // IN THE MAIN, LEAVE ONLY THE ENABLING CALL
glutMainLoop();
return EXIT_SUCCESS;
}
Set your light position after you set your camera transform:
void display()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// orient observer
if(moveDelta)
flatMovement(moveDelta);
if(alphaDelta || betaDelta)
{
alpha += alphaDelta;
alphaDelta = 0;
beta += betaDelta;
betaDelta = 0;
orientMe(alpha, beta);
}
setupLights();
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_QUADS);
glNormal3d(0,1,0);
glVertex3f(-100.0, 0.0, -100.0);
glVertex3f(-100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, -100.0);
glEnd();
glColor3f(1,0,0);
glutSolidTeapot(1);
glutSwapBuffers();
}
See here. Your existing code follows the "Moving the Light Source Together with Your Viewpoint" call sequence.