1
General / OpenGL Help
« on: November 26, 2014, 11:13:08 pm »
HI! I'm currently working on an OpenGL/SFML project, and I'm having some trouble. I *hope* it's something simple in the code, but me and a friend have looked over it again and again and we can't find the problem. Basically, we should be drawing a textured block, but nothing's appearing! We have no problem clearing the screen to a different colour, but the .obj model we load isn't there. The code has previously been tested with SDL, so we know the model loader and positions should be right. Is there some secret call I'm missing or something? Thansk in advance.
int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
// Request a 32-bits depth buffer when creating the window
sf::ContextSettings contextSettings;
contextSettings.depthBits = 32;
contextSettings.majorVersion = 3;
contextSettings.minorVersion = 3;
// Create the main window
sf::Window window(sf::VideoMode(640, 480), "SFML window with OpenGL", sf::Style::Default, contextSettings);
// Make it the active window for OpenGL calls
window.setActive();
// Set the color and depth clear values
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 1.f);
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
// Disable lighting and texturing
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
// Configure the viewport (the same size as the window)
glViewport(0, 0, window.getSize().x, window.getSize().y);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat ratio = static_cast<float>(window.getSize().x) / window.getSize().y;
glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
textures[0].loadFromFile("fabric.bmp");
vector<GLfloat> verts;
vector<GLfloat> norms;
vector<GLfloat> tex_coords;
vector<GLuint> indices;
loadObj("cube.obj", verts, norms, tex_coords, indices);
GLuint size = indices.size();
meshIndexCount = size;
meshObjects[0] = createMesh(verts.size()/3, verts.data(), nullptr, norms.data(), tex_coords.data(), size, indices.data());
shaderProgram = initShaders("phong-tex.vert","phong-tex.frag");
setLight(shaderProgram, light0);
setMaterial(shaderProgram, material0);
// Enable position and color vertex components
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// Disable normal and texture coordinates vertex components
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
// Create a clock for measuring the time elapsed
sf::Clock clock;
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed)
window.close();
// Escape key: exit
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
window.close();
// Resize event: adjust the viewport
if (event.type == sf::Event::Resized)
glViewport(0, 0, event.size.width, event.size.height);
}
// clear the screen
glEnable(GL_CULL_FACE);
glClearColor(0.5f,0.5f,0.5f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 projection(1.0);
projection = glm::perspective(60.0f,800.0f/600.0f,1.0f,150.0f);
rt3d::setUniformMatrix4fv(shaderProgram, "projection", glm::value_ptr(projection));
GLfloat scale(1.0f); // just to allow easy scaling of complete scene
glm::mat4 modelview(1.0); // set base position for scene
mvStack.push(modelview);
//glBindTexture(GL_TEXTURE_2D, textures[0]);
sf::Texture::bind(&textures[0]);
mvStack.push(mvStack.top());
mvStack.top() = glm::translate(mvStack.top(), glm::vec3(0.0f, 0.0f, -10.0f));
mvStack.top() = glm::scale(mvStack.top(),glm::vec3(20.0f, 0.1f, 20.0f));
setUniformMatrix4fv(shaderProgram, "modelview", glm::value_ptr(mvStack.top()));
setMaterial(shaderProgram, material0);
drawIndexedMesh(meshObjects[0],meshIndexCount,GL_TRIANGLES);
mvStack.pop();
// Finally, display the rendered frame on screen
window.display();
}
return EXIT_SUCCESS;
}
{
// Request a 32-bits depth buffer when creating the window
sf::ContextSettings contextSettings;
contextSettings.depthBits = 32;
contextSettings.majorVersion = 3;
contextSettings.minorVersion = 3;
// Create the main window
sf::Window window(sf::VideoMode(640, 480), "SFML window with OpenGL", sf::Style::Default, contextSettings);
// Make it the active window for OpenGL calls
window.setActive();
// Set the color and depth clear values
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 1.f);
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
// Disable lighting and texturing
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
// Configure the viewport (the same size as the window)
glViewport(0, 0, window.getSize().x, window.getSize().y);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat ratio = static_cast<float>(window.getSize().x) / window.getSize().y;
glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
textures[0].loadFromFile("fabric.bmp");
vector<GLfloat> verts;
vector<GLfloat> norms;
vector<GLfloat> tex_coords;
vector<GLuint> indices;
loadObj("cube.obj", verts, norms, tex_coords, indices);
GLuint size = indices.size();
meshIndexCount = size;
meshObjects[0] = createMesh(verts.size()/3, verts.data(), nullptr, norms.data(), tex_coords.data(), size, indices.data());
shaderProgram = initShaders("phong-tex.vert","phong-tex.frag");
setLight(shaderProgram, light0);
setMaterial(shaderProgram, material0);
// Enable position and color vertex components
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// Disable normal and texture coordinates vertex components
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
// Create a clock for measuring the time elapsed
sf::Clock clock;
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed)
window.close();
// Escape key: exit
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
window.close();
// Resize event: adjust the viewport
if (event.type == sf::Event::Resized)
glViewport(0, 0, event.size.width, event.size.height);
}
// clear the screen
glEnable(GL_CULL_FACE);
glClearColor(0.5f,0.5f,0.5f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 projection(1.0);
projection = glm::perspective(60.0f,800.0f/600.0f,1.0f,150.0f);
rt3d::setUniformMatrix4fv(shaderProgram, "projection", glm::value_ptr(projection));
GLfloat scale(1.0f); // just to allow easy scaling of complete scene
glm::mat4 modelview(1.0); // set base position for scene
mvStack.push(modelview);
//glBindTexture(GL_TEXTURE_2D, textures[0]);
sf::Texture::bind(&textures[0]);
mvStack.push(mvStack.top());
mvStack.top() = glm::translate(mvStack.top(), glm::vec3(0.0f, 0.0f, -10.0f));
mvStack.top() = glm::scale(mvStack.top(),glm::vec3(20.0f, 0.1f, 20.0f));
setUniformMatrix4fv(shaderProgram, "modelview", glm::value_ptr(mvStack.top()));
setMaterial(shaderProgram, material0);
drawIndexedMesh(meshObjects[0],meshIndexCount,GL_TRIANGLES);
mvStack.pop();
// Finally, display the rendered frame on screen
window.display();
}
return EXIT_SUCCESS;
}