hi i am trying to create a program where a cube rotates, but it only rotates when the window is resized. im 99% positive it has to do with the main while loop in the main function. here is my code:
#include "gl_core_4_3.h"
#include "matrixfuncs.h"
#include <stdio.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <string.h>
#include <math.h>
GLuint yaxis = 0, xaxis = 0;
GLuint vao, buffers[2], matrixloc;
GLfloat matrixy[] = {1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
GLfloat matrixx[] = {1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
Display *dpy;
Window root;
GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
Window win;
GLXContext glc;
XWindowAttributes gwa;
XEvent xev;
void display();
void init();
void rotate();
int main(int argc, char **argv) {
dpy = XOpenDisplay(NULL);
if(dpy == NULL) {
printf("
cannot connect to X server
");
exit(0);
}
root = DefaultRootWindow(dpy);
vi = glXChooseVisual(dpy, 0, att);
if(vi == NULL) {
printf("
no appropriate visual found
");
exit(0);
}
else {
printf("
visual %p selected
", (void *)vi->visualid); /* %p creates hexadecimal output like in glxinfo */
}
cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
swa.colormap = cmap;
swa.event_mask = ExposureMask | KeyPressMask;
win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);
XMapWindow(dpy, win);
XStoreName(dpy, win, "Testing Transformations");
glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
glXMakeCurrent(dpy, win, glc);
init();
while(1) {
XNextEvent(dpy, &xev);
if(xev.type == Expose) {
XGetWindowAttributes(dpy, win, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
rotate();
display();
glXSwapBuffers(dpy, win);
}
if(xev.type == KeyPress) {
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, glc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
exit(0);
}
} /* this closes while(1) { */
} /* this is the } which closes int main(int argc, char *argv[]) { */
void display()
{
if(ogl_LoadFunctions() == ogl_LOAD_FAILED)
{
printf("error with ogl");
exit(1);
}
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(1000);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLE_STRIP, 17, GL_UNSIGNED_SHORT, NULL);
glDisable(GL_PRIMITIVE_RESTART);
glFlush();
}
void init()
{
if(ogl_LoadFunctions() == ogl_LOAD_FAILED)
{
printf("error with ogl");
exit(1);
}
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glClearColor(0, 1, 0, 1);
const char *fshader =
"#version 430 core
"
"out vec4 fColor;
"
"void main()
"
"{
"
"fColor = vec4(1, 0, 0, 1);
"
"}
";
const char *vshader =
"#version 430 core
"
"uniform mat4 Matrix;
"
"layout (location = 0) in vec4 vPosition;
"
"void main()
"
"{
"
"gl_Position = Matrix * vPosition;
"
"}
";
GLuint prog = glCreateProgram();
GLuint vert = glCreateShader(GL_VERTEX_SHADER);
GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);
GLint vlen = (GLint)strlen(vshader);
GLint flen = (GLint)strlen(fshader);
glShaderSource(vert, 1, &vshader, &vlen);
glShaderSource(frag, 1, &fshader, &flen);
glCompileShader(vert);
glCompileShader(frag);
glAttachShader(prog, vert);
glAttachShader(prog, frag);
glLinkProgram(prog);
glUseProgram(prog);
matrixloc = glGetUniformLocation(prog, "Matrix");
GLfloat vertices[] = {
-.5, -.5, -.5, 1,
-.5, -.5, .5, 1,
-.5, .5, -.5, 1,
-.5, .5, .5, 1,
.5, -.5, -.5, 1,
.5, -.5, .5, 1,
.5, .5, -.5, 1,
.5, .5, .5, 1
};
GLushort cubeindices[] = {
0, 1, 2, 3, 6, 7, 4, 5,
1000,
2, 6, 0, 4, 1, 5, 3, 7
};
glGenBuffers(2, buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof cubeindices, cubeindices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (void *)0);
glEnableVertexAttribArray(0);
}
void rotate()
{
if(ogl_LoadFunctions() == ogl_LOAD_FAILED)
{
printf("error with ogl");
exit(1);
}
GLfloat matrixresult[16];
matrixy[0] = cos(yaxis);
matrixy[2] = sin(yaxis);
matrixy[8] = (-1) * sin(yaxis);
matrixy[10] = cos(yaxis);
matrixx[5] = cos(xaxis);
matrixx[6] = sin(xaxis);
matrixx[9] = (-1) * sin(xaxis);
matrixx[10] = cos(xaxis);
Mult4fv(matrixy, matrixx, matrixresult);
glUniformMatrix4fv(matrixloc, 1, GL_FALSE, matrixresult);
yaxis += 2;
xaxis++;
}