I'm following a tutorial on creating a Game Engine in Java using OpenGL.
I'm trying to render a triangle on the screen. Everything is running fine and I can change the background color but the triangle won't show. I've also tried running the code provided as part of the tutorial series and it still doesn't work.
Link to the tutorial: http://bit.ly/1EUnvz4
Link to the code used in the video: http://bit.ly/1z7XUlE
Setup
I've tried checking for OpenGL version and belive I have 2.1.
Mac OSX
Java - Eclipse
Mesh.java
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
public class Mesh
{
private int vbo; //pointer to the buffer
private int size; //size of the data to buffer
public Mesh ()
{
vbo = glGenBuffers();
size = 0;
}
public void addVertices (Vertex[] vertices)
{
size = vertices.length;
//add the data by first binding the buffer
glBindBuffer (GL_ARRAY_BUFFER, vbo); //vbo is now the buffer
//and then buffering the data
glBufferData (GL_ARRAY_BUFFER, Util.createFlippedBuffer(vertices), GL_STATIC_DRAW);
}
public void draw ()
{
glEnableVertexAttribArray (0); //divide up the data into a segment
glBindBuffer (GL_ARRAY_BUFFER, vbo); //vbo is now the buffer
//tell OpenGL more about the segment:
//segment = 0, elements = 3, type = float, normalize? = false, vertex size, where to start = 0)
glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0);
//draw GL_TRIANGLES starting from '0' with a given 'size'
glDrawArrays (GL_TRIANGLES, 0, size);
glDisableVertexAttribArray (0);
}
}
RenderUtil.java
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL30.*;
public class RenderUtil
{
public static void clearScreen ()
{
//TODO: Stencil Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
//set everything to engine defaults
public static void initGraphics ()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // default color
glFrontFace(GL_CW); // direction for visible faces
glCullFace(GL_BACK); // direction for back faces
glEnable (GL_CULL_FACE); // don't draw back faces
glEnable (GL_DEPTH_TEST); // determines draw order by pixel depth testing
//TODO: Depth clamp for later
glEnable (GL_FRAMEBUFFER_SRGB); // do exponential correction on gamma so we don't have to
}
}
Util.java
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
public class Util
{
//create a float buffer (we need this because java is weird)
public static FloatBuffer createFloatBuffer (int size)
{
return BufferUtils.createFloatBuffer(size);
}
//flip the buffer to fit what OpenGL expects
public static FloatBuffer createFlippedBuffer (Vertex[] vertices)
{
FloatBuffer buffer = createFloatBuffer(vertices.length * Vertex.SIZE);
for (int i = 0; i < vertices.length; i++)
{
buffer.put(vertices[i].getPos().getX());
buffer.put(vertices[i].getPos().getY());
buffer.put(vertices[i].getPos().getZ());
}
buffer.flip();
return buffer;
}
}
You are using an invalid mix of legacy and modern OpenGL.
The glVertexAttribPointer() and glEnableVertexAttribArray() functions you are calling are used for setting up generic vertex attributes. This is the only way to set up vertex attribues in current versions of OpenGL (Core Profile of desktop OpenGL, or OpenGL ES 2.0 and later). They can be used in older versions of OpenGL as well, but only in combination with providing your own shaders implemented in GLSL.
If you are just getting started, your best option is probably to stick with what you have, and study how to start implementing your own shaders. If you wanted to get the code working with the legacy fixed pipeline (which is only supported in the Compatibility Profile of OpenGL), you would need to use the glVertexPointer() and glEnableClientState() functions instead.
Try a single import?
import static org.lwjgl.opengl.GL11.*
I only have one import on mine, also try importing the packages you need separately. One thing you are likely doing wrong is importing multiple versions of OpenGL
Related
My task is to show several pictures. I implemented it as a class to make several instances. Each instance represents a picture. It compiles shaders, set two triangles and loads picture data in constructor. The main program creates instances and then goes to loop to switch prigramid and call render() method for each instance.
while(true)
for (uint g = 0; g < pictures.size(); g++){
glUseProgram(pictures[g]->ProgramId);
pictures[g]->render();
}
It works well and shows the pictures but I do not like it. It could be done much better.
Here is partial code of the class
Picture::Picture(picPosition* pPosition, const char * fileName)
:BasePicture(pPosition)
{
pos = glGetAttribLocation(ProgramId, "position");
uv = glGetAttribLocation(ProgramId, "texture_vert");
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
int n;
textureData = stbi_load(fileName, &picWidth, &picHeight, &n, STBI_rgb_alpha);
TextureID = glGetUniformLocation(ProgramId, "myTextureSampler");
glBindTexture(GL_TEXTURE_2D, TextureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glActiveTexture(GL_TEXTURE0);
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
//calculating the vertex matrix using MVP calculated in parent class
for (int i = 0; i < 6; i++)
ModeledVerts.push_back(MVP * verts[i]);
v = glm::value_ptr(ModeledVerts[0]);
}
Picture::~Picture()
{
stbi_image_free(textureData);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &uvbuffer);
}
void Picture::render()
{
glBufferData(GL_ARRAY_BUFFER, 96, v, GL_STATIC_DRAW);
glVertexAttribPointer(pos, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid*) 0);
glEnableVertexAttribArray(pos);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesUV), verticesUV, GL_STATIC_DRAW);
glVertexAttribPointer(uv, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*) 0);
glEnableVertexAttribArray(uv);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, picWidth, picHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
I played a lot with the code to make render() function as light as possible but I cannot make it lighter then it is now.
The biggest problem is sending textureData every time (glTexImage2D). The data never gets changed. I tried to move it to constructor but in this case all picture objects show the same picture that was loaded latest. It looks like one instance overrides texture data uploaded before. I am looking for a way to load the picture data once in the constructor and not every time it renders. It looks like there is something in OpenGL API for that but I do not know it yet.
Another improvement could be for taking vertex data set up from render(). That data never changes. But it is not that significant as glTexImage2D call in render().
Could you point me to the OpenGL API to separate shader's data? Or show me what I am doing wrong...
You stated:
It works well and shows the pictures but I do not like it. It could be done much better.
From a design approach I think that this may help you.
Separate the functionality of opening, reading & parsing the image files for texture data from the actual texture struct or class. This would be a sample pseudo code:
struct Texture {
unsigned int width;
unsigned int height;
bool hasTransparency;
GLint id; // ID that is used by OpenGL to setActive, bind, and pass to shaders as either a uniform or sampler2D.
std::string filenameAndPath; // Keep this filename associated with texture so you can prevent trying to reload the same file over and over.
GLuchar* data; // the actual color - pixel texture data.
}
// This function will handle the opening and reading in of the texture data
// it would return back the ID value generated by OpenGL which will also be
// stored into the texture struct. The texture struct is returned by reference so that it can be populated with data.
GLuint loadTexture( const char* filenameAndPath, Texture& texture, /*loading parameters & flags*/ ) {
// Check to see if file exists or is already loaded
if ( fileanameAndPath already exists ) {
// get the existing ID from the already loaded texture
// and just return that.
} else {
// Try to open the new file for reading.
// parse the data for general purposes that will support
// your application. You can simply use `stbi_load` as it is a fairly
// decent third party library.
// Check the internal formatting of how the data is stored
// Compression, pixel orientation etc.
// configure the data to your needs (color format),
// (flipping the pixels either horizontally, vertically or both),
// now copy the actual pixel data into your buffer.
// close the file handle
// save all the information into your struct
// return the ID value that was generated by OpenGL
}
}
The within your main engine code before the render loop you will want to load your texture(s) from file and then you can use this Texture object where needed. Finally in your render loop is where you would want to set the texture(s) to active and bind them to the render target and pass them off to your shaders. In some cases you might want to set them active & bind them before your render loop depending on the type of shader-technique you are implementing.
Answering my own question.
The solution is to use atlas map. Software generates atlas that contains all pictures, upload it once (glTexImage2D) and use coordinates for each picture. That improves performance very significantly as glTexImage2D was called just once.
I'm writing a FFGL video effect plugin (using this shadertoy port). I want to store a previously rendered frame to use in a future calculation. Specifically I am trying to make the equivalent of a video delay.
Is it possible to write to an external buffer from a fragment shader and then use that stored value at a later time (say, 30 frames later)?
you create a FBO, and bind it. whatever you render next goes onto it. so if you're using a shader, that still applies like on the default FBO.
here's some code to help you out
import java.nio.ByteBuffer;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL14.*;
import static org.lwjgl.opengl.GL30.*;
public class FBO {
public int fbo,tex,depth;
public void delete() {glDeleteTextures(tex);glDeleteRenderbuffers(depth);glDeleteFramebuffers(fbo);}
public void bindTexture() {glBindTexture(GL_TEXTURE_2D,tex);}
public void bind() {glBindFramebuffer(GL_FRAMEBUFFER,fbo);}
public static void unbind() {glBindFramebuffer(GL_FRAMEBUFFER,0);}
public FBO(){ // create the fbo
glBindTexture(GL_TEXTURE_2D,tex=glGenTextures()); // create texture, set correct filters
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
glBindRenderbuffer(GL_RENDERBUFFER,depth=glGenRenderbuffers()); // create buffer for depth
glBindFramebuffer(GL_FRAMEBUFFER,fbo=glGenFramebuffers()); // create framebuffer
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,tex,0); // attach texture
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,depth); // attach depth
unbind(); // incase not using immediately
}
public void resize(int w,int h) {
glBindTexture(GL_TEXTURE_2D,tex); // update texture size
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,w,h,0,GL_RGBA,GL_UNSIGNED_BYTE,(ByteBuffer)null);
glBindRenderbuffer(GL_RENDERBUFFER,depth); // update depth size
glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COMPONENT24,w,h);
}
public void render() {
// glColor4f(1,1,1,1); // you may want to do these customly
// glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, tex);
Tess.begin();
Tess.vertex( 0, height, 0, 0, 0);
Tess.vertex(width, height, 0, 1, 0);
Tess.vertex(width, 0, 0, 1, 1);
Tess.vertex( 0, 0, 0, 0, 1);
Tess.draw();
// Tess btw, just manually handles verts and tex coords
// that could be pure pipeline, or by vbo / vao / interleaved vbo
// width / height are just the 2d size. could render for post processing
}
}
I take multiple images of the same mesh using OpenGL, GLEW and GLFW. The mesh (triangles) doesn't change in each shot, only the ModelViewMatrix does.
Here's the important code of my mainloop:
for (int i = 0; i < number_of_images; i++) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* set GL_MODELVIEW matrix depending on i */
glBegin(GL_TRIANGLES);
for (Triangle &t : mesh) {
for (Point &p : t) {
glVertex3f(p.x, p.y, p.z);
}
}
glReadPixels(/*...*/) // get picture and store it somewhere
glfwSwapBuffers();
}
As you can see, I set/transfer the triangle vertices for each shot I want to take. Is there a solution in which I only need to transfer them once? My mesh is quite large, so this transfer takes quite some time.
In the year 2016 you must not use glBegin/glEnd. No way. Use Vertex Array Obejcts instead; and use custom vertex and/or geometry shaders to reposition and modify your vertex data. Using these techniques, you will upload your data to the GPU once, and then you'll be able to draw the same mesh with various transformations.
Here is an outline of how your code may look like:
// 1. Initialization.
// Object handles:
GLuint vao;
GLuint verticesVbo;
// Generate and bind vertex array object.
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// Generate a buffer object.
glGenBuffers(1, &verticesVbo);
// Enable vertex attribute number 0, which
// corresponds to vertex coordinates in older OpenGL versions.
const GLuint ATTRIBINDEX_VERTEX = 0;
glEnableVertexAttribArray(ATTRIBINDEX_VERTEX);
// Bind buffer object.
glBindBuffer(GL_ARRAY_BUFFER, verticesVbo);
// Mesh geometry. In your actual code you probably will generate
// or load these data instead of hard-coding.
// This is an example of a single triangle.
GLfloat vertices[] = {
0.0f, 0.0f, -9.0f,
0.0f, 0.1f, -9.0f,
1.0f, 1.0f, -9.0f
};
// Determine vertex data format.
glVertexAttribPointer(ATTRIBINDEX_VERTEX, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Pass actual data to the GPU.
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*3, vertices, GL_STATIC_DRAW);
// Initialization complete - unbinding objects.
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// 2. Draw calls.
while(/* draw calls are needed */) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(vao);
// Set transformation matrix and/or other
// transformation parameters here using glUniform* calls.
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0); // Unbinding just as an example in case if some other code will bind something else later.
}
And a vertex shader may look like this:
layout(location=0) in vec3 vertex_pos;
uniform mat4 viewProjectionMatrix; // Assuming you set this before glDrawArrays.
void main(void) {
gl_Position = viewProjectionMatrix * vec4(vertex_pos, 1.0f);
}
Also take a look at this page for a good modern accelerated graphics book.
#BDL already commented that you should abandon the immediate mode drawing calls (glBegin … glEnd) and switch to Vertex Array drawing (glDrawElements, glDrawArrays) that fetch their data from Vertex Buffer Objects (VBOs). #Sergey mentioned Vertex Array Objects in his answer, but those are actually state containers for VBOs.
A very important thing you have to understand – and the way you asked your question it's apparently something you're not aware of, yet – is, that OpenGL does not deal with "meshes", "scenes" or the like. OpenGL is just a drawing API. It draws points… lines… and triangles… one at a time… with no connection between them whatsoever. That's it. So when you show multiple views of the "same" thing, you must draw it several times. There's no way around this.
Most recent versions of OpenGL support multiple viewport rendering, but it still takes a geometry shader to multiply the geometry into several pieces to be drawn.
I'm having a hard time getting multiple vertex array objects to render multiple primitives. Nearly all the OpenGL tutorials I've found online show using only a single VAO, so I'm not sure what I might be doing wrong.
I'm using Qt-OpenGL and trying to render a square and a cube (on multiple VAOs).
Using the following code, I'm only getting one primitive displayed on the screen (whichever one is initialized second). I can see either primitive when I turn off initialization of the other one, but not at the same time.
Data Struct:
struct VBO : public QOpenGLBuffer {
};
struct VAO : public QOpenGLVertexArrayObject {
VBO vbo[1];
};
enum { CIRCLE, RECT, NUM_VAOs };
enum { POS, NUM_VBOs };
VAO vao[NUM_VAOs];
Initialization:
static void init_objects() {
for(int i = 0; i < NUM_VAOs; ++i) {
vao[i].create();
}
vao[CIRCLE].bind();
for(int i = 0; i < NUM_VBOs; ++i) {
vao[CIRCLE].vbo[i].create();
vao[CIRCLE].vbo[i].setUsagePattern( QOpenGLBuffer::StaticDraw );
}
vao[CIRCLE].vbo[POS].bind();
vao[CIRCLE].vbo[POS].allocate(circle.getVertexData(), circle.getNumVertices()*3*sizeof(float));
vao[CIRCLE].release();
// repeat for RECTANGLE instead of CIRCLE
vao[RECT].bind();
for(int i = 0; i < NUM_VBOs; ++i) {
vao[RECT].vbo[i].create();
vao[RECT].vbo[i].setUsagePattern( QOpenGLBuffer::StaticDraw );
}
vao[RECT].vbo[POS].bind();
vao[RECT].vbo[POS].allocate(circle.getVertexData(), circle.getNumVertices()*3*sizeof(float));
}
Rendering Code:
void game::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
vao[RECT].bind();
vao[RECT].vbo[POS].bind();
QMatrix4x4 id;
Game.setMVP( id );
Game.setColor( Colour(0.0, 1.0, 0.0) );
glDrawElements(GL_TRIANGLES, rect.getSolidIndices().size()*sizeof(unsigned int),
GL_UNSIGNED_INT, &(rect.getSolidIndices()[0]));
glFinish();
vao[RECT].release();
// Now for circle:
vao[CIRCLE].bind();
vao[CIRCLE].vbo[POS].bind();
Game.setMVP( id );
Game.setColor( Colour(1.0, 0.0, 0.0) );
glDrawElements(GL_TRIANGLES, circle.getSolidIndices().size()*sizeof(unsigned int),
GL_UNSIGNED_INT, &(circle.getSolidIndices()[0]));
glFlush();
}
I've tried reading the data from the buffers before rendering (they are distinct and what I would expect for each primitive), so I know the write occurred properly. I'm guessing now I might be binding the buffers wrong, or missing a step while rendering.
Hi I am using VBO to load image texture and then draw it in C++. VBO id generate and bind and draw occurs here
void ViewManager::render(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if(decompressTileImage->tileTexure == 0)
{
loadTexture(decompressTileImage);
glGenBuffers(1,&decompressTileImage->VBOId);
glBindBuffer(GL_ARRAY_BUFFER,decompressTileImage->VBOId);
glBufferData(GL_ARRAY_BUFFER,sizeof(*(this->tileCoordList))+sizeof(*(this->tileTextureCoordList)),0,GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER,0,sizeof(*(this->tileCoordList)),this->tileCoordList);
glBufferSubData(GL_ARRAY_BUFFER,sizeof(*(this->tileCoordList)),sizeof(*(this->tileTextureCoordList)),this->tileTextureCoordList);
}
else
{
glBindBuffer(GL_ARRAY_BUFFER,decompressTileImage->VBOId);
glBindTexture(GL_TEXTURE_2D, decompressTileImage->tileTexure);
}
glColor4f(1.0f, 1.0f, 1.0f, textureAlpha);
if(textureAlpha < 1.0)
{
textureAlpha = textureAlpha + .03;
this->tiledMapView->renderNow();
}
glTexCoordPointer(3, GL_FLOAT, 0, (void*)sizeof(*(this->tileCoordList)));
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindBuffer(GL_ARRAY_BUFFER,0);
glDisable(GL_BLEND);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
}
This function is in a class named MapTile. MapTile is created 35 times for 35 images downloaded from the internet. And then a thread calls this method 35 times for 35 MapTile object and keeps doing it. That is why I first check if the method is called for the first time so that I can load data and generate VBO id only once for each MapTile object. I check this with if(decompressTileImage->tileTexure == 0) this line. and then each time I just bind the vbo id to draw. No need to load the data again.
Here decompressTileImage is a TextureImageInfo class. The implementation is
#include "TextureImageInfo.h"
TextureImageInfo::TextureImageInfo(unsigned char * image,GLuint format,int texWidth,int texHeight,int imageWidth,int imageHeight,float tex_x,float tex_y)
{
// TODO Auto-generated constructor stub
this->format = format;
this->image = image;
this->imageHeight = imageHeight;
this->imageWidth = imageWidth;
this->texHeight = texHeight;
this->texWidth = texWidth;
this->tileTexure = 0;
this->VBOId = 0;
this->time = 0;
}
TextureImageInfo::~TextureImageInfo()
{
if(VBOId!=0)
glDeleteBuffers(1,&VBOId);
}
It draws and does everything fine but crashes when I try to clean up the memory in the destructor of TextureImageInfo class which is given here. I don't understand why. I checked to see if the VBOId is generated and loaded in the memory with the if condition in the destructor too.
As indicated in the comments, OpendGL ES commands should be submitted from the same thread where the context was created.
From the Blackberry docs Parallel processing with OpenGL ES:
It is important to note that each OpenGL ES rendering context targets
a single thread of execution.
If you want to render multiple scenes, you can separate each scene
into its own thread, making sure each thread has its own context