Results 1 to 2 of 2

Thread: Create a triangle that continuously rotates around itís y-axis in OpenGl

  1. #1
    Newbie Newbie
    Join Date
    Oct 2013

    Create a triangle that continuously rotates around itís y-axis in OpenGl

    Im wondering how to rotate a OpenGL triangle around its Y-Axis in OpenGL, Ive been able to translate the triangle from object space to clip space. Im wondering if anyone has any experience with this issue and may be able to lend a hand.

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <GL/glew.h>
    #include <GL/freeglut.h>
    #include <glm/glm.hpp>
    #include <glm/gtc/matrix_transform.hpp>
    #include <Windows.h>

    using namespace std;
    using namespace glm;

    const int windowWidth = 1024;
    const int windowHeight = 768;

    GLuint VBO;
    const int NUMVERTS = 3;
    GLuint gModelToWorldTransformLocation;
    GLuint gWorldToViewTransformLocation;
    GLuint gProjectionTransformLocation;
    struct SimpleVertex
    vec3 pos;
    vec4 colour;

    static void renderSceneCallBack()
    static mat4 modelToWorldTransform = mat4(1.0f);
    static mat4 worldToViewTransform = lookAt(
    vec3(0.0f,0.0f,3.0f), // position of your camera, in world space
    vec3(0.0f,0.0f,0.0f), // look at in world space
    vec3(0.0f,1.0f,0.0f) // Camera up direction (set to 0,-1,0 to look upside-down)
    static mat4 projectionTransform = perspective(45.0f, (float)windowWidth / (float)windowHeight, 1.0f, 100.0f);
    glUniformMatrix4fv(gModelToWorldTransformLocation, 1, GL_FALSE, &modelToWorldTransform[0][0]);
    glUniformMatrix4fv(gWorldToViewTransformLocation, 1, GL_FALSE, &worldToViewTransform[0][0]);
    glUniformMatrix4fv(gProjectionTransformLocation, 1, GL_FALSE, &projectionTransform[0][0]);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), 0);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), (const GLvoid*)12);
    glDrawArrays(GL_TRIANGLES, 0, NUMVERTS);

    static void initializeGlutCallbacks()

    static void createVertexBuffer()
    // Create some vertices to put in our VBO.
    // Create vertex buffer
    SimpleVertex vertices[] =
    {vec3(-0.5f, -0.5f, 0.0f), vec4(1.0f, 0.0f, 0.0f, 1.0f)},
    {vec3(0.5f, -0.5f, 0.0f), vec4(0.0f, 1.0f, 0.0f, 1.0f)},
    {vec3( 0.0f, 0.5f, 0.0f), vec4(0.0f, 0.0f, 1.0f, 1.0f)}

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(SimpleVertex) * 3, vertices, GL_STATIC_DRAW);

    static void addShader(GLuint shaderProgram, const char* pShaderText, GLenum shaderType)
    GLuint shaderObj = glCreateShader(shaderType);

    if (shaderObj == 0)
    cerr<<"Error creating shader type "<<shaderType<<endl;

    const GLchar* p[1];
    p[0] = pShaderText;
    GLint Lengths[1];
    Lengths[0]= strlen(pShaderText);
    glShaderSource(shaderObj, 1, p, Lengths);
    GLint success;
    glGetShaderiv(shaderObj, GL_COMPILE_STATUS, &success);
    if (!success)
    GLchar InfoLog[1024];
    glGetShaderInfoLog(shaderObj, 1024, NULL, InfoLog);
    cerr<<"Error compiling shader type "<<shaderType<<": "<<InfoLog<<endl;

    glAttachShader(shaderProgram, shaderObj);

    const string readFileToString(char* filename)
    ifstream file (filename, ios::in);
    if (file.is_open())
    stringstream continut;
    continut << file.rdbuf();
    continut << '\0';
    return continut.str();
    return "";

    static void buildShaders()
    GLuint shaderProgram = glCreateProgram();

    if (shaderProgram == 0)
    cerr<<"Error creating shader program\n";

    string VS = readFileToString("vertexShader.glsl");
    string FS = readFileToString("fragmentShader.glsl");

    addShader(shaderProgram, VS.c_str(), GL_VERTEX_SHADER);
    addShader(shaderProgram, FS.c_str(), GL_FRAGMENT_SHADER);

    GLint success = 0;
    GLchar errorLog[1024] = { 0 };

    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (success == 0)
    glGetProgramInfoLog(shaderProgram, sizeof(errorLog), NULL, errorLog);
    cerr<<"Error linking shader program: "<<errorLog<<endl;

    glGetProgramiv(shaderProgram, GL_VALIDATE_STATUS, &success);
    if (!success)
    glGetProgramInfoLog(shaderProgram, sizeof(errorLog), NULL, errorLog);
    cerr<<"Error linking shader program: "<<errorLog<<endl;


    gModelToWorldTransformLocation = glGetUniformLocation(shaderProgram, "gModelToWorldTransform");
    //assert(gModelToWorldTransformLocation != 0xFFFFFFFF);
    gWorldToViewTransformLocation = glGetUniformLocation(shaderProgram, "gWorldToViewTransform");
    //assert(gWorldToViewTransformLocation != 0xFFFFFFFF);
    gProjectionTransformLocation = glGetUniformLocation(shaderProgram, "gProjectionTransform");
    //assert(gProjectionTransformLocation != 0xFFFFFFFF);


    int main(int argc, char** argv)
    glutInit(&argc, argv);
    glutInitWindowSize(windowWidth, windowHeight);
    glutInitWindowPosition(100, 100);


    // Must be done after glut is initialized!
    GLenum res = glewInit();
    if (res != GLEW_OK)
    cerr<<"Error: "<<glewGetErrorString(res)<<"\n";
    return 1;


    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    // Create a vertex buffer


    return 0;

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    modelToWorldTransform is the matrix to modify to move your object around. Have a look at the glm library for functions to manipulate this matrix

Similar Threads

  1. Create a Triangle Strip from unordered 2d points.
    By MrCybin in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 06-16-2016, 05:22 AM
  2. Rotating an axis to create height change for obj
    By chronophile in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 05-18-2011, 03:53 AM
  3. Problem to create a triangle or square with menu
    By neo87 in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 03-09-2011, 03:04 PM
  4. glutPassiveMotionFunc continuously fires
    By deadeye in forum OpenGL: General
    Replies: 0
    Last Post: 03-03-2011, 01:50 PM
  5. create triangle for each vertex in geometry shade
    By zhigao in forum OpenGL: GLSL
    Replies: 1
    Last Post: 10-19-2010, 10:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean