I'm trying to write fonts on my SDL's window and glutBitmapCharacter.
But I'm failling to compile it.
My compilator error message is :
undefine reference to « glutBitmap9By15 »
undefine reference to « glutBitmapCharacter »
This code :
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cstdlib>
#include <cmath>
#include <string>
#include <iostream>
int main()
{
SDL_Init(SDL_INIT_VIDEO);
atexit(SDL_Quit);
SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,WIDTH,0,HEIGHT);
SDL_EnableKeyRepeat(100,100);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(0.0, 1.0, 0.0); // Green
glRasterPos2i(10, 10);
std::string s = "Respect mah authoritah!";
void * font = GLUT_BITMAP_9_BY_15;
for (std::string::iterator i = s.begin(); i != s.end(); ++i)
{
char c = *i;
glutBitmapCharacter(font, c);
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
Does someone know what I have to do to make it work ?
Thanks
Related
When I execute the code I get a hot air balloon formed of three elements. My issue is that when I move the objects from the keyboard, the objects loose color, and become more like wire than solid.
From what I discovered until now, my trouble comes from this function call:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
But I need it to make the ropes...
/*
This program shows a hot air balloon rotating around its own axe to the left and to the right
*/
#include "glos.h"
#include<math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
void myinit(void);
void CALLBACK display(void);
void CALLBACK myReshape(GLsizei w, GLsizei h);
void CALLBACK rotateRight(void);
void CALLBACK rotateLeft(void);
static GLfloat x = 0;
static GLfloat y = 0;
static GLfloat z = 0;
static GLfloat alfa = 0;
double PI = 3.14159265;
void myinit (void) {
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
}
void CALLBACK rotateLeft(void) { y -= 5; }
void CALLBACK rotateRight(void) { y += 5; }
void CALLBACK display (void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(x, 1, 0, 0);
glRotatef(y, 0, 1, 0);
glTranslatef(0, -80, 0);
//cube = basket
glColor3f(1, 1, 0);
auxSolidCube(50);
//full sphere = baloon
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(0,200,0);
glRotatef(-90, 1, 0, 0);
auxSolidSphere(130.0);
glPopMatrix();
//polygon cylinder = ropes
glPushMatrix();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_QUAD_STRIP);
glColor3f(0.0, 1.0, 1.0);
for (alfa = 0; alfa <= 360; alfa+=30) {
glVertex3f(65 * sin((PI * alfa) / 180), 100, 65 * cos((PI * alfa) / 180));//top of the cylinder
glVertex3f(15 * sin((PI * alfa) / 180),0, 15 * cos((PI * alfa) / 180));//base of the cylinder
}
glEnd();
glPopMatrix();
glPopMatrix();
glFlush();
}
void CALLBACK myReshape(GLsizei w, GLsizei h)
{
if (!h) return;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-400,400, -400 *(GLfloat)h / (GLfloat)w, +400.0*(GLfloat)h/(GLfloat)w, -1000.0, 1000.0);
else
glOrtho (-400*(GLfloat)w / (GLfloat)h, 400.0*(GLfloat)w/(GLfloat)h, -400, 400.0, -1000.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
auxInitDisplayMode (AUX_SINGLE | AUX_RGB | AUX_DEPTH);
auxInitPosition (100, 0, 600, 400);
auxInitWindow ("Hot air balloon");
myinit ();
auxKeyFunc(AUX_RIGHT, rotateRight);
auxKeyFunc(AUX_LEFT, rotateLeft);
auxReshapeFunc (myReshape);
auxMainLoop(display);
return(0);
}
OpenGL is a state engine. Once a state has been set, it is retained until it is changed again, even beyond frames. Therefore, you need to set the polygon mode GL_FILL before rendering the solid geometry:
void CALLBACK display (void)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// render solid geometry
// [...]
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render wireframe geometry
// [...]
}
When I update VTK to version 8.2.0, I encounter a specical bug that VTK will blend color with 'vtkAssembly' in wierd way comparing to behavior by vtkActor.
While blend color with vtkAssembly, it seems that VTK just add color by (r,g,b), thus white color appears offen.
In the following example, left is drawn by vtkActor, and right is added to scene by vtkAssembly
I reproduce bug with following simple code :
#include "vtkActor.h"
#include "vtkNew.h"
#include "vtkCamera.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include <vtkGenericOpenGLRenderWindow.h>
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include <vtkLookupTable.h>
#include <vtkProperty.h>
#include <vtkCellData.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkAssembly.h>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
void createScene(vtkActor* actor)
{
int i;
//
static float x[10][3]={{ 0, 0, 1}, { 0, 0,-1}
, { 0, 1, 0}, { 1, 1, 0}
, { 1, 0, 0}, { 1,-1, 0}
, { 0,-1, 0}, {-1,-1, 0}
, {-1, 0, 0}, {-1, 1, 0}};
//
static vtkIdType pts[8][4]={{0,1,2}, {0,1,3}, {0,1,4}, {0,1,5}
, {0,1,6}, {0,1,7}, {0,1,8}, {0,1,9}};
//
vtkPolyData *cube = vtkPolyData::New();
vtkPoints *points = vtkPoints::New();
vtkCellArray *polys = vtkCellArray::New();
//
vtkFloatArray *scalars = vtkFloatArray::New();
//
for(i=0;i<10;i++)points->InsertPoint(i,x[i]);
//
for(i=0;i<8;i++)polys->InsertNextCell(4,pts[i]);
//
for(i=0;i<8;i++)scalars->InsertTuple1(i,i);
//
cube->SetPoints(points);
//
cube->SetPolys(polys);
//
cube->GetCellData()->SetScalars(scalars);
points->Delete();
polys->Delete();
scalars->Delete();
//
vtkLookupTable *pColorTable=vtkLookupTable::New();
//
pColorTable->SetNumberOfColors(6);
pColorTable->SetTableValue(0, 1.0, 0.0, 0.0, 1.0);
pColorTable->SetTableValue(1, 0.0, 1.0, 0.0, 1.0);
pColorTable->SetTableValue(2, 1.0, 1.0, 0.0, 1.0);
pColorTable->SetTableValue(3, 0.0, 0.0, 1.0, 1.0);
pColorTable->SetTableValue(4, 1.0, 0.0, 1.0, 1.0);
pColorTable->SetTableValue(5, 0.0, 1.0, 1.0, 1.0);
pColorTable->Build();
//
vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
cubeMapper->SetScalarModeToUseCellData();
cubeMapper->SetInputData(cube);
cubeMapper->SetScalarRange(0,7);
cubeMapper->SetLookupTable(pColorTable);
actor->SetMapper(cubeMapper);
actor->GetProperty()->SetOpacity(0.5);
}
int main(int argc, char** argv)
{
vtkCamera *camera = vtkCamera::New();
camera->SetPosition(1,1,1);
camera->SetFocalPoint(0,0,0);
vtkRenderer *renderer = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(renderer);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
iren->SetInteractorStyle(vtkInteractorStyleTrackballCamera::New());
vtkNew<vtkActor> actor1, actor2;
createScene(actor1);
renderer->AddActor(actor1);
createScene(actor2);
vtkNew<vtkAssembly> assembly;
assembly->AddPart(actor2);
assembly->SetPosition(3,0,0);
renderer->AddActor(assembly);
renderer->SetActiveCamera(camera);
renderer->ResetCamera();
renderer->SetBackground(0,0,0);
renWin->SetSize(600,600);
renWin->Render();
iren->Start();
return 0;
}
See answer in the vtk discourse thread here.
I am using SDL2.00 with OpenGL to create a program. The program isn't supposed to do anything at the moment it's just a test bed. However I ran into a problem. When I create a window using SDL_CreateWindow, the window goes into a busy state and stops responding. The program flow isn't really affected by this however the window itself just won't work. All it does is show a white blank window and accept no input, can't resize can't move and can't quit. I will attach the code but I doubt it is code related since I already made a few programs using SDL and they seem to work just fine.
Using VS2013 SDL2.00 OpenGL
=========main========
#include "stdafx.h"
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 640.0 / 480.0, 1.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 2.0, -5.0);
glVertex3f(-2.0, -2.0, -5.0);
glVertex3f(2.0, -2.0, -5.0);
glEnd();
}
int main(int argc, char* argv[]){
SDL_Init(SDL_INIT_VIDEO);
SDL_Window * window;
window = SDL_CreateWindow("OpenGLTest", 300, 300, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
init();
while (true){
display();
SDL_GL_SwapWindow(window);
}
return 0;
}
=========stdafx======
#pragma once
#include <iostream>
#include <SDL.h>
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#define PI 3.14159265
using namespace std;
You forgot to process all the events, so the SDL window is just waiting and waiting and waiting, thereby "not responding" - Simply adding that should fix the problem!
while (true) {
SDL_PumpEvents();
display();
SDL_GL_SwapWindow(window);
}
You could also pull all the events manually with a loop calling SDL_PollEvent(SDL_Event *event)
SDL_Event event;
while (SDL_PollEvent(&event)) {
// Process the event...
}
Wiki
SDL_PumpEvents() - http://wiki.libsdl.org/SDL_PumpEvents
SDL_PollEvent() - http://wiki.libsdl.org/SDL_PollEvent
here is my code
#include "stdafx.h"
#include <GL/glut.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include <math.h>
#define pi 3.14;
float angle=3;
void reshape(int w, int h)
{
if(h==0) h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,0,h,-1,1);
}
void display(void)
{
glDepthMask(GL_TRUE);
glClear(GL_COLOR_BUFFER_BIT);
glRotatef(angle++,0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex2f(10,10);
glVertex2f(30,20);
glVertex2f(30,30);
glVertex2f(10,30);
glEnd();
glRotatef(angle++,0,1.0,1.0);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(300,300);
int segments=20;
GLfloat angle=0;
for(int i=0;i<=segments; i++)
{
angle=i*2*pi; angle=angle / (segments);
glVertex2f(300+cos(angle)*30,300+sin(angle)*30);
}
glEnd();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc , argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutCreateWindow("first");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutMainLoop();
return(0);
}
here the problems are
cannot see the rotation correctly just blinking the rectangle. Don't know is it rotating or not.
2.the circle is not rotating even there is code for rotation. why?
3.what change I made if I want only rectangle or circle is rotated?
1. cannot see the rotation correctly just blinking the rectangle.
You're probably running waaaaay too fast. Use a timer callback to issue glutPostRedisplay()s every 16ms or so.
2. the circle is not rotating even there is code for rotation. why?
glRotatef(angle++,0,1.0,1.0);
^^^
You're rotating outside the X/Y plane and exceeding the depth limits of your glOrtho() call resulting in clipped fragment. Bump your nearVal/farVal to accommodate:
glOrtho(0,w,0,h,-1000,1000);
3. what change I made if I want only rectangle or circle is rotated?
Remove one of the glRotatef()s.
All together:
#include <GL/glut.h>
#include <cmath>
using namespace std;
const double pi = 3.14159;
float angle=3;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho(-w,w,-h,h,-1000,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDepthMask(GL_TRUE);
//glRotatef(angle++,0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex2f(10,10);
glVertex2f(30,20);
glVertex2f(30,30);
glVertex2f(10,30);
glEnd();
glRotatef(angle++,0,1.0,1.0);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(300,300);
const int segments=20;
for(int i=0;i<=segments; i++)
{
const GLfloat angle = ( i*2*pi ) / (float)segments;
glVertex2f(300+cos(angle)*30,300+sin(angle)*30);
}
glEnd();
glutSwapBuffers();
}
void timer( int value )
{
glutPostRedisplay();
glutTimerFunc( 16, timer, 0 );
}
int main(int argc, char** argv)
{
glutInit(&argc , argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutCreateWindow("first");
glutDisplayFunc(display);
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return(0);
}
can someone please explain me why is the camera in my code moving on every onIdle callback? I am not changing any parameters so it seems to me that it should display the same all the time. Still cannot figure it out by myself, thanks for help
#include <iostream>
#include <math.h>
#include "gl/glut.h"
#include <windows.h>
using namespace std;
void Display(void){
glMatrixMode(GL_PROJECTION);
glFrustum(-0.5, 0.5, -0.5, 0.5, 0.8, 2);
gluLookAt (0,0,1, 0,0,0, 0,1,1);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glutWireCube(0.2);
glFlush();
}
void onIdle(void){
Sleep(1000);
glutPostRedisplay();
}
int main(void){
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA);
glEnable(GL_DEPTH_TEST);
glutCreateWindow("camera");
glutDisplayFunc(Display);
glutIdleFunc(onIdle);
glutMainLoop();
return 0;
}
That is because you don't reset the matrices, after each render or at the beginning of each render.
Basically, you need to call glLoadIdentity(); that will reset the current selected matrix, but setting the matrix's values to the default values (Matrix Identity).