Open GL stars are not rendering
- by Darestium
I doing Nehe's Open GL Lesson 9. I'm using SFML for windowing, the strange thing is no stars are rendering.
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
void processEvents(sf::Window *app);
void processInput(sf::Window *app);
void renderGlScene(sf::Window *app);
void init();
int loadResources();
const int NUM_OF_STARS = 50;
float triRot = 0.0f;
float quadRot = 0.0f;
bool twinkle = false;
bool tKey = false;
float zoom = 15.0f;
float tilt = 90.0f;
float spin = 0.0f;
unsigned int loop;
unsigned int texture_handle[1];
typedef struct {
int r, g, b;
float distance;
float angle;
} stars;
stars star[NUM_OF_STARS];
int main() {
sf::Window app(sf::VideoMode(800, 600, 32), "Nehe Lesson 9");
app.UseVerticalSync(false);
init();
if (loadResources() == -1) {
return EXIT_FAILURE;
}
while (app.IsOpened()) {
processEvents(&app);
processInput(&app);
renderGlScene(&app);
app.Display();
}
return EXIT_SUCCESS;
}
int loadResources() {
sf::Image img_data;
// Load Texture
if (!img_data.LoadFromFile("data/images/star.bmp")) {
std::cout << "Could not load data/images/star.bmp";
return -1;
}
// Generate 1 texture
glGenTextures(1, &texture_handle[0]);
// Linear filtering
glBindTexture(GL_TEXTURE_2D, texture_handle[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img_data.GetWidth(), img_data.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data.GetPixelsPtr());
return 0;
}
void processInput(sf::Window *app) {
const sf::Input& input = app->GetInput();
if (input.IsKeyDown(sf::Key::T) &&
!tKey) {
tKey = true;
twinkle = !twinkle;
}
if (!input.IsKeyDown(sf::Key::T)) {
tKey = false;
}
if (input.IsKeyDown(sf::Key::Up)) {
tilt -= 0.05f;
}
if (input.IsKeyDown(sf::Key::Down)) {
tilt += 0.05f;
}
if (input.IsKeyDown(sf::Key::PageUp)) {
zoom -= 0.02f;
}
if (input.IsKeyDown(sf::Key::Up)) {
zoom += 0.02f;
}
}
void init() {
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 0.f);
// Enable texturing
glEnable(GL_TEXTURE_2D);
//glDepthMask(GL_TRUE);
// Setup a perpective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.f, 1.f, 1.f, 500.f);
glShadeModel(GL_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_BLEND);
for (loop = 0; loop < NUM_OF_STARS; loop++) {
star[loop].distance = (float)loop / NUM_OF_STARS * 5.0f; // Calculate distance from the centre
// Give stars random rgb value
star[loop].r = rand() % 256;
star[loop].g = rand() % 256;
star[loop].b = rand() % 256;
}
}
void processEvents(sf::Window *app) {
sf::Event event;
while (app->GetEvent(event)) {
if (event.Type == sf::Event::Closed) {
app->Close();
}
if (event.Type == sf::Event::KeyPressed &&
event.Key.Code == sf::Key::Escape) {
app->Close();
}
}
}
void renderGlScene(sf::Window *app) {
app->SetActive();
// Clear color depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Apply some transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Select texture
glBindTexture(GL_TEXTURE_2D, texture_handle[0]);
for (loop = 0; loop < NUM_OF_STARS; loop++) {
glLoadIdentity(); // Reset The View Before We Draw Each Star
glTranslatef(0.0f, 0.0f, zoom); // Zoom Into The Screen (Using The Value In 'zoom')
glRotatef(tilt, 1.0f, 0.0f, 0.0f); // Tilt The View (Using The Value In 'tilt')
glRotatef(star[loop].angle, 0.0f, 1.0f, 0.0f); // Rotate To The Current Stars Angle
glTranslatef(star[loop].distance, 0.0f, 0.0f); // Move Forward On The X Plane
glRotatef(-star[loop].angle,0.0f,1.0f,0.0f); // Cancel The Current Stars Angle
glRotatef(-tilt,1.0f,0.0f,0.0f); // Cancel The Screen Tilt
if (twinkle) {
glColor4ub(star[(NUM_OF_STARS - loop) - 1].r, star[(NUM_OF_STARS - loop)-1].g, star[(NUM_OF_STARS - loop) - 1].b, 255);
glBegin(GL_QUADS); // Begin Drawing The Textured Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd(); // Done Drawing The Textured Quad
}
glRotatef(spin,0.0f,0.0f,1.0f); // Rotate The Star On The Z Axis
// Assign A Color Using Bytes
glColor4ub(star[loop].r, star[loop].g, star[loop].b, 255);
glBegin(GL_QUADS); // Begin Drawing The Textured Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd(); // Done Drawing The Textured Quad
spin += 0.01f; // Used To Spin The Stars
star[loop].angle += (float)loop / NUM_OF_STARS; // Changes The Angle Of A Star
star[loop].distance -= 0.01f; // Changes The Distance Of A Star
if (star[loop].distance < 0.0f) {
star[loop].distance += 5.0f; // Move The Star 5 Units From The Center
star[loop].r = rand() % 256; // Give It A New Red Value
star[loop].g = rand() % 256; // Give It A New Green Value
star[loop].b = rand() % 256; // Give It A New Blue Value
}
}
}
I've looked over the code atleast 10 times now and I can't figure out the problem. Any help would be much appreciated.