OpenGL 3.3 won't draw my triangle - c

I'm following these tutorials using C instead of C++:
Tutorial 2, short extension to tutorial 2.
The only change I made to port it was changing Vector3f[3] into GLfloat[9]. The version with GLfloat[1] instead of Vector3f[1] works correctly. I think this change might be the reason of glDrawArrays not working but I don't know how to fix it.
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
GLuint VBO_id;
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO_id);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glutSwapBuffers();
}
static void CreateVertexBuffer()
{
GLfloat Vertices[9] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f,
1.0f, 0.0f };
glGenBuffers(1, &VBO_id);
glBindBuffer(GL_ARRAY_BUFFER, VBO_id);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices,
GL_STATIC_DRAW);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutorial 03");
glutDisplayFunc(RenderSceneCB);
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
CreateVertexBuffer();
glutMainLoop();
return 0;
}

From here.
OpenGL 3.0 was the last revision of the specification which fully supported both fixed and programmable functionality. Even so, most hardware since the OpenGL 2.0 generation lacked the actual fixed-function hardware. Instead, fixed-function processes are emulated with shaders built by the system.
In OpenGL 3.2, the Core Profile lacks these fixed-function concepts. The compatibility profile keeps them around. However, most newer features of OpenGL cannot work with fixed function, even when it might seem theoretically possible for them to interact.
Sounds like your version of OpenGL doesn't support the fixed function pipeline. Either use an older version of OpenGL that does or write and load a shader as shown in Tutorial 4.

Related

Object rendering strangely after adding transformations

I'm adding transformations to my C OpenGL program. I'm using CGLM as my maths library. The program has no warnings or errors. Still however, when I compile and run the program, I get a distorted version of my intended image (it was not distorted before adding transformations).
The following is my program's main loop:
// Initialize variables for framerate counting
double lastTime = glfwGetTime();
int frameCount = 0;
// Program loop
while (!glfwWindowShouldClose(window)) {
// Calculate framerate
double thisTime = glfwGetTime();
frameCount++;
// If a second has passed.
if (thisTime - lastTime >= 1.0) {
printf("%i FPS\n", frameCount);
frameCount = 0;
lastTime = thisTime;
}
processInput(window);
// Clear the window
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Bind textures on texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
// Create transformations
mat4 transform = {{1.0f}};
glm_mat4_identity(transform);
glm_translate(transform, (vec3){0.5f, -0.5f, 0.0f});
glm_rotate(transform, (float)glfwGetTime(), (vec3){0.0f, 0.0f, 1.0f});
// Get matrix's uniform location and set matrix
shaderUse(myShaderPtr);
GLint transformLoc = glGetUniformLocation(myShaderPtr->shaderID, "transform");
// mat4 transform;
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, (float*)transform);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window); // Swap the front and back buffers
glfwPollEvents(); // Check for events (mouse movement, mouse click, keyboard press, keyboard release etc.)
}
The Program is up on github here if you'd like to check out the full code.
The Output of the program is this (The square also rotates):
However, the intended output of the program is the penguin at 20% opacity on top and the box at 100% opacity underneath the penguin.
In the vertex shader, the location of the texture coordinate is 1:
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
However, when you specify the vertices, location 1 is used for the color attribute and position 2 for the text coordinates:
// Colour attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// Texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
Remove the color attribute and use location 1 for the texture coordinates. e.g.:
// Texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(1);
Looking at your source code, you're passing in three attributes (position, color and texture coordinates), but your vertex shader only takes two.
Removing the color attribute and instead passing the texture coordinates as attribute #1 instead of #2 should make it look like intended.

Half cylinder/Closed cylinder in opengl

I currently have a cylinder
glPushMatrix();
glTranslatef(cylinder->pos[0], cylinder->pos[1], cylinder->pos[2]);
glRotatef(cylinder->angle, -1.0, 0, 0);
gluDisk(quad, 0.0, cylinder->radius, 300, 90);
gluCylinder(quad, cylinder->radius, cylinder->radius, cylinder->height, 30, 1);
gluDeleteQuadric(quad);
glPopMatrix();
but it only renders half of a cylinder instead of the whole thing (the half coming towards the view). Second issue is I don't know how to close the cylinder. The gludisk only closes the bottom of the cylinder and I'm not sure how to close the top side.
If I understand what you're describing, it sounds like you might have back face culling enabled. Do you have a glEnable(GL_CULL_FACE) call in your code? This will eliminate all polygons that face away from you. In any case, once you draw a cylinder that is closed at both sides, it will be fine to keep it enabled.
You can close the cylinder at the top with another gluDisk() call, after applying a translation that moves it to (0, 0, height). For correctness, I believe you need to rotate the disk that closes the cylinder at the bottom. Otherwise it will face to the inside of the cylinder instead of to the outside, which affects the normals, and would get it eliminated by back face culling when it should be visible.
For the full thing, it should look something like this (untested):
glPushMatrix();
glTranslatef(cylinder->pos[0], cylinder->pos[1], cylinder->pos[2]);
glRotatef(cylinder->angle, -1.0f, 0.0f, 0.0f);
gluCylinder(quad, cylinder->radius, cylinder->radius, cylinder->height, 30, 1);
glPushMatrix();
glTranslatef(0.0f, 0.0f, cylinder->height);
gluDisk(quad, 0.0f, cylinder->radius, 30, 1);
glPopMatrix();
glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
gluDisk(quad, 0.0f, cylinder->radius, 30, 1);
gluDeleteQuadric(quad);
glPopMatrix();

OpenGL 2D moving relation

I have the two methods hich are doing the same thing. Now I am wondering what is their differences and which one can perform faster.
Here are the methods:
1th Method
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f( 0, 0); glVertex2f( x, y);
glTexCoord2f( 1, 0); glVertex2f( x+texWidth, y);
glTexCoord2f( 1, 1); glVertex2f( x+texWidth, y+texHeight);
glTexCoord2f( 0, 1); glVertex2f( x, y+texHeight);
glEnd();
x++; y++;
2th Method
glPushMatrix();
glTranslatef(x, y, 0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f( 0, 0); glVertex2f( 0.0f, 0.0f);
glTexCoord2f( 1, 0); glVertex2f( 1.0f, 0.0f);
glTexCoord2f( 1, 1); glVertex2f( 1.0f, 1.0f);
glTexCoord2f( 0, 1); glVertex2f( 0.0f, 1.0f);
glEnd();
glPopMatrix();
x++; y++;
Regarding performance, you are using immediate mode, which is slow anyway (its the "old" way of doing things; unfortunately most OpenGL tutorials on the web are hopelessly outdated). Use buffers (gpu-side) like VBOs and VAOs if you really care about performance.
A nice modern tutorial is http://open.gl/ which introduces VBOs here: http://open.gl/drawing
The second method is alot faster, because it is translating the whole matrix, and doesn't do many calculations as it does in method one.
The second method is a lot faster, because it translates the whole QUAD matrix with less calculations.

Rotate object around another object

I drew two rectangles in my scene, and now I'm trying to rotate one rectangle around the other.
When I press the "Left key" my first rectangle moves in direction to the other, but I don't have the effect of rotate because it is only moving in a straight line in direction of the other rectangle.
How do I change the Z direction of the first rectangle?
tl;dr: It's like a solar system, but I don't understand how can I have the depth effect.
I'm using GLUT and OpenGL.
EDIT: More Info:
This is my current code (function that shows the scene):
void RenderScene(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glPushMatrix();
glTranslatef(-70.0f, 0.0f, 0.0f);
glRectf(-25.0f, 25.0f, 25.0f, -25.0f);
glPopMatrix();
glColor3f(1.0f, 1.0f, 0.0f);
glPushMatrix();
glTranslatef(xCor, 0.0f, 0.0f);
glRectf(-25.0f, 25.0f, 25.0f, -25.0f);
glPopMatrix();
glFlush();
}
And a image to help:
Please enable depth test to hide objects beyond other objects.
Give a high value z vaue for the far object.
glEnable( GL_DEPTH_TEST );
glClear( GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-70.0f, 0.0f, 0.0f);
glRectf(-25.0f, 25.0f, 25.0f, -25.0f);
glPopMatrix();
glColor3f(1.0f, 1.0f, 0.0f);
glPushMatrix();
glTranslatef(xCor, 0.0f, 0.0f);
glRectf(-25.0f, 25.0f, 25.0f, -26.0f); // Z value of behind object is set far from eye
glPopMatrix();
What you want to do is moderately complicated.
The only way I know to locate rectangles in 3-space is to build them using glVertex3f() like this:
glBegin(GL_QUADS);
glVertex3f(x0, y0, z0);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glVertex3f(x3, y3, z3);
glEnd();
Your "solar system" will have to be located in screen coordinates: X->left/right, Y->up/down, Z->into/out of screen. To create the effect of orbiting, you'll have to recalculate all four X and Z for each movement increment.
I also suggest using gluLookAt() if you want to see a different view.
The man page for glPerspective() says that zNear is always positive but you've given it a negative number. I'd leave that line commented out until I saw some orbiting.
HTH.

GLUT timer loop stopping prematurely

I've encountered a strange issue where glutTimerFunc seems to randomly stop working when I call it with a zero delay.
Here is my code:
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
int x = 0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.125, 0.875, -1.0, 1.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(1.0, x ? 1.0 : 0.0, 0.0);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
glFlush();
glutSwapBuffers();
}
void timer(int value)
{
x = !x;
glutPostRedisplay();
glutTimerFunc(0, timer, 0); // The line in question
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(200, 200);
glutCreateWindow("hello");
init();
glutDisplayFunc(display);
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}
I expected this to show a flickering square, that is changing color as fast as the GPU can keep up.
That is what it actually does initially, but the timer loop seems to randomly stop, and the square stops changing color. Sometimes it doesn't flicker perceptibly at all, and sometimes it flickers for several seconds before stopping.
It doesn't stop if I set the delay to 1ms (glutTimerFunc(1, timer, 0);).
Why does the timer loop stop unexpectedly?
I don't really care about how to fix it, just why it happens.
Your GPU is changing the value faster than your monitor can draw it.
If you had a monitor with an extremely high refresh rate, you could probably see it, but unfortunately we're limited to 60Hz/120Hz/240Hz for now.
When you remove the 1ms forced delay, you are causing the system to become a non-deterministic system (based on the speed of other programs, rather than just yours), and that's why you're getting the random behavior.

Resources