Weird glitch involving my code - c++

So today, I was messing around with some examples and decided I wanted to make an app. So I attempted. When I click once, the screen becomes black, another click then white, and after some spamming the block appears, clicking again makes the background go black, another click, then white. After some more spamming there is another block inside the block, pictures are attached.
CODE:
#include "SFML/Graphics.hpp"
#include "SFML/OpenGL.hpp"
#include <GL\GLU.h>
#include <iostream>
#include "cube.cpp"
void createblock() {
gluPerspective(90.f, 1.f, 1.f, 300.0f);
glClearColor(0.3f, 0.3f, 0.3f, 0.f);
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glBegin(GL_QUADS);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
glVertex3f(50.f, -50.f, -50.f);
glVertex3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(50.f, 50.f, 50.f);
glVertex3f(50.f, -50.f, 50.f);
glColor3f(1, 0, 1);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, -50.f, 50.f);
glColor3f(0, 1, 0);
glVertex3f(50.f, -50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, 50.f);
glVertex3f(50.f, -50.f, 50.f);
glColor3f(1, 1, 0);
glVertex3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(50.f, -50.f, -50.f);
glVertex3f(50.f, -50.f, 50.f);
glColor3f(1, 0, 0);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, 50.f);
glEnd();
}
sf::RenderWindow window(sf::VideoMode(800, 600), "SimpleBlocks");
sf::Event maine;
int main() {
while (window.isOpen()) {
// load defaults here
while (window.pollEvent(maine)) {
// events here
if (maine.type == sf::Event::Closed) {
window.close();
}
if (maine.type == sf::Event::MouseButtonPressed) {
createblock();
window.display();
// make block later
}
}
}
}
IMAGES:
imgur album here

You never call glClear(GL_COLOR_BUFFER_BIT) before you draw, so there are always leftover fragments or some overlap in depth information. Adding a clear call should fix your issue.

Call glPushMatrix before gluPerspective and glPopMatrix at the end of createblock

Related

OpenGL, SOIL, Textures and Transparent backgrounds

I can't seem to get the transparent background on my image to load as transparent. It's always white instead. I'm still fairly new to OpenGL/SOIL so if I did something stupid don't be mad :/ Does it have to do with my flag on the load being RGB instead of RGBA?
void Scene::loadTexShips()
{
texShips[0] = SOIL_load_OGL_texture(
"Textures/Carrier.png",
SOIL_LOAD_LA,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB
);
if (texShips[0] == 0)
{
printf("SOIL loading error: '%s'\n", SOIL_last_result());
}
}
void Scene::drawShips()
{
glColor4ub(255, 255, 255, 255);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLoadIdentity();
glTranslatef(6.0f, 2.0f, -12.0f);
glBindTexture(GL_TEXTURE_2D, texShips[0]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-4.0f, -3.0f, 2.0);
glTexCoord2f(0, 1);
glVertex3f(-4.0f, -2.3, 2.0);
glTexCoord2f(1, 1);
glVertex3f(-1.0, -2.3, 2.0);
glTexCoord2f(1, 0);
glVertex3f(-1.0, -3.0, 2.0);
glEnd();
glDisable(GL_TEXTURE_2D);
}

Texture problems in LWJGL (OpenGL) using slick-util library

I’m just starting out with 3D graphics programming and I’ve got a nicely encapsulated Cube object. This class has a render() method, which proceeds to push a new matrix, perform transformations, glBegin, specify all the vertices and texture coordinates, glEnd, and pop the matrix. Now, I have written this Cube class with methods for using different textures on different faces of the cube, but that isn't happening at runtime. I understood textures to be like colors, which I can easily change per face of the cube by making a call to glColor before the appropriate vertices, but using texture.bind() (from the slick-util library) seems to do nothing.
This is my render method:
public void render(){
glPushMatrix();
top.bind();
top.setTextureFilter(GL_NEAREST);
glColor4f(1, 1, 1, 0);
glTranslatef(x, y, z);
glRotatef(yaw, 0, 1, 0);
glBegin(GL_QUADS);
//Top
glTexCoord2f(0, 0); glVertex3f(0, size, 0);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 0); glVertex3f(size, size, 0);
//Front
top.release();
side.bind();
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(0, 1); glVertex3f(0, size, 0);
glTexCoord2f(1, 1); glVertex3f(size, size, 0);
glTexCoord2f(1, 0); glVertex3f(size, 0, 0);
//Left
glTexCoord2f(0, 0); glVertex3f(0, 0, size);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(0, size, 0);
glTexCoord2f(1, 0); glVertex3f(0, 0, 0);
//Right
glTexCoord2f(0, 0); glVertex3f(size, 0, size);
glTexCoord2f(0, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, 0);
glTexCoord2f(1, 0); glVertex3f(size, 0, 0);
//Back
glTexCoord2f(0, 0); glVertex3f(0, 0, size);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 0); glVertex3f(size, 0, size);
//Bottom
side.release();
bottom.bind();
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(0, 1); glVertex3f(0, 0, size);
glTexCoord2f(1, 1); glVertex3f(size, 0, size);
glTexCoord2f(1, 0); glVertex3f(size, 0, 0);
bottom.release();
glEnd();
glPopMatrix();
}
Variables top, side, and bottom are the textures I want to put on the faces of the cube.
The result is that every face of the cube has the “top” texture.
The result is the same whether I call texture.release() or not.
You can't bind textures between glBegin() and glEnd(). Only a limited set of GL calls can be made between glBegin() and glEnd() (see https://www.opengl.org/sdk/docs/man2/xhtml/glBegin.xml for the full list), and glBindTexture() is not one of them.
To bind a different texture for each side, you need to start a new begin/end pair for each side:
//Top
top.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, size, 0);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 0); glVertex3f(size, size, 0);
glEnd();
//Front
side.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
...
glEnd();
//Bottom
bottom.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
...
glEnd();

OpenGL Lighting Changes Brightness With Camera

So I've just started playing around with OpenGL, and decided to make a little voxel render thing. I'm trying to light it, but weird effects happen when I rotate the camera around the y axis. For example, when I first spawn in, the light looks like this:
http://i.stack.imgur.com/zQ49y.png
But when I rotate around a bit I get this:
http://i.stack.imgur.com/PWvVo.png
Here's the code that does this stuff:
glRotatef(xrot, 1.0f, 0.0f, 0.0);
glRotatef(yrot, 0.0f, 1.0f, 0.0);
glTranslatef(-3.5f-xcam, ycam, -3.5f-zcam);
glEnable (GL_DEPTH_TEST); //enable the depth testing
glEnable (GL_LIGHTING); //enable the lighting
glEnable (GL_LIGHT0);
GLfloat specular[] = {1.0f-xcam, ycam+1.0, 1.0f-zcam, 1.0};
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, grnd);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex3f(0, 0, 0);
glTexCoord2i(0, 1);
glVertex3f(0, 0, 7.0f);
glTexCoord2i(1, 1);
glVertex3f(7.0f, 0, 7.0f);
glTexCoord2i(1, 0);
glVertex3f(7.0f, 0, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
glLoadIdentity();
drawVoxel(2.f, 0.f, 2.f, tex);
drawVoxel(1.f, 0.f, 1.f, tex);
drawVoxel(2.f, 0.f, 2.f, tex);
drawVoxel(3.f, 0.f, 3.f, tex);
drawVoxel(4.f, 0.f, 4.f, tree);
drawVoxel(4.f, 1.f, 4.f, tree);
drawVoxel(4.f, 2.f, 4.f, tree);
drawVoxel(4.f, 3.f, 4.f, tree);
test.Flip();
Does anybody have any idea what's going on?
You also need to use glNormal3f().
.
.
.
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glNormal3f(0,1,0);
glVertex3f(0, 0, 0);
glTexCoord2i(0, 1);
glNormal3f(0,1,0);
glVertex3f(0, 0, 7.0f);
glTexCoord2i(1, 1);
glNormal3f(0,1,0);
glVertex3f(7.0f, 0, 7.0f);
glTexCoord2i(1, 0);
glNormal3f(0,1,0);
glVertex3f(7.0f, 0, 0);
glEnd();

How to assign texture coordinates in a for loop at OpenGL?

I want to modify the code snippet below in such a way, that I want to generate the geometry with a particular texture using for-loops. I want to input the vertex coordinates, and the amount of vertices and then generate texture planes using glVertex3f. Is this possible?
Edit: To clarify, I want to be able to give the information contained in
glTexCoord2f(0.f, 0.f); glVertex3f(-1, -1, -1);
glTexCoord2f(rep, 0.f); glVertex3f(-1, 1, -1);
glTexCoord2f(rep, rep); glVertex3f(-1, 1, 1);
glTexCoord2f(0.f, rep); glVertex3f(-1, -1, 1);
segment using a for-loop. It's like I'd like to have a glVertex3f[n] array with the coordinates given by the user.
// Obstacle texture
// draw with different ppm file
obstProg->enable();
obstProg->bindTexture("tex", obstTex, GL_TEXTURE_2D, 0);
obstProg->bindTexture("shadowTex", renderer->getShadowTexture(), GL_TEXTURE_2D, 1);
obstProg->setUniformfv("lightPosEye", renderer->getLightPositionEyeSpace(), 3);
obstProg->setUniformfv("lightColor", lightColor, 3);
// set shadow matrix as texture matrix
matrix4f shadowMatrix3 = renderer->getShadowMatrix();
glActiveTexture(GL_TEXTURE0);
glMatrixMode(GL_TEXTURE);
glLoadMatrixf((GLfloat *) shadowMatrix3.get_value());
glColor3f(1.0, 1.0, 1.0);
glNormal3f(0.0, 1.0, 0.0);
glBegin(GL_QUADS);
{
float rep = 10.f;
glTexCoord2f(0.f, 0.f); glVertex3f(-1, -1, -1);
glTexCoord2f(rep, 0.f); glVertex3f(-1, 1, -1);
glTexCoord2f(rep, rep); glVertex3f(-1, 1, 1);
glTexCoord2f(0.f, rep); glVertex3f(-1, -1, 1);
glTexCoord2f(0.f, 0.f); glVertex3f(1, -1, -1);
glTexCoord2f(rep, 0.f); glVertex3f(1, 1, -1);
glTexCoord2f(rep, rep); glVertex3f(1, 1, 1);
glTexCoord2f(0.f, rep); glVertex3f(1, -1, 1);
glTexCoord2f(0.f, 0.f); glVertex3f(-1, -1, 1);
glTexCoord2f(rep, 0.f); glVertex3f(-1, 1, 1);
glTexCoord2f(rep, rep); glVertex3f(1, 1, 1);
glTexCoord2f(0.f, rep); glVertex3f(1, -1, 1);
glTexCoord2f(0.f, 0.f); glVertex3f(-1, -1, -1);
glTexCoord2f(rep, 0.f); glVertex3f(-1, 1, -1);
glTexCoord2f(rep, rep); glVertex3f(1, 1, -1);
glTexCoord2f(0.f, rep); glVertex3f(1, -1, -1);
glTexCoord2f(0.f, 0.f); glVertex3f(-1, 1, -1);
glTexCoord2f(rep, 0.f); glVertex3f(1, 1, -1);
glTexCoord2f(rep, rep); glVertex3f(1, 1, 1);
glTexCoord2f(0.f, rep); glVertex3f(-1, 1, 1);
}
glEnd();
obstProg->disable();
Since you already have the vertex data in a array it makes sense to send them to OpenGL directly, instead of manually looping over them and call immediate mode functions. Google for "Vertex Arrays"

.gif image display using OpenGl

Can I display *.gif image in openGL?
I want to use it like texture of glQuad display list.
glNewList(base+loop,GL_COMPILE); // Start Building A List
glBegin(GL_QUADS); // Use A Quad For Each Character
glTexCoord2f(cx,1-cy-0.0625f); // Texture Coord (Bottom Left)
glVertex2i(0,0); // Vertex Coord (Bottom Left)
glTexCoord2f(cx+0.0625f,1-cy-0.0625f); // Texture Coord (Bottom Right)
glVertex2i(16,0); // Vertex Coord (Bottom Right)
glTexCoord2f(cx+0.0625f,1-cy); // Texture Coord (Top Right)
glVertex2i(16,16); // Vertex Coord (Top Right)
glTexCoord2f(cx,1-cy); // Texture Coord (Top Left)
glVertex2i(0,16); // Vertex Coord (Top Left)
glEnd(); // Done Building Our Quad (Character)
glTranslated(10,0,0); // Move To The Right Of The Character
glEndList();
Thx
I m using yhis libraries
#include <windows.h> // Header File For Windows
#include <stdio.h> // Header File For Standard Input/Output
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sstream>
//#include "glut.h"
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glut.h>
#include <gl\glaux.h>
I recommend you to use SFML - it supports lot of image formats and image loading/processing is made simple with it. Here's an example application with textured cube using SFML:
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
// Create main window
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML OpenGL");
App.PreserveOpenGLStates(true);
// Create a sprite for the background
sf::Image BackgroundImage;
if (!BackgroundImage.LoadFromFile("datas/opengl/background.jpg"))
return EXIT_FAILURE;
sf::Sprite Background(BackgroundImage);
// Load an OpenGL texture.
// We could directly use a sf::Image as an OpenGL texture (with its Bind() member function),
// but here we want more control on it (generate mipmaps, ...) so we create a new one
GLuint Texture = 0;
{
sf::Image Image;
if (!Image.LoadFromFile("datas/opengl/texture.jpg"))
return EXIT_FAILURE;
glGenTextures(1, &Texture);
glBindTexture(GL_TEXTURE_2D, Texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Image.GetWidth(), Image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, Image.GetPixelsPtr());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);
// Bind our texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texture);
glColor4f(1.f, 1.f, 1.f, 1.f);
// Create a clock for measuring the time elapsed
sf::Clock Clock;
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
// Adjust the viewport when the window is resized
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}
// Draw background
App.Draw(Background);
// Clear depth buffer
glClear(GL_DEPTH_BUFFER_BIT);
// Apply some transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -200.f);
glRotatef(Clock.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
glRotatef(Clock.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
glRotatef(Clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);
// Draw a cube
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f( 50.f, 50.f, -50.f);
glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, -50.f);
glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, 50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, 50.f);
glTexCoord2f(1, 1); glVertex3f( 50.f, 50.f, 50.f);
glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, 50.f);
glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f(-50.f, 50.f, 50.f);
glTexCoord2f(1, 0); glVertex3f(-50.f, -50.f, 50.f);
glTexCoord2f(0, 0); glVertex3f(50.f, -50.f, -50.f);
glTexCoord2f(0, 1); glVertex3f(50.f, 50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f(50.f, 50.f, 50.f);
glTexCoord2f(1, 0); glVertex3f(50.f, -50.f, 50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, -50.f, 50.f);
glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f( 50.f, -50.f, 50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, 50.f);
glTexCoord2f(0, 0); glVertex3f(-50.f, 50.f, -50.f);
glTexCoord2f(1, 0); glVertex3f( 50.f, 50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f( 50.f, 50.f, 50.f);
glEnd();
// Draw some text on top of our OpenGL object
sf::String Text("This is a rotating cube");
Text.SetPosition(250.f, 300.f);
Text.SetColor(sf::Color(128, 0, 128));
App.Draw(Text);
// Finally, display the rendered frame on screen
App.Display();
}
// Don't forget to destroy our texture
glDeleteTextures(1, &Texture);
return EXIT_SUCCESS;
}
Note: And do not use glaux - it is terribly lost in time.
Note: demo uses .jpg image format. This one is better than .gif, because it is more lightweight and, afaik, widespreaded and easier to implement in application.
It's kind of overkill if you just want to support GIF, but DevIL supports GIF (among many others). There are lighter solutions around as well. Alternatively, you can get the GIF spec, and write it yourself (makes a nice, relaxing afternoon project as I recall).