For a game I'm working on, I'm hoping to use OpenGL for a lot of the graphics and SDL_TTF for the text. I can get both to work but not at the same time. Here's my code (based off Lazy Foo):
#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include "GL/gl.h"
const bool useOpenGl = true;
//The surfaces
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//The font that's going to be used
TTF_Font *font = NULL;
//The color of the font
SDL_Color textColor = {255, 255, 255};
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface(source, clip, destination, &offset);
}
bool init()
{
SDL_Init (SDL_INIT_EVERYTHING);
if (useOpenGl)
{
screen = SDL_SetVideoMode (1280, 720, 32, SDL_SWSURFACE | SDL_OPENGL); //With SDL_OPENGL flag only opengl is sceen, without only text is
} else {
screen = SDL_SetVideoMode (1280, 720, 32, SDL_SWSURFACE);
}
TTF_Init();
SDL_WM_SetCaption ("TTF Not Working With OpenGL", NULL);
if (useOpenGl)
{
glClearColor(1.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, screen->w, screen->h, 1.0, -1.0, 1.0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
return true;
}
bool load_files()
{
font = TTF_OpenFont ("arial.ttf", 28);
return true;
}
void clean_up()
{
SDL_FreeSurface (message);
TTF_CloseFont (font);
TTF_Quit();
SDL_Quit();
}
int main(int argc, char* args[])
{
//Quit flag
bool quit = false;
init();
load_files();
if (useOpenGl)
{
glClear(GL_COLOR_BUFFER_BIT); //clearing the screen
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(1280, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(1280, 720);
glColor4f(0.5, 0.5, 1.0, 0.1);
glVertex2f(0, 720);
glEnd();
glPopMatrix();
glFlush();
}
//Render the text
message = TTF_RenderText_Solid (font, "The quick brown fox jumps over the lazy dog", textColor);
//Apply the images to the screen
apply_surface (0, 150, message, screen);
//I'm guessing this is where the problem is coming from
SDL_GL_SwapBuffers();
SDL_Flip (screen);
while (quit == false)
{
while (SDL_PollEvent (&event))
{
if (event.type == SDL_QUIT)
{
quit = true;
}
}
}
clean_up();
return 0;
}
If the variable useOpenGl is set to false the program will only use SDL_TTF, if it's set to true it will use both SDL_TTF and OpenGL.
From playing around with it the problem seems to be with whether or not I use the "SDL_OPENGL" flag when creating the window.
SDL_TTF uses software rendering and is not compatible with OpenGL mode.
You might have to look for another library such as FTGL or freetype-gl.
Related
I am trying this sample open gl program on my codelite ide.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>
GLenum doubleBuffer;
GLint thing1, thing2;
static void Init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearAccum(0.0, 0.0, 0.0, 0.0);
thing1 = glGenLists(1);
glNewList(thing1, GL_COMPILE);
glColor3f(1.0, 0.0, 0.0);
glRectf(-1.0, -1.0, 1.0, 0.0);
glEndList();
thing2 = glGenLists(1);
glNewList(thing2, GL_COMPILE);
glColor3f(0.0, 1.0, 0.0);
glRectf(0.0, -1.0, 1.0, 1.0);
glEndList();
}
static void Reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
static void Key(unsigned char key, int x, int y)
{
switch (key) {
case '1':
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glutPostRedisplay();
break;
case '2':
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glutPostRedisplay();
break;
case 27:
exit(0);
}
}
static void Draw(void)
{
glPushMatrix();
glScalef(0.8, 0.8, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glCallList(thing1);
glAccum(GL_LOAD, 0.5);
glClear(GL_COLOR_BUFFER_BIT);
glCallList(thing2);
glAccum(GL_ACCUM, 0.5);
glAccum(GL_RETURN, 1.0);
glPopMatrix();
if (doubleBuffer) {
glutSwapBuffers();
} else {
glFlush();
}
}
static void Args(int argc, char **argv)
{
GLint i;
doubleBuffer = GL_FALSE;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-sb") == 0) {
doubleBuffer = GL_FALSE;
} else if (strcmp(argv[i], "-db") == 0) {
doubleBuffer = GL_TRUE;
}
}
}
int main(int argc, char **argv)
{
GLenum type;
glutInit(&argc, argv);
Args(argc, argv);
type = GLUT_RGB | GLUT_ACCUM;
type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
glutInitDisplayMode(type);
glutInitWindowSize(300, 300);
glutCreateWindow("Accum Test");
Init();
glutReshapeFunc(Reshape);
glutKeyboardFunc(Key);
glutDisplayFunc(Draw);
glutMainLoop();
}
Program compiles fine on my machine, when tried to run it just output press any key to continue and program exits after key is pressed.
What I am missing ?
What command line arguments have you passed? You have no choice but to use -db for reasons I will explain.
In Windows 7, you cannot draw a single-buffered window because the window manager makes a copy of the framebuffer when you swap buffers and that is what it displays (composites). Without swapping buffers, nothing will ever be displayed. This modern window system design prevents painting partially completed frames, but it mandates double-buffered rendering.
In fact, you should re-write your software so that it defaults to double-buffered rendering unless you specify a command line argument instead of the other way around. This issue affects other platforms besides modern Windows (Vista and newer).
Within my project, I've been having trouble getting an triangle to display within OnRender(), but for some reason, nothing other than the background color (green) is visible.
int main(int argc, char **argv)
{
if (!OnInit())
return -1;
SDL_Event Event;
while (_isRunning)
{
while (SDL_PollEvent(&Event))
OnEvent(&Event);
OnRender();
OnLoop();
SDL_GL_SwapWindow(_screen);
}
OnCleanup();
return 0;
}
void generalSetup()
{
// Initialize SDL2
if (SDL_Init(SDL_INIT_VIDEO) < 0)
sdldie("Failed to initial SDL2.");
else
{
/* Request OpenGL 3.2 */
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
// Create window
_screen = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
// Create Context
_mainContext = SDL_GL_CreateContext(_screen);
// Create Surface
_surface = SDL_GetWindowSurface(_screen);
SDL_FillRect(_surface, NULL, SDL_MapRGB(_surface->format, 0xCC, 0x20, 0x20));
SDL_UpdateWindowSurface(_screen);
/* swap synchronized */
SDL_GL_SetSwapInterval(1);
// Initialize GLew 1.10
glewExperimental = GL_TRUE;
GLenum error = glewInit();
if (error != GLEW_OK)
printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
else
std::cout << "GLew Initialized" << std::endl;
glClearColor(0, 1, 0, 0);
glViewport(0, 0, 800, 600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 800 / 600, 1, 1000);
gluLookAt(0, 0, 20, 0, 0, 0, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
}
bool OnInit()
{
generalSetup();
return true;
}
void OnEvent(SDL_Event* Event)
{
if (Event->type == SDL_QUIT)
_isRunning = false;
}
void OnLoop()
{
}
void OnRender()
{
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.f, 0.f, -10.f);
glBegin(GL_TRIANGLES);
glColor3f(0.1, 0.2, 0.3);
glVertex3f(0, 0, 0);
glVertex3f(1, 0, 0);
glVertex3f(0, 1, 0);
glEnd();
glPopMatrix();
}
void OnCleanup()
{
SDL_GL_DeleteContext(_mainContext);
SDL_DestroyWindow(_screen);
SDL_Quit();
}
You requested a Core context. None of your immediate-mode (matrix stack, glBegin(), etc.) code will work.
Drop back to a compatibility context (SDL_GL_CONTEXT_PROFILE_COMPATIBILITY) or supply all the necessary shaders, vertex buffers, etc. that Core requires.
You are trying to render through the immediate mode, but it is not supported by the OpenGL 3.2. Try using the version 2.0 or 2.1, which support both shaders (if you are intending to use them) and the immediate mode.
I'm trying to display an image in openGL using SDL and SOIL but it isn't working.
Globals.h
#include <SDL.h>
#include <SDL_OpenGL.h>
#include <SOIL.h>
#include "player.h"
main.cpp
#include "Globals.h"
int main(int argc, char** argv){
//Begin SDL initialization
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* Screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE|SDL_OPENGL);
SDL_Event Event;
//End SDL initialization
//Begin OpenGL initialization
glClearColor(1, 1, 1, 1);//Set colour for empty screen
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);//Enable transparency
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(-400, 400, -300, 300, -1, 1);//Set grid for display
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//End OpenGL initialization
Player Player(0, 0, 32, 64, 1, 0, 0, 0.5);
bool gameRunning = true;
while(gameRunning){
while(SDL_PollEvent(&Event)){
if(Event.type == SDL_QUIT)
gameRunning = false;
//If user closed the window in any way stop gameRunning
Player.Events(Event);
}
Player.Update();
glClear(GL_COLOR_BUFFER_BIT);//Clear screen
glPushMatrix();
Player.DisplayWithImage("Brick.png", 32, 64);
glPopMatrix();
SDL_GL_SwapBuffers();//Update screen with new shit
}
SDL_Quit();//Quit SDL
return 0;//End program
}
player.h
#pragma once
class Player{
public:
GLfloat X, Y, W, H, R, G, B, A;
bool movingUp, movingRight, movingDown, movingLeft;
GLuint image;
Player(GLfloat x, GLfloat y, GLfloat w, GLfloat h,
GLfloat r, GLfloat g, GLfloat b, GLfloat a);
void Events(SDL_Event &Event);
void Update();
void Display();
void DisplayWithImage(const char* filename, GLsizei w, GLsizei h);
};
player.cpp
#include "Globals.h"
Player::Player(GLfloat x, GLfloat y, GLfloat w, GLfloat h, GLfloat r, GLfloat g, GLfloat b, GLfloat a){
X = x;
Y = y;
W = w;
H = h;
R = r;
G = g;
B = b;
A = a;
movingUp = false;
movingRight = false;
movingDown = false;
movingLeft = false;
}
void Player::Events(SDL_Event &Event){
if(Event.type == SDL_KEYDOWN){
if(Event.key.keysym.sym == SDLK_w)
movingUp = true;
if(Event.key.keysym.sym == SDLK_d)
movingRight = true;
if(Event.key.keysym.sym == SDLK_s)
movingDown = true;
if(Event.key.keysym.sym == SDLK_a)
movingLeft = true;
}
if(Event.type == SDL_KEYUP){
if(Event.key.keysym.sym == SDLK_w)
movingUp = false;
if(Event.key.keysym.sym == SDLK_d)
movingRight = false;
if(Event.key.keysym.sym == SDLK_s)
movingDown = false;
if(Event.key.keysym.sym == SDLK_a)
movingLeft = false;
}
}
void Player::Update(){
if(movingUp)
Y ++;
if(movingRight)
X ++;
if(movingDown)
Y --;
if(movingLeft)
X --;
}
void Player::Display(){
glBegin(GL_QUADS);
glColor4f(R, G, B, A);
//bottom right
glVertex2f(X+(W/2), Y-(H/2));
//top right
glVertex2f(X+(W/2), Y+(H/2));
//top left
glVertex2f(X-(W/2), Y+(H/2));
//bottom left
glVertex2f(X-(W/2), Y-(H/2));
glEnd();
}
void Player::DisplayWithImage(const char* filename, GLsizei w, GLsizei h){
image = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
(
filename,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
glBindTexture(GL_TEXTURE_2D, image);
glBegin(GL_QUADS);
//bottom right
glTexCoord2f(0, 0); glVertex2f(X+(W/2), Y-(H/2));
//top right
glTexCoord2f(0, H); glVertex2f(X+(W/2), Y+(H/2));
//top left
glTexCoord2f(W, H); glVertex2f(X-(W/2), Y+(H/2));
//bottom left
glTexCoord2f(W, 0); glVertex2f(X-(W/2), Y-(H/2));
glEnd();
}
You have three problems in your code:
GL_TEXTURE_2D uses normalized texture coordinates (0.0 - 1.0). Unless H and W are 1.0 or less you do not want to use them for your texture coordinates. If you just want to stretch this texture across your quad one time, use 1.0 in-place of W and H.
Most Serious Issue: You are creating a new texture on each frame, you should load the texture one time and then pass the loaded texture handle to DisplayWithImage (...) instead of having that function load a texture each time you call it.
glOrtho (...) creates an orthographic projection matrix, any GL operation that uses the projection matrix in isolation (e.g. not Projection * ModelView) is going to fail to work properly if you use an identity projection matrix and an orthographic projection matrix for your modelview matrix. By the same token, things like vertex lighting will not work properly with a projection matrix serving as your modelview matrix...
You can correct bullet point 3 by re-writing part of your code this way:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-400, 400, -300, 300, -1, 1);//Set grid for display
~~~~~~~ This should come AFTER you set the matrix mode to projection!
I have trouble displaying a texture using OpenGL and C++. I am loading the texture from a RGB Raw file, binding it to a texture, but when I am trying to display the texture on a quad, the texture is white. I am using glGetError and it catches no errors. Here is my code:
#include <iostream>
#include <windows.h> // Standard header for MS Windows applications
#include <GL/gl.h> // Open Graphics Library (OpenGL) header
#include <GL/glut.h> // The GL Utility Toolkit (GLUT) Header
using namespace std;
typedef struct {
int width;
int height;
char* title;
float field_of_view_angle;
float z_near;
float z_far;
} glutWindow;
glutWindow win;
float rotation, distance1;
bool light=TRUE;
bool trans=FALSE;
GLuint tex;
void drawFlatFace(float size)
{
glBegin(GL_QUADS);
glTexCoord2f (0, 0);
glVertex3f(-size, -2*size, size);
glTexCoord2f (1.0, 0);
glVertex3f(size, -2*size, size);
glTexCoord2f (1.0, 1.0);
glVertex3f(size, 2*size, size);
glTexCoord2f (0, 1.0);
glVertex3f(-size, 2*size, size);
glEnd();
}
void drawBlock(float size){
glColor3f(1.0f,1.0f,1.0f);
glPushMatrix();
drawFlatFace (size);
glRotatef (90, 0, 1, 0);
drawFlatFace (size);
glRotatef (90, 0, 1, 0);
drawFlatFace (size);
glRotatef (90, 0, 1, 0);
drawFlatFace (size);
glPopMatrix();
}
GLuint loadTexture(const char * filename) {
GLuint text[1];
GLubyte* bits = new GLubyte[512*256*3];
FILE *file;
fopen_s(&file,filename,"rb");
fread(&bits,(512*256*3),1,file);
fclose(file);
glGenTextures(1,&text[0]);
glBindTexture(GL_TEXTURE_2D,text[0]);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, 3,256,512,0,GL_RGB,GL_UNSIGNED_BYTE,bits);
free(bits);
return text[0];
}
void display()
{
GLenum err;
err = glGetError();
if (err != GL_NO_ERROR)
{
cerr << "OpenGL error: " << gluErrorString(err) << endl;
}
tex=loadTexture("tex.raw");
glBindTexture(GL_TEXTURE_2D,tex);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen and Depth Buffer
glLoadIdentity();
gluLookAt( 10+distance1 ,1.4,0, 0,0,0, 0,1,0); // Define a viewing transformation
glRotatef(rotation, 0.f, 1.f, 0.f);
drawBlock(3);
if (light) glEnable(GL_LIGHTING); // Enable Lighting
else glDisable(GL_LIGHTING); // Disable Lighting
if (trans) glEnable (GL_BLEND); // Enable Transparency
else glDisable(GL_BLEND); // Disable Transparency
glutSwapBuffers();
}
void initialize ()
{
glMatrixMode(GL_PROJECTION); // select projection matrix
glViewport(0, 0, win.width, win.height); // set the viewport
glLoadIdentity(); // reset projection matrix
GLfloat aspect = (GLfloat) win.width / win.height;
gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far); // set up a perspective projection matrix
glMatrixMode(GL_MODELVIEW); // specify which matrix is the current matrix
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); // specify implementation-specific hints
GLfloat amb_light[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat diffuse[] = { 0.4, 0.4, 0.4, 1.0 };
GLfloat specular[] = { 0.5, 0.5, 0.5, 1.0 };
glLightModelfv( GL_LIGHT_MODEL_AMBIENT, amb_light );
glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
GLfloat light_position[] = { 1.0, 1.0, 0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable( GL_LIGHT0 );
glEnable( GL_COLOR_MATERIAL );
glEnable( GL_LIGHTING );
//glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
void keyboard ( int key, int mousePositionX, int mousePositionY )
{
switch ( key )
{
case GLUT_KEY_DOWN:
if (distance1<9){
distance1=distance1+.3;
}
break;
case GLUT_KEY_UP:
if (distance1>-9){
distance1=distance1-.3;
}
break;
case GLUT_KEY_LEFT:
rotation=rotation-2;
break;
case GLUT_KEY_RIGHT:
rotation=rotation+2;
break;
case GLUT_KEY_PAGE_UP:
light=TRUE;
break;
case GLUT_KEY_PAGE_DOWN:
light=FALSE;
break;
case GLUT_KEY_HOME:
trans=TRUE;
break;
case GLUT_KEY_END:
trans=FALSE;
break;
default:
break;
}
}
int main(int argc, char **argv)
{
// set window values
win.width = 800;
win.height = 600;
win.title = "Test Texture";
win.field_of_view_angle = 45;
win.z_near = 1.0f;
win.z_far = 500.0f;
// initialize and run program
glutInit(&argc, argv); // GLUT initialization
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); // Display Mode
glutInitWindowSize(win.width,win.height); // set window size
glutCreateWindow(win.title); // create Window
// Callback functions
glutDisplayFunc(display); // register Display Function
glutIdleFunc( display ); // register Idle Function
glutSpecialFunc( keyboard ); // register Keyboard Handler
initialize();
glutMainLoop(); // run GLUT mainloop
return 0;
}
I want to add that the Raw file is displayed correctly when loaded in IrfanView as 256x512, 24BPP, RGB (the size is 393,216 bytes or 3*512*256)
This line:
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Shouldn't be in your main intialisation, but instead needs to be done when setting up the texture itself (i.e. after binding).
You may also want to set the MAG_FILTER, and the texture coordinate wrapping modes, as a general rule.
Also, you appear to be loading the texture every time you render. Don't do that, performance will be terrible and you'll run out of memory as GL will keep allocating new textures.
I am using cygwin SDL 1.2.15 using the latest cygwin
Here is my code using SDL and openGL
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <iostream>
size_t sx=600, sy=600, bpp=32;
void render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // set location in front of camera
//glTranslated(0, 0, -10);
glBegin(GL_QUADS); // draw a square
glColor3d(1, 0, 0);
glVertex3d(-2, 2, 0);
glVertex3d( 2, 2, 0);
glVertex3d( 2, -2, 0);
glVertex3d(-2, -2, 0);
glEnd();
glFlush();
SDL_GL_SwapBuffers();
GLenum e;
while ((e =glGetError()) != GL_NO_ERROR)
std::cout<<"Error "<< e << std::endl;
}
int input(void) {
SDL_Event event;
while (SDL_PollEvent(&event))
if (event.type == SDL_QUIT || (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE)) return 0;
return 1;
}
and this is my main function
int main(int argc, char *argv[]) {
SDL_Surface *surf;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) return 0;
if (!(surf = SDL_SetVideoMode(sx, sy, bpp, SDL_OPENGL))) return 0;
glViewport(0, 0, sx, sy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)sx / (float)sy, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 1);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
GLenum e;
while ((e =glGetError()) != GL_NO_ERROR)
std::cout<<"Error "<< e << std::endl;
for (;;) {
if (!input()) break;
render();
SDL_Delay(10);
}
SDL_FreeSurface(surf);
SDL_Quit();
return 0;
}
it compiles with no error but when I run it only the window shows up and now openGL rectangle..
You have setup a near plane to one :
gluPerspective(45.0, (float)sx / (float)sy, 1.0/*near plane*/, 100.0);
Everything that is closer to the camera is clipped.
Your quad lies in plane z = 0. Try moving it a bit backward.
glBegin(GL_QUADS); // draw a square
glColor3d(1, 0, 0);
glVertex3d(-2, 2, 5);
glVertex3d( 2, 2, 5);
glVertex3d( 2, -2, 5);
glVertex3d(-2, -2, 5);
glEnd();
I don't remember if Z is facing the camera, so you might need negative Z value.
You also need to pay attention to face culling. It might be better to deactivate it to be sure ( glDisable( GL_CULL_FACE ))
Try changing the black color of the SDL window. Some times it renders the drawing with black color...may be this helps!